/
VfxUtils.java
123 lines (112 loc) · 3.4 KB
/
VfxUtils.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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package codex.vfx.utils;
import com.jme3.asset.AssetManager;
import com.jme3.light.LightProbe;
import com.jme3.math.FastMath;
import com.jme3.math.Plane;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
/**
* Utils for vfx and general applications.
*
* @author codex
*/
public class VfxUtils {
public static final VfxRandomGenerator gen = new VfxRandomGenerator();
private static final Plane tempPlane = new Plane();
/**
* Gets the shared generator instance.
* <p>
* Useful for quick'n'dirty randomization.
*
* @return
*/
public static VfxRandomGenerator getGenerator() {
return gen;
}
/**
* Generates a random vector within a box with
* the vectors A and B at opposite corners.
*
* @param a
* @param b
* @return random vector within box
*/
public static Vector3f random(Vector3f a, Vector3f b) {
return new Vector3f(
gen.nextFloat(a.x, b.x),
gen.nextFloat(a.y, b.y),
gen.nextFloat(a.z, b.z)
);
}
/**
* Constructs a random unit vector with its magnitude between
* the minimum and maximum distances.
*
* @param minDist
* @param maxDist
* @return
*/
public static Vector3f random(float minDist, float maxDist) {
return gen.nextUnitVector3f().multLocal(gen.nextFloat(minDist, maxDist));
}
/**
* Generates a random vector within a box.
*
* @param center center point of the box
* @param x x radius
* @param y y radius
* @param z z radius
* @return
*/
public static Vector3f random(Vector3f center, float x, float y, float z) {
return new Vector3f(
gen.nextFloat(center.x-x, center.x+x),
gen.nextFloat(center.y-y, center.y+y),
gen.nextFloat(center.z-z, center.z+z)
);
}
/**
* Loads light probe from j3o file.
*
* @param assetManager
* @param path
* @return
*/
public static LightProbe loadLightProbe(AssetManager assetManager, String path) {
Node probeNode = (Node)assetManager.loadModel(path);
return (LightProbe)probeNode.getLocalLightList().iterator().next();
}
/**
* Offsets the vector by an angle and stores the result.
* <p>
* Work in progress.
*
* @param vec
* @param angle
* @param store stores result, or null
* @return
*/
public static Vector3f offsetByAngle(Vector3f vec, float angle, Vector3f store) {
if (store == null) {
store = new Vector3f();
}
store.set(vec).normalizeLocal().multLocal(FastMath.cos(angle));
tempPlane.setOriginNormal(Vector3f.ZERO, store);
store.set(tempPlane.getClosestPoint(VfxUtils.gen.nextUnitVector3f()
.multLocal(VfxUtils.gen.nextFloat(FastMath.abs(FastMath.sin(angle)))).addLocal(store)));
return store.normalizeLocal().multLocal(vec.length());
}
/**
* Returns the average of all vector components.
*
* @param vec
* @return
*/
public static float vectorAverage(Vector3f vec) {
return (vec.x + vec.y + vec.z) / 3;
}
}