How to put an object in front of camera in THREE.JS?

后端 未结 4 1619
野性不改
野性不改 2021-02-09 03:47

I\'m trying to put an object in front of the camera, but have not been able to.

I\'m using the FlyControls what moves the camera, and I now want to put an object in fron

4条回答
  •  一生所求
    2021-02-09 03:56

    var cx, cy, cz, lx, ly, lz;
    
    
    
    dir.set(0,0,-1);
    dir.applyAxisAngle(0,camera.rotation.x);
    dir.applyAxisAngle(1,camera.rotation.y);
    dir.applyAxisAngle(2,camera.rotation.z);
    var dist = 100;
    
    cx = camera.position.x;
    cy = camera.position.y;
    cz = camera.position.z;
    
    lx = dir.x;
    ly = dir.y;
    lz = dir.z;
    
    
    var l;
    
    l = Math.sqrt((dist*dist)/(lx*lx+ly*ly+lz*lz));
    
    var x1, x2;
    var y1, y2;
    var z1, z2;     
    
    x1 = cx + lx*l;
    x2 = cx - lx*l;
    
    y1 = cy + ly*l;
    y2 = cy - ly*l;
    
    z1 = cz + lz*l;
    z2 = cz - lz*l;
    
    
    nanobot.position.set(x1, y1, z1 );
    

    I tried to calculate the direction vector of the direction of the camera, and then calculate the line that passes through the chamber, put a point on this line at a distance from the camera

提交回复
热议问题