-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.js
400 lines (357 loc) · 16.3 KB
/
renderer.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
define([
'esri/Camera',
'esri/core/declare',
'esri/views/3d/externalRenderers'
], function (
Camera,
declare,
externalRenderers
) {
// Enforce strict mode
'use strict';
// Constants
var THREE = window.THREE;
var RADIUS = 6378137;
var THRESHOLD = 1000000;
var TRAJECTORY_SEGMENTS = 1000;
return declare([], {
constructor: function (satellites) {
// All satellite and the date for computation.
this.satellites = satellites;
this.satelliteHover = null;
this.satelliteIdentified = null;
// Layers
this.normal = null; // All satellites. Colored white.
this.selected = null; // Colored red based on filters.
this.hover = null; // Colored cyan when the user's mouse hovers over.
this.trajectory = null; // Orbital trajectory of a clicked satellite.
this.identified = null; // A clicked satellite. Colored cyan.
// SceneView
this.view = null;
},
setup: function (context) {
// Store view
this.view = context.view;
// Create the THREE.js webgl renderer
this.renderer = new THREE.WebGLRenderer({
context: context.gl,
premultipliedAlpha: false
});
//
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(
this.view.size[0],
this.view.size[1]
);
// Make sure it does not clear anything before rendering
this.renderer.autoClearDepth = false;
this.renderer.autoClearColor = false;
this.renderer.autoClearStencil = false;
// The ArcGIS JS API renders to custom offscreen buffers, and not to the default framebuffers.
// We have to inject this bit of code into the three.js runtime in order for it to bind those
// buffers instead of the default ones.
var originalSetRenderTarget = this.renderer.setRenderTarget.bind(this.renderer);
this.renderer.setRenderTarget = function (target) {
originalSetRenderTarget(target);
if (target === null) {
context.bindRenderTarget();
}
};
//
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera();
// Create both a directional light, as well as an ambient light
this.directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
this.ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
this.scene.add(
this.directionalLight,
this.ambientLight
);
// Create objects and add them to the scene
this.normal = new THREE.Points(
new THREE.BufferGeometry(),
new THREE.PointsMaterial({
color: 0xffffff,
size: 1,
sizeAttenuation: false,
vertexColors: THREE.NoColors,
fog: false
})
);
this.selected = new THREE.Points(
new THREE.BufferGeometry(),
new THREE.PointsMaterial({
color: 0xff0000,
size: 4,
sizeAttenuation: false,
vertexColors: THREE.NoColors,
fog: false
})
);
this.selected.frustumCulled = false;
this.hover = new THREE.Points(
new THREE.BufferGeometry(),
new THREE.PointsMaterial({
color: 0x00ffff,
size: 6,
sizeAttenuation: false,
vertexColors: THREE.NoColors,
fog: false
})
);
this.hover.frustumCulled = false;
this.trajectory = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0xffffff,
linewidth: 1,
vertexColors: THREE.NoColors,
fog: false
})
);
this.trajectory.frustumCulled = false;
this.identified = new THREE.Points(
new THREE.BufferGeometry(),
new THREE.PointsMaterial({
color: 0xff7f00,
size: 6,
sizeAttenuation: false,
vertexColors: THREE.NoColors,
fog: false
})
);
this.identified.frustumCulled = false;
// Add all satellites to normal layer.
var vertex = [];
var valid = [];
var date = new Date();
for (var i = 0; i < this.satellites.length; i++) {
var satellite = this.satellites[i];
var eci = this.getSatelliteLocation(satellite.satrec, date);
if (eci === null || eci === undefined || isNaN(eci.x) || isNaN(eci.y) || isNaN(eci.z)) {
continue;
}
vertex.push(
eci.x * 1000,
eci.y * 1000,
eci.z * 1000
);
valid.push(satellite);
}
// Satellites that returned a computable location.
this.satellites = valid;
// Assign buffer.
var position = new THREE.Float32Attribute(vertex, 3);
position.setDynamic(true);
this.normal.geometry.addAttribute('position', position);
// Add to scene
this.scene.add(
this.normal,
this.selected,
this.hover,
this.trajectory,
this.identified
);
// Start web worker
var worker = new Worker('worker.js');
worker.onmessage = function (e) {
// Update all satellites.
position.set(e.data.vertex);
position.needsUpdate = true;
// Update selected satellites.
var vertex = [];
for (var i = 0; i < this.satellites.length; i++) {
var satellite = this.satellites[i];
if (!satellite.selected) {
continue;
}
vertex.push(
position.getX(i),
position.getY(i),
position.getZ(i)
);
}
this.selected.geometry.removeAttribute('position');
this.selected.geometry.addAttribute('position', new THREE.Float32Attribute(vertex, 3));
this.selected.needsUpdate = true;
// Update hover-over satellite.
if (this.satelliteHover !== null) {
var index2 = this.satellites.indexOf(this.satelliteHover);
var vertex2 = [];
vertex2.push(
position.getX(index2),
position.getY(index2),
position.getZ(index2)
);
this.hover.geometry.removeAttribute('position');
this.hover.geometry.addAttribute('position', new THREE.Float32Attribute(vertex2, 3));
this.hover.needsUpdate = true;
}
// Update highlighted satellite.
if (this.satelliteIdentified !== null) {
var index3 = this.satellites.indexOf(this.satelliteIdentified);
var vertex3 = [];
vertex3.push(
position.getX(index3),
position.getY(index3),
position.getZ(index3)
);
this.identified.geometry.removeAttribute('position');
this.identified.geometry.addAttribute('position', new THREE.Float32Attribute(vertex3, 3));
this.identified.needsUpdate = true;
}
}.bind(this);
worker.postMessage({
satellites: this.satellites.map(function (e) {
return {
satrec: e.satrec
};
})
});
// Refresh the screen
externalRenderers.requestRender(this.view);
},
render: function (context) {
// Get Esri's camera
var c = context.camera;
// Update three.js camera
this.camera.position.set(c.eye[0], c.eye[1], c.eye[2]);
this.camera.up.set(c.up[0], c.up[1], c.up[2]);
this.camera.lookAt(new THREE.Vector3(c.center[0], c.center[1], c.center[2]));
this.camera.projectionMatrix.fromArray(c.projectionMatrix);
// Get Esri's current sun settings
this.view.environment.lighting.date = this.date;
// Update lighting
var direction = context.sunLight.direction;
var diffuse = context.sunLight.diffuse;
var ambient = context.sunLight.ambient;
// Update the directional light color, intensity and position
this.directionalLight.color.setRGB(diffuse.color[0], diffuse.color[1], diffuse.color[2]);
this.directionalLight.intensity = diffuse.intensity;
this.directionalLight.position.set(direction[0], direction[1], direction[2]);
// Update the ambient light color and intensity
this.ambientLight.color.setRGB(ambient.color[0], ambient.color[1], ambient.color[2]);
this.ambientLight.intensity = ambient.intensity;
// Render the scene
this.renderer.resetGLState();
this.renderer.render(this.scene, this.camera);
// Request a re-render
externalRenderers.requestRender(this.view);
// Cleanup
context.resetWebGLState();
},
dispose: function (content) { },
getSatelliteLocation: function (satrec, date) {
var position_and_velocity = satellite.propagate(
satrec,
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds()
);
return position_and_velocity.position;
},
updateSelection: function () {
// Get locations of selected satellites
var vertex = [];
for (var i = 0; i < this.satellites.length; i++) {
var satellite = this.satellites[i];
if (!satellite.selected) {
continue;
}
var p = this.normal.geometry.getAttribute('position');
vertex.push(
p.getX(i),
p.getY(i),
p.getZ(i)
);
}
// Assign location array to
this.selected.geometry.removeAttribute('position');
this.selected.geometry.addAttribute('position', new THREE.Float32Attribute(vertex, 3));
// Immediately request a new redraw
externalRenderers.requestRender(this.view);
},
mousemove: function (x, y) {
// Normalize mouse
var mouse = new THREE.Vector2(
x / $('#map').width() * 2 - 1,
y / $('#map').height() * -2 + 1
);
var raycaster = new THREE.Raycaster();
raycaster.params.Points.threshold = THRESHOLD;
raycaster.setFromCamera(mouse, this.camera);
var intersections = raycaster.intersectObject(this.normal);
//
var satellite = null;
var vertex = [];
var index = null;
if (intersections.length !== 0) {
intersections.sort(function (a, b) {
return a.distanceToRay - b.distanceToRay;
});
var intersection = intersections[0];
index = intersection.index;
satellite = this.satellites[index];
}
if (satellite === null && this.satelliteHover === null) { return; }
if (satellite === this.satelliteHover) { return; }
//
this.satelliteHover = satellite;
if (this.satelliteHover !== null) {
var p = this.normal.geometry.getAttribute('position');
vertex.push(
p.getX(index),
p.getY(index),
p.getZ(index)
);
}
this.hover.geometry.removeAttribute('position');
this.hover.geometry.addAttribute('position', new THREE.Float32Attribute(vertex, 3));
// Immediately request a new redraw
externalRenderers.requestRender(this.view);
},
hideOrbit: function () {
// Clear orbit
this.trajectory.geometry.removeAttribute('position');
this.identified.geometry.removeAttribute('position');
//
this.satelliteIdentified = null;
// Immediately request a new redraw
externalRenderers.requestRender(this.view);
},
showOrbit: function () {
// Exit if no satellite currently under the user's mouse.
if (this.satelliteHover === null) { return; }
this.satelliteIdentified = this.satelliteHover;
// Time between orbital vertex
var ms = this.satelliteIdentified.metadata.period * 60000;
ms /= TRAJECTORY_SEGMENTS;
// Show orbit
var vertex = [];
var start = new Date();
for (var i = 0; i <= TRAJECTORY_SEGMENTS; i++) {
var date = new Date(start.valueOf() + i * ms);
var eci = this.getSatelliteLocation(this.satelliteIdentified.satrec, date);
if (eci === null || eci === undefined || isNaN(eci.x) || isNaN(eci.y) || isNaN(eci.z)) {
continue;
}
vertex.push(
eci.x * 1000,
eci.y * 1000,
eci.z * 1000
);
}
this.trajectory.geometry.removeAttribute('position');
this.trajectory.geometry.addAttribute('position', new THREE.Float32Attribute(vertex, 3));
// Show satellite
this.identified.geometry.removeAttribute('position');
this.identified.geometry.addAttribute('position', new THREE.Float32Attribute(vertex.slice(0, 3), 3));
// Immediately request a new redraw
externalRenderers.requestRender(this.view);
}
});
}
);