【问题标题】:Follow in 3D space and rotation correct towards camera在 3D 空间中跟随并正确朝向相机旋转
【发布时间】:2016-06-08 10:45:27
【问题描述】:

我正在尝试让一些 3D 文本跟随移动的立方体。我希望文本始终平放在屏幕上,就像在屏幕空间中移动一样(即使它会在所有轴上移动)。因此,当它移动时,它需要对相机进行旋转校正。我正在尝试通过 meirm 在这里 (Converting 3D position to 2d screen position [r69!]) 调整答案以进行定位及其工作,但我最后的计算 (*1000) 完全是猜测工作(参见下面的函数)。应该是什么?以及如何解决朝向相机的旋转校正问题?

function toScreenPosition(obj, camera)
{
var vector = new THREE.Vector3();

//var widthHalf = 0.5*renderer.context.canvas.width;
//var heightHalf = 0.5*renderer.context.canvas.height;

obj.updateMatrixWorld();
vector.setFromMatrixPosition(obj.matrixWorld);
vector.project(camera);

//vector.x = ( vector.x * widthHalf ) + widthHalf;
//vector.y = - ( vector.y * heightHalf ) + heightHalf;

//how should I be calculating position here?
vector.x = vector.x * 1000;
vector.y = - vector.y * 1000;

console.log(vector.x + 'px, ' + vector.y + 'px');

return { 
    x: vector.x,
    y: vector.y
};

}

我的完整工作示例在这里: http://www.asquare.org/in-progress/webgldemo/rotation-test.html

【问题讨论】:

    标签: 3d three.js 2d


    【解决方案1】:

    您只需将 obj 的外观设置为朝向相机:

    obj.lookAt( camera.position );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多