Re-using loaded texture from gltf loader in RawShaderMaterial


I’m trying to re-use a texture loaded in a gltf loader getting the texture from associations.

When rendering the object with the standard material everything looks fine but when using the shader compiled in Unity it looks like the texture format is wrong.

See the following example (left Unity, right in threejs with a stripped down fragment shader)

Here is the shader

#version 300 es
#extension GL_EXT_shader_texture_lod : enable
precision highp float;
precision highp int;

uniform mediump sampler2D _BaseMap;
in highp vec2 vs_TEXCOORD0;
out highp vec4 SV_Target0;

void main()
{ = texture(_BaseMap, vs_TEXCOORD0.xy).xyz;
    SV_Target0.w = 1.0;

when loading the texture all I do is forward it from the gltf file as a uniform value (getTexture only does some plumbing finding the correct texture using a guid from unity)


and using a RawShaderMaterial:

const material = new THREE.RawShaderMaterial({
                    uniforms: this.uniforms,
                    vertexShader: bundle.vertexShader,
                    fragmentShader: bundle.fragmentShader

In case it is helpful here is the loaded texture

this is with the MeshStandardMaterial without changing the material/shader/anything after load

This looks like a gamma issue – see Color management in three.js. Normally three.js does sRGB decoding in the shader, but Unity would do that in graphics hardware instead so that’s probably why it’s not included in the generated shader.

Would that explain channels being flipped also? Looks like it in the very first image to me and I’m just sampling the same texture in the fragment.

Perhaps. Try texture.flipY = false maybe? If these are default spheres in three.js and Unity, rather than the same GLB model of a sphere, then they might have different texture coordinate / UV conventions too.

They are the same model and texture in both cases.

The screenshot here only differs in the shader being used (three standard vs raw shader mat).

When comparing the texture in the MeshStandard material and the one I assign to the RawShaderMaterial I see no difference in settings unfortunately.

I will try to compare shaders later and see if I can reproduce it in an empty environment if I cant find any other mistake :slight_smile:

Hello, found my stupid mistake. I was still flipping the tex coord because of an test few days ago when I tried loading the textures from disc and was flipping the sampler state y on export in codegen and didnt realize using a checkerboard texture :roll_eyes:

Thanks for your help as always :slight_smile:

1 Like