将3D坐标转换为正交场景而没有相反的方向重复

时间:2019-01-23 18:35:46

标签: three.js

我有一个功能,可以将对象(v)的3D位置转换为相对于画布尺寸的相对坐标,然后使用正交摄影机将此位置投影到第二个场景以制作简单的HUD。

可以正常工作,但这会在与相机相反的方向上创建相同的位置。如何避免再次计算反方向的坐标?

var sprite = new THREE.Sprite(...);

var scene = net THREE.Scene();
var camera = new THREE.PerspectiveCamera(...);

var ortho_scene = net THREE.Scene();
var ortho_camera = new THREE.OrthographicCamera(...);

function normalize(val, min, max) { 
    // clip a number to range between -1 and 1 
    // to keep sprite always in the screen (to screen border)
    return Math.min(Math.max(val,min),max); 
}


function toScreenPosition(v, camera) {

  var vector = new THREE.Vector3().copy(v).project(camera);

  var x = normalize(vector.x, -1, 1)*(SCREEN_WIDTH*0.5);
  var y = normalize(vector.y, -1, 1)*(SCREEN_HEIGHT*0.5);

  sprite.position.set( x, y, 0 );   


    // sets the 'sprite' in 'ortho_scene' corectly between 'ortho_camera' 
    // and 'v', but when I rotate camera to 180°, the 'vector' will  be again 
    // vector.x < 1 AND vector.x > -1 
    // and will be rendered again on 'ortho_scene' on opposite direction. 
    // How to avoid that?

    // one idea, but it is not a solution
    if(new THREE.Vector2(vector.x,vector.y).angle() < Math.PI*0.5) {
       //keep sprite
       sprite.visible = true;
    } else {
       //hide sprite
       sprite.visible = false;
    }    


 }

我可以测试透视相机和点之间的角度是否大于Math.PI / 2

但是我需要始终在每个摄影机角度都指向对象。

0 个答案:

没有答案