Repository navigation
Expand file tree
/
Copy pathsolar_sys.js
More file actions
166 lines (134 loc) · 4.21 KB
/
Copy pathsolar_sys.js
File metadata and controls
166 lines (134 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
PerspectiveCamera,
Scene,
WebGLRenderer,
BoxGeometry,
Mesh,
MeshNormalMaterial,
AmbientLight,
Clock,
Sphere,
SphereGeometry,
MeshPhongMaterial,
PointLight,
Object3D,
AxesHelper,
GridHelper,
TextureLoader
} from 'three';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import {
OrbitControls
} from 'three/addons/controls/OrbitControls.js';
import {
GLTFLoader
} from 'three/addons/loaders/GLTFLoader.js';
const scene = new Scene();
const aspect = window.innerWidth / window.innerHeight;
//const camera = new PerspectiveCamera(75, aspect, 0.1, 1000);
const camera = new PerspectiveCamera(75, aspect, 0.1, 1000);
camera.position.set(0, 50, 0);
camera.up.set(0, 0, 1);
camera.lookAt(0, 0, 0);
const loader = new TextureLoader();
const sunTex = loader.load('assets/textures/2k_sun.jpg');
/*{
const color = 0xFFFFFF;
const intensity = 3;
const light = new PointLight(color, intensity);
scene.add(light);
}*/
//const light = new AmbientLight(0xffffff, 1.0); // soft white light
//scene.add(light);
const renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const gui = new GUI();
const controls = new OrbitControls(camera, renderer.domElement);
controls.listenToKeyEvents(window); // optional
const objects = [];
const radius = 1;
const widthSegments = 50;
const heightSegments = 50;
const sphereGeometry = new SphereGeometry(radius, widthSegments, heightSegments);
const solarSystem = new Object3D();
scene.add(solarSystem);
objects.push(solarSystem);
const sunMaterial = new MeshPhongMaterial({ emissive: 0xe38f3b, map: sunTex });
sunMaterial.map = sunTex;
const sunMesh = new Mesh(sphereGeometry, sunMaterial);
sunMesh.scale.set(5, 5, 5);
solarSystem.add(sunMesh);
objects.push(sunMesh);
const earthOrbit = new Object3D();
earthOrbit.position.x = 10;
solarSystem.add(earthOrbit);
objects.push(earthOrbit);
const earthMaterial = new MeshPhongMaterial({ color: 0x2233FF, emissive: 0x112244 });
const earthMesh = new Mesh(sphereGeometry, earthMaterial);
earthOrbit.add(earthMesh);
objects.push(earthMesh);
const moonOrbit = new Object3D();
moonOrbit.position.x = 2;
earthOrbit.add(moonOrbit);
const moonMaterial = new MeshPhongMaterial({ color: 0x888888, emissive: 0x222222 });
const moonMesh = new Mesh(sphereGeometry, moonMaterial);
moonMesh.scale.set(.5, .5, .5);
moonOrbit.add(moonMesh);
objects.push(moonMesh);
// add an AxesHelper to each node
/*objects.forEach((node) => {
const axes = new AxesHelper();
axes.material.depthTest = false;
axes.renderOrder = 1;
node.add(axes);
});*/
class AxisGridHelper {
constructor(node, units = 10) {
const axes = new AxesHelper();
axes.material.depthTest = false;
axes.renderOrder = 2; // after the grid
node.add(axes);
const grid = new GridHelper(units, units);
grid.material.depthTest = false;
grid.renderOrder = 1;
node.add(grid);
this.grid = grid;
this.axes = axes;
this.visible = false;
}
get visible() {
return this._visible;
}
set visible(v) {
this._visible = v;
this.grid.visible = v;
this.axes.visible = v;
}
}
function makeAxisGrid(node, label, units) {
const helper = new AxisGridHelper(node, units);
gui.add(helper, 'visible').name(label);
}
makeAxisGrid(solarSystem, 'solarSystem', 25);
makeAxisGrid(sunMesh, 'sunMesh');
makeAxisGrid(earthOrbit, 'earthOrbit');
makeAxisGrid(earthMesh, 'earthMesh');
makeAxisGrid(moonOrbit, 'moonOrbit');
makeAxisGrid(moonMesh, 'moonMesh');
const clock = new Clock();
const animation = () => {
renderer.setAnimationLoop(animation); // requestAnimationFrame() replacement, compatible with XR
const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
// can be used in shaders: uniforms.u_time.value = elapsed;
/*sunMesh.rotation.x = elapsed / 2;
sunMesh.rotation.y = elapsed / 1;
earthMesh.rotation.x = elapsed / 2;
earthMesh.rotation.y = elapsed / 1;*/
objects.forEach((obj) => {
obj.rotation.y = elapsed / 2;
});
renderer.render(scene, camera);
};
animation();