From 0cf71d7abbf5679405ae546cf92e08a4aa6a6534 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 00:54:40 +0200 Subject: [PATCH 1/6] Nah I want performance --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a7f52e..725c7a3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ Simple cpu ray-marching in go -Focus on Lisiblity & Features, sacrifice performance +Focus on Lisiblity & Features From a6a3785a9f8ca96e23bb99bff01cd4ac63060ea7 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 02:45:46 +0200 Subject: [PATCH 2/6] small opti --- src/main.go | 4 ++-- src/material.go | 4 +++- src/ray_marching.go | 4 ++-- src/sdf.go | 18 +++++++++--------- src/vec3.go | 8 +++++++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main.go b/src/main.go index 1868e3b..e9e0ad8 100644 --- a/src/main.go +++ b/src/main.go @@ -13,8 +13,8 @@ import ( rl "github.com/gen2brain/raylib-go/raylib" ) -const WIDTH = 100 -const HEIGHT = 100 +const WIDTH = 200 +const HEIGHT = 200 const TEXTURE_SCALE = 4 const MAX_DIST = 500.0 diff --git a/src/material.go b/src/material.go index d9075ee..e9d3b70 100644 --- a/src/material.go +++ b/src/material.go @@ -1,6 +1,8 @@ package main -import "math" +import ( + "math" +) type Color interface { GetColor(Vector3) Vector3 diff --git a/src/ray_marching.go b/src/ray_marching.go index d7b806f..911ac74 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -39,7 +39,7 @@ func shadow(point Vector3) float64 { } res = min(res, SOFT_SHADOW_COEFF*dist/dist_total) dist_total += dist - p = p.Add(direction.Scale(dist)) + p.Radd(direction.Scale(dist)) } return res } @@ -55,7 +55,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { if dist > MAX_DIST { break } - p = p.Add(direction.Scale(dist)) + p.Radd(direction.Scale(dist)) } return false, GREY } diff --git a/src/sdf.go b/src/sdf.go index 64f52c0..ca576d2 100644 --- a/src/sdf.go +++ b/src/sdf.go @@ -73,7 +73,7 @@ type UnionSDF struct { 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) + d := min(d1, d2) color := color1 if d2 < d1 { @@ -91,7 +91,7 @@ func (s SubstractionSDF) Distance(p Vector3) (float64, *Material) { d1, color1 := s.primitive1.Distance(p) d2, _ := s.primitive2.Distance(p) - d := math.Max(d1, -d2) + d := max(d1, -d2) return d, color1 } @@ -104,7 +104,7 @@ 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) + d := max(d1, d2) var mat Material = MixedMaterial{mat1, mat2, 0.5, p} return d, &mat @@ -120,8 +120,8 @@ 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 + h := max(k-math.Abs(d1-d2), 0.0) + d := min(d1, d2) - h*h*0.25/k t := SmoothStep(d2-d1, -k, k) var mat Material = MixedMaterial{mat2, mat1, t, p} return d, &mat @@ -137,8 +137,8 @@ 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 + h := max(k-math.Abs(-d1-d2), 0.0) + d := max(d1, -d2) + h*h*0.25/k t := SmoothStep(d1-d2, -k, k) var mat Material = MixedMaterial{mat1, mat2, t, p} return d, &mat @@ -154,8 +154,8 @@ 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 + h := max(k-math.Abs(d1-d2), 0.0) + d := max(d1, d2) + h*h*0.25/k t := SmoothStep(d2-d1, -k, k) var mat Material = MixedMaterial{mat1, mat2, t, p} return d, &mat diff --git a/src/vec3.go b/src/vec3.go index 7aea4c9..3f02686 100644 --- a/src/vec3.go +++ b/src/vec3.go @@ -16,12 +16,18 @@ func (u Vector3) Add(v Vector3) Vector3 { return Vector3{u.X + v.X, u.Y + v.Y, u.Z + v.Z} } +func (u *Vector3) Radd(v Vector3) { + u.X += v.X + u.Y += v.Y + u.Z += v.Z +} + func (u Vector3) Neg() Vector3 { return Vector3{-u.X, -u.Y, -u.Z} } func (u Vector3) Sub(v Vector3) Vector3 { - return u.Add(v.Neg()) + return Vector3{u.X - v.X, u.Y - v.Y, u.Z - v.Z} } func (u Vector3) Scale(a float64) Vector3 { From 0ded879dced29c61c498cceff6f881ffe91ad269 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 03:08:27 +0200 Subject: [PATCH 3/6] 2x Perf, because wtf *Material is useless, Material is an interface so it's already a ref --- src/material.go | 6 +++--- src/primitives_sdf.go | 12 ++++++------ src/ray_marching.go | 3 ++- src/sdf.go | 30 +++++++++++++++--------------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/material.go b/src/material.go index e9d3b70..ed6c735 100644 --- a/src/material.go +++ b/src/material.go @@ -70,14 +70,14 @@ func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) Material } type MixedMaterial struct { - mat1 *Material - mat2 *Material + 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) + return MixMat((mixedMat.mat1).GetMaterialBrut(), (mixedMat.mat2).GetMaterialBrut(), mixedMat.t, mixedMat.p) } func DefaultMaterial(diffuseColor Color) MaterialBrut { diff --git a/src/primitives_sdf.go b/src/primitives_sdf.go index 9999c1e..ba173ab 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 911ac74..6af7946 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -50,7 +50,8 @@ 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)) + // return true, phongShading(p, normal, *mat) } if dist > MAX_DIST { break diff --git a/src/sdf.go b/src/sdf.go index ca576d2..c57df23 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 := 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 := max(d1, d2) var mat Material = MixedMaterial{mat1, mat2, 0.5, p} - return d, &mat + return d, mat } type SmoothUnionSDF struct { @@ -116,7 +116,7 @@ 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) @@ -124,7 +124,7 @@ func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) { d := min(d1, d2) - h*h*0.25/k t := SmoothStep(d2-d1, -k, k) var mat Material = MixedMaterial{mat2, mat1, t, p} - return d, &mat + return d, mat } type SmoothSubstractionSDF struct { @@ -133,7 +133,7 @@ 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) @@ -141,7 +141,7 @@ func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) { d := max(d1, -d2) + h*h*0.25/k t := SmoothStep(d1-d2, -k, k) var mat Material = MixedMaterial{mat1, mat2, t, p} - return d, &mat + return d, mat } type SmoothIntersectionSDF struct { @@ -150,7 +150,7 @@ 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) @@ -158,5 +158,5 @@ func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) { d := max(d1, d2) + h*h*0.25/k t := SmoothStep(d2-d1, -k, k) var mat Material = MixedMaterial{mat1, mat2, t, p} - return d, &mat + return d, mat } From 40c0846029ee7d3dc4bb20f91fb9c2620002f133 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 04:09:58 +0200 Subject: [PATCH 4/6] little opti ? --- src/ray_marching.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ray_marching.go b/src/ray_marching.go index 6af7946..2a1ccd9 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -8,8 +8,7 @@ import ( "math" ) -func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { - lightVec := (lightPos.Sub(point)).Normalized() +func phongShading(point Vector3, normal Vector3, lightVec Vector3, mat Material) Vector3 { reflectLight := Reflect(lightVec, normal) eyeVec := (cameraPos.Sub(point)).Normalized() brutMat := mat.GetMaterialBrut() @@ -24,8 +23,7 @@ func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { return ambiant.Add(diffuse).Add(specular) } -func shadow(point Vector3) float64 { - direction := (lightPos.Sub(point)).Normalized() +func shadow(point Vector3, direction Vector3) float64 { p := point.Add(direction.Scale(5 * EPS)) res := 1.0 dist_total := 0.0 @@ -50,7 +48,8 @@ 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)) + lightVec := (lightPos.Sub(p)).Normalized() + return true, phongShading(p, normal, lightVec, mat).Scale(max(shadow(p, lightVec), 0.3)) // return true, phongShading(p, normal, *mat) } if dist > MAX_DIST { From 6fc7d7a4c3d53c647d88e981b83957e7f00c8bfc Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 04:15:28 +0200 Subject: [PATCH 5/6] Revert "little opti ?" This reverts commit 40c0846029ee7d3dc4bb20f91fb9c2620002f133. --- src/ray_marching.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ray_marching.go b/src/ray_marching.go index 2a1ccd9..6af7946 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -8,7 +8,8 @@ import ( "math" ) -func phongShading(point Vector3, normal Vector3, lightVec Vector3, mat Material) Vector3 { +func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { + lightVec := (lightPos.Sub(point)).Normalized() reflectLight := Reflect(lightVec, normal) eyeVec := (cameraPos.Sub(point)).Normalized() brutMat := mat.GetMaterialBrut() @@ -23,7 +24,8 @@ func phongShading(point Vector3, normal Vector3, lightVec Vector3, mat Material) return ambiant.Add(diffuse).Add(specular) } -func shadow(point Vector3, direction Vector3) float64 { +func shadow(point Vector3) float64 { + direction := (lightPos.Sub(point)).Normalized() p := point.Add(direction.Scale(5 * EPS)) res := 1.0 dist_total := 0.0 @@ -48,8 +50,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { dist, mat := scene.Distance(p) if dist < EPS { normal := Gradient(scene, p, EPS).Normalized() - lightVec := (lightPos.Sub(p)).Normalized() - return true, phongShading(p, normal, lightVec, mat).Scale(max(shadow(p, lightVec), 0.3)) + return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3)) // return true, phongShading(p, normal, *mat) } if dist > MAX_DIST { From a6770d7fac9ef332a15ba21c23a5f1c13ce9c27e Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 04:46:54 +0200 Subject: [PATCH 6/6] reflection added --- src/main.go | 2 +- src/material.go | 6 +++--- src/ray_marching.go | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.go b/src/main.go index e9e0ad8..8b092f2 100644 --- a/src/main.go +++ b/src/main.go @@ -69,7 +69,7 @@ func compute_pixel(y0 int, y1 int, wg *sync.WaitGroup, screenCenter Vector3, phi origin := screenCenter.Add(phiVec.Scale(-sx)).Add(thetaVec.Scale(sy)) direction := (origin.Sub(cameraPos)).Normalized() - hit, colorVec := rayMarch(origin, direction) + hit, colorVec := rayMarch(origin, direction, 1) var fr, fg, fb float64 if hit { fr, fg, fb = colorVec.Unpack() diff --git a/src/material.go b/src/material.go index ed6c735..d238e1a 100644 --- a/src/material.go +++ b/src/material.go @@ -62,7 +62,7 @@ func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) Material mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)), mat1.specularFac*(1-t) + mat2.specularFac*t, mat1.specularExp*(1-t) + mat2.specularExp*t, - (mat1.reflectanceFac*(1-t) + mat2.reflectanceFac) * t, + mat1.reflectanceFac*(1-t) + mat2.reflectanceFac*t, mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)), mat1.refractFac*(1-t) + mat2.refractFac*t, mat1.refractIndice*(1-t) + mat2.refractIndice*t, @@ -88,7 +88,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut { specularColor: Vector3{255, 255, 255}, specularFac: 1, specularExp: 35.0, - reflectanceFac: 0.0, + reflectanceFac: 0.2, reflectanceTint: Vector3{255, 255, 255}, refractFac: 0.0, refractIndice: 1.0, @@ -97,7 +97,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut { // Colors -var RED = Vector3{255, 0, 0} +var RED = Vector3{125, 0, 0} var GREEN = Vector3{0, 255, 0} var BLUE = Vector3{0, 0, 255} var WHITE = Vector3{255, 255, 255} diff --git a/src/ray_marching.go b/src/ray_marching.go index 6af7946..8a8aec7 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -44,13 +44,31 @@ func shadow(point Vector3) float64 { return res } -func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { +func reflect(point Vector3, direction Vector3, normal Vector3, mat Material, reflectNumber int) Vector3 { + brutMat := mat.GetMaterialBrut() + if reflectNumber <= 0 || brutMat.reflectanceFac < EPS { + return Vector3{0, 0, 0} + } + reflected := Reflect(direction, normal).Scale(-1) + reflectRayOrigin := point.Add(reflected.Scale(5 * EPS)) + hit, color := rayMarch(reflectRayOrigin, reflected, reflectNumber-1) + if !hit { + color = Vector3{0, 0, 0} + } + // fmt.Println("Reflect") + return color.Scale(brutMat.reflectanceFac) +} + +func rayMarch(origin Vector3, direction Vector3, reflectNumber int) (bool, Vector3) { p := origin for range MAX_STEP { 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)) + phongShade := phongShading(p, normal, mat) + shadowCoeff := max(shadow(p), 0.3) + reflectColor := reflect(p, direction, normal, mat, reflectNumber) + return true, phongShade.Add(reflectColor).Scale(shadowCoeff) // return true, phongShading(p, normal, *mat) } if dist > MAX_DIST {