Hello, I'm having trouble with a custom shader that's a modified version of MeshStandardMaterial. Here is how I'm modifying it but now after update vUv is undefined. How can I find the right varying name for the UV map now after the update? Thanks
mat.onBeforeCompile = (shader) => {
Object.assign(shader.uniforms,
{
uColorIdMap: { value: THIS.colorIdMap },
uTexId: { value: colorIdTexture }
});
console.log(shader.fragmentShader);
const keyword1 = 'void main() {';
shader.fragmentShader = shader.fragmentShader.replace(keyword1, `
// Converts a color from linear light gamma to sRGB gamma
vec4 fromLinear(vec4 linearRGB)
{
bvec4 cutoff = lessThan(linearRGB, vec4(0.0031308));
vec4 higher = vec4(1.055)*pow(linearRGB, vec4(1.0/2.4)) - vec4(0.055);
vec4 lower = linearRGB * vec4(12.92);
return mix(higher, lower, cutoff);
}
// Converts a color from sRGB gamma to linear light gamma
vec4 toLinear(vec4 sRGB)
{
bvec4 cutoff = lessThan(sRGB, vec4(0.04045));
vec4 higher = pow((sRGB + vec4(0.055))/vec4(1.055), vec4(2.4));
vec4 lower = sRGB/vec4(12.92);
return mix(higher, lower, cutoff);
}
uniform vec3 uColorIdMap[3];
uniform sampler2D uTexId;
${ keyword1 }`);
const keyword3 = '#include <color_fragment>';
shader.fragmentShader = shader.fragmentShader.replace(keyword3, `
int level = 0;
ivec2 size = textureSize(uTexId, level);
highp float width = float(size.x);
highp float height = float(size.y);
//highp int x = int( round( (vUv.x * width) + (0.5 / width) ) );
//highp int y = int( round( (vUv.y * height) + (0.5 / height) ) );
highp int x = int( round( vUv.x * (width - 1.0) ));
highp int y = int( round( vUv.y * (height - 1.0) ));
//vec4 tCol = texture2D(uTexId, vec2(float(x), float(y)));
//vec4 tCol = texture2D(uTexId, vUv);
vec4 tCol = texelFetch(uTexId, ivec2(x, y), level);
// Default to white seams for areas that are blended weird
vec3 hCol = vec3(1.0, 1.0, 1.0);
// find specified color based on id map
if (tCol.r == 1.0 && tCol.g == 0.0 && tCol.b == 0.0) {hCol = uColorIdMap[0];}
else if (tCol.g == 1.0 && tCol.r == 0.0 && tCol.b == 0.0) {hCol = uColorIdMap[1];}
else if (tCol.b == 1.0 && tCol.r == 0.0 && tCol.g == 0.0) {hCol = uColorIdMap[2];}
// Mix in specified color
diffuseColor = vec4(hCol, 1.0); // was *=
${ keyword3 }
`)
};
mat.needsUpdate = true;