#Shader After Upgrade

40 messages · Page 1 of 1 (latest)

worthy hare
#

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;
#

Result:

FRAGMENT

ERROR: 0:1386: 'uv' : undeclared identifier
ERROR: 0:1386: 'x' :  field selection requires structure, vector, or interface block on left hand side
ERROR: 0:1387: 'vUv' : undeclared identifier
ERROR: 0:1387: 'y' :  field selection requires structure, vector, or interface block on left hand side
worldly panther
#

Would you like to be served a casual, easy solution - or a hardcore, potentially time-consuming one?

worthy hare
#

Hmm

#

How about we start with the casual one which I assume is adding my own to be passed in

#

But I'd like to know what changed too

#

I'm actually not even sure why vUv was around by default before

worldly panther
#

nope, the casual one would be to fix three at r150, since there isn't much gain from upgrading to r153 either way

worldly panther
worthy hare
#

Hmmm okay I see. Is it some type of array now?

#

What do you mean by split

sullen wyvern
#

I'd look at the release notes. There were many related changes.

worldly panther
worthy hare
#

Ok I only looked at the latest sounds like it was two back I'll read them

#

Ok so mapUv

worldly panther
#

nope

#

it maps to another things

#

first things first, maybe try adding #define USE_UV at the top of your shader

worthy hare
#

Ok sec

worldly panther
#

(I'm personally lost in the complexity of that new channels' UV and what sets what - but it is what it is 🥲 )

#

if USE_UV doesn't do the trick, try USE_UV1 , replace uv with uv1, and vMapUv instead of vUv

worthy hare
#

Alright that did not have vUv defined, when I print out the standard shader it does a #include <uv_pars_fragment>

#

Ok I get trying to use USE_UV1 and uv1 but should I be using the map at all?

worldly panther
#

Only UVs got changed

worthy hare
# worldly panther Only UVs got changed

Ok, for the record simply changing it to vMapUv worked, I got confused since you earlier said that it maps to other things, maybe by default if there is one it just takes first one?

#

What did you mean it maps to other things?

#

Also I found the two tickets that lead to multi-uv support will read them now

worldly panther
#

As earlier - I’m not yet sure myself how this new uv machine works, iirc in code vMapUv maps to uv<channel of map texture>, but only map texture is defined, which maps to attrib uvTransform0/1/2 and so on - vMapUv will work, but it exists only when .map texture is defined

#

(vUv earlier existed always, that’s why I think using uv0 would be safer - but I might have messed but the uniform name, would need to double-check later what is called how 💀)

worthy hare
#

Ah okay I see waht you mean

#

In my case .map is always set I think

#

So I'm looking at tickets related to release notes for multi-channel support and dont see any referencs to what is vMapUv but that would make sense. So is it the case when using multiple we are supposed to reference like vUv1...vUvN to correspond to the channels?

worldly panther
#

but earlier you mentioned uv / uv1 etc. aren't defined for you?

#

(ohhhh wait - maybe they aren't indeed defined, because your code is in fragment shader, and the UV vectors are generated in vertex shader)

worthy hare
#

Yeah I think that would be the issue

#

I was getting away with not modifying my vertex

worldly panther
#

then you have only two choices:
a) define USE_UV, because:

#ifdef USE_UV

    vUv = vec3( uv, 1 ).xy;

#endif

then you can use vUv in fragments
b) use vMapUv and assume .map is always defined

#

(these are code snippets from uv_vertex.glsl.js)