cmd = gl.regl({
vert: `
// the vertex shader turns {radius, angle, side} into {x, y}
// the 'side' varies from -1 to +1, and is 0 along the center
// of the petal
precision mediump float;
attribute vec3 ras;
varying vec3 v_ras;
uniform float width, power, phase;
void main () {
float radius = ras.r;
// shape the petal! could be anything, sine waves, exponentials, but it's
// angular width as a function of the radius, ideally 0 at radius 0 and 0 at radius 1
float w = 1.0 - pow(radius, power);
// now that we know the width of the petal, we can use that to shift
// the angle left or right based on the 'side' and the width
float angle = ras.g + width * w * ras.b;
// wave the petal back and forth
angle += phase * 0.2 * radius;
// convert polar to cartesian
vec2 pos = vec2(radius * cos(angle), radius * sin(angle));
gl_Position = vec4(pos, ras.b, 1);
// pass this along for the fragment shader to use in the color
v_ras = ras;
}
`,
frag: `
precision mediump float;
varying vec3 v_ras;
void main () {
// color by how far we are from the center line of the petal
float s = abs(v_ras.b);
gl_FragColor = vec4(0.5 + 0.5*s, 0.3, 0.8 - 0.5*s, 1);
}
`,
attributes: {ras: flowerMesh},
uniforms: {
width: width,
power: power,
phase: gl.regl.prop('phase'),
},
count: flowerMesh.length,
})