-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathKeplerParticles.ts
More file actions
326 lines (268 loc) · 9.74 KB
/
KeplerParticles.ts
File metadata and controls
326 lines (268 loc) · 9.74 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
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
import * as THREE from 'three';
import { getThreeJsTexture } from './util';
import { Coordinate3d } from './Coordinates';
import { getOrbitShaderVertex, getOrbitShaderFragment } from './shaders';
import { Orbit, OrbitType } from './Orbit';
import type { Ephem } from './Ephem';
import type { Simulation, SimulationContext } from './Simulation';
interface BaseKeplerParticleOptions {
color?: number;
textureUrl?: string;
basePath?: string;
jd?: number;
maxNumParticles?: number;
}
// TODO(ian): Clean this up - we probably don't need a separate type.
type KeplerParticlesOptions = BaseKeplerParticleOptions & {
defaultSize?: number;
};
type KeplerParticleOptions = BaseKeplerParticleOptions & {
particleSize?: number;
};
interface ShaderAttributes {
size: THREE.BufferAttribute;
origin: THREE.BufferAttribute;
position: THREE.BufferAttribute;
fuzzColor: THREE.BufferAttribute;
a: THREE.BufferAttribute;
e: THREE.BufferAttribute;
i: THREE.BufferAttribute;
om: THREE.BufferAttribute;
ma: THREE.BufferAttribute;
n: THREE.BufferAttribute;
w: THREE.BufferAttribute;
wBar: THREE.BufferAttribute;
q: THREE.BufferAttribute;
M: THREE.BufferAttribute;
a0: THREE.BufferAttribute;
}
const DEFAULT_PARTICLE_COUNT = 4096;
/**
* Compute mean anomaly at date. Used for elliptical and hyperbolic orbits.
*/
function getM(ephem: Ephem, jd: number): number {
const d = jd - ephem.get('epoch');
return ephem.get('ma') + ephem.get('n') * d;
}
const PARABOLIC_K = 0.01720209895;
function getA0(ephem: Ephem, jd: number): number {
const tp = ephem.get('tp');
const e = ephem.get('e');
const q = ephem.get('q');
const d = jd - tp;
return 0.75 * d * PARABOLIC_K * Math.sqrt((1 + e) / (q * q * q));
}
/**
* An efficient way to render many objects in space with Kepler orbits.
* Primarily used by Simulation to render all non-static objects.
* @see Simulation
*/
export class KeplerParticles {
static instanceCount: number;
private id: string;
private options: KeplerParticlesOptions;
private simulation: Simulation;
private context: SimulationContext;
private addedToScene: boolean;
private particleCount: number;
private elements: Ephem[];
private uniforms: {
texture: { value: THREE.Texture };
};
private geometry: THREE.BufferGeometry;
private shaderMaterial: THREE.ShaderMaterial;
private particleSystem: THREE.Points;
private attributes: ShaderAttributes;
/**
* @param {Object} options Options container
* @param {Object} options.textureUrl Template url for sprite
* @param {Object} options.basePath Base path for simulation supporting files
* @param {Number} options.jd JD date value
* @param {Number} options.maxNumParticles Maximum number of particles to display. Defaults to 4096
* @param {Number} options.defaultSize Default size of particles. Note this
* can be overriden by SpaceObject particleSize. Defaults to 25
* @param {Object} contextOrSimulation Simulation context or object
*/
constructor(
options: KeplerParticlesOptions,
contextOrSimulation: Simulation,
) {
this.options = options;
this.id = `KeplerParticles__${KeplerParticles.instanceCount}`;
this.simulation = contextOrSimulation;
this.context = contextOrSimulation.getContext();
// Whether Points object has been added to the Simulation/Scene. This
// happens lazily when the first data point is added in order to prevent
// WebGL render warnings.
this.addedToScene = false;
// Number of particles in the scene.
this.particleCount = 0;
if (!this.options.textureUrl) {
throw new Error('ParticleSystem requires textureUrl to be set');
}
const defaultMapTexture = getThreeJsTexture(
this.options.textureUrl,
this.context.options.basePath,
);
this.uniforms = {
texture: { value: defaultMapTexture },
};
const particleCount =
this.options.maxNumParticles || DEFAULT_PARTICLE_COUNT;
this.elements = [];
this.attributes = {
size: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
origin: new THREE.BufferAttribute(new Float32Array(particleCount * 3), 3),
position: new THREE.BufferAttribute(
new Float32Array(particleCount * 3),
3,
),
fuzzColor: new THREE.BufferAttribute(
new Float32Array(particleCount * 3),
3,
),
a: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
e: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
i: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
om: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
ma: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
n: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
w: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
wBar: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
q: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
M: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
a0: new THREE.BufferAttribute(new Float32Array(particleCount), 1),
};
this.attributes.M.setUsage(THREE.DynamicDrawUsage);
this.attributes.a0.setUsage(THREE.DynamicDrawUsage);
const geometry = new THREE.BufferGeometry();
geometry.setDrawRange(0, 0);
Object.keys(this.attributes).forEach((attributeName) => {
const attribute =
this.attributes[attributeName as keyof ShaderAttributes];
geometry.setAttribute(attributeName, attribute);
});
const shader = new THREE.ShaderMaterial({
uniforms: this.uniforms,
vertexShader: getOrbitShaderVertex(),
fragmentShader: getOrbitShaderFragment(),
depthTest: true,
depthWrite: false,
transparent: true,
});
this.shaderMaterial = shader;
this.geometry = geometry;
this.particleSystem = new THREE.Points(geometry, shader);
if (this.get3jsObjects() && this.get3jsObjects()[0]) {
this.get3jsObjects()[0].frustumCulled = false;
}
}
/**
* Add a particle to this particle system.
* @param {Ephem} ephem Kepler ephemeris
* @param {Object} options Options container
* @param {Number} options.particleSize Size of particles
* @param {Number} options.color Color of particles
* @return {Number} The index of this article in the attribute list.
*/
addParticle(ephem: Ephem, options: KeplerParticleOptions = {}): number {
this.elements.push(ephem);
const attributes = this.attributes;
const offset = this.particleCount++;
attributes.size.set(
[options.particleSize || this.options.defaultSize || 15],
offset,
);
const color = new THREE.Color(options.color || 0xffffff);
attributes.fuzzColor.set([color.r, color.g, color.b], offset * 3);
attributes.origin.set([0, 0, 0], offset * 3);
attributes.a.set([ephem.get('a')], offset);
attributes.e.set([ephem.get('e')], offset);
attributes.i.set([ephem.get('i', 'rad')], offset);
attributes.om.set([ephem.get('om', 'rad')], offset);
attributes.wBar.set([ephem.get('wBar', 'rad')], offset);
attributes.q.set([ephem.get('q')], offset);
if (Orbit.getOrbitType(ephem) === OrbitType.PARABOLIC) {
attributes.a0.set([getA0(ephem, this.options.jd || 0)], offset);
} else {
attributes.M.set([getM(ephem, this.options.jd || 0)], offset);
}
// TODO(ian): Set the update range
for (const attributeKey in attributes) {
if (attributes.hasOwnProperty(attributeKey)) {
attributes[attributeKey as keyof ShaderAttributes].needsUpdate = true;
}
}
this.geometry.setDrawRange(0, this.particleCount);
if (!this.addedToScene && this.simulation) {
// This happens lazily when the first data point is added in order to
// prevent WebGL render warnings.
this.simulation.addObject(this);
this.addedToScene = true;
}
return offset;
}
/**
* Hides the particle at the given offset so it is no longer drawn. The particle still takes up space in the array
* though.
* @param offset
*/
hideParticle(offset: number) {
const attributes = this.attributes;
attributes.size.set([0], offset);
for (const attributeKey in attributes) {
if (attributes.hasOwnProperty(attributeKey)) {
attributes[attributeKey as keyof ShaderAttributes].needsUpdate = true;
}
}
}
/**
* Change the `origin` attribute of a particle.
* @param {Number} offset The location of this particle in the attributes * array.
* @param {Array.<Number>} newOrigin The new XYZ coordinates of the body that this particle orbits.
*/
setParticleOrigin(offset: number, newOrigin: Coordinate3d) {
this.attributes.origin.set(newOrigin, offset * 3);
this.attributes.origin.needsUpdate = true;
}
/**
* Update the position for all particles
* @param {Number} jd JD date
*/
update(jd: number) {
const Ms: number[] = [];
const a0s: number[] = [];
for (let i = 0; i < this.elements.length; i++) {
const ephem = this.elements[i];
let M, a0;
if (Orbit.getOrbitType(ephem) === OrbitType.PARABOLIC) {
a0 = getA0(ephem, jd);
M = 0;
} else {
a0 = 0;
M = getM(ephem, jd);
}
Ms.push(M);
a0s.push(a0);
}
this.attributes.M.set(Ms);
this.attributes.M.needsUpdate = true;
this.attributes.a0.set(a0s);
this.attributes.a0.needsUpdate = true;
}
/**
* Get THREE.js objects that comprise this point cloud
* @return {Array.<THREE.Object3D>} List of objects to add to THREE.js scene
*/
get3jsObjects(): THREE.Object3D[] {
return [this.particleSystem];
}
/**
* Get unique id for this object.
* @return {String} Unique id
*/
getId(): string {
return this.id;
}
}
KeplerParticles.instanceCount = 0;