# Re-using loaded texture from gltf loader in RawShaderMaterial

**URL:** <https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501>\
**Category:** Questions\
**Tags:** gltf-loader, materials, textures\
**Created:** [December 8, 2021, 4:23pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501 "2021-12-08T16:23:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 4:23pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/1 "2021-12-08T16:23:41Z")

</div>

Hello,

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)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/5/e/5e088d7f92340759c12259ca7da688cc403062e0.png)

Here is the shader

```javascript
#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()
{
    SV_Target0.xyz = texture(_BaseMap, vs_TEXCOORD0.xy).xyz;
    SV_Target0.w = 1.0;
    return;
}

```

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)

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/9/8/981dcf60a74ea13d2ff2af23fbd1d1bec382bb3b.png)

and using a RawShaderMaterial:

```javascript
const material = new THREE.RawShaderMaterial({
                    uniforms: this.uniforms,
                    vertexShader: bundle.vertexShader,
                    fragmentShader: bundle.fragmentShader
                });

```

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 4:25pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/2 "2021-12-08T16:25:27Z")

</div>

In case it is helpful here is the loaded texture

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/c/ec421a431059892ca317682cfe9c381819da301c.png)

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 4:35pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/3 "2021-12-08T16:35:14Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/6/d/6d0542f9745642793590ae0263e79ef4562dd07b.png)

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [December 8, 2021, 5:35pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/4 "2021-12-08T17:35:47Z")

</div>

This looks like a gamma issue – see [Color management in three.js](https://www.donmccurdy.com/2020/06/17/color-management-in-threejs/). 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.

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 5:41pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/5 "2021-12-08T17:41:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [December 8, 2021, 6:10pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/6 "2021-12-08T18:10:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 6:18pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/7 "2021-12-08T18:18:56Z")

</div>

They are the same model and texture in both cases.

The screenshot [here](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/3) 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.

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 6:34pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/8 "2021-12-08T18:34:20Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![marwie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marwie/32/22424_2.png) [@marwie](https://discourse.threejs.org/u/marwie)\
**Post date:** [December 8, 2021, 9:26pm UTC](https://discourse.threejs.org/t/re-using-loaded-texture-from-gltf-loader-in-rawshadermaterial/32501/9 "2021-12-08T21:26:28Z")

</div>

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 🙄

Thanks for your help as always 🙂
