three js vertices does not update

戏子无情 提交于 2019-11-29 11:15:33

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/24531109/three-js-vertices-does-not-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!