Compare commits

..

2 commits

Author SHA1 Message Date
10086e103b Multithreading 2025-09-27 21:38:10 +02:00
71c86e3570 Sdf return *Material change, 2x perf boost 2025-09-27 21:01:32 +02:00
4 changed files with 111 additions and 70 deletions

View file

@ -31,8 +31,11 @@ func (grid GridColor) GetColor(p Vector3) Vector3 {
return grid.color2.GetColor(p) return grid.color2.GetColor(p)
} }
// Ambre je te copie type Material interface {
type Material struct { GetMaterialBrut() MaterialBrut
}
type MaterialBrut struct {
ambiantColor Color ambiantColor Color
diffuseColor Color diffuseColor Color
diffuseFac float64 diffuseFac float64
@ -45,23 +48,12 @@ type Material struct {
refractIndice float64 refractIndice float64
} }
func DefaultMaterial(diffuseColor Color) Material { func (mat MaterialBrut) GetMaterialBrut() MaterialBrut {
return Material{ return mat
ambiantColor: ScaledColor{diffuseColor, 0.2},
diffuseColor: diffuseColor,
diffuseFac: 1,
specularColor: Vector3{255, 255, 255},
specularFac: 1,
specularExp: 35.0,
reflectanceFac: 0.0,
reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.0,
refractIndice: 1.0,
}
} }
func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) MaterialBrut {
return Material{ return MaterialBrut{
(mat1.ambiantColor.GetColor(p).Scale(1 - t)).Add((mat2.ambiantColor.GetColor(p)).Scale(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.diffuseColor.GetColor(p).Scale(1 - t)).Add((mat2.diffuseColor.GetColor(p)).Scale(t)),
mat1.diffuseFac*(1-t) + mat2.diffuseFac*t, mat1.diffuseFac*(1-t) + mat2.diffuseFac*t,
@ -75,6 +67,32 @@ func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material {
} }
} }
type MixedMaterial struct {
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)
}
func DefaultMaterial(diffuseColor Color) MaterialBrut {
return MaterialBrut{
ambiantColor: ScaledColor{diffuseColor, 0.2},
diffuseColor: diffuseColor,
diffuseFac: 1,
specularColor: Vector3{255, 255, 255},
specularFac: 1,
specularExp: 35.0,
reflectanceFac: 0.0,
reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.0,
refractIndice: 1.0,
}
}
// Colors // Colors
var RED = Vector3{255, 0, 0} var RED = Vector3{255, 0, 0}

View file

@ -6,8 +6,8 @@ type Sphere struct {
material Material material Material
} }
func (s Sphere) Distance(p Vector3) (float64, Material) { func (s Sphere) Distance(p Vector3) (float64, *Material) {
return p.Length() - s.radius, s.material return p.Length() - s.radius, &s.material
} }
// Box // Box
@ -16,9 +16,9 @@ type Box struct {
material Material material Material
} }
func (s Box) Distance(p Vector3) (float64, Material) { func (s Box) Distance(p Vector3) (float64, *Material) {
q := p.Abs().Sub(s.dimensions) 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 // Plane
@ -27,6 +27,6 @@ type Plane struct {
material Material material Material
} }
func (s Plane) Distance(p Vector3) (float64, Material) { func (s Plane) Distance(p Vector3) (float64, *Material) {
return p.Dot(s.normal), s.material return p.Dot(s.normal), &s.material
} }

View file

@ -6,6 +6,8 @@ package main
import ( import (
"math" "math"
"runtime"
"sync"
rl "github.com/gen2brain/raylib-go/raylib" rl "github.com/gen2brain/raylib-go/raylib"
) )
@ -13,8 +15,8 @@ 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 = 250 const WIDTH = 500
const HEIGHT = 250 const HEIGHT = 500
const PI = math.Pi const PI = math.Pi
const SOFT_SHADOW_COEFF = 32 const SOFT_SHADOW_COEFF = 32
@ -36,7 +38,7 @@ func init() {
sphereMat := RED_MAT sphereMat := RED_MAT
sphereMat.specularFac = 0.5 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 // boxMat := BLUE_MAT
// box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}} // box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}}
@ -44,8 +46,8 @@ func init() {
// 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 = SmoothUnionSDF{SubstractionSDF{box, sphere}, plane, 2.5} scene = SmoothUnionSDF{sphere, plane, 2.5}
scene = UnionSDF{plane, sphere} // scene = UnionSDF{plane, sphere}
} }
@ -53,13 +55,14 @@ 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) brutMat := mat.GetMaterialBrut()
ambiant := brutMat.ambiantColor.GetColor(point)
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVec))
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower)
specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp) specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp)
specular := mat.specularColor.GetColor(point).Scale(specularPower) specular := brutMat.specularColor.GetColor(point).Scale(specularPower)
return ambiant.Add(diffuse).Add(specular) return ambiant.Add(diffuse).Add(specular)
} }
@ -90,7 +93,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(max(shadow(p), 0.3)) return true, phongShading(p, normal, *mat).Scale(max(shadow(p), 0.3))
} }
if dist > MAX_DIST { if dist > MAX_DIST {
break break
@ -101,32 +104,52 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
} }
func genImage() *rl.Image { func genImage() *rl.Image {
screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian() screenCartesian, _, thetaVec, phiVec := screenSpherical.SphericalToCartesian()
screenCenter := cameraPos.Add(screenCartesian) screenCenter := cameraPos.Add(screenCartesian)
image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black) image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black)
for pixel_x := range WIDTH { numWorkers := runtime.NumCPU()
for pixel_y := range HEIGHT { var wg sync.WaitGroup
sx := screenPhysicalSize * (float64(pixel_x)/WIDTH - 0.5) wg.Add(numWorkers)
sy := screenPhysicalSize * (float64(pixel_y)/WIDTH - 0.5)
origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy)) bandHeight := HEIGHT / numWorkers
direction := (origin.Sub(cameraPos)).Normalized()
hit, color_vec := rayMarch(origin, direction) for worker := range numWorkers {
var fr, fg, fb float64 startY := worker * bandHeight
if hit { endY := startY + bandHeight
fr, fg, fb = color_vec.Unpack() if worker == numWorkers-1 {
} else { endY = HEIGHT
fr, fg, fb = 0, 0, 0
}
fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255)
r, g, b := uint8(fr), uint8(fg), uint8(fb)
rl.ImageDrawPixel(image, int32(pixel_x), int32(pixel_y), rl.Color{R: r, G: g, B: b, A: 255})
} }
go func(y0, y1 int) {
defer wg.Done()
for pixelY := y0; pixelY < y1; pixelY++ {
for pixelX := range WIDTH {
sx := screenPhysicalSize * (float64(pixelX)/WIDTH - 0.5)
sy := screenPhysicalSize * (float64(pixelY)/HEIGHT - 0.5)
origin := screenCenter.Add(phiVec.Scale(-sx)).Add(thetaVec.Scale(sy))
direction := (origin.Sub(cameraPos)).Normalized()
hit, colorVec := rayMarch(origin, direction)
var fr, fg, fb float64
if hit {
fr, fg, fb = colorVec.Unpack()
} else {
fr, fg, fb = 0, 0, 0
}
fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255)
r, g, b := uint8(fr), uint8(fg), uint8(fb)
rl.ImageDrawPixel(image, int32(pixelX), int32(pixelY), rl.Color{R: r, G: g, B: b, A: 255})
}
}
}(startY, endY)
} }
wg.Wait()
return image return image
} }

