From 40c0846029ee7d3dc4bb20f91fb9c2620002f133 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 28 Sep 2025 04:09:58 +0200 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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 {