[SOLVED] Get UV1 inside of a shader?

Hi,

I’m wondering how to pass or force UV1 data into a fragment shader. I’ve seen the forceUv1 variable here but am unsure exactly how to pass it in.

When using the UV1 in the shader I’m currently getting:

ERROR: 0:185: 'vUv1' : undeclared identifier 

Thanks!

I’ve never tried it but I believe something like this may work:

this.material.onUpdateShader( function(options){
   options.forceUv1 = true;
   return options;
});
this.material.update();

Thanks for your the help, but unfortunately that didn’t work.

Try posting an issue in the engine repo, maybe someone from the engine team knows how this is triggered.

This works, I’ve just tested it:

            // this generates this in shader: vec2 uv1 = getUv1();
            mat.onUpdateShader = function(options) {
                options.forceUv1 = true;
                return options;
            };
            
            // you need texture assigned to slot that uses uv1. 
            // Set some texture to lightMap slot (unless you already have some texture assigned). 
            // This generates VS instructions that output uv1 to fragment shader 
            // (varying vec2 vUv1; vUv1 = uv1;) 
            mat.lightMap = new pc.Texture(this.app.graphicsDevice, {
                width: 1,
                height: 1,
                format: pc.PIXELFORMAT_R8_G8_B8
            });

            mat.update();

I tried this custom fragment shader and that outputs UV1 as expected

void getAlbedo() {
    dAlbedo = vec3(vUv1.x, vUv1.y, 0.0);
}
3 Likes

Ah thank you. I think my issue was that I wasn’t assigning a texture.

2 Likes

Working through this now. Is there a way to test is a lightmap already exists, so I don’t overwrite it if there is one?
(also, I made one texture and I’m assigning it to various materials as I step through meshes in an entity, that’s OK, right?)

Bruce

Getting close, but I have a glass material and uv1 isn’t being created. Is it something about a glass material that would prevent uv1 being passed in?

FYI - in the current codebase it seems to show up as vUV1_1 not vUv1.

Bruce

Still not getting anywhere with this. Some materials don’t put a uv1 in the shader.

It also appears that when I make a fake lightmap to force uv1, the actual lighting method switches to lightmapping and breaks normal lighting.

Hi @brucewheaton,

Try creating a sample repro project to take a look at which materials fail to include this coord.

Will do - it seems to be materials with specular maybe? I have a glass material and a concrete material that don’t get UV1.

But also, the trick with the lightmap seems to kick it to lightmapping and other stuff is borked - a less tricky workaround would be great if there is one.

Bruce

1 Like