Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js: How to Update BufferGeometry Vertices

Tags:

three.js

I just have to update a line that have 2 vertices, and i trying to use these things above that are changed in r58, but the lines cant move, i just made this to initialize:

var geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', Float32Array, 2, 3);
geometry.dynamic = true;

var position = geometry.attributes.position;
position.needsUpdate = true;

var p = position.array;

var i = 0;
p[i++] = vertex1.position.x;
p[i++] = vertex1.position.y;
p[i++] = vertex1.position.z;
p[i++] = vertex2.position.x;
p[i++] = vertex2.position.y;
p[i] = vertex2.position.z;

var color = new THREE.Color();
color.g = color.b = 1 - this.value;

var material = new THREE.LineBasicMaterial({
  color: color.getHex(),
  linewidth: 5 // THIS DON'T WORKS IN WINDOWS?
});

this.model = new THREE.Line(geometry, material);

and in update rendering i´m just doing this:

var p = this.model.geometry.attributes.position.array;

var i = 0;
p[i++] = vertex1.position.x;
p[i++] = vertex1.position.y;
p[i++] = vertex1.position.z;
p[i++] = vertex2.position.x;
p[i++] = vertex2.position.y;
p[i] = vertex2.position.z;

but the lines didn´t move or render itself.

like image 612
Filipe Avatar asked Nov 30 '13 17:11

Filipe


Video Answer


1 Answers

In your case, you need to add the following in your render loop when you update vertices:

this.model.geometry.attributes.position.needsUpdate = true;

You can remove the line position.needsUpdate = true when you create the geometry.

I would also suggest that you update to the current version.

three.js r.63

like image 176
WestLangley Avatar answered Nov 05 '22 05:11

WestLangley