Materials

This commit is contained in:
Crizomb 2025-09-26 04:33:09 +02:00
parent c0c129db88
commit f5d4a5cb7e
5 changed files with 113 additions and 106 deletions

63
sdf.go
View file

@ -3,7 +3,7 @@ package main
import "math"
type SDF interface {
Distance(Vector3) (float64, Color)
Distance(Vector3) (float64, Material)
}
func DistanceOnly(sdf SDF, p Vector3) float64 {
@ -25,7 +25,7 @@ type TranslatedSDF struct {
translate Vector3
}
func (s TranslatedSDF) Distance(p Vector3) (float64, Color) {
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
return s.primitive.Distance(p.Sub(s.translate))
}
@ -35,7 +35,7 @@ type RotatedSDF struct {
angle float64
}
func (s RotatedSDF) Distance(p Vector3) (float64, Color) {
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
rotated_p := Rotate(p, s.rotVector, s.angle)
return s.primitive.Distance(rotated_p)
}
@ -45,7 +45,7 @@ type ScaledSDF struct {
scale float64
}
func (s ScaledSDF) Distance(p Vector3) (float64, Color) {
func (s ScaledSDF) Distance(p Vector3) (float64, Material) {
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
return dist * s.scale, color
}
@ -55,7 +55,7 @@ type RepeatSDF struct {
cellSize Vector3
}
func (s RepeatSDF) Distance(p Vector3) (float64, Color) {
func (s RepeatSDF) Distance(p Vector3) (float64, Material) {
x, y, z := p.Unpack()
sx, sy, sz := s.cellSize.Unpack()
round := math.RoundToEven
@ -68,7 +68,7 @@ type UnionSDF struct {
primitive2 SDF
}
func (s UnionSDF) Distance(p Vector3) (float64, Color) {
func (s UnionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
d := math.Min(d1, d2)
@ -85,7 +85,7 @@ type SubstractionSDF struct {
primitive2 SDF
}
func (s SubstractionSDF) Distance(p Vector3) (float64, Color) {
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p)
d2, _ := s.primitive2.Distance(p)
@ -98,15 +98,14 @@ type IntersectionSDF struct {
primitive2 SDF
}
func (s IntersectionSDF) Distance(p Vector3) (float64, Color) {
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) {
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
c1 := color1.GetColor(p)
c2 := color2.GetColor(p)
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
d := math.Max(d1, d2)
color := c1.Add(c2).Scale(0.5)
return d, color
mat := MixMat(mat1, mat2, 0.5, p)
return d, mat
}
type SmoothUnionSDF struct {
@ -115,17 +114,15 @@ type SmoothUnionSDF struct {
k float64
}
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Color) {
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
c1 := color1.GetColor(p)
c2 := color2.GetColor(p)
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
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)
color := Mix(c1, c2, t)
return d, color
mat := MixMat(mat1, mat2, t, p)
return d, mat
}
type SmoothSubstractionSDF struct {
@ -134,17 +131,15 @@ type SmoothSubstractionSDF struct {
k float64
}
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Color) {
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
c1 := color1.GetColor(p)
c2 := color2.GetColor(p)
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
h := math.Max(k-math.Abs(-d1-d2), 0.0)
d := math.Max(-d1, d2) + h*h*0.25/k
t := SmoothStep(d2-d1, -k, k)
color := Mix(c1, c2, t)
return d, color
mat := MixMat(mat1, mat2, t, p)
return d, mat
}
type SmoothIntersectionSDF struct {
@ -153,14 +148,12 @@ type SmoothIntersectionSDF struct {
k float64
}
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Color) {
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
k := 4 * s.k
d1, color1 := s.primitive1.Distance(p)
d2, color2 := s.primitive2.Distance(p)
c1 := color1.GetColor(p)
c2 := color2.GetColor(p)
d1, mat1 := s.primitive1.Distance(p)
d2, mat2 := s.primitive2.Distance(p)
d := math.Max(k-math.Abs(d1-d2), 0.0)
t := SmoothStep(d2-d1, -k, k)
color := Mix(c1, c2, t)
return d, color
mat := MixMat(mat1, mat2, t, p)
return d, mat
}