简体   繁体   English

在three.js中绕圆旋转一条线

[英]Rotate a line around a circle in three.js

var lineGeometry  = new THREE.Geometry();
var lineMaterial = new THREE.LineBasicMaterial({
    color: 0x000000
});
lineGeometry.vertices.push(
    new THREE.Vector3(0, 0, 0),
    new THREE.Vector3(0, 10, 0),
);
var line = new THREE.Line(lineGeometry, lineMaterial);
scene.add(line);

I want to update this line's endpoint in a circular fashion.我想以循环方式更新这条线的端点。 In other languages it is straightforward, I would increase some variable and update the endpoint like radius * cos(dt) and radius * sin(dt).在其他语言中,这很简单,我会增加一些变量并更新端点,如半径 * cos(dt) 和半径 * sin(dt)。 In three.js I tried the following, but it doesn't work:在three.js中,我尝试了以下操作,但不起作用:

var dt = 0;
function render(){
    dt += 0.01;
    line.geometry.vertices[1].x = 10*Math.cos(dt);
    line.geometry.vertices[1].z = 10*Math.sin(dt);
    renderer.render(scene, camera);
    requestAnimationFrame(render);
}

edit: I don't want to use line.rotation.z += 0.01 method.编辑:我不想使用line.rotation.z += 0.01方法。

line.geometry.verticesNeedUpdate = true;

Add the above line before calling render .在调用render之前添加上面的行。 This will signal three.js that the vertex buffer has changed, so it will update it appropriately.这将通知three.js顶点缓冲区已更改,因此它将适当地更新它。

Since you mentioned using line.rotation , I must mention that updating the vertices is inefficient compared to rotating the line shape.由于您提到使用line.rotation ,我必须提到更新顶点与旋转线条形状相比效率低下。 When you rotate a line shape, you are simply updating the shape's transformation matrix.旋转线条形状时,您只需更新形状的变换矩阵。 But when you update vertices, three.js needs to re-upload the entire set of vertices to the GPU.但是当你更新顶点时,three.js 需要将整个顶点集重新上传到 GPU。 This may seem trivial for a single line segment, but it's a bad habit to get into if you ever intend to use larger shapes later.对于单个线段来说,这似乎微不足道,但如果您以后打算使用更大的形状,则养成这个坏习惯。

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

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