clean up
This commit is contained in:
parent
f5c912c489
commit
a7f8b46910
8 changed files with 28 additions and 52 deletions
162
sdf.go
162
sdf.go
|
|
@ -1,162 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type SDF interface {
|
||||
Distance(Vector3) (float64, Material)
|
||||
}
|
||||
|
||||
func DistanceOnly(sdf SDF, p Vector3) float64 {
|
||||
dist, _ := sdf.Distance(p)
|
||||
return dist
|
||||
}
|
||||
|
||||
func Gradient(sdf SDF, p Vector3, eps float64) Vector3 {
|
||||
dx := (DistanceOnly(sdf, p.Add(Vector3{X: eps})) - DistanceOnly(sdf, p.Add(Vector3{X: -eps}))) / (2 * eps)
|
||||
dy := (DistanceOnly(sdf, p.Add(Vector3{Y: eps})) - DistanceOnly(sdf, p.Add(Vector3{Y: -eps}))) / (2 * eps)
|
||||
dz := (DistanceOnly(sdf, p.Add(Vector3{Z: eps})) - DistanceOnly(sdf, p.Add(Vector3{Z: -eps}))) / (2 * eps)
|
||||
return Vector3{X: dx, Y: dy, Z: dz}
|
||||
}
|
||||
|
||||
// Some transformations see https://iquilezles.org/articles/distfunctions/
|
||||
|
||||
type TranslatedSDF struct {
|
||||
primitive SDF
|
||||
translate Vector3
|
||||
}
|
||||
|
||||
func (s TranslatedSDF) Distance(p Vector3) (float64, Material) {
|
||||
return s.primitive.Distance(p.Sub(s.translate))
|
||||
}
|
||||
|
||||
type RotatedSDF struct {
|
||||
primitive SDF
|
||||
rotVector Vector3
|
||||
angle float64
|
||||
}
|
||||
|
||||
func (s RotatedSDF) Distance(p Vector3) (float64, Material) {
|
||||
rotated_p := Rotate(p, s.rotVector, s.angle)
|
||||
return s.primitive.Distance(rotated_p)
|
||||
}
|
||||
|
||||
type ScaledSDF struct {
|
||||
primitive SDF
|
||||
scale float64
|
||||
}
|
||||
|
||||
func (s ScaledSDF) Distance(p Vector3) (float64, Material) {
|
||||
dist, color := s.primitive.Distance(p.Scale(1 / s.scale))
|
||||
return dist * s.scale, color
|
||||
}
|
||||
|
||||
type RepeatSDF struct {
|
||||
primitive SDF
|
||||
cellSize Vector3
|
||||
}
|
||||
|
||||
func (s RepeatSDF) Distance(p Vector3) (float64, Material) {
|
||||
x, y, z := p.Unpack()
|
||||
sx, sy, sz := s.cellSize.Unpack()
|
||||
round := math.RoundToEven
|
||||
nearest_cell := Vector3{sx * round(x/sx), sy * round(y/sy), sz * round(z/sz)}
|
||||
return s.primitive.Distance(p.Sub(nearest_cell))
|
||||
}
|
||||
|
||||
type UnionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
color := color1
|
||||
if d2 < d1 {
|
||||
color = color2
|
||||
}
|
||||
return d, color
|
||||
}
|
||||
|
||||
type SubstractionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
}
|
||||
|
||||
func (s SubstractionSDF) Distance(p Vector3) (float64, Material) {
|
||||
d1, color1 := s.primitive1.Distance(p)
|
||||
d2, _ := s.primitive2.Distance(p)
|
||||
|
||||
d := math.Max(-d1, d2)
|
||||
return d, color1
|
||||
}
|
||||
|
||||
type IntersectionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
}
|
||||
|
||||
func (s IntersectionSDF) Distance(p Vector3) (float64, Material) {
|
||||
|
||||
d1, mat1 := s.primitive1.Distance(p)
|
||||
d2, mat2 := s.primitive2.Distance(p)
|
||||
d := math.Max(d1, d2)
|
||||
|
||||
mat := MixMat(mat1, mat2, 0.5, p)
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothUnionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
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)
|
||||
mat := MixMat(mat2, mat1, t, p)
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothSubstractionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
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)
|
||||
mat := MixMat(mat2, mat1, t, p)
|
||||
return d, mat
|
||||
}
|
||||
|
||||
type SmoothIntersectionSDF struct {
|
||||
primitive1 SDF
|
||||
primitive2 SDF
|
||||
k float64
|
||||
}
|
||||
|
||||
func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) {
|
||||
k := 4 * s.k
|
||||
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)
|
||||
mat := MixMat(mat2, mat1, t, p)
|
||||
return d, mat
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue