THREE.js 向 Points 几何体动态添加点不渲染 [英] THREE.js dynamically add points to a Points geometry does not render
问题描述
我使用 Three.js r83.
I am using Three.js r83.
我正在尝试向几何体动态添加点,但场景永远不会更新.
I am trying to dynamically add points to a geometry, but the scene never gets updated.
这有效:
var tmaterial = new THREE.PointsMaterial({
color: 0xff0000,
size: 5,
opacity: 1
});
var tgeometry = new THREE.Geometry();
var pointCloud = new THREE.Points(tgeometry, tmaterial);
for(var i = 0; i< 1000; i++) {
x = (Math.random() * 200) - 100;
y = (Math.random() * 200) - 100;
z = (Math.random() * 200) - 100;
tgeometry.vertices.push(new THREE.Vector3(x, y, z));
}
tgeometry.verticesNeedUpdate = true;
tgeometry.computeVertexNormals();
scene.add(pointCloud);
这不起作用:
var tmaterial = new THREE.PointsMaterial({
color: 0xff0000,
size: 5,
opacity: 1
});
var tgeometry = new THREE.Geometry();
var pointCloud = new THREE.Points(tgeometry, tmaterial);
scene.add(pointCloud);
for(var i = 0; i< 1000; i++) {
x = (Math.random() * 200) - 100;
y = (Math.random() * 200) - 100;
z = (Math.random() * 200) - 100;
tgeometry.vertices.push(new THREE.Vector3(x, y, z));
}
tgeometry.verticesNeedUpdate = true;
tgeometry.elementsNeedUpdate = true;
tgeometry.computeVertexNormals();
renderer.render(scene, camera);
如您所见,唯一的区别是我在添加顶点之前添加了 scene.add(pointCloud);
.
As you can see, the only difference is the fact that I add scene.add(pointCloud);
before adding vertexes.
我想念什么?
您可以找到 fiddle 感谢@hectate
You can find a fiddle Thanks to @hectate
要明白我的意思,只需替换
To see what I means, just replace
init();设置点();动画();
由
init();动画();设置点();
推荐答案
我不确定为什么 THREE.Geometry
对象在初始渲染后不更新 Points,但我使用THREE.BufferGeometry
代替.
I am not sure why the THREE.Geometry
object doesn't update Points after initial rendering, but I got it working with a THREE.BufferGeometry
instead.
感谢 @Hectate
为我提供了一个工作小提琴,以及 @WestLangley
为我提供了提示,这里是工作小提琴
Thanks to @Hectate
who got a working fiddle for me and @WestLangley
who directed me to the hints, here is the working fiddle
BufferGeometry 具有固定数量的顶点,但您可以决定要渲染的顶点数量.诀窍是利用 geometry.attributes.position.needsUpdate = true;
和 geometry.setDrawRange( 0, nbPointsYouWantToDisplay );
BufferGeometry has a fixed number of Vertices, but you can decide how many of them you want to render. The trick is to make use of geometry.attributes.position.needsUpdate = true;
and geometry.setDrawRange( 0, nbPointsYouWantToDisplay );
var MAX_POINTS = 1000000;
var geometry = new THREE.BufferGeometry();
var positions = new Float32Array( MAX_POINTS * 3 );
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
然后您可以创建您的云点并将其添加到场景中:
Then you can create your cloudpoints and add it to the scene:
//material and scene defined in question
pointCloud = new THREE.Points(geometry, material);
scene.add(pointCloud);
现在我想每 10
毫秒添加和渲染 500
个新点.
Now I want to add and render 500
new points every 10
milliseconds.
var nbPoints = 500;
var INTERVAL_DURATION = 10;
我所要做的就是:
var interval = setInterval(function() {
setPoints();
}, INTERVAL_DURATION)
function setPoints() {
var positions = pointCloud.geometry.attributes.position.array;
var x, y, z, index;
var l = currentPoints + nbPoints;
if(l >= MAX_POINTS) {
clearInterval(interval);
}
for ( var i = currentPoints; i < l; i ++ ) {
x = ( Math.random() - 0.5 ) * 300;
y = ( Math.random() - 0.5 ) * 300;
z = ( Math.random() - 0.5 ) * 300;
positions[ currentPointsIndex ++ ] = x;
positions[ currentPointsIndex ++ ] = y;
positions[ currentPointsIndex ++ ] = z;
}
currentPoints = l;
pointCloud.geometry.attributes.position.needsUpdate = true;
pointCloud.geometry.setDrawRange( 0, currentPoints );
controls.update();
renderer.render(scene, camera);
}
这篇关于THREE.js 向 Points 几何体动态添加点不渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!