THREE.JS无法读取未定义的属性“ matrixWorld”

时间:2019-08-06 07:24:07

标签: three.js 3d

当我创建一个精灵时,OrbitControls可以完美地工作。 但是当我切换到FirstPersonControls时,所有内容都会冻结,并且控制台会显示此错误:

Uncaught TypeError: Cannot read property 'matrixWorld' of undefined
    at Sprite.raycast (three.js:26051)
    at intersectObject (three.js:44583)
    at Raycaster.intersectObjects (three.js:44658)
    at animate (mainview.php?project_id=46&tipo=1:936)

射线广播代码:


   if (firstorbit) {

           if (l % 5 == 0) {
               var vector = new THREE.Vector3(0, -1, 0);

               vector = camera.localToWorld(vector);
               vector.sub(camera.position);

               var raycasterGround = new THREE.Raycaster(camera.position, vector);

               var intersectsGround = raycasterGround.intersectObjects(scene.children, true);


               if (intersectsGround.length > 0) {          

                   if (intersectsGround[0].distance < 1.21) {

                       camera.position.y = (camera.position.y + (1.21 - intersectsGround[0].distance));

                   }


                   if (intersectsGround[0].distance > 1.36 && intersectsGround[0].distance < 1.45) {

                       camera.position.y = (camera.position.y - (intersectsGround[0].distance - 1.215));


                   }

               }


           }
           if (l % 3 == 0) {

               var vector1 = new THREE.Vector3(0, 0, -1);

               vector1 = camera.localToWorld(vector1);
               vector1.sub(camera.position);

               var raycasterFront = new THREE.Raycaster(camera.position, vector1);

               var intersectsFront = raycasterFront.intersectObjects(scene.children, true);


               if (intersectsFront.length > 0) {


                   if (intersectsFront[0].distance < 0.3) {
                       controls.moveForward = false;
                       controls.w = false;
                   } else {
                       controls.w = true;
                   }                  

               } else controls.w = true;
           }
       }

我在鼠标单击时用来测量某些内容的光线投射代码:

function getIntersections( event ) {
  var vectorMouse = new THREE.Vector2();

  vectorMouse.set(
   mouse.x,
    mouse.y );

  var raycasterClick = new THREE.Raycaster();
  raycasterClick.setFromCamera( vectorMouse, camera );

var intersectsClick = raycasterClick.intersectObjects( scene.children,true );


  return intersectsClick;

}

我不确定问题是否出在精灵或我的光线投射代码上。但是,当我删除精灵时,一切运行正常。而且,当我删除Raycasting代码时,一切也将正常运行。

0 个答案:

没有答案