I'm importing a glTF model that contains a UV set, and I want to assign a texture to it dynamically in ThreeJS by injecting shader code into the existing using onBeforeCompile().
So far everything works, however if I remove the test texture from the model in Blender and re-export it, then the shader refuses to compile with a vUv undeclared identifier error. When viewing the geometry buffer attributes in the debugging console, the UV maps seem to be defined, but aren't getting defined in the shader for some reason.
I don't want to export the model with a texture, that would mean loading the texture twice (once when the model was loaded, once when the texture is loaded dynamically).
Does anyone know how to get around this?
the UV maps seem to be defined, but aren't getting defined in the shader for some reason.
That happens because built-in materials only define the vUv varying if maps are sampled in the fragment shader. Since you have removed all maps from your asset, no varying is defined.
One solution to solve the problem is to define the varying manually via onBeforeCompile() by overwriting the uv_pars_fragment shader chunk with just varying vec2 vUv;.
After quite a bit of digging I found I can just set
material.defines.USE_UV = '';
And it will enable the UVs for the model