2016-11-08 1 views
0

제 문제점은 별에서 생성 된 행성 (구)이 축소 될 때 three.js 내에서 임의의 스타 차트를 만들려고한다는 것입니다.랜덤 큐브를 세 개의 j에서 임의의 구로 바꾸기

현재 메신저를 사용하는 코드는 내 별을 생성 할 때 무작위로 내 별을 생성하지만 아직 축소하면 큐브가 아닌 구입니다.

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

내가 내 별과 라인 3-6를 생성하는 루프에 대해 내가 할 수 있었던 모든이를 만들 임의 수학으로 다시 위치를 곱입니다 별 그러나 산란 방식을 결정하는 무엇이다 원하는 구체 대신 약간 덜 정의 된 입방체. 대신 당신이 같은 메시의 x,y,z 위치를 설정

답변

0

, 다음 다음 1의 길이가 동일하고, 그것을 정상화 무작위로 곱 그것을

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

은 따라서 당신이 임의의 벡터를 설정이 방법을 시도 값. 간단한 말로 방향과 거리를 설정합니다.

jsfiddle 두 솔루션과 예 (광산 및 가산기의)

1

그냥 구형 외부 별 치기있다 :

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; 
    } 
} 
+0

가'것을 경우 (mesh.position.length() <= R)' – prisoner849

+0

그래, 그것에 대해 몰랐다. 그러나 사용하는 제곱근 연산은 다소 느리다. – Adder

+0

나는 주장하지 않는다.) 또한 mesh.position.lengthSq() <= R * R' 일 수도있다. – prisoner849

관련 문제