@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
58 lines (48 loc) • 1.76 kB
JavaScript
const SHADER_PREAMBLE = `
attribute mat4 instanceMatrix;
attribute vec4 instanceColor;
\n`;
export function rewriteMaterial(shader) {
const originalVertexShader = shader.vertexShader;
const newVertexShader = SHADER_PREAMBLE
+ originalVertexShader
.replace(
'#include <begin_vertex>',
`
vec3 transformed = ( instanceMatrix * vec4(position,1.0) ).xyz;
`
)
.replace(
'#include <beginnormal_vertex>',
`
vec3 objectNormal = vec3( normal );
{
// this is in lieu of a per-instance normal-matrix
// shear transforms in the instance matrix are not supported
mat3 m = mat3( instanceMatrix );
objectNormal /= vec3( dot( m[ 0 ], m[ 0 ] ), dot( m[ 1 ], m[ 1 ] ), dot( m[ 2 ], m[ 2 ] ) );
objectNormal = m * objectNormal;
}
vec3 objectTangent = vec3( tangent.xyz );
`
)
.replace(
'#include <color_vertex>',
`
vColor = instanceColor;
vColor = instanceColor.xyz;
`
);
shader.vertexShader = newVertexShader;
shader.fragmentShader = `
`+shader.fragmentShader;
}