javascript - Turning random cube to random sphere in three js -


so issue trying create random star chart within three.js when zoomed out becomes planet(sphere) created stars.

currently code im using randomly generates stars yet when zoom out cube, not sphere.

    (var = 0;i<2000;i++){                 var mesh = new three.mesh( geometry, material);                 mesh.position.x = ( math.random() - 0.5) * 4000 * math.random();                 mesh.position.y = ( math.random() - 0.5) * 4000* math.random() ;                 mesh.position.z = ( math.random() - 0.5) * 4000* math.random() ;                  mesh.rotation.x = math.random();                 mesh.rotation.y = math.random();                 mesh.rotation.z = math.random();                  scene.add(mesh);                 objects.push(mesh);             } 

this loop in spawn stars , line 3-6 determines way stars spawn have been able multiply positioning again math random create less defined cube instead of desired sphere.

instead of setting mesh's x,y,z position do, try way

var newpos = new three.vector3(math.random() - 0.5, math.random() - 0.5, math.random() - 0.5 ).normalize().multiplyscalar( 4000 * math.random() ); mesh.position.copy(newpos); 

thus set random vector, normalize it, making length equal 1, multiply random value. in simple words: set direction , distance.

there's jsfiddle example both solutions (mine , adder's)


Comments

Popular posts from this blog

sql - can we replace full join with union of left and right join? why not? -

javascript - Parallax scrolling and fixed footer code causing width issues -

iOS: Performance of reloading UIImage(name:...) -