From 71c86e3570fe7fa8e5e81acf0e2c46211ae90cef Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sat, 27 Sep 2025 21:01:32 +0200 Subject: [PATCH] Sdf return *Material change, 2x perf boost --- src/material.go | 52 +++++++++++++++++++++++++++++-------------- src/primitives_sdf.go | 12 +++++----- src/ray_marching.go | 23 ++++++++++--------- src/sdf.go | 38 +++++++++++++++---------------- 4 files changed, 72 insertions(+), 53 deletions(-) diff --git a/src/material.go b/src/material.go index bca37d7..d9075ee 100644 --- a/src/material.go +++ b/src/material.go @@ -31,8 +31,11 @@ func (grid GridColor) GetColor(p Vector3) Vector3 { return grid.color2.GetColor(p) } -// Ambre je te copie -type Material struct { +type Material interface { + GetMaterialBrut() MaterialBrut +} + +type MaterialBrut struct { ambiantColor Color diffuseColor Color diffuseFac float64 @@ -45,23 +48,12 @@ type Material struct { refractIndice float64 } -func DefaultMaterial(diffuseColor Color) Material { - return Material{ - ambiantColor: ScaledColor{diffuseColor, 0.2}, - diffuseColor: diffuseColor, - diffuseFac: 1, - specularColor: Vector3{255, 255, 255}, - specularFac: 1, - specularExp: 35.0, - reflectanceFac: 0.0, - reflectanceTint: Vector3{255, 255, 255}, - refractFac: 0.0, - refractIndice: 1.0, - } +func (mat MaterialBrut) GetMaterialBrut() MaterialBrut { + return mat } -func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { - return Material{ +func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) MaterialBrut { + return MaterialBrut{ (mat1.ambiantColor.GetColor(p).Scale(1 - t)).Add((mat2.ambiantColor.GetColor(p)).Scale(t)), (mat1.diffuseColor.GetColor(p).Scale(1 - t)).Add((mat2.diffuseColor.GetColor(p)).Scale(t)), mat1.diffuseFac*(1-t) + mat2.diffuseFac*t, @@ -75,6 +67,32 @@ func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { } } +type MixedMaterial struct { + mat1 *Material + mat2 *Material + t float64 + p Vector3 +} + +func (mixedMat MixedMaterial) GetMaterialBrut() MaterialBrut { + return MixMat((*mixedMat.mat1).GetMaterialBrut(), (*mixedMat.mat2).GetMaterialBrut(), mixedMat.t, mixedMat.p) +} + +func DefaultMaterial(diffuseColor Color) MaterialBrut { + return MaterialBrut{ + ambiantColor: ScaledColor{diffuseColor, 0.2}, + diffuseColor: diffuseColor, + diffuseFac: 1, + specularColor: Vector3{255, 255, 255}, + specularFac: 1, + specularExp: 35.0, + reflectanceFac: 0.0, + reflectanceTint: Vector3{255, 255, 255}, + refractFac: 0.0, + refractIndice: 1.0, + } +} + // Colors var RED = Vector3{255, 0, 0} diff --git a/src/primitives_sdf.go b/src/primitives_sdf.go index ba173ab..9999c1e 100644 --- a/src/primitives_sdf.go +++ b/src/primitives_sdf.go @@ -6,8 +6,8 @@ type Sphere struct { material Material } -func (s Sphere) Distance(p Vector3) (float64, Material) { - return p.Length() - s.radius, s.material +func (s Sphere) Distance(p Vector3) (float64, *Material) { + return p.Length() - s.radius, &s.material } // Box @@ -16,9 +16,9 @@ type Box struct { material Material } -func (s Box) Distance(p Vector3) (float64, Material) { +func (s Box) Distance(p Vector3) (float64, *Material) { q := p.Abs().Sub(s.dimensions) - return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), s.material + return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), &s.material } // Plane @@ -27,6 +27,6 @@ type Plane struct { material Material } -func (s Plane) Distance(p Vector3) (float64, Material) { - return p.Dot(s.normal), s.material +func (s Plane) Distance(p Vector3) (float64, *Material) { + return p.Dot(s.normal), &s.material } diff --git a/src/ray_marching.go b/src/ray_marching.go index 65e6bf3..bb3b4e9 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -13,8 +13,8 @@ import ( const MAX_DIST = 1000.0 const MAX_STEP = 1000 const EPS = 0.01 -const WIDTH = 250 -const HEIGHT = 250 +const WIDTH = 1000 +const HEIGHT = 1000 const PI = math.Pi const SOFT_SHADOW_COEFF = 32 @@ -36,7 +36,7 @@ func init() { sphereMat := RED_MAT sphereMat.specularFac = 0.5 - sphere := TranslatedSDF{Sphere{5, sphereMat}, Vector3{0, 100, 10}} + sphere := TranslatedSDF{Sphere{10, sphereMat}, Vector3{0, 100, 10}} // boxMat := BLUE_MAT // box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}} @@ -44,8 +44,8 @@ func init() { // sphere := RepeatSDF{Sphere{20, sphereMat}, Vector3{50, 50, 50}} plane := Plane{Vector3{0, 0, 1}, WHITE_GREY_GRID_MAT} - // scene = SmoothUnionSDF{SubstractionSDF{box, sphere}, plane, 2.5} - scene = UnionSDF{plane, sphere} + scene = SmoothUnionSDF{sphere, plane, 2.5} + // scene = UnionSDF{plane, sphere} } @@ -53,13 +53,14 @@ func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { lightVec := (lightPos.Sub(point)).Normalized() reflectLight := Reflect(lightVec, normal) eyeVec := (cameraPos.Sub(point)).Normalized() - ambiant := mat.ambiantColor.GetColor(point) + brutMat := mat.GetMaterialBrut() + ambiant := brutMat.ambiantColor.GetColor(point) - diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) - diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) + diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVec)) + diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower) - specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp) - specular := mat.specularColor.GetColor(point).Scale(specularPower) + specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp) + specular := brutMat.specularColor.GetColor(point).Scale(specularPower) return ambiant.Add(diffuse).Add(specular) } @@ -90,7 +91,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { dist, mat := scene.Distance(p) if dist < EPS { normal := Gradient(scene, p, EPS).Normalized() - return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3)) + return true, phongShading(p, normal, *mat).Scale(max(shadow(p), 0.3)) } if dist > MAX_DIST { break diff --git a/src/sdf.go b/src/sdf.go index a43c447..64f52c0 100644 --- a/src/sdf.go +++ b/src/sdf.go @@ -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 }