类型错误:无法读取未定义的属性(读取“updateWorldMatrix”)

问题描述 投票:0回答:2

我对 ThreeJS 不太熟练。

我正在尝试创建边界框来包裹 3D 模型。模型加载得很好,因为我可以在场景中看到它

const loader = new GLTFLoader();
loader.load(
  modelsPath[ 0 ],
  ( gltf ) => {
    mesh = gltf.scene;
    mesh.visible = false;
    mesh.matrixAutoUpdate = false;
    scene.add( mesh );
    box1 = new THREE.Box3().setFromObject( mesh.asset );
    group.add( box1 );
  },
  null,
  function ( error ) {
    console.log( error );
  }
);

但是当我使用

box1 = new THREE.Box3().setFromObject( mesh.asset );
时,我收到以下错误:

TypeError: Cannot read properties of undefined (reading 'updateWorldMatrix')
at Box3.expandByObject (Box3.js:160:1)

你有什么想法吗?

three.js 3d-model
2个回答
0
投票

试试这个

const loader = new GLTFLoader();
let mesh;
let bb;

loader.load("REACT.glb", (gltf) => {
  mesh = gltf.scene;
  scene.add(mesh);
  bb = new THREE.Box3().setFromObject(mesh);
  const boxHelper = new THREE.Box3Helper(bb, 0x00ff00);
  scene.add(boxHelper);
});

https://thirdjs.org/docs/#api/en/helpers/BoxHelper

顺便说一句,当您将可见性属性设置为

false
中的网格时,如何才能看到网格?哦哦

 mesh.visible = false;

0
投票

Box3 是一个数学对象,而不是

Object3D
,这意味着它不能添加到
Scene
对象。 您要找的参考是BoxHelper,如下:

const box = new THREE.BoxHelper(model, 0xff0000);
scene.add(box);

示例

© www.soinside.com 2019 - 2024. All rights reserved.