Description of the problem
sorry for my poor english!
@pailhead use the code from your example:
http://dusanbosnjak.com/test/webGL/three-materials-extended/webgl_materials_extended_multiple_uvs_props_transform.html
var material = new THREE.MeshStandardMaterial({metalness:0});
var validMaps = {
'map': true,
'metalnessMap': true,
'normalMap': true
};
var setUvTransform = function ( tx, ty, sx, sy, rotation, cx, cy ) {
var c = Math.cos( rotation );
var s = Math.sin( rotation );
this.set(
sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
- sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
0, 0, 0
);
};
var pattern = /#include <(.*)>/gm;
function parseIncludes( string ){
function replace( match , include ){
var replace = THREE.ShaderChunk[ include ];
return parseIncludes( replace );
}
return string.replace( pattern, replace );
};
var mapRegex = /texture2D\( (.*Map|map), vUv \)/gm;
var onBeforeCompile = function ( shader ) {
var prependUniforms = '';
var _this = this;
function replaceMaps( string ){
function replace( match, mapName ) {
var uniformName = `u_${mapName}Transform`;
if(!validMaps[mapName] || shader.uniforms[uniformName]) return match; //modify by leven
prependUniforms += `uniform mat3 ${uniformName};\n`;
shader.uniforms[uniformName] = _this.userData.extraUniforms[uniformName];
shader.uniforms[uniformName].name = uniformName;
var replace = `texture2D( ${mapName}, ( ${uniformName} * vec3( vUv, 1. ) ).xy )`;
return replaceMaps( replace ) ;
}
return string.replace( mapRegex, replace );
}
shader.fragmentShader = parseIncludes(shader.fragmentShader) ;//"unroll" the entire shader
shader.fragmentShader = replaceMaps(shader.fragmentShader); //patch in the mapping stuff
shader.fragmentShader = prependUniforms + shader.fragmentShader;
};
var extraUniforms = {}
material.userData.extraUniforms = extraUniforms
var extraProps = []
material.userData.extraProps = extraProps
for ( var mapName in validMaps ){
if(material[mapName] !== undefined) {
material[`${mapName}Scale`] = new THREE.Vector2(1,1);
material[`${mapName}Rotation`] = 0;
var uniformName = `u_${mapName}Transform`;
var uniform = { value: new THREE.Matrix3() , name: mapName };
uniform.value.name = mapName;
uniform.value.setUvTransform = setUvTransform.bind(uniform.value);
extraUniforms[uniformName] = uniform ;
extraProps.push(mapName);
material[`${mapName}UpdateMatrix`] = function(_mapName){
var _uniformName = `u_${_mapName}Transform`;
this.userData.extraUniforms[_uniformName].value.setUvTransform(
0,
0,
this[`${_mapName}Scale`].x,
this[`${_mapName}Scale`].y,
this[`${_mapName}Rotation`],
0,
0
)
}.bind(material);
}
else{
validMaps[mapName] = false;
}
}
material.onBeforeCompile = onBeforeCompile.bind(material);
when set normalMap then get a error: