-
Notifications
You must be signed in to change notification settings - Fork 208
/
Snow.ts
261 lines (225 loc) · 10.2 KB
/
Snow.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
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
/*---------------------------------------------------------------------------------------------
* 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 { dispose } from "@itwin/core-bentley";
import { Point2d, Range1d, Range2d, Vector2d } from "@itwin/core-geometry";
import { RenderTexture, TextureTransparency } from "@itwin/core-common";
import {
DecorateContext, Decorator, GraphicType, imageElementFromUrl, IModelApp, ParticleCollectionBuilder, ParticleProps, Tool, Viewport,
} from "@itwin/core-frontend";
import { parseToggle } from "../tools/parseToggle";
import { randomFloat, randomInteger } from "./Random";
/** Represents one particle displayed by a [[SnowDecorator]].
* Particle positions are in [CoordSystem.View]($frontend).
* @beta
*/
export interface SnowParticle extends ParticleProps {
/** Make x, y, and z from ParticleProps writable. */
x: number;
y: number;
z: number;
/** Current velocity, in pixels per second. */
velocity: Vector2d;
}
/** Parameters controlling how a [[SnowDecorator]] works.
* @beta
*/
export interface SnowParams {
/** The number of snow particles to produce. This could alternatively be expressed as a density so that small viewports would not be more crowded than larger ones. */
numParticles: number;
/** Range from which to randomly select each particle's size, in pixels. */
sizeRange: Range1d;
/** Range from which to randomly select each particle's transparency. */
transparencyRange: Range1d;
/** Range from which to randomly select each particle's initial velocity, in pixels per second. */
velocityRange: Range2d;
/** Range from which to randomly select an acceleration to apply to each particle's velocity each frame, in pixels per second squared, to simulate wind. */
accelerationRange: Range2d;
/** Wind velocity in pixels per second in X. */
windVelocity: number;
}
/** The default snow effect parameters used by newly-created SnowDecorators. */
const defaultSnowParams: SnowParams = {
numParticles: 2000,
sizeRange: Range1d.createXX(3, 22),
transparencyRange: Range1d.createXX(0, 50),
velocityRange: new Range2d(-30, 50, 30, 130),
accelerationRange: new Range2d(-1, -0.25, 1, 0.25),
windVelocity: 0,
};
/** Simulates snowfall in a [Viewport]($frontend) using particle effects.
* @see [[SnowEffect]] for a [Tool]($frontend) that toggles this decorator.
* @see [ParticleCollectionBuilder]($frontend) for defining custom particle effects.
* @beta
*/
export class SnowDecorator implements Decorator {
/** The viewport being decorated. */
public readonly viewport: Viewport;
/** Invoked when this decorator is to be destroyed. */
public readonly dispose: VoidFunction;
/** The initial width and height of the viewport, from which we randomly select each particle's initial position. */
private readonly _dimensions: Point2d;
/** The list of particles being drawn. */
private readonly _particles: SnowParticle[] = [];
/** The image to display for each particle. */
private _texture?: RenderTexture;
/** The last time `updateParticles()` was invoked, in milliseconds. */
private _lastUpdateTime: number;
private readonly _params: SnowParams;
private constructor(viewport: Viewport, texture: RenderTexture | undefined) {
this._params = { ...defaultSnowParams };
this.viewport = viewport;
this._dimensions = new Point2d(viewport.viewRect.width, viewport.viewRect.height);
this._lastUpdateTime = Date.now();
this._texture = texture;
// Tell the viewport to re-render the decorations every frame so that the snow particles animate smoothly.
const removeOnRender = viewport.onRender.addListener(() => viewport.invalidateDecorations());
// When the viewport is resized, replace this decorator with a new one to match the new dimensions.
const removeOnResized = viewport.onResized.addListener(() => {
// Transfer ownership of the texture to the new decorator.
const tex = this._texture;
this._texture = undefined;
this.dispose();
new SnowDecorator(viewport, tex);
});
// When the viewport is destroyed, dispose of this decorator too.
const removeOnDispose = viewport.onDisposed.addListener(() => this.dispose());
const removeDecorator = IModelApp.viewManager.addDecorator(this);
this.dispose = () => {
removeDecorator();
removeOnRender();
removeOnDispose();
removeOnResized();
this._texture = dispose(this._texture);
SnowDecorator._decorators.delete(viewport);
};
SnowDecorator._decorators.set(viewport, this);
// Initialize the particles.
for (let i = 0; i < this._params.numParticles; i++)
this._particles.push(this.emit(true));
}
public decorate(context: DecorateContext): void {
if (context.viewport !== this.viewport || !this._texture)
return;
// Update the particles.
const now = Date.now();
const deltaMillis = now - this._lastUpdateTime;
this._lastUpdateTime = now;
this.updateParticles(deltaMillis / 1000);
// Create particle graphics.
const builder = ParticleCollectionBuilder.create({
viewport: this.viewport,
isViewCoords: true,
texture: this._texture,
size: (this._params.sizeRange.high - this._params.sizeRange.low) / 2,
});
for (const particle of this._particles)
builder.addParticle(particle);
const graphic = builder.finish();
if (graphic)
context.addDecoration(GraphicType.ViewOverlay, graphic);
}
/** Change some of the parameters affecting this decorator. */
public configure(params: Partial<SnowParams>): void {
for (const key of Object.keys(params)) {
const val = (params as any)[key];
if (undefined !== val)
(this._params as any)[key] = val;
}
}
/** Emit a new particle with randomized properties. */
private emit(randomizeHeight: boolean): SnowParticle {
return {
x: randomInteger(0, this._dimensions.x),
y: randomizeHeight ? randomInteger(0, this._dimensions.y) : 0,
z: 0,
size: randomInteger(this._params.sizeRange.low, this._params.sizeRange.high),
transparency: randomInteger(this._params.transparencyRange.low, this._params.transparencyRange.high),
velocity: new Vector2d(randomFloat(this._params.velocityRange.low.x, this._params.velocityRange.high.x),
randomFloat(this._params.velocityRange.low.y, this._params.velocityRange.high.y)),
};
}
// Update the positions and velocities of all the particles based on the amount of time that has passed since the last update.
private updateParticles(elapsedSeconds: number): void {
// Determine if someone changed the desired number of particles.
const particleDiscrepancy = this._params.numParticles - this._particles.length;
if (particleDiscrepancy > 0) {
// Birth new particles up to the new maximum.
for (let i = 0; i < particleDiscrepancy; i++)
this._particles.push(this.emit(true));
} else {
// Destroy extra particles.
this._particles.length = this._params.numParticles;
}
const acceleration = new Vector2d();
const velocity = new Vector2d();
for (let i = 0; i < this._particles.length; i++) {
// Apply some acceleration to produce random drift.
const particle = this._particles[i];
acceleration.set(randomFloat(this._params.accelerationRange.low.x, this._params.accelerationRange.high.x),
randomFloat(this._params.accelerationRange.low.y, this._params.accelerationRange.high.y));
acceleration.scale(elapsedSeconds, acceleration);
particle.velocity.plus(acceleration, particle.velocity);
// Apply velocity.
particle.velocity.clone(velocity);
velocity.scale(elapsedSeconds, velocity);
particle.x += velocity.x;
particle.y += velocity.y;
// Apply wind
particle.x += this._params.windVelocity * elapsedSeconds;
// Particles that travel beyond the viewport's left or right edges wrap around to the other side.
if (particle.x < 0)
particle.x = this._dimensions.x - 1;
else if (particle.x >= this._dimensions.x)
particle.x = 0;
// Particles that travel beyond the viewport's bottom or top edges are replaced by newborn particles.
if (particle.y < 0 || particle.y >= this._dimensions.y)
this._particles[i] = this.emit(false);
}
}
private static readonly _decorators = new Map<Viewport, SnowDecorator>();
/** Toggle this decorator for the specified viewport.
* @param viewport The viewport to which the effect should be applied or removed.
* @param enable `true` to enable the effect, `false` to disable it, or `undefined` to toggle the current state.
*/
public static async toggle(viewport: Viewport, enable?: boolean): Promise<void> {
const decorator = this._decorators.get(viewport);
if (undefined === enable)
enable = undefined === decorator;
if (undefined !== decorator && !enable)
decorator.dispose();
else if (undefined === decorator && enable) {
// Create a texture to use for the particles.
// 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_snow.png`);
const texture = IModelApp.renderSystem.createTexture({
ownership: "external",
image: { source: image, transparency: TextureTransparency.Mixed },
});
new SnowDecorator(viewport, texture);
}
}
}
/** Toggles a decorator that simulates snow using particle effects.
* @see [[SnowDecorator]] for the implementation of the decorator.
* @beta
*/
export class SnowEffect extends Tool {
public static override toolId = "SnowEffect";
public override async run(enable?: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (vp)
await SnowDecorator.toggle(vp, enable);
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
const enable = parseToggle(args[0]);
if (typeof enable !== "string")
await this.run(enable);
return true;
}
}