Skip to content

Commit

Permalink
fix elongate dimensions and bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Aug 17, 2024
1 parent eae4183 commit d67bdd0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (e *elongate) Evaluate(pos []ms3.Vec, dist []float32, userData any) error {
defer vp.V3.Release(transformed)
aux := vp.Float.Acquire(len(pos))
defer vp.Float.Release(aux)
h := e.h
h := ms3.Scale(0.5, e.h)
for i, p := range pos {
q := ms3.Sub(ms3.AbsElem(p), h)
aux[i] = math32.Min(q.Max(), 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/test/glsdf3test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var SmoothBinaryOps = []func(a, b glbuild.Shader3D, k float32) glbuild.Shader3D{
var OtherUnaryRandomizedOps = []func(a glbuild.Shader3D, rng *rand.Rand) glbuild.Shader3D{
randomRotation,
randomShell,
// randomElongate,
randomElongate,
randomRound,
randomScale,
randomSymmetry,
Expand Down
13 changes: 9 additions & 4 deletions operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ return mix( d2, d1, h ) + k*h*(1.0-h);`...)
}

// Elongate "stretches" the SDF in a direction by splitting it on the origin in
// the plane perpendicular to the argument direction. Arguments are distances, so zero-valued arguments are no-op.
// the plane perpendicular to the argument direction. The part of the shape in the negative
// plane is discarded and replaced with the elongated positive part.
//
// Arguments are distances, so zero-valued arguments are no-op.
func Elongate(s glbuild.Shader3D, dirX, dirY, dirZ float32) glbuild.Shader3D {
return &elongate{s: s, h: ms3.Vec{X: dirX, Y: dirY, Z: dirZ}}
}
Expand All @@ -559,8 +562,10 @@ type elongate struct {

func (u *elongate) Bounds() ms3.Box {
box := u.s.Bounds()
box.Min = ms3.MinElem(box.Min, ms3.Add(box.Min, u.h))
box.Max = ms3.MaxElem(box.Max, ms3.Add(box.Max, u.h))
// Elongate splits shape around origin and keeps positive bits only.
box.Max = ms3.MaxElem(box.Max, ms3.Vec{})
box.Max = ms3.Add(box.Max, ms3.Scale(0.5, u.h))
box.Min = ms3.Scale(-1, box.Max) // Discard negative side of shape.
return box
}

Expand All @@ -577,7 +582,7 @@ func (s *elongate) AppendShaderName(b []byte) []byte {
}

func (s *elongate) AppendShaderBody(b []byte) []byte {
b = appendVec3Decl(b, "h", s.h)
b = appendVec3Decl(b, "h", ms3.Scale(0.5, s.h))
b = append(b, "vec3 q = abs(p)-h;"...)
b = appendDistanceDecl(b, s.s, "d", "max(q,0.0)")
b = append(b, "return d + min(max(q.x,max(q.y,q.z)),0.0);"...)
Expand Down

0 comments on commit d67bdd0

Please sign in to comment.