/
RadialVelocityInfluencer.java
587 lines (503 loc) · 16.5 KB
/
RadialVelocityInfluencer.java
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package tonegod.emitter.influencers.impl;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tonegod.emitter.EmitterMesh;
import tonegod.emitter.Messages;
import tonegod.emitter.ParticleEmitterNode;
import tonegod.emitter.influencers.ParticleInfluencer;
import tonegod.emitter.particle.ParticleData;
import tonegod.emitter.util.RandomUtils;
import java.io.IOException;
import java.util.Random;
/**
* The implementation of the {@link ParticleInfluencer} to radial rotation particles.
*
* @author t0neg0d, JavaSaBr
*/
public class RadialVelocityInfluencer extends AbstractWithDataParticleInfluencer<RadialVelocityInfluencer.RadialVelocityData> {
protected static class RadialVelocityData implements JmeCloneable {
private float tangentForce;
@Override
public @NotNull Object jmeClone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
@Override
public void cloneFields(@NotNull Cloner cloner, @NotNull Object original) {
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final RadialVelocityData that = (RadialVelocityData) o;
return Float.compare(that.tangentForce, tangentForce) == 0;
}
@Override
public int hashCode() {
return (tangentForce != +0.0f ? Float.floatToIntBits(tangentForce) : 0);
}
@Override
public String toString() {
return "RadialVelocityData{" + "tangentForce=" + tangentForce + '}';
}
}
/**
* The list of radial pull alignments.
*/
public enum RadialPullAlignment {
/**
* Emission point radial pull alignment.
*/
EMISSION_POINT(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_ALIGNMENT_EMISSION_POINT),
/**
* Emitter center radial pull alignment.
*/
EMITTER_CENTER(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_ALIGNMENT_EMITTER_CENTER);
private static final RadialPullAlignment[] VALUES = values();
/**
* Get a radial pull alignment by the index.
*
* @param index the index.
* @return the radial pull alignment.
*/
public static @NotNull RadialPullAlignment valueOf(int index) {
return VALUES[index];
}
@NotNull
private final String name;
RadialPullAlignment(@NotNull String name) {
this.name = name;
}
@Override
public @NotNull String toString() {
return name;
}
}
/**
* The list of radial pull centers.
*/
public enum RadialPullCenter {
/**
* Absolute radial pull center.
*/
ABSOLUTE(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_CENTER_ABSOLUTE),
/**
* Position x radial pull center.
*/
POSITION_X(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_CENTER_POSITION_X),
/**
* Position y radial pull center.
*/
POSITION_Y(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_CENTER_POSITION_Y),
/**
* Position z radial pull center.
*/
POSITION_Z(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_PULL_CENTER_POSITION_Z);
private static final RadialPullCenter[] VALUES = values();
/**
* Get a radial pull center by the index.
*
* @param index the index.
* @return the radial pull center.
*/
public static @NotNull RadialPullCenter valueOf(int index) {
return VALUES[index];
}
@NotNull
private final String name;
RadialPullCenter(@NotNull String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
/**
* The list of radial up alignments.
*/
public enum RadialUpAlignment {
/**
* Normal radial up alignment.
*/
NORMAL(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_UP_ALIGNMENT_NORMAL),
/**
* Unit x radial up alignment.
*/
UNIT_X(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_UP_ALIGNMENT_UNIT_X),
/**
* Unit y radial up alignment.
*/
UNIT_Y(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_UP_ALIGNMENT_UNIT_Y),
/**
* Unit z radial up alignment.
*/
UNIT_Z(Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY_UP_ALIGNMENT_UNIT_Z);
private static final RadialUpAlignment[] VALUES = values();
/**
* Get a radial up alignment by the index.
*
* @param index the index.
* @return the radial up alignment.
*/
public static @NotNull RadialUpAlignment valueOf(int index) {
return VALUES[index];
}
@NotNull
private final String name;
RadialUpAlignment(@NotNull String name) {
this.name = name;
}
@Override
public @NotNull String toString() {
return name;
}
}
/**
* The tangent.
*/
@NotNull
private final Vector3f tangent;
/**
* The vector store.
*/
@NotNull
private final Vector3f store;
/**
* The up vector.
*/
@NotNull
private final Vector3f up;
/**
* The left vector.
*/
@NotNull
private final Vector3f left;
/**
* The vector for storing up vector.
*/
@NotNull
private final Vector3f upStore;
/**
* The temp store vector.
*/
@NotNull
private final Vector3f tempStore;
/**
* The inverse rotation.
*/
@NotNull
private final Quaternion inverseRotation;
/**
* The radial pull alignment.
*/
@NotNull
private RadialPullAlignment pullAlignment;
/**
* The radial pull center.
*/
@NotNull
private RadialPullCenter pullCenter;
/**
* The radial up alignment.
*/
@NotNull
private RadialUpAlignment upAlignment;
/**
* The value of radial pulling.
*/
private float radialPull;
/**
* The value of tangent force.
*/
private float tangentForce;
/**
* The flag of using random directions.
*/
private boolean randomDirection;
public RadialVelocityInfluencer() {
this.tangent = new Vector3f();
this.store = new Vector3f();
this.up = Vector3f.UNIT_Y.clone();
this.left = new Vector3f();
this.upStore = new Vector3f();
this.tempStore = new Vector3f();
this.inverseRotation = new Quaternion();
this.pullAlignment = RadialPullAlignment.EMISSION_POINT;
this.pullCenter = RadialPullCenter.ABSOLUTE;
this.upAlignment = RadialUpAlignment.UNIT_Y;
this.radialPull = 1;
this.tangentForce = 1;
}
@Override
public @NotNull String getName() {
return Messages.PARTICLE_INFLUENCER_RADIAL_VELOCITY;
}
@Override
public @NotNull RadialVelocityInfluencer.RadialVelocityData newDataObject() {
return new RadialVelocityData();
}
@Override
protected void updateImpl(
@NotNull ParticleEmitterNode emitterNode,
@NotNull ParticleData particleData,
@NotNull RadialVelocityInfluencer.RadialVelocityData data,
float tpf
) {
EmitterMesh emitterShape = emitterNode.getEmitterShape();
Quaternion localRotation = emitterNode.getLocalRotation();
processPullAlignment(particleData, emitterNode, emitterShape);
processCenter(particleData);
store.subtractLocal(particleData.getPosition())
.normalizeLocal()
.multLocal(particleData.getInitialLength() * radialPull)
.multLocal(tpf);
processUpAlignment(emitterNode, emitterShape);
up.set(store).crossLocal(upStore)
.normalizeLocal()
.set(localRotation.mult(up, tempStore));
left.set(store).crossLocal(up)
.normalizeLocal();
tangent.set(store)
.crossLocal(left)
.normalizeLocal()
.multLocal(data.tangentForce)
.multLocal(tpf);
particleData.velocity.subtractLocal(tangent);
particleData.velocity.addLocal(store.mult(radialPull, tempStore));
super.updateImpl(emitterNode, particleData, data, tpf);
}
/**
* Handle up alignment.
*/
private void processUpAlignment(
@NotNull ParticleEmitterNode emitterNode,
@NotNull EmitterMesh emitterShape
) {
switch (getRadialUpAlignment()) {
case NORMAL: {
inverseRotation.set(emitterNode.getLocalRotation()).inverseLocal();
upStore.set(inverseRotation.mult(upStore.set(emitterShape.getNormal()), tempStore));
break;
}
case UNIT_X: {
upStore.set(Vector3f.UNIT_X);
break;
}
case UNIT_Y: {
upStore.set(Vector3f.UNIT_Y);
break;
}
case UNIT_Z: {
upStore.set(Vector3f.UNIT_Z);
break;
}
}
}
/**
* Handle pull center.
*/
private void processCenter(@NotNull ParticleData particleData) {
switch (getRadialPullCenter()) {
case ABSOLUTE: {
break;
}
case POSITION_X: {
store.setX(particleData.position.x);
break;
}
case POSITION_Y: {
store.setY(particleData.position.y);
break;
}
case POSITION_Z: {
store.setZ(particleData.position.z);
break;
}
}
}
/**
* Handle pull alignment.
*/
private void processPullAlignment(
@NotNull ParticleData particleData,
@NotNull ParticleEmitterNode emitterNode,
@NotNull EmitterMesh emitterShape
) {
switch (getRadialPullAlignment()) {
case EMISSION_POINT: {
emitterShape.setNext(particleData.triangleIndex);
if (emitterNode.isRandomEmissionPoint()) {
store.set(emitterShape.getNextTranslation()
.addLocal(particleData.getRandomOffset()));
} else {
store.set(emitterShape.getNextTranslation());
}
break;
}
case EMITTER_CENTER: {
store.set(emitterShape.getMesh().getBound().getCenter());
break;
}
}
}
@Override
protected void initializeImpl(
@NotNull ParticleEmitterNode emitterNode,
@NotNull ParticleData particleData,
@NotNull RadialVelocityInfluencer.RadialVelocityData data
) {
if (!isRandomDirection()) {
data.tangentForce = tangentForce;
return;
}
Random random = RandomUtils.getRandom();
if (random.nextBoolean()) {
data.tangentForce = tangentForce;
} else {
data.tangentForce = -tangentForce;
}
super.initializeImpl(emitterNode, particleData, data);
}
/**
* The tangent force to apply when updating the particles trajectory.
*
* @param tangentForce the tangent force.
*/
public void setTangentForce(float tangentForce) {
this.tangentForce = tangentForce;
}
/**
* Returns the defined tangent force used when calculating the particles trajectory.
*
* @return the tangent force.
*/
public float getTangentForce() {
return tangentForce;
}
/**
* Defines the point of origin that the particle will use in calculating it's trajectory.
*
* @param alignment the pullAlignment.
*/
public void setRadialPullAlignment(@NotNull RadialPullAlignment alignment) {
this.pullAlignment = alignment;
}
/**
* Returns the defined point of origin parameter.
*
* @return the radial pull alignment.
*/
public @NotNull RadialPullAlignment getRadialPullAlignment() {
return pullAlignment;
}
/**
* Alters how the particle will orbit it's radial pull pullAlignment. For example, POSITION_Y, will use the X/Z
* components of the point of origin vector, but use the individual particles Y component when calculating the
* updated trajectory.
*
* @param center the center.
*/
public void setRadialPullCenter(@NotNull RadialPullCenter center) {
this.pullCenter = center;
}
/**
* Returns the defined variant for the point of origin vector.
*
* @return the radial pull center.
*/
public @NotNull RadialPullCenter getRadialPullCenter() {
return pullCenter;
}
/**
* Defines the gravitational force pulling against the tangent force - Or, how the orbit will tighten or decay over
* time.
*
* @param radialPull the radial pull.
*/
public void setRadialPull(float radialPull) {
this.radialPull = radialPull;
}
/**
* Returns the defined radial pull used when calculating the particles trajectory.
*
* @return the radial pull.
*/
public float getRadialPull() {
return radialPull;
}
/**
* Defines the up vector used to calculate rotation around a pullCenter point.
*
* @param upAlignment the up alignment.
*/
public void setRadialUpAlignment(@NotNull RadialUpAlignment upAlignment) {
this.upAlignment = upAlignment;
}
/**
* Returns the defined up vector parameter.
*
* @return the radial up alignment.
*/
public @NotNull RadialUpAlignment getRadialUpAlignment() {
return upAlignment;
}
/**
* Allows the influencer to randomly select the negative of the defined tangentForce to reverse the direction of
* rotation
*
* @param randomDirection the random direction
*/
public void setRandomDirection(boolean randomDirection) {
this.randomDirection = randomDirection;
}
/**
* Returns true if the influencer allows random reverse rotation.
*
* @return true if the influencer allows random reverse rotation.
*/
public boolean isRandomDirection() {
return randomDirection;
}
@Override
public void write(@NotNull JmeExporter exporter) throws IOException {
super.write(exporter);
OutputCapsule capsule = exporter.getCapsule(this);
capsule.write(radialPull, "radialPull", 1.0f);
capsule.write(tangentForce, "tangentForce", 1.0f);
capsule.write(pullAlignment.ordinal(), "pullAlignment", RadialPullAlignment.EMISSION_POINT.ordinal());
capsule.write(pullCenter.ordinal(), "pullCenter", RadialPullCenter.ABSOLUTE.ordinal());
capsule.write(upAlignment.ordinal(), "upAlignment", RadialUpAlignment.UNIT_Y.ordinal());
}
@Override
public void read(@NotNull JmeImporter importer) throws IOException {
super.read(importer);
InputCapsule capsule = importer.getCapsule(this);
radialPull = capsule.readFloat("radialPull", 1.0f);
tangentForce = capsule.readFloat("tangentForce", 1.0f);
pullAlignment = RadialPullAlignment.valueOf(capsule.readInt("pullAlignment", capsule.readInt("alignment", RadialPullAlignment.EMISSION_POINT.ordinal())));
pullCenter = RadialPullCenter.valueOf(capsule.readInt("pullCenter", capsule.readInt("center", RadialPullCenter.ABSOLUTE.ordinal())));
upAlignment = RadialUpAlignment.valueOf(capsule.readInt("upAlignment", RadialUpAlignment.UNIT_Y.ordinal()));
}
@Override
public @NotNull ParticleInfluencer clone() {
RadialVelocityInfluencer clone = (RadialVelocityInfluencer) super.clone();
clone.setRadialPull(radialPull);
clone.setTangentForce(tangentForce);
clone.setRadialPullAlignment(pullAlignment);
clone.setRadialPullCenter(pullCenter);
clone.setRadialUpAlignment(upAlignment);
return clone;
}
}