You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
650 B

package sdf
import "math"
const (
minDistance = 1e-3
maxDistance = 1e3
shiftCoefficient = 0.5
)
type SDF func(p Vector3) float64
func Min(functions ...SDF) SDF {
return func(p Vector3) float64 {
dist := -math.MaxFloat64
for _, sdf := range functions {
dist = math.Max(sdf(p), dist)
}
return dist
}
}
func GetIntersection(ray Ray, sdf SDF) (Vector3, bool) {
dist := sdf(ray.origin)
for dist > minDistance && dist < maxDistance {
ray.origin = ray.origin.Add(ray.dir.Multiply(dist * shiftCoefficient))
dist = sdf(ray.origin)
}
return ray.origin.Add(ray.dir.Multiply(dist * shiftCoefficient)), dist < maxDistance
}