Skip to main content

flowerSDF: Parametric Flower Distance Field

Organic Petal Generation through Polar Mathematics

The flowerSDF function creates flower-like shapes with configurable petal count using polar coordinate transformations. This 2D distance field generates smooth, organic forms through cosine modulation of angular components.

Mathematical Foundation

The flower shape emerges from polar coordinate manipulation:

dflower=1cos(Nθ2)0.5+0.5rd_{\text{flower}} = 1 - \frac{|\cos(\frac{N \cdot \theta}{2})| \cdot 0.5 + 0.5}{r}

where:

  • r=2p0.54r = 2|\vec{p} - 0.5| \cdot 4 represents the radial distance from center
  • θ=arctan(py,px)\theta = \arctan(p_y, p_x) gives the angular position
  • NN controls the number of petals

The cosine modulation creates regular undulations in the radial function, forming petal structures. The absolute value ensures positive petal formation, while the scaling factors control shape smoothness.

Function Signature

ParameterTypeDescription
stvec22D coordinate position
NintNumber of flower petals

Implementation Demonstrations

Live Editor
const fragment = () => {
      const petalCount = int(iTime.mul(2).sin().mul(3).add(6))
      const dist = flowerSDF(uv, petalCount)
      const inside = dist.step(0)
      const edge = float(0.02).smoothstep(0, dist.abs())
      const color = inside.mul(0.9).add(edge.mul(0.6))
      return vec4(color.mul(vec3(1, 0.7, 0.8)), 1)
}