-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathViroParticleEmitter.tsx
412 lines (396 loc) · 14.1 KB
/
ViroParticleEmitter.tsx
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
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Copyright (c) 2017-present, Viro Media, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ViroParticleEmitter
* @flow
*/
"use strict";
import * as React from "react";
import {
ColorValue,
findNodeHandle,
NativeModules,
NativeSyntheticEvent,
processColor,
requireNativeComponent,
} from "react-native";
// @ts-ignore
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
import { ViroObjectProps } from "./AR/ViroCommonProps";
import { ViroNativeTransformUpdateEvent } from "./Types/ViroEvents";
import { ViroNativeRef, ViroSource } from "./Types/ViroUtils";
import { checkMisnamedProps } from "./Utilities/ViroProps";
type ViroInterpolation = {
interval?: number[];
endValue: number[];
}[];
type Props = ViroObjectProps & {
// The delay in milliseconds to apply before this emitter starts a new emission cycle. Particles are produced during each emission cycle.
delay?: number;
duration?: number;
loop?: boolean;
run?: boolean;
fixedToEmitter?: boolean;
image: {
source: ViroSource;
height: number;
width: number;
bloomThreshold: number;
};
height?: number;
width?: number;
bloomThreshold?: number;
blendMode?: string;
spawnBehavior?: {
// The total number of particles this emitter spawns in a second. This value is
// chosen at random from the given [min, max] range array, and is in addition
// to the other emission parameters.
emissionRatePerSecond?: number[];
// The total number of particles this emitter spawns per meter travelled. This
// value is chosen at random from the given [min, max] range array, and is in
// addition to the other emission parameters.
emissionRatePerMeter?: number[];
// The lifetime of a particle in milliseconds.
particleLifetime?: number[];
// The maximum number of live particles that can exist at any moment from
// this emitter. This includes particles that have been created by the
// emissionRatePerSecond, emissionRatePerMeter, and emissionBurst parameters.
// When the cap is reached, new particles will only be spawned as existing particles die off.
maxParticles?: number;
// An array of parameters controlling how this emitter spawns particles in
// bursts, if any. Bursts can be scheduled to occur at certain times, or
// after a certain distance travelled by the emitter.
emissionBurst?:
| {
// Time in milliseconds, at which to emit this burst of particles. This time is set in reference to the start of the emission cycle of this emitter.
time?: number;
// The minimum number of particles to burst.
min?: number;
// The maximum number of particles to burst.
max?: number;
// The number of times to loop and repeat this burst.
cycles?: number;
// The cool down / waiting duration between cycles before the emitter
// spawns another burst of particles.
cooldownPeriod?: number;
}
| {
// Can also be in terms of distance travelled, in meters.|
distance?: number;
// The minimum number of particles to burst.
min?: number;
// The maximum number of particles to burst.
max?: number;
// The number of times to loop and repeat this burst.
cycles?: number;
// The cool down / waiting duration between cycles before the emitter
// spawns another burst of particles.
cooldownDistance?: number;
};
// The shape of the volume within which to spawn particles, and the parameters
// that describe that volume. If spawnOnSurface is true, particles will spawn on
// the surface of the volume, instead of within the volume. Note that particles
// will be uniformly distributed throughout the volume.
spawnVolume?: {
// Box: [width, height, and length]
// Sphere: A single float describing a radius parameter for a perfect sphere,
// or a vector representing the [x, y, z] length of an ellipsoid.
shape?: string;
params?: number[];
spawnOnSurface?: boolean;
};
};
particleAppearance?: {
opacity?: {
// The [min, max] range array within which to initialize a randomized opacity for
// particles to start of with.
initialRange: number[];
// The unit of reference against which to interpolate your visual property values,
// can be either time or distance.
factor?: "Time" | "Distance" | "time" | "distance";
// An array of data points, each containing an endValue and an interpolation
// interval, thereby "charting" the behavior of this property over the lifetime
// of the particle.
interpolation?: {
interval?: number[];
endValue?: number;
}[];
};
scale?: {
// The [min, max] range array within which to initialize a randomized opacity for
// particles to start of with. Here, min and max are vectors, as particles are
// scaled in 3d space.
initialRange?: number[][];
// The unit of reference against which to interpolate your visual property values,
// can be either time or distance.
factor?: "Time" | "Distance" | "time" | "distance";
// An array of data points, each containing an endValue and an interpolation
// interval, thereby "charting" the behavior of this property over the lifetime
// of the particle.
interpolation?: {
interval?: number[];
endValue?: number[];
}[];
};
// rotation is only about the Z axis
rotation?: {
// The [min, max] range array defining the interval of initial rotation values
// for each particle. Min and max are floats. Rotation is performed on the
// quad's Z axis, and are then billboarded to the user.
initialRange?: number[];
// The unit of reference against which to interpolate visual property values, can
// be either time or distance.
factor?: "Time" | "Distance" | "time" | "distance";
// An array of data points, each containing an endValue and an interpolation
// interval, thereby "charting" the behavior of this property over the lifetime
// of the particle.
interpolation?: {
interval?: number[];
endValue?: number;
}[];
};
color?: {
// The [min, max] range within which to initialize each particle's color.
initialRange?: ColorValue[];
// The unit of reference against which to interpolate your visual property values,
// can be either time or distance.
factor?: "Time" | "Distance" | "time" | "distance";
// An array of data points, each containing an endValue and an interpolation interval,
// thereby "charting" the behavior of this property over the lifetime of the particle.
interpolation?: {
interval?: number[];
endValue?: ColorValue;
}[];
};
};
particlePhysics?: {
velocity?: {
initialRange?: number[][];
};
acceleration?: {
initialRange?: number[][];
};
explosiveImpulse?: {
impulse?: number;
position?: number[];
decelerationPeriod?: number;
};
};
};
type State = {
nativePositionState: any;
propsPositionState: any;
};
export class ViroParticleEmitter extends React.Component<Props, State> {
state = {
propsPositionState: this.props.position,
nativePositionState: undefined,
};
_component: ViroNativeRef = null;
async getTransformAsync() {
return await NativeModules.VRTNodeModule.getNodeTransform(
findNodeHandle(this)
);
}
async getBoundingBoxAsync() {
return await NativeModules.VRTNodeModule.getBoundingBox(
findNodeHandle(this)
);
}
// Called from native on the event a positional change has occured
// for the underlying control within the renderer.
_onNativeTransformUpdate(
event: NativeSyntheticEvent<ViroNativeTransformUpdateEvent>
) {
var position = event.nativeEvent.position;
this.setState(
{
nativePositionState: position,
},
() => {
if (this.props.onTransformUpdate) {
this.props.onTransformUpdate(position);
}
}
);
}
// Ignore all changes in native position state as it is only required to
// keep track of the latest position prop set on this control.
shouldComponentUpdate(_nextProps: Props, nextState: State) {
if (nextState.nativePositionState != this.state.nativePositionState) {
return false;
}
return true;
}
setNativeProps(nativeProps: Props) {
this._component?.setNativeProps(nativeProps);
}
render() {
checkMisnamedProps("ViroParticleEmitter", this.props);
let image = { ...this.props.image };
if (image.source != undefined) {
image.source = resolveAssetSource(image.source);
}
let transformBehaviors =
typeof this.props.transformBehaviors === "string"
? new Array(this.props.transformBehaviors)
: this.props.transformBehaviors;
let transformDelegate =
this.props.onTransformUpdate != undefined
? this._onNativeTransformUpdate
: undefined;
// Create native props object.
let nativeProps = Object.assign({} as any, this.props);
nativeProps.position = this.state.propsPositionState;
nativeProps.onNativeTransformDelegateViro = transformDelegate;
nativeProps.hasTransformDelegate =
this.props.onTransformUpdate != undefined;
nativeProps.image = image;
nativeProps.transformBehaviors = transformBehaviors;
// For color modifiers, we'll need to processColor for each color value.
if (this.props.particleAppearance && this.props.particleAppearance.color) {
let colorModifier = this.props.particleAppearance.color;
if (colorModifier.initialRange?.length != 2) {
console.error(
"The <ViroParticleEmitter> component requires initial value of [min, max] when defining inital rotation property!"
);
return;
}
let minColorFinal = processColor(colorModifier.initialRange[0]);
let maxColorFinal = processColor(colorModifier.initialRange[1]);
let modifierFinal = [];
let interpolationLength =
colorModifier.interpolation != undefined
? colorModifier.interpolation.length
: 0;
for (let i = 0; i < interpolationLength; i++) {
let processedColor = processColor(
(colorModifier.interpolation as any)[i].endValue as ColorValue
);
let mod = {
interval: (colorModifier.interpolation as any[])[i].interval,
endValue: processedColor,
};
modifierFinal.push(mod);
}
let newAppearanceColorMod = {
initialRange: [minColorFinal, maxColorFinal],
factor: colorModifier.factor,
interpolation: modifierFinal,
};
nativeProps.particleAppearance.color = newAppearanceColorMod;
}
// For rotation modifiers, convert degrees to radians, then apply the
// Z rotation (due to billboarding for quad particles)
if (
this.props.particleAppearance &&
this.props.particleAppearance.rotation
) {
let rotMod = this.props.particleAppearance.rotation;
if ((rotMod.initialRange as any[]).length !== 2) {
console.error(
"The <ViroParticleEmitter> component requires initial value of [min, max] when defining inital rotation property!"
);
}
let minRotFinal = [
0,
0,
((rotMod.initialRange as number[])[0] * Math.PI) / 180,
];
let maxRotFinal = [
0,
0,
((rotMod.initialRange as number[])[1] * Math.PI) / 180,
];
let modifierFinal = [];
let interpolationLength =
rotMod.interpolation != undefined ? rotMod.interpolation.length : 0;
for (var i = 0; i < interpolationLength; i++) {
let processedRot = [
0,
0,
((rotMod.interpolation as any[])[i].endValue * Math.PI) / 180,
];
let mod = {
interval: (rotMod.interpolation as any[])[i].interval,
endValue: processedRot,
};
modifierFinal.push(mod);
}
let newAppearanceRotMod = {
initialRange: [minRotFinal, maxRotFinal],
factor: rotMod.factor,
interpolation: modifierFinal,
};
nativeProps.particleAppearance.rotation = newAppearanceRotMod;
}
nativeProps.ref = (component: ViroNativeRef) => {
this._component = component;
};
return <VRTParticleEmitter {...nativeProps} />;
}
// Set the propsPositionState on the native control if the
// nextProps.position state differs from the nativePositionState that
// reflects this control's current vroNode position.
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
if (
nextProps.position &&
nextProps.position != prevState.nativePositionState
) {
var newPosition = [
nextProps.position[0],
nextProps.position[1],
nextProps.position[2],
Math.random(),
];
return {
propsPositionState: newPosition,
};
}
return {};
}
}
var VRTParticleEmitter = requireNativeComponent(
"VRTParticleEmitter",
// @ts-ignore
ViroParticleEmitter,
{
nativeOnly: {
onNativeTransformDelegateViro: true,
hasTransformDelegate: true,
canHover: true,
canClick: true,
canTouch: true,
canScroll: true,
canSwipe: true,
canDrag: true,
canPinch: true,
canRotate: true,
canFuse: true,
canCollide: true,
onHoverViro: true,
onClickViro: true,
onTouchViro: true,
onScrollViro: true,
onSwipeViro: true,
onDragViro: true,
onPinchViro: true,
onRotateViro: true,
onPlatformUpdateViro: true,
onFuseViro: true,
timeToFuse: true,
physicsBody: true,
onCollisionViro: true,
animation: true,
materials: true,
dragType: true,
dragPlane: true,
ignoreEventHandling: true,
},
}
);