THREE.js 动态添加点到 Points 几何体不渲染

Ant*_*Ant 5 javascript geometry points three.js

我正在使用 Three.js r83。

我试图动态地将点添加到几何体中,但场景永远不会更新。

这有效:

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);
Run Code Online (Sandbox Code Playgroud)

这不起作用:

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);
Run Code Online (Sandbox Code Playgroud)

scene.add(pointCloud);正如您所看到的,唯一的区别是我在添加顶点之前添加。

我想念什么?

感谢@hectate,你可以找到一把小提琴

要明白我的意思,只需替换

init(); setPoints(); animate();

经过

init(); animate(); setPoints();

Ant*_*Ant 6

我不确定为什么该THREE.Geometry对象在初始渲染后不更新点,但我让它使用 aTHREE.BufferGeometry代替。

感谢@Hectate谁为我提供了一个工作小提琴并@WestLangley指导我找到提示,这是工作小提琴

BufferGeometry 有固定数量的顶点,但您可以决定要渲染的顶点数量。诀窍是利用geometry.attributes.position.needsUpdate = true;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 ) );
Run Code Online (Sandbox Code Playgroud)

然后您可以创建云点并将其添加到场景中:

//material and scene defined in question
pointCloud = new THREE.Points(geometry, material);
scene.add(pointCloud);
Run Code Online (Sandbox Code Playgroud)

现在我想500每毫秒添加和渲染新点10

var nbPoints = 500;
var INTERVAL_DURATION = 10;
Run Code Online (Sandbox Code Playgroud)

我所要做的就是:

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);

}
Run Code Online (Sandbox Code Playgroud)