myREGL = `
#extension GL_OES_standard_derivatives : enable
#define MAXSDF 5.0
/**
* Compute norm2 between src and dst.
**/
float norm2(vec2 src, vec2 dst) {
return dot(src-dst, src-dst);
}
/**
* Compute distance between src and dst.
**/
float dist(vec2 src, vec2 dst) {
return sqrt(norm2(src, dst));
}
/**
* Compute the dist value among the signed distance field
**/
vec3 dist(sampler2D sdf, vec2 src, vec2 dst) {
float minsdf = MAXSDF;
float d0 = dist(src, dst), d, remain;
for (int i=0; i<1000; ++i) {
d = texture2D(sdf, src).x;
// Record the min sdf value during the trace
minsdf = min(minsdf, d);
// It is blocked, thus it will not reach dst
if (d <= 0.000001) {
return vec3(sqrt(abs(d)*20.0), 0.3, 0.3);
}
// It sees the dst directly, without being blocked at all
remain = dist(src, dst);
if (remain < d) {
return vec3(1.0-d0, 0.5, 0.0);
}
src = mix(src, dst, d/remain);
}
return vec3(minsdf, 0.0, 0.0);
}
/**
* Compute sdf from p to v.
* p is the given point;
* v is the edge of the triangle;
* c is the bary center of the triangle.
* Return sdf value in float.
**/
float sdf(vec2 p, vec2 v, vec2 c) {
vec2 u = vec2(v.y, -v.x);
if (dot(c, u) * dot(p, u) >= 0.0) {
return -abs(dot(p, u) / sqrt(dot(u, u)));
}
float vv = dot(v, v);
float pv = dot(p, v);
if (pv < 0.0) {
return sqrt(dot(p, p));
}
if (pv > vv) {
return sqrt(dot(p-v, p-v));
}
return abs(dot(p, u) / sqrt(dot(u, u)));
}
/**
* Compute the sdf of the uv against the triangle
* a is the triangle
* [[edge1, 1],
* [edge2, 1],
* [edge3, 1],
* [0, 0, 0, 1]]
**/
float sdf(vec2 uv, mat4 a) {
float d = 0.0;
d = dist(uv, vec2(a[0]));
vec4 center = (a[0] + a[1] + a[2]) / 3.0;
float d1 = sdf(uv-vec2(a[0]), vec2(a[1]-a[0]), vec2(center-a[0]));
float d2 = sdf(uv-vec2(a[1]), vec2(a[2]-a[1]), vec2(center-a[1]));
float d3 = sdf(uv-vec2(a[2]), vec2(a[0]-a[2]), vec2(center-a[2]));
d = max(max(d1, d2), d3);
if (d > 0.0) {
if (d > d1 && d1 > 0.0) {d = d1;}
if (d > d2 && d2 > 0.0) {d = d2;}
if (d > d3 && d3 > 0.0) {d = d3;}
}
return d;
}
float wireframe (float parameter, float width, float feather) {
float w1 = width - feather * 0.5;
float d = fwidth(parameter);
float looped = 0.5 - abs(mod(parameter, 0.1) - 0.5);
return smoothstep(d * w1, d * (w1 + feather), looped);
}
`