所以我的问题是,我试图在 Three.js 中创建一个随机星图,当缩小时,它会变成一个由星星创建的行星(球体)。
目前,我使用的代码随机生成我的星星,但当我缩小时,它是一个立方体,而不是一个球体。
for (var i = 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);
}
这是我生成星星的 for 循环,第 3-6 行决定了星星生成的方式,但是我所能做的就是再次将定位乘以数学随机,以创建一个稍微不太定义的立方体而不是我想要的球体。
最佳答案
只需 trim 球体外部的星星即可:
var R=2000;
for (var i = 0;i<2000;){
var mesh = new THREE.Mesh( geometry, material);
mesh.position.x = ( Math.random() - 0.5) * R*2 * Math.random();
mesh.position.y = ( Math.random() - 0.5) * R*2 * Math.random() ;
mesh.position.z = ( Math.random() - 0.5) * R*2 * Math.random() ;
mesh.rotation.x = Math.random();
mesh.rotation.y = Math.random();
mesh.rotation.z = Math.random();
var distance_squared = mesh.position.x*mesh.position.x + mesh.position.y*mesh.position.y + mesh.position.z*mesh.position.z;
if(distance_squared <= R*R) {
scene.add(mesh);
objects.push(mesh);
++i;
}
}
关于javascript - 用三个js将随机立方体变成随机球体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40485197/