From e35cca183031a7ab3941bbecdb705df2255727a0 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Fri, 26 Sep 2025 23:23:52 +0200 Subject: [PATCH] smooth + mixmat bug fix --- material.go | 18 +++++++++--------- ray_marching.go | 12 +++++++----- sdf.go | 6 ++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/material.go b/material.go index 51f0b16..b07ddf2 100644 --- a/material.go +++ b/material.go @@ -41,8 +41,8 @@ func DefaultMaterial(ambiantColor Color) Material { return Material{ ambiantColor: ambiantColor, diffuseColor: Vector3{255, 255, 255}, - diffuseFac: 1.0, - specularColor: Vector3{0, 255, 0}, + diffuseFac: 0.5, + specularColor: Vector3{255, 255, 255}, specularFac: 1.0, specularExp: 32.0, reflectanceFac: 0.0, @@ -54,16 +54,16 @@ func DefaultMaterial(ambiantColor Color) Material { func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { return Material{ - 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, + (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, 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.specularFac*(1-t) + mat2.specularFac*t, + mat1.specularExp*(1-t) + mat2.specularExp*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, + mat1.refractFac*(1-t) + mat2.refractFac*t, + mat1.refractIndice*(1-t) + mat2.refractIndice*t, } } diff --git a/ray_marching.go b/ray_marching.go index b077eff..58fd065 100644 --- a/ray_marching.go +++ b/ray_marching.go @@ -13,15 +13,17 @@ import ( const MAX_DIST = 1000.0 const MAX_STEP = 1000 const EPS = 0.01 -const WIDTH = 500 -const HEIGHT = 500 +const WIDTH = 250 +const HEIGHT = 250 const PI = math.Pi var lightPos Vector3 = Vector3{0, -200, 600} -var sphere Sphere = Sphere{Vector3{0, 100, 10}, 10, RED_MAT} -var plane Plane = Plane{Vector3{0, 0, 1}, -10, GRID_OF_GRID_MAT} -var scene UnionSDF = UnionSDF{sphere, plane} +var sphere Sphere = Sphere{Vector3{0, 100, 5}, 10, RED_MAT} +var plane Plane = Plane{Vector3{0, 0, 1}, 0, WHITE_GREY_GRID_MAT} +var scene SmoothUnionSDF = SmoothUnionSDF{sphere, plane, 3.5} + +// var scene UnionSDF = UnionSDF{sphere, plane} var cameraPos Vector3 = Vector3{0, -10, 30} // radius, theta, phi diff --git a/sdf.go b/sdf.go index 1603f6b..d18de8e 100644 --- a/sdf.go +++ b/sdf.go @@ -1,6 +1,8 @@ package main -import "math" +import ( + "math" +) type SDF interface { Distance(Vector3) (float64, Material) @@ -121,7 +123,7 @@ func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) { 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(mat1, mat2, t, p) + mat := MixMat(mat2, mat1, t, p) return d, mat }