var scene = new THREE.Scene();
var aspect = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
solarsystem.forEach(d => create(d, scene))
scene.add(sphere({radius:5, orbit:0}))
function create(d, target) {
var o = new THREE.Object3D(d.name);
o.add(orbit(d));
let p = sphere(d)
o.add(p);
d.satellites && d.satellites.forEach(d1 => create(d1, p))
target.add(o);
d.o=o;
}
function orbit(d) {
var o = new THREE.Object3D('orbit '+d.name);
o.rotateX(Math.PI/2);
o.add( new THREE.Line(
new THREE.CircleGeometry( d.orbit, 64 ),
new THREE.LineBasicMaterial( { color: 0xffffff } ) ));
return o;
}
function sphere(d){
var o = new THREE.Object3D('sphere '+d.name);
o.add(new THREE.Mesh(
new THREE.SphereGeometry(d.radius, 16, 16),
new THREE.MeshBasicMaterial({
color: 0x2980b9, wireframe: true
})));
o.translateX(d.orbit)
return o;
}
var grid = new THREE.GridHelper(500, 100, 0x666666, 0x444444)
grid.rotateY(Math.PI/2);
scene.add(grid);
camera.position.set(5,5,0);
new THREE.OrbitControls( camera, renderer.domElement );
let t = 0;
function render(dt) {
let t2= dt-t;
requestAnimationFrame( render );
renderer.render( scene, camera );
solarsystem.forEach(upd)
t = dt;
function upd(d) {
d.o.rotateY(t2/10000*d.speed);
d.satellites && d.satellites.forEach(upd)
}
}
requestAnimationFrame( render );
body, canvas {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/103/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script>
let solarsystem = [{
name: 'earth', radius: 2, orbit: 30, speed: 2,
satellites: [{
name: 'moon', radius: 1, orbit: 6, speed: 1,
}]
}, {
name: 'mars', radius: 2, orbit: 50, speed: 1,
satellites: [{
name: 'phobos', radius: 0.5, orbit: 3, speed: 1,
}, {
name: 'deimos', radius: 0.5, orbit: 4, speed: 3,
}]
}];
</script>