メインコンテンツまでスキップ

kochSDF: Koch Snowflake Fractal Distance Field

Iterative Fractal Boundary Generation through Recursive Subdivision

The kochSDF function generates the Koch snowflake fractal, a classic example of self-similar geometric structures. Through iterative subdivision and transformation, it constructs the characteristic spiky boundary pattern that defines this mathematical fractal.

Mathematical Foundation

The Koch snowflake construction follows a recursive subdivision process:

Kn+1=T(Kn)K_{n+1} = T(K_n)

where TT represents the transformation that replaces each line segment with four segments arranged in a triangular spike pattern. The transformation matrix:

M=12(3333)M = \frac{1}{2} \begin{pmatrix} \sqrt{3} & 3 \\ -3 & \sqrt{3} \end{pmatrix}

The distance calculation involves iterative coordinate transformations:

stn+1=12r3M(sty,stx)\vec{st}_{n+1} = \frac{1}{2}\vec{r_3} - M \cdot (st_y, |st_x|)

where r3=(3,3)\vec{r_3} = (-\sqrt{3}, 3) and the width scaling factor wn+1=wn3w_{n+1} = \frac{w_n}{\sqrt{3}}.

Function Variants

FunctionParametersDescription
kochSDFst, center, NFull parameterization with center and iteration count
kochSDFSimplest, NSimplified version with default center at (0.5, 0.5)

Implementation Demonstrations

ライブエディター
const fragment = () => {
      const pos = uv.sub(0.5)
      const dist = kochSDF(pos.add(0.5), vec2(0.5), int(10))
      const inside = dist.step(0)
      const edge = float(0.005).smoothstep(0, dist.abs())
      const color = inside.mul(0.9).add(edge.mul(0.7))
      return vec4(color.mul(vec3(0.2, 0.8, 1)), 1)
}