xonecas.ioDon't click! Sean Caetano Martin Sign in
space-browser / client / src / view / bodies.ts 77 lines2.8 kB Raw
1// The celestial bodies: one mesh per kind, built from disks, a lit limb and plates.
2// A body is the only thing in the scene drawn behind the ships.
3import * as THREE from 'three';
4import { lum } from './ink';
5import { plate } from './shapes';
6
7const disk = (r: number, v: number, idx: number, x = 0, y = 0): THREE.Mesh => {
8  const m = new THREE.Mesh(
9    new THREE.CircleGeometry(r, 48),
10    new THREE.MeshBasicMaterial({ color: lum(v, idx) }),
11  );
12  m.position.set(x, y, 0);
13  return m;
14};
15
16// A lit limb: the bright edge that makes a disk pop off the black.
17const limb = (r: number, v: number, idx: number): THREE.Mesh =>
18  new THREE.Mesh(
19    new THREE.RingGeometry(r * 0.88, r, 64),
20    new THREE.MeshBasicMaterial({ color: lum(v, idx), side: THREE.DoubleSide }),
21  );
22
23// A star reads as a hot core with long rays, so it can never be mistaken for a
24// planet at any zoom: it is the only thing in the scene that shines.
25export function starMesh(r: number): THREE.Group {
26  const g = new THREE.Group();
27  g.add(disk(r, 0.8, 6));
28  g.add(disk(r * 0.62, 1, 7));
29  g.add(disk(r * 0.3, 1, 12));
30  const rays = new THREE.Group();
31  for (let i = 0; i < 8; i++) {
32    const a = (i / 8) * Math.PI * 2;
33    const len = (i % 2 ? 0.5 : 0.9) * r;
34    const m = plate(r * 0.24, len, 0.7);
35    (m.material as THREE.MeshBasicMaterial).color.copy(lum(0.6, 6));
36    m.position.set(Math.cos(a) * (r * 0.8 + len / 2), Math.sin(a) * (r * 0.8 + len / 2), 0);
37    m.rotation.z = a - Math.PI / 2;
38    rays.add(m);
39  }
40  g.add(rays);
41  return g;
42}
43
44// A barren planet: a rust disk lit from one side, with craters punched out of it.
45// These are the ore bodies of the galaxy.
46export function barrenMesh(r: number): THREE.Group {
47  const g = new THREE.Group();
48  g.add(disk(r, 0.7, 9));
49  g.add(limb(r, 1, 9));
50  for (const [dx, dy, cr] of [[0.3, 0.28, 0.34], [-0.12, 0.5, 0.2], [0.36, -0.34, 0.16]]) {
51    g.add(disk(r * cr, 0.3, 8, dx * r, dy * r)); // craters
52  }
53  return g;
54}
55
56// A shipyard planet: cool steel, a lit cap, and a cream ring studded with gold
57// docks. It is the only body in the system with anything bolted around it.
58export function shipyardMesh(r: number): THREE.Group {
59  const g = new THREE.Group();
60  g.add(disk(r, 0.55, 10));
61  g.add(disk(r * 0.82, 0.9, 11, -r * 0.07, r * 0.09));
62  // The berth ring floats clear of the hull: a station, not a moon.
63  const ring = new THREE.Mesh(
64    new THREE.RingGeometry(r * 1.18, r * 1.38, 64),
65    new THREE.MeshBasicMaterial({ color: lum(1, 12), side: THREE.DoubleSide }),
66  );
67  g.add(ring);
68  for (let i = 0; i < 4; i++) {
69    const a = (i / 4) * Math.PI * 2 + Math.PI / 4;
70    const d = plate(r * 0.34, r * 0.34, 1);
71    (d.material as THREE.MeshBasicMaterial).color.copy(lum(1, 7));
72    d.position.set(Math.cos(a) * r * 1.5, Math.sin(a) * r * 1.5, 0.06);
73    d.rotation.z = a;
74    g.add(d);
75  }
76  return g;
77}