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

  1. package sdf
  2. import "math"
  3. const (
  4. minDistance = 1e-3
  5. maxDistance = 1e3
  6. shiftCoefficient = 0.5
  7. )
  8. type SDF func(p Vector3) float64
  9. func Min(functions ...SDF) SDF {
  10. return func(p Vector3) float64 {
  11. dist := -math.MaxFloat64
  12. for _, sdf := range functions {
  13. dist = math.Max(sdf(p), dist)
  14. }
  15. return dist
  16. }
  17. }
  18. func GetIntersection(ray Ray, sdf SDF) (Vector3, bool) {
  19. dist := sdf(ray.origin)
  20. for dist > minDistance && dist < maxDistance {
  21. ray.origin = ray.origin.Add(ray.dir.Multiply(dist * shiftCoefficient))
  22. dist = sdf(ray.origin)
  23. }
  24. return ray.origin.Add(ray.dir.Multiply(dist * shiftCoefficient)), dist < maxDistance
  25. }