Random Position in Sphere
Vector3()
by Paul West
HTML
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>
CSS
body {
overflow: hidden;
margin: 0;
}
JavaScript
var camera, scene, renderer, controls;
var randBoxGroup, spereGroup;
var pi2 = Math.PI * 2;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 20000);
camera.position.set(0, 2000, 10000);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
var gridGeom = new THREE.PlaneGeometry(8000, 8000, 20, 20);
gridGeom.rotateX(THREE.Math.degToRad(-90));
var gridMat = new THREE.MeshBasicMaterial({
color: 0x333333,
wireframe: true
});
var grid = new THREE.Mesh(gridGeom, gridMat);
scene.add(grid);
randBoxGroup = new THREE.Group();
for (var i = 0; i < 2000; i++) {
var geometry = new THREE.BoxGeometry(Math.random() * 100, Math.random() * 100, Math.random() * 100);
var material = new THREE.MeshBasicMaterial({color: Math.random() * 0xffffff});
var mesh = new THREE.Mesh(geometry, material);
var newPos = setRandomPoint(4000);
mesh.position.copy(newPos);
mesh.rotation.x = Math.random();
mesh.rotation.y = Math.random();
mesh.rotation.z = Math.random();
randBoxGroup.add(mesh);
}
scene.add(randBoxGroup);
sphereGroup = new THREE.Group();
for (var j=0; j<500;j++ ){
var sphGeom = new THREE.SphereGeometry(100, 16, 8);
var sphMat = new THREE.MeshBasicMaterial({color: "red"});
var sph = new THREE.Mesh(sphGeom, sphMat);
var sphPos = new THREE.Vector3(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ).normalize().multiplyScalar( 4250 + Math.random() * 250 );
sph.position.copy(sphPos);
sphereGroup.add(sph);
}
scene.add(sphereGroup);
}
function randomVector( val ){
return new THREE.Vector3(
val * 2 * (Math.random() * 2 - 1),
val * 2 * (Math.random() * 2 - 1),
val * 2 * (Math.random() * 2 - 1)
...