-
Notifications
You must be signed in to change notification settings - Fork 208
/
Explosion.ts
213 lines (179 loc) · 7.57 KB
/
Explosion.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Effects
*/
import { Id64String } from "@itwin/core-bentley";
import { Point3d, Range1d, Vector3d } from "@itwin/core-geometry";
import { RenderTexture, TextureTransparency } from "@itwin/core-common";
import {
DecorateContext, GraphicType, HitDetail, imageElementFromUrl, IModelApp, IModelConnection, ParticleCollectionBuilder, ParticleProps, Tool,
} from "@itwin/core-frontend";
import { randomFloat, randomFloatInRange, randomIntegerInRange, randomPositionInRange } from "./Random";
/** Represents one particle in the system. */
class Particle implements ParticleProps {
/** Current position in the particle system's local coordinate space. */
public readonly position: Point3d;
/** Current velocity in meters per second. */
public readonly velocity: Vector3d;
/** Current age in seconds, incremented each frame. */
public age = 0;
/** Maximum age in seconds. When `this.age` exceeds `this.lifetime`, the particle expires. */
public readonly lifetime: number;
/** Particle size in meters. */
public size: number;
/** Particle transparency in [0..255]. */
public transparency = 0;
public get x() { return this.position.x; }
public get y() { return this.position.y; }
public get z() { return this.position.z; }
public constructor(position: Point3d, velocity: Vector3d, lifetime: number, size: number) {
this.position = position;
this.velocity = velocity;
this.lifetime = lifetime;
this.size = size;
}
public get isExpired() { return this.age >= this.lifetime; }
}
/** Emits particles in a sphere with its center at the origin.
* Each particle is emitted from the center of the sphere with random velocity toward the surface of the sphere.
*/
class ParticleEmitter {
/** Range from which each particle's initial speed in meters per second will be selected. */
public readonly speedRange = Range1d.createXX(1, 2);
/** Range from which each particle's lifetime in seconds will be selected. */
public readonly lifetimeRange = Range1d.createXX(5, 10);
/** Range from which each particle's size in meters will be selected. */
public readonly sizeRange = Range1d.createXX(0.2, 1.0);
/** Range from which the number of particles emitted will be selected. */
public numParticlesRange = Range1d.createXX(1600, 2200);
/** Emit an explosion of particles from the center of the sphere. */
public emit(): Particle[] {
const particles = [];
const numParticles = randomIntegerInRange(this.numParticlesRange);
for (let i = 0; i < numParticles; i++) {
const velocity = new Vector3d(randomFloat(-1.0, 1.0), randomFloat(-1.0, 1.0), randomFloat(-1.0, 1.0));
velocity.normalizeInPlace();
velocity.scaleInPlace(randomFloatInRange(this.speedRange));
const lifetime = randomFloatInRange(this.lifetimeRange);
const size = randomFloatInRange(this.sizeRange);
particles.push(new Particle(new Point3d(0, 0, 0), velocity, lifetime, size));
}
return particles;
}
}
class ParticleSystem {
private readonly _origin: Point3d;
private readonly _pickableId: Id64String;
private readonly _emitter = new ParticleEmitter();
private _numEmissions: number;
private readonly _texture: RenderTexture;
private _lastUpdateTime: number;
private _particles: Particle[] = [];
private readonly _scratchVector3d = new Vector3d();
private _dispose?: VoidFunction;
/** Acceleration in Z applied to particles, in meters per second squared. */
public gravity = -3;
public static numEmissionsRange = Range1d.createXX(1, 5);
public constructor(texture: RenderTexture, iModel: IModelConnection, numEmissions: number) {
this._texture = texture;
this._pickableId = iModel.transientIds.getNext();
this._numEmissions = numEmissions;
this._lastUpdateTime = Date.now();
this._origin = randomPositionInRange(iModel.projectExtents);
this._dispose = iModel.onClose.addListener(() => this.dispose());
}
public dispose(): void {
if (this._dispose) {
this._dispose();
this._dispose = undefined;
}
IModelApp.viewManager.dropDecorator(this);
this._texture.dispose();
}
public update(): void {
const now = Date.now();
let deltaMillis = now - this._lastUpdateTime;
deltaMillis = Math.min(100, deltaMillis);
this._lastUpdateTime = now;
let numParticles = this._particles.length;
if (numParticles === 0) {
this._numEmissions--;
if (this._numEmissions < 0)
this.dispose();
else
this._particles = this._emitter.emit();
return;
}
const elapsedSeconds = deltaMillis / 1000;
for (let i = 0; i < numParticles; i++) {
const particle = this._particles[i];
this.updateParticle(particle, elapsedSeconds);
if (particle.isExpired) {
this._particles[i] = this._particles[numParticles - 1];
--i;
--numParticles;
}
}
this._particles.length = numParticles;
}
private updateParticle(particle: Particle, elapsedSeconds: number): void {
const velocity = particle.velocity.clone(this._scratchVector3d);
velocity.scale(elapsedSeconds, velocity);
velocity.z += elapsedSeconds * this.gravity;
particle.position.addInPlace(velocity);
particle.transparency = 255 * (particle.age / particle.lifetime);
particle.age += elapsedSeconds;
}
public decorate(context: DecorateContext): void {
if (!context.viewport.view.isSpatialView())
return;
this.update();
const builder = ParticleCollectionBuilder.create({
viewport: context.viewport,
texture: this._texture,
size: (this._emitter.sizeRange.high - this._emitter.sizeRange.low) / 2,
transparency: 0,
origin: this._origin,
pickableId: this._pickableId,
});
for (const particle of this._particles)
builder.addParticle(particle);
const graphic = builder.finish();
if (graphic) {
context.addDecoration(GraphicType.WorldDecoration, graphic);
context.viewport.onRender.addOnce((vp) => vp.invalidateDecorations());
}
}
public testDecorationHit(id: Id64String): boolean {
return id === this._pickableId;
}
public async getDecorationToolTip(_hit: HitDetail): Promise<HTMLElement | string> {
return "Explosion effect";
}
public static async addDecorator(iModel: IModelConnection): Promise<void> {
// Note: The decorator takes ownership of the texture, and disposes of it when the decorator is disposed.
const image = await imageElementFromUrl(`${IModelApp.publicPath}sprites/particle_explosion.png`);
const texture = IModelApp.renderSystem.createTexture({
ownership: "external",
image: { source: image, transparency: TextureTransparency.Mixed },
});
if (texture)
IModelApp.viewManager.addDecorator(new ParticleSystem(texture, iModel, randomIntegerInRange(this.numEmissionsRange)));
}
}
/** This tool applies an explosion particle effect used for testing [ParticleCollectionBuilder]($frontend).
* @beta
*/
export class ExplosionEffect extends Tool {
public static override toolId = "ExplosionEffect";
/** This method runs the tool, applying an explosion particle effect. */
public override async run(): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (vp)
await ParticleSystem.addDecorator(vp.iModel);
return true;
}
}