将 material 应用于网格时出现控制台错误

Console error when applying material to mesh

我正在尝试将 MeshStandardMaterial 应用于 GLTF 导入模型的网格。用于测试的 GLTF 模型是从 Blender 导出的简单立方体。

成功加载 GLTF 模型后我做了:

this.material = new THREE.MeshStandardMaterial({
    map: this.texture,
})


this.cubePost2Model = this.resources.items.cubePost2.scene

this.cubePost2Model.traverse((child) =>
{
    if(child instanceof THREE.Mesh)
    {
        child.castShadow = true
        child.material = this.material  // This line throw the error
        console.log(child)              // Returns succesfully one mesh
    }
})

this.scene.add(this.cubePost2Model)

在前面的代码中,这一行:child.material = this.material 在控制台中抛出一个错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'visible')

projectObject() 中的那个点发生错误时,这意味着您正在将 undefined 分配给 child.material。所以似乎 this.material 没有指向预期的 material 对象。