[英]Three.js GLTF change object material to another objects material
所以我有这个场景,它来自我使用 GLTFLoader 导入的 glb 文件。 它包含不同 colors 中的这些对象。每个 object 都有其材料(RedMat、BlueMat、GreenMat 等)。 这些材料是在 Blender 中制作的,它们都使用相同的纹理文件(这个文件),它基本上只是一个调色板。 因此,对象的不同 colors 仅来自具有不同 UV 的每种材料。
到目前为止效果很好,当我使用 GLTFLoader 加载对象并将其显示在我的 three.js 场景中时,对象具有正确的 colors(屏幕截图来自 three.js 渲染)。 但我希望能够更新一个 object 的材质,例如红色 object 采用绿色 object 的材质。
new GLTFLoader().load('myfile.glb', function (gltf) {
const redGuy = gltf.scene.children.find(x => x.name === 'RedGuy');
const greenGuy = gltf.scene.children.find(x => x.name === 'GreenGuy');
redGuy.material = greenGuy.material; // I expect here that the red guy would become green, but nothing happens
// But if I instead do this:
redGuy.material = new THREE.MeshPhongMaterial({color: '#FF0000'});
// that works, but I want to be able to use the materials I already setup in Blender that are in the glb-file
});
此外,如果我在控制台中使用 console.log redGuy.material 和 greenGuy.material,它们看起来几乎相同,我觉得这很奇怪,它们只是具有不同的名称和 ID。
显然这是一个演示我的问题的简短片段,我希望这就足够了。 否则,如果需要的话,我可以将整个项目和 Blender 文件上传到某个地方。
好的,所以我终于找到了答案。 代替
redGuy.material = greenGuy.material;
我应该写
redGuy.geometry = greenGuy.geometry;
在这种情况下,不同对象的 colors 由 UV 坐标确定。 我认为这些将是材质节点的一部分,但它们似乎在几何节点中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.