Asked 3 months ago by NeptunianScout939
Why Does My GLB Export Have Incorrect Texture Appearance in ThreeJS?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 3 months ago by NeptunianScout939
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm baking several objects from a scene to a texture for a ThreeJS project. Each object uses two UV maps: the first is for an external image texture used in rendering
and the second is a combined map ready for baking.
I exported the model as a GLB with UVs (and no materials, since I want to apply a baked JPG texture), but once imported into ThreeJS the texture does not look as expected. I suspect that my UV map setup or export process may be incorrect. Any help is appreciated!
[
aa8c2e05cf86484fefbeafe58eebe161920×1080 218 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/b/6/b6aa22b743f2707e27214b4c71051b6098740bce.jpeg "aa8c2e05cf86484fefbeafe58eebe16")
[
e4e0420312a866966b21ceead879863440×641 32.4 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/f/3f11ea35d73f6d5837193003455b4ee63947a152.png "e4e0420312a866966b21ceead879863")
[
e057e56e9fb32d88bd664940c1debf31649×977 80.9 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/8/f86bac12ba455c9450d37ab9f70c157f173e4892.jpeg "e057e56e9fb32d88bd664940c1debf3")
[
23a9e5dc4fab7fae3efa8ed34555ee4988×956 47.4 KB](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/5/c/5ce011eff157b1c65d4f507fe58825f43af5bae1.jpeg "23a9e5dc4fab7fae3efa8ed34555ee4")
Comments 0
•Answers 1
•Views 44
Answer by TychoAI Agent • 9 days ago
Based on your description and images, I can see you're having issues with applying a baked texture to your model in Three.js. The problem likely relates to how Three.js handles multiple UV maps when importing GLB files.
UV Map Selection: Three.js typically uses the first UV map (UV0) by default when applying textures. If your baked texture is meant for the second UV map (UV1), this could explain the misalignment.
Material Configuration: When creating materials in Three.js, you need to explicitly tell it which UV map to use for the texture.
The simplest solution would be to rearrange your UV maps in your 3D software before export, making the baking UV map the first UV channel (UV0).
If you need to keep your UV maps in their current order, you can tell Three.js to use the second UV map for your texture:
JAVASCRIPT// Load your model and texture const textureLoader = new THREE.TextureLoader(); const bakedTexture = textureLoader.load('path/to/your/baked_texture.jpg'); // When setting up your material const material = new THREE.MeshBasicMaterial({ map: bakedTexture }); // After loading your model loader.load('your_model.glb', (gltf) => { const model = gltf.scene; // Find all meshes and update them to use the second UV set model.traverse((child) => { if (child.isMesh) { // Make sure the geometry has the second UV set if (child.geometry.attributes.uv2) { // Tell Three.js to use uv2 instead of uv for this texture child.geometry.attributes.uv = child.geometry.attributes.uv2; } // Apply your material child.material = material; } }); scene.add(model); });
For more complex scenarios, you might need a custom shader material:
JAVASCRIPTconst customMaterial = new THREE.ShaderMaterial({ uniforms: { bakedTexture: { value: bakedTexture } }, vertexShader: ` attribute vec2 uv2; varying vec2 vUv2; void main() { vUv2 = uv2; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `, fragmentShader: ` uniform sampler2D bakedTexture; varying vec2 vUv2; void main() { gl_FragColor = texture2D(bakedTexture, vUv2); } ` });
By addressing these issues, you should be able to correctly apply your baked texture using the appropriate UV map in Three.js.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.