nothing important

This commit is contained in:
Crizomb 2025-09-27 20:25:52 +02:00
parent 2e6f0a0730
commit 264a759ac5
3 changed files with 26 additions and 13 deletions

View file

@ -5,6 +5,14 @@ import "math"
type Color interface { type Color interface {
GetColor(Vector3) Vector3 GetColor(Vector3) Vector3
} }
type ScaledColor struct {
color Color
scale float64
}
func (sColor ScaledColor) GetColor(p Vector3) Vector3 {
return sColor.color.GetColor(p).Scale(sColor.scale)
}
type GridColor struct { type GridColor struct {
color1 Color color1 Color
@ -39,7 +47,7 @@ type Material struct {
func DefaultMaterial(diffuseColor Color) Material { func DefaultMaterial(diffuseColor Color) Material {
return Material{ return Material{
ambiantColor: Vector3{0, 0, 0}, ambiantColor: ScaledColor{diffuseColor, 0.2},
diffuseColor: diffuseColor, diffuseColor: diffuseColor,
diffuseFac: 1, diffuseFac: 1,
specularColor: Vector3{255, 255, 255}, specularColor: Vector3{255, 255, 255},

View file

@ -13,11 +13,11 @@ import (
const MAX_DIST = 1000.0 const MAX_DIST = 1000.0
const MAX_STEP = 1000 const MAX_STEP = 1000
const EPS = 0.01 const EPS = 0.01
const WIDTH = 500 const WIDTH = 250
const HEIGHT = 500 const HEIGHT = 250
const PI = math.Pi const PI = math.Pi
const SOFT_SHADOW_COEFF = 4 const SOFT_SHADOW_COEFF = 32
var lightPos Vector3 var lightPos Vector3
var scene SDF var scene SDF
@ -28,7 +28,7 @@ var screenPhysicalSize float64
var cameraPos Vector3 var cameraPos Vector3
func init() { func init() {
lightPos = Vector3{0, -200, 800} lightPos = Vector3{100, -400, 400}
cameraPos = Vector3{0, -10, 25} cameraPos = Vector3{0, -10, 25}
screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2} screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2}
@ -36,11 +36,16 @@ func init() {
sphereMat := RED_MAT sphereMat := RED_MAT
sphereMat.specularFac = 0.5 sphereMat.specularFac = 0.5
sphere := TranslatedSDF{Sphere{10, sphereMat}, Vector3{0, 100, 15}} sphere := TranslatedSDF{Sphere{5, sphereMat}, Vector3{0, 100, 10}}
// boxMat := BLUE_MAT
// box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}}
// sphere := RepeatSDF{Sphere{20, sphereMat}, Vector3{50, 50, 50}} // sphere := RepeatSDF{Sphere{20, sphereMat}, Vector3{50, 50, 50}}
plane := Plane{Vector3{0, 0, 1}, WHITE_GREY_GRID_MAT} plane := Plane{Vector3{0, 0, 1}, WHITE_GREY_GRID_MAT}
scene = UnionSDF{sphere, plane} // scene = SmoothUnionSDF{SubstractionSDF{box, sphere}, plane, 2.5}
scene = UnionSDF{plane, sphere}
} }
@ -48,7 +53,7 @@ func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
lightVec := (lightPos.Sub(point)).Normalized() lightVec := (lightPos.Sub(point)).Normalized()
reflectLight := Reflect(lightVec, normal) reflectLight := Reflect(lightVec, normal)
eyeVec := (cameraPos.Sub(point)).Normalized() eyeVec := (cameraPos.Sub(point)).Normalized()
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1) ambiant := mat.ambiantColor.GetColor(point)
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec))
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
@ -67,7 +72,7 @@ func shadow(point Vector3) float64 {
for range MAX_STEP { for range MAX_STEP {
dist, _ := scene.Distance(p) dist, _ := scene.Distance(p)
if dist < EPS { if dist < EPS {
return 0.1 return 0
} }
if dist > MAX_DIST { if dist > MAX_DIST {
break break
@ -85,7 +90,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
dist, mat := scene.Distance(p) dist, mat := scene.Distance(p)
if dist < EPS { if dist < EPS {
normal := Gradient(scene, p, EPS).Normalized() normal := Gradient(scene, p, EPS).Normalized()
return true, phongShading(p, normal, mat).Scale(shadow(p)) return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3))
} }
if dist > MAX_DIST { if dist > MAX_DIST {
break break

View file

@ -91,7 +91,7 @@ func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p) d1, color1 := s.primitive1.Distance(p)
d2, _ := s.primitive2.Distance(p) d2, _ := s.primitive2.Distance(p)
d := math.Max(-d1, d2) d := math.Max(d1, -d2)
return d, color1 return d, color1
} }
@ -138,8 +138,8 @@ func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
d1, mat1 := s.primitive1.Distance(p) d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p) d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(-d1-d2), 0.0) h := math.Max(k-math.Abs(-d1-d2), 0.0)
d := math.Max(-d1, d2) + h*h*0.25/k d := math.Max(d1, -d2) + h*h*0.25/k
t := SmoothStep(d2-d1, -k, k) t := SmoothStep(d1-d2, -k, k)
mat := MixMat(mat2, mat1, t, p) mat := MixMat(mat2, mat1, t, p)
return d, mat return d, mat
} }