RayMarchingGo/src/primitives_sdf.go

32 lines
593 B
Go

package main
// Sphere
type Sphere struct {
radius float64
material Material
}
func (s Sphere) Distance(p Vector3) (float64, *Material) {
return p.Length() - s.radius, &s.material
}
// Box
type Box struct {
dimensions Vector3
material Material
}
func (s Box) Distance(p Vector3) (float64, *Material) {
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
}
// Plane
type Plane struct {
normal Vector3
material Material
}
func (s Plane) Distance(p Vector3) (float64, *Material) {
return p.Dot(s.normal), &s.material
}