Sdf return *Material change, 2x perf boost

This commit is contained in:
Crizomb 2025-09-27 21:01:32 +02:00
parent 264a759ac5
commit 71c86e3570
4 changed files with 72 additions and 53 deletions

View file

@ -5,7 +5,7 @@ import (
)
type SDF interface {
Distance(Vector3) (float64, Material)
Distance(Vector3) (float64, *Material)
}
func DistanceOnly(sdf SDF, p Vector3) float64 {
@ -27,7 +27,7 @@ type TranslatedSDF struct {
translate Vector3
}
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
func (s TranslatedSDF) Distance(p Vector3) (float64, *Material) {
return s.primitive.Distance(p.Sub(s.translate))
}
@ -37,7 +37,7 @@ type RotatedSDF struct {
angle float64
}
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
func (s RotatedSDF) Distance(p Vector3) (float64, *Material) {
rotated_p := Rotate(p, s.rotVector, s.angle)
return s.primitive.Distance(rotated_p)
}
@ -47,7 +47,7 @@ type ScaledSDF struct {
scale float64
}
func (s ScaledSDF) Distance(p Vector3) (float64, Material) {
func (s ScaledSDF) Distance(p Vector3) (float64, *Material) {
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
return dist * s.scale, color
}
@ -57,7 +57,7 @@ type RepeatSDF struct {
cellSize Vector3
}
func (s RepeatSDF) Distance(p Vector3) (float64, Material) {
func (s RepeatSDF) Distance(p Vector3) (float64, *Material) {
x, y, z := p.Unpack()
sx, sy, sz := s.cellSize.Unpack()
round := math.RoundToEven
@ -70,7 +70,7 @@ type UnionSDF struct {
primitive2 SDF
}
func (s UnionSDF) Distance(p Vector3) (float64, Material) {
func (s UnionSDF) Distance(p Vector3) (float64, *Material) {
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
d := math.Min(d1, d2)
@ -87,7 +87,7 @@ type SubstractionSDF struct {
primitive2 SDF
}
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
func (s SubstractionSDF) Distance(p Vector3) (float64, *Material) {
d1, color1 := s.primitive1.Distance(p)
d2, _ := s.primitive2.Distance(p)
@ -100,14 +100,14 @@ type IntersectionSDF struct {
primitive2 SDF
}
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) {
func (s IntersectionSDF) Distance(p Vector3) (float64, *Material) {
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
d := math.Max(d1, d2)
mat := MixMat(mat1, mat2, 0.5, p)
return d, mat
var mat Material = MixedMaterial{mat1, mat2, 0.5, p}
return d, &mat
}
type SmoothUnionSDF struct {
@ -116,15 +116,15 @@ type SmoothUnionSDF struct {
k float64
}
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) {
func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(d1-d2), 0.0)
d := math.Min(d1, d2) - h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
var mat Material = MixedMaterial{mat2, mat1, t, p}
return d, &mat
}
type SmoothSubstractionSDF struct {
@ -133,15 +133,15 @@ type SmoothSubstractionSDF struct {
k float64
}
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(-d1-d2), 0.0)
d := math.Max(d1, -d2) + h*h*0.25/k
t := SmoothStep(d1-d2, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
var mat Material = MixedMaterial{mat1, mat2, t, p}
return d, &mat
}
type SmoothIntersectionSDF struct {
@ -150,13 +150,13 @@ type SmoothIntersectionSDF struct {
k float64
}
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(d1-d2), 0.0)
d := math.Max(d1, d2) + h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p)
return d, mat
var mat Material = MixedMaterial{mat1, mat2, t, p}
return d, &mat
}