Skip to content

Instantly share code, notes, and snippets.

@elricmann
Created October 28, 2024 15:50
Show Gist options
  • Save elricmann/8195b41b4c5d7bf302d8f2d734c55746 to your computer and use it in GitHub Desktop.
Save elricmann/8195b41b4c5d7bf302d8f2d734c55746 to your computer and use it in GitHub Desktop.
Fragment shader for non-recursive De Casteljau's algorithm & cubic Bézier spline
// Copyright (c) 2024 Elric Neumann. All rights reserved. MIT license.
// Title: non-recursive De Casteljau's algorithm & cubic Bézier spline
#ifdef GL_ES
precision mediump float;
#endif
uniform float u_time;
uniform vec2 u_resolution;
vec3 contour(float t) {
t = clamp(t, 0.0, 1.0);
return t < 0.25 ? mix(vec3(0.0, 0.0, 0.5), vec3(0.0, 0.5, 1.0), t * 4.0) :
t < 0.5 ? mix(vec3(0.0, 0.5, 1.0), vec3(0.0, 1.0, 0.5), (t - 0.25) * 4.0) :
t < 0.75 ? mix(vec3(0.0, 1.0, 0.5), vec3(1.0, 1.0, 0.0), (t - 0.5) * 4.0) :
mix(vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0), (t - 0.75) * 4.0);
}
vec3 deCasteljau(vec3 p0, vec3 p1, vec3 p2, vec3 p3, float t) {
vec3 p01 = mix(p0, p1, t);
vec3 p12 = mix(p1, p2, t);
vec3 p23 = mix(p2, p3, t);
vec3 p012 = mix(p01, p12, t);
vec3 p123 = mix(p12, p23, t);
return mix(p012, p123, t);
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
vec3 p0 = vec3(0.2, 0.2, 0.0);
vec3 p1 = vec3(0.4, 0.8, 0.0);
vec3 p2 = vec3(0.6, 0.2, 0.0);
vec3 p3 = vec3(0.8, 0.8, 0.0);
float t = mod(u_time * 0.2, 1.0);
vec3 pos = deCasteljau(p0, p1, p2, p3, t);
vec2 direction = normalize(pos.xy - vec2(0.5, 0.5));
vec2 shiftedUV = uv + direction * 0.1 * sin(u_time * 0.5);
float distance = length(shiftedUV - pos.xy);
float heatmapIntensity = smoothstep(0.5, 0.0, distance);
vec3 heatmapColor = contour(heatmapIntensity);
float pointRadius = 0.02;
float pointIntensity = smoothstep(pointRadius, 0.0, length(uv - pos.xy));
vec3 pointColor = vec3(1.0, 0.6, 0.2);
vec3 color = mix(heatmapColor, pointColor, pointIntensity);
gl_FragColor = vec4(color, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment