Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

three js vertices does not update

Tags:

I'm using three.js r67, and vertices does not seem to be updated. I set geometry.dynamic = true, geometry.verticesNeedUpdate = true. Circle is moving, but line is static....

Someone could help me?

var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer();

var g = new THREE.CircleGeometry( 4, 16 );
var m = new THREE.MeshBasicMaterial({color: 0x114949});
var circle = new THREE.Mesh( g, m );

circle.position.x = 2;
circle.position.y = 2;
circle.position.z = -1;
scene.add( circle );

var material = new THREE.LineBasicMaterial({color: 0xDF4949, linewidth: 5});

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
geometry.vertices.push(new THREE.Vector3(1, 1, 0));
geometry.verticesNeedUpdate = true;
geometry.dynamic = true;

var line = new THREE.Line(geometry, material);
scene.add(line);

var update = function() {
  circle.position.x += 0.01;
  line.geometry.vertices[0].x = circle.position.x;
};

var render = function() {
  renderer.render(scene, camera);
};

var loop = function() {
  update();
  render();
  requestAnimationFrame(loop, renderer.canvas);
};
loop();
like image 818
NHK Avatar asked Jul 02 '14 12:07

NHK


1 Answers

If you want change the vertices of your geometry, you need to set geometry.verticesNeedUpdate = true;.

function update() {
    circle.position.x += 0.01;
    line.geometry.vertices[ 0 ].x = circle.position.x;
    line.geometry.verticesNeedUpdate = true;
};

After rendering, geometry.verticesNeedUpdate is reset to false, so you need to reset it to true every time the geometry vertices are changed.

EDIT: If you are using BufferGeometry, use this pattern instead:

geometry.attributes.position.needsUpdate = true;

three.js r.74

like image 120
WestLangley Avatar answered Sep 21 '22 17:09

WestLangley