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

@ -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