modifyShader function

dynamic modifyShader(
  1. dynamic material,
  2. dynamic uniforms, {
  3. dynamic numberOfCurves = 1,
})

Implementation

modifyShader(material, uniforms, {numberOfCurves = 1}) {
  if (material.extra["__ok"] != null) return;
  material.extra["__ok"] = true;

  material.onBeforeCompile = (shader) {
    if (shader.__modified == null) {
      return;
    }

    shader.__modified = true;

    // Object.assign( shader.uniforms, uniforms );
    shader.uniforms.addAll(uniforms);

    var mainReplace = """
void main() {
#include <beginnormal_vertex>

vec4 worldPos = modelMatrix * vec4(position, 1.);

bool bend = flow > 0;
float xWeight = bend ? 0. : 1.;

#ifdef USE_INSTANCING
float pathOffsetFromInstanceMatrix = instanceMatrix[3][2];
float spineLengthFromInstanceMatrix = instanceMatrix[3][0];
float spinePortion = bend ? (worldPos.x + spineOffset) / spineLengthFromInstanceMatrix : 0.;
float mt = (spinePortion * pathSegment + pathOffset + pathOffsetFromInstanceMatrix)*textureStacks;
#else
float spinePortion = bend ? (worldPos.x + spineOffset) / spineLength : 0.;
float mt = (spinePortion * pathSegment + pathOffset)*textureStacks;
#endif

mt = mod(mt, textureStacks);
float rowOffset = floor(mt);

#ifdef USE_INSTANCING
rowOffset += instanceMatrix[3][1] * ${TEXTURE_HEIGHT}.;
#endif

vec3 spinePos = texture2D(spineTexture, vec2(mt, (0. + rowOffset + 0.5) / textureLayers)).xyz;
vec3 a =        texture2D(spineTexture, vec2(mt, (1. + rowOffset + 0.5) / textureLayers)).xyz;
vec3 b =        texture2D(spineTexture, vec2(mt, (2. + rowOffset + 0.5) / textureLayers)).xyz;
vec3 c =        texture2D(spineTexture, vec2(mt, (3. + rowOffset + 0.5) / textureLayers)).xyz;
mat3 basis = mat3(a, b, c);

vec3 transformed = basis
	* vec3(worldPos.x * xWeight, worldPos.y * 1., worldPos.z * 1.)
	+ spinePos;

vec3 transformedNormal = normalMatrix * (basis * objectNormal);
			""";

    var vertexShader = """
		uniform sampler2D spineTexture;
		uniform float pathOffset;
		uniform float pathSegment;
		uniform float spineOffset;
		uniform float spineLength;
		uniform int flow;

		float textureLayers = ${TEXTURE_HEIGHT * numberOfCurves}.;
		float textureStacks = ${TEXTURE_HEIGHT / 4}.;

		${shader.vertexShader}
		"""
        // chunk import moved in front of modified shader below
        .replaceAll('#include <beginnormal_vertex>', '')

        // vec3 transformedNormal declaration overriden below
        .replaceAll('#include <defaultnormal_vertex>', '')

        // vec3 transformed declaration overriden below
        .replaceAll('#include <begin_vertex>', '')

        // shader override
        .replaceFirst(RegExp("void\s*main\s*\(\)\s*\{"), mainReplace)
        .replaceFirst('#include <project_vertex>', """
        vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 );
				gl_Position = projectionMatrix * mvPosition;
        """);

    shader.vertexShader = vertexShader;
  };
}