简体   繁体   English

如何在 THREE.js 中修改 gltf 模型的材质

[英]How can I modify a material of a gltf model in THREE.js

So I have a model I made in Blender, with all the lighting and textures baked, but when I import it into THREE.js (saved in .glb format), it defaults to the standard material.所以我有一个在 Blender 中制作的模型,所有的光照和纹理都已烘焙,但是当我将它导入 THREE.js(以 .glb 格式保存)时,它默认为标准材质。 This might be fine in some situations, but, as I said earlier, I have all the lighting and textures baked.在某些情况下这可能很好,但是,正如我之前所说,我已经烘焙了所有的光照和纹理。 How can I change the material from standard to basic without loosing my baking info and textures/having to import them as separate images?如何在不丢失烘焙信息和纹理/必须将它们作为单独图像导入的情况下将材料从标准更改为基本?

What you're looking for is called KHR_materials_unlit in glTF, or "Shadeless" in Blender.您正在寻找的是KHR_materials_unlit中的 KHR_materials_unlit 或 Blender 中的“Shadeless”。 You can export this from Blender to glTF using the following node graph :您可以使用以下节点图将其从 Blender 导出到 glTF:

节点图截图

With this arrangement, the exported glTF file will have KHR_materials_unlit applied to the material, and the baked lighting in the texture will be preserved in ThreeJS.通过这种安排,导出的 glTF 文件将KHR_materials_unlit应用于材质,纹理中的烘焙光照将保留在 ThreeJS 中。

In this way you can load your model and put a custom material on it.通过这种方式,您可以加载模型并在其上放置自定义材料。

This also should work for your glb model.这也应该适用于您的 glb 模型。 But Three js highly recommend to use gltf.但是三个js强烈推荐使用gltf。

const modelPath = "your model path";
const texturePath = "your texture path"

const loader = new GLTFLoader();
const texture = new TextureLoader().load(texturePath);
loader.load( modelPath, function ( gltf ) {
    const model = gltf.scene;
    model.traverse((obj) => {
        if(obj instanceof Mesh){
            obj.material = new MeshBasicMaterial({map: texture})
            }
        }
    )

}, undefined, function ( error ) {
    console.error( error );
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM