-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
270 lines (154 loc) · 7 KB
/
main.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import './style.css'
// import * as THREE from 'three'
import * as THREE from 'node_modules/three/'
import {InteractionManager} from "three.interactive";
/* ============================================================================================= */
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const mouseP = document.getElementById("mouse");
const cameraP = document.getElementById("camera_position");
const cameraA = document.getElementById("camera_angle");
const earthP = document.getElementById("earth");
const moonP = document.getElementById("moon");
const scene_objids = document.getElementById("scene_objids");
const moon_objids = document.getElementById("moon_objids");
let obj_to_follow = null;
/* ============================================================================================= */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
function onPointerMove(event) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
window.addEventListener('pointermove', onPointerMove);
/* ============================================================================================= */
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#bg'),
antialias: true,
logarithmicDepthBuffer: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
/* ============================================================================================= */
function makeSphere(ctx, size, color, wireframe) {
const geometry = new THREE.SphereGeometry(size, size * 2, size * 2, 0, Math.PI * 2);
const material = new THREE.MeshStandardMaterial({
color: color,
wireframe: wireframe
});
const mesh = new THREE.Mesh(geometry, material);
ctx.add(mesh);
return mesh;
}
function makeRing(ctx, size, color) {
const geometry = new THREE.RingGeometry(size, size + 9, size * 2);
const material = new THREE.MeshBasicMaterial({
color: color,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
ctx.add(mesh);
return mesh;
}
function makeSpotLight(ctx, color, helper) {
const pointLight = new THREE.PointLight(color);
ctx.add(pointLight);
if(helper) {
const lightHelper = new THREE.PointLightHelper(pointLight);
ctx.add(lightHelper);
}
return pointLight;
}
function makeEllipse(ctx, color, apogee, perigee, semi_minor_axys) {
const curve = new THREE.EllipseCurve(
perigee - apogee, 0,
(apogee + perigee)/2, semi_minor_axys,
0, 2 * Math.PI,
false,
0
);
const points = curve.getPoints(500);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: color });
const ellipse = new THREE.Line(geometry, material);
ctx.add(ellipse);
return ellipse;
}
function orbitPosition(obj, a, b, apogee, perigee, iteration) {
obj.position.set(a * Math.cos(iteration) + (perigee - apogee), b * Math.sin(iteration), 0);
}
/* ============================================================================================= */
function resetMaterial() {
scene.traverse((object)=>{
if (object.isMesh) object.material.color = new THREE.Color(0xFFFFFF);
});
}
function objectHover() {
raycaster.setFromCamera(mouse,camera);
const intersects = raycaster.intersectObjects(scene.children);
for(let i = 0; i < intersects.length; i++) {
if(intersects[i].object.geometry.type === "SphereGeometry")
intersects[i].object.material.color = new THREE.Color(0xFAD578);
}
}
function cameraFollow(target) {
if(target !== null && target.geometry.type === "SphereGeometry") {
const target_world_position = new THREE.Vector3();
target.getWorldPosition(target_world_position);
const zoom = target.geometry.boundingSphere.radius * 2;
camera.position.set(target_world_position.x, target_world_position.y, target_world_position.z + zoom);
}
else camera.position.set(0, 0, 6000);
}
/* ============================================================================================= */
const sun = makeSpotLight(scene, 0xFFFFFF, false);
const earth = makeSphere(scene, 127, 0xFFFFFF, false);
const moon_reference = makeSphere(scene, 0, 0x000000, false);
const moon = makeSphere(moon_reference, 34, 0xFFFFFF, false);
const moon_orbit = makeEllipse(moon_reference, 0x222222, 4054, 3632, 3842);
sun.position.set(-10000, 0, 0);
moon_reference.rotation.set(0, 5.14 * Math.PI / 180, 0);
orbitPosition(moon, 3847, 3842, 4054, 3632, 0);
scene_objids.innerHTML += "<span>Sun</span> : " + sun.uuid + "<br>"
scene_objids.innerHTML += "<span>Earth</span> : " + earth.uuid + "<br>"
scene_objids.innerHTML += "<span>moon_reference</span> : " + moon_reference.uuid + "<br>"
moon_objids.innerHTML += "<span>Moon</span> : " + moon.uuid + "<br>"
// moon_objids.innerHTML += "<span>moon_orbit</span> : " + moon_orbit.uuid + "<br>"
const interactionManager = new InteractionManager(renderer, camera, renderer.domElement);
interactionManager.add(earth);
interactionManager.add(moon);
earth.addEventListener("click", ()=>{obj_to_follow = earth});
moon.addEventListener("click", ()=>{obj_to_follow = moon});
/* ============================================================================================= */
let i = 0;
function animate() {
requestAnimationFrame(animate);
orbitPosition(moon, 3847, 3842, 4054, 3632, i);
i += 0.001;
resetMaterial();
objectHover();
cameraFollow(obj_to_follow);
const moon_world_position = new THREE.Vector3();
moon.getWorldPosition(moon_world_position);
mouseP.innerHTML = mouse.x.toFixed(3) + " | " + mouse.y.toFixed(3);
cameraP.innerHTML = camera.position.x.toFixed(1) + " | " + camera.position.y.toFixed(1) + " | " + camera.position.z.toFixed(1);
cameraA.innerHTML = camera.rotation.x.toFixed(1) + " | " + camera.rotation.y.toFixed(1) + " | " + camera.rotation.z.toFixed(1);
earthP.innerHTML = earth.position.x.toFixed(1) + " | " + earth.position.y.toFixed(1) + " | " + earth.position.z.toFixed(1);
moonP.innerHTML = moon.position.x.toFixed(1) + " | " + moon.position.y.toFixed(1) + " | " + moon_world_position.z.toFixed(1);
renderer.render(scene, camera);
}
animate();
/* ============================================================================================= */
window.addEventListener("keydown", (e)=>{
if(e.code === "Escape") {
camera.position.set(0,0,6000);
camera.rotation.set(0,0,0);
obj_to_follow = null;
}
})