Search code examples
three.jsgltf

simulate separate alpha map in glTF


I have an existing 3D application that was developed back in the days of the ThreeJS r92 that made use of a separate alpha map to simulate holes cut in wooden panels. This allowed re-use of a high quality woodgrain texture across all models, while using easily compressible black and white images to create the 'holes' in the wooden panels.

Now that I have begun migration of the project to the current glTF format, I find that the base color and alpha map now have to be combined. The result for my project is that now each of 130+ wooden panels will need their own "wood grain + alpha" texture, rather than being able to share a single wood grain texture.

From all my research, it seems like there are no obvious options to this situation using glTF.. my question now is - does anyone know of ANY workaround using glTF that allows me to separate the base color texture from the alpha map texture?

At this point the best way ahead sadly seems to be to avoid glTF and go back to using ObjectLoader, which is a pain as the binary objects of glTF are a huge plus.


Solution

  • You could export the GLTF with only the base color. Once imported into Three.js, you can manually assign the black/white texture as the material's .alphaMap property.

    // Fetch texture
    const texLoader = new THREE.TextureLoader();
    const alphaTexture = texLoader.load("path/to/alphaTexture");
    
    gltfLoader.load("path/to/model", function(gltf){
        // Find the mesh you want to assign an alphaMap to
        const myMesh = gltf.getObjectByName("meshName");
    
        // Now just bind your texture to the alphaMap property
        myMesh.material.alphaMap = alphaTexture;
        myMesh.material.transparent = true;
    });
    

    The docs state that only the green channel is used so it doesn't have to be black and white.