String replacement is performed
<script id="vs" type="notjs"> uniform vec2 uMyPoints[<length>]; ... </script>
Js
var numPoints = 10; var vSrc = document.getElementById("vs").text; vSrc = vSrc.replace(/<length>/g, numPoints);
These are the most complex programs for shaders. They generate shaders with string manipulations.
Of course, you can use a more convenient function for string substitution. For example, it might be something like
var replaceParams = (function() { var replaceParamsRE = /%\(([^\)]+)\)s/g; return function(str, params) { if (!params.length) { params = [params]; } return str.replace(replaceParamsRE, function(match, key) { var keys = key.split('.'); for (var ii = 0; ii < params.length; ++ii) { var obj = params[ii]; for (var jj = 0; jj < keys.length; ++jj) { var part = keys[jj]; obj = obj[part]; if (obj === undefined) { break; } } if (obj !== undefined) { return obj; } } console.error("unknown key: " + key); return "%(" + key + ")s"; }); }; }());
now if you look like a shader
uniform Lights u_lights[%(numLights)s]; uniform vec2 u_points[%(numPoints)s];
you can replace
vSrc = replaceParams(vsrc, { numLights: 4, numPoints: 10, });
You can also use `#define in the shader
#define NUM_LIGHTS %(numLights)s #define NUM_POINTS %(numPoints)s uniform Lights u_lights[NUM_LIGHTS]; uniform vec2 u_points[NUM_POINTS]; void main() { for (int i = 0; i < NUM_LIGHTS; ++i) { ... } }
etc..
But honestly, most people did not pass the bezier breakpoints as uniforms, because there is a serious limitation on the number of uniforms. Most people passed bezier breakpoints in attributes. You can probably even set the pitch and offset when calling gl.vertexAttribPointer so that if your points go
[pt0, pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8, ..]
You can make 4 attributes
attribute vec2 p0; attribute vec2 p1; attribute vec2 p2; attribute vec2 p3;
And specify all of them with offset and step to set 4 attributes so that the points are pulled
p0 = pt0, p1 = pt1, p2 = pt2, p3 = pt3, p0 = pt1, p1 = pt2, p2 = pt3, p3 = pt4, p0 = pt2, p1 = pt3, p2 = pt4, p3 = pt5, p0 = pt3, p1 = pt4, p2 = pt5, p3 = pt6,
etc.