40 lines
No EOL
946 B
Text
40 lines
No EOL
946 B
Text
#include "../math/const.msl"
|
|
|
|
/*
|
|
contributors: Kathy kfahn22
|
|
description: Returns a Koch curve SDF.
|
|
use: <float2> kochSDF(<float2> st, <int> iterations)
|
|
*/
|
|
|
|
#ifndef FNC_KOCHSDF
|
|
#define FNC_KOCHSDF
|
|
float kochSDF( float2 st, float2 center, int N ) {
|
|
st -= center;
|
|
st *= 3.0;
|
|
float r3 = sqrt(3.);
|
|
st = abs(st);
|
|
st += r3*float2(-st.y,st.x); // 60° rotation, scale 2
|
|
st.y -= 1.;
|
|
float w = .5;
|
|
mat2 m = mat2(r3,3,-3,r3)*.5;
|
|
#ifdef PLATFORM_WEBGL
|
|
for (int i = 0; i< 20; i++) {
|
|
if (i >= N) break;
|
|
#else
|
|
for (int i = 0; i< N; i++) {
|
|
#endif
|
|
st = float2(-r3,3)*.5 - m*float2(st.y,abs(st.x));
|
|
w /= r3;
|
|
}
|
|
float d = sign(st.y)*length(float2(st.y,max(0.,abs(st.x)-r3)));
|
|
return (d*w);
|
|
}
|
|
|
|
float kochSDF( float2 st, int N ) {
|
|
#ifdef CENTER_2D
|
|
return kochSDF(st, CENTER_2D, N);
|
|
#else
|
|
return kochSDF(st, float2(0.5), N);
|
|
#endif
|
|
}
|
|
#endif |