简体   繁体   English

相机移动时更新对象的几何形状会导致毛刺-three.js

[英]updating an object's geometry when camera is moving causes glitches - three.js

I have a problem updating a vertex of a line in three.js 我在更新three.js中的线的顶点时遇到问题

So, I want to have a line in my scene, that its start is always at the (0,0,0) and its end is always in a specific position of the users screen (in x,y coordinates). 因此,我想在场景中有一条线,其起点始终在(0,0,0),终点始终在用户屏幕的特定位置(以x,y坐标表示)。

What I do to achieve that (and I almost succeed) is to have an invisible plane looking always to the camera and also have its position always a little bit in front of the camera. 我要做的(我几乎成功了)要做的是让一个不可见的平面始终朝着相机看,并且它的位置总是在相机前面。 The reason I do that is because I want the line to seem like "going towards" the user's screen. 之所以这样做,是因为我希望该行看起来像“朝着”用户的屏幕前进。 So I "send" a raycaster from the desired screen position (in x,y) and I check in which point of the plane it intersect and that's my 3D point in three.js scene. 因此,我从所需的屏幕位置(以x,y为单位)“发送”了一个射线广播器,然后检查它在平面的哪个点相交,这就是我在three.js场景中的3D点。 Then I update one of the 2 vertices of the line. 然后,我更新直线的两个顶点之一。

The problem 问题

What I do works fine, the line end is where I want to be, but something in updating the camera and the vertex is not synchronized and causes some noticeable glitches. 我所做的工作正常,行尾是我想要的位置,但是更新相机和顶点时有些不同步,并导致一些明显的故障。 When I move the camera, the line do not update itself quickly and smoothly, and as a result I see the line in other position before I see it in the calculated and desireable one. 当我移动相机时,线条不会快速,流畅地更新,因此,在看到已计算且令人满意的线条之前,我会看到线条处于其他位置。

Please take a look at this jsfiddle I created to emulate the problem. 请看一下我创建的用于模拟该问题的jsfiddle

What can I do to avoid these glitches? 我该如何避免这些故障?

Thanks 谢谢

code i use in render function : 我在render函数中使用的代码:

    var cameToCenterScaled = camera.position.clone();
    cameToCenterScaled.setLength(cameToCenterScaled.length()*0.9);
    plane.position.set(cameToCenterScaled.x, cameToCenterScaled.y, cameToCenterScaled.z); 

    plane.lookAt(camera.position);

    // define in pixels where in screen we want the line to end

    var notePos = findNotePoint(120,30); 
    linemesh.geometry.vertices[ 1 ].set(notePos.x, notePos.y, notePos.z) ; 
    linemesh.geometry.verticesNeedUpdate = true;

when you raycast you set the raycaster from camera, you have to make sure the camera matrices are updated 光线投射时,请从相机设置光线投射器,则必须确保更新了相机矩阵

simply add 只需添加

camera.updateMatrixWorld();

before you call 致电之前

raycaster.setFromCamera( new THREE.Vector2( x_, y_ ) , camera ); 

and the line will behave as you described 该行将按照您描述的方式运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM