sourcehypertextpublicsandboxorrery.js

import * as THREE from "../cosmetics/three.module.js";

import { TrackballControls } from "../cosmetics/TrackballControls.js";

import {
	getJ2000Offset,
	getElements,
	getOrbitTrail
} from "./orrery_calculations.js";
import planets from "./orrery_planets.js";

const orbitColours = {
	mercury: 0x887766,
	venus: 0xcc7700,
	earth: 0x229955,
	mars: 0xcc3322,
	jupiter: 0xcc7788,
	saturn: 0xddaa44,
	uranus: 0x22bbbb,
	neptune: 0x2266bb,
	pluto: 0x7722aa,
	eris: 0xbb1188,
	ceres: 0xccddbb,
	haumea: 0xbbddcc,
	makemake: 0xbbccdd,
	sedna: 0xccbbdd
};

const meshes = {};

const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);

const camera = new THREE.PerspectiveCamera(
	75,
	window.innerWidth / window.innerHeight,
	0.1,
	1e15
);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const controls = new TrackballControls(camera, renderer.domElement);

for (const planetKey in orbits) {
	console.log(planetKey);

	const planet = orbits[planetKey];
	const planetGeometry = new THREE.BufferGeometry().setFromPoints(
		planet.orbit.map(vec => new THREE.Vector3(...vec))
	);
	const planetMesh = new THREE.Line(
		planetGeometry,
		new THREE.LineBasicMaterial({ color: planet.colour })
	);

	meshes[planetKey] = planetMesh;
	scene.add(planetMesh);

	const map = new THREE.TextureLoader().load(
		`/sandbox/${
			[
				"venus",
				"uranus",
				"mars",
				"earth",
				"jupiter",
				"saturn",
				"neptune",
				"pluto",
				"eris"
			].includes(planetKey)
				? planetKey
				: "mercury"
		}.png`
	);
	map.colorSpace = THREE.SRGBColorSpace;
	const material = new THREE.PointsMaterial({
		map: map,
		size: 20,
		sizeAttenuation: false,
		alphaTest: 0.5,
		depthTest: true,
		depthWrite: true
	});
	const sprite = new THREE.Points(
		new THREE.BufferGeometry().setFromPoints([
			new THREE.Vector3(...planet.orbit[0])
		]),
		material
	);

	scene.add(sprite);
}

camera.position.z = 1e12;
controls.update();

function animate(time) {
	controls.update();
	renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);