View file

@ -5,7 +5,7 @@ import (
) )
type SDF interface { type SDF interface {
Distance(Vector3) (float64, Material) Distance(Vector3) (float64, *Material)
} }
func DistanceOnly(sdf SDF, p Vector3) float64 { func DistanceOnly(sdf SDF, p Vector3) float64 {
@ -27,7 +27,7 @@ type TranslatedSDF struct {
translate Vector3 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)) return s.primitive.Distance(p.Sub(s.translate))
} }
@ -37,7 +37,7 @@ type RotatedSDF struct {
angle float64 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) rotated_p := Rotate(p, s.rotVector, s.angle)
return s.primitive.Distance(rotated_p) return s.primitive.Distance(rotated_p)
} }
@ -47,7 +47,7 @@ type ScaledSDF struct {
scale float64 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)) dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
return dist * s.scale, color return dist * s.scale, color
} }
@ -57,7 +57,7 @@ type RepeatSDF struct {
cellSize Vector3 cellSize Vector3
} }
func (s RepeatSDF) Distance(p Vector3) (float64, Material) { func (s RepeatSDF) Distance(p Vector3) (float64, *Material) {
x, y, z := p.Unpack() x, y, z := p.Unpack()
sx, sy, sz := s.cellSize.Unpack() sx, sy, sz := s.cellSize.Unpack()
round := math.RoundToEven round := math.RoundToEven
@ -70,7 +70,7 @@ type UnionSDF struct {
primitive2 SDF primitive2 SDF
} }
func (s UnionSDF) Distance(p Vector3) (float64, Material) { func (s UnionSDF) Distance(p Vector3) (float64, *Material) {
d1, color1 := s.primitive1.Distance(p) d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p) d2, color2 := s.primitive2.Distance(p)
d := math.Min(d1, d2) d := math.Min(d1, d2)
@ -87,7 +87,7 @@ type SubstractionSDF struct {
primitive2 SDF primitive2 SDF
} }
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) { 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)
@ -100,14 +100,14 @@ type IntersectionSDF struct {
primitive2 SDF primitive2 SDF
} }
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) { func (s IntersectionSDF) 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)
d := math.Max(d1, d2) d := math.Max(d1, d2)
mat := MixMat(mat1, mat2, 0.5, p) var mat Material = MixedMaterial{mat1, mat2, 0.5, p}
return d, mat return d, &mat
} }
type SmoothUnionSDF struct { type SmoothUnionSDF struct {
@ -116,15 +116,15 @@ type SmoothUnionSDF struct {
k float64 k float64
} }
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) { func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k k := 4 * s.k
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.Min(d1, d2) - h*h*0.25/k d := math.Min(d1, d2) - h*h*0.25/k
t := SmoothStep(d2-d1, -k, k) t := SmoothStep(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p) var mat Material = MixedMaterial{mat2, mat1, t, p}
return d, mat return d, &mat
} }
type SmoothSubstractionSDF struct { type SmoothSubstractionSDF struct {
@ -133,15 +133,15 @@ type SmoothSubstractionSDF struct {
k float64 k float64
} }
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) { func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k k := 4 * s.k
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(d1-d2, -k, k) t := SmoothStep(d1-d2, -k, k)
mat := MixMat(mat2, mat1, t, p) var mat Material = MixedMaterial{mat1, mat2, t, p}
return d, mat return d, &mat
} }
type SmoothIntersectionSDF struct { type SmoothIntersectionSDF struct {
@ -150,13 +150,13 @@ type SmoothIntersectionSDF struct {
k float64 k float64
} }
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) { func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) {
k := 4 * s.k k := 4 * s.k
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(d2-d1, -k, k)
mat := MixMat(mat2, mat1, t, p) var mat Material = MixedMaterial{mat1, mat2, t, p}
return d, mat return d, &mat
} }