I currently have a loop of circles that are moving independently. My goal is to draw lines from the center of the canvas to each circle's location. The issue I am facing is that the lines are not updating their vertices correctly during rendering, even though I have set
testLine.geometry.verticesNeedUpdate = true
. Upon inspection, it appears that this property remains false.
var container;
var camera, scene, renderer, lines=[], items =[];
var numItems = 40;
var xspeed;
var yspeed;
var lineGeometry;
var testLineGeometry;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf7f7f7);
var geometry = new THREE.CircleBufferGeometry( 20, 20 );
lineGeometry = new THREE.Geometry();
var testLineGeometry = new THREE.Geometry();
for ( var i = 0; i < numItems; i ++ ) {
var item = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0x000000 } ) );
item.position.x = (Math.random() * 20.1 - 1);
item.position.y = (Math.random() * 20.1 - 1);
item.xspeed = Math.random() * 2.1 - 1;
item.yspeed = Math.random() * 2.1 - 1;
// item.position.normalize();
items.push(item);
scene.add( item );
lineGeometry.vertices.push( item.position );
testLineGeometry.vertices.push(new THREE.Vector3(item.position.x, item.position.y, 0));
testLineGeometry.vertices.push(new THREE.Vector3(0, 0, 0));
testLine = new THREE.Line( testLineGeometry, new THREE.LineBasicMaterial( {color: 0x000000} ) );
lines.push(testLine);
scene.add( testLine );
}
line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( {color: 0x000000} ) );
// scene.add( line );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
//
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
items.forEach(circle => {
circle.position.x += circle.xspeed;
circle.position.y += circle.yspeed;
if (circle.position.x > window.innerWidth / 2 || circle.position.x < -window.innerWidth / 2 ) {
circle.xspeed *= -1;
}
if (circle.position.y > window.innerWidth / 2 || circle.position.y < -window.innerWidth / 2) {
circle.yspeed *= -1;
}
});
testLine.geometry.verticesNeedUpdate = true;
camera.position.x = 0;
camera.position.z = 1000;
renderer.render( scene, camera );
}
My main concern is how I can ensure the correct vertices of testLine
are updated during the render process.