Skip to content

Commit 84a43c1

Browse files
VectorEffects (#2577)
* Add files via upload * Add Example * Update VectorGroup.java * Add FastNoiseLite.java Add FastNoiseLite (MIT license) from https://github.com/Auburn/FastNoiseLite/tree/master * Add NoiseVectorEffect.java * Update VectorGroup.java * refactor: make typesafe and immutable * Add files via upload * Add files via upload * Update VectorEffectSimpleTest.java * Add files via upload * Add files via upload * Update NoiseVectorEffect.java * Update SequencedVectorEffect.java * Update EaseVectorEffect.java * Update AbstractVectorEffect.java * Update Vector Effects based on Recent Reviews * Update Examples * Update Examples * Update VectorEffects based on recent reviews * Update NoiseVectorEffectTest.java --------- Co-authored-by: Riccardo Balbo <os@rblb.it>
1 parent f7b69cf commit 84a43c1

12 files changed

Lines changed: 4088 additions & 0 deletions

File tree

jme3-core/src/main/java/com/jme3/math/FastNoiseLite.java

Lines changed: 2593 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
package com.jme3.vectoreffect;
34+
35+
import com.jme3.app.state.AppStateManager;
36+
import java.util.ArrayList;
37+
38+
/**
39+
* @author yaRnMcDonuts
40+
*/
41+
42+
public abstract class AbstractVectorEffect implements VectorEffect {
43+
44+
protected VectorGroup vectorsToModify;
45+
private final ArrayList<Runnable> onFinishedCallbacks = new ArrayList<>();
46+
protected boolean isFinished = false;
47+
private String name;
48+
49+
public AbstractVectorEffect(){
50+
51+
}
52+
53+
public AbstractVectorEffect(VectorGroup vectorsToModify) {
54+
this.vectorsToModify = vectorsToModify;
55+
}
56+
57+
@Override
58+
public void setIsFinished(boolean isFinished) {
59+
this.isFinished = isFinished;
60+
if (isFinished) {
61+
for(Runnable r : onFinishedCallbacks) {
62+
r.run();
63+
}
64+
}
65+
}
66+
67+
@Override
68+
public VectorGroup getVectorsToModify(){
69+
return vectorsToModify;
70+
}
71+
72+
@Override
73+
public boolean isFinished() {
74+
return isFinished;
75+
}
76+
77+
78+
@Override
79+
public void reset() {
80+
isFinished = false;
81+
}
82+
83+
public void setName(String name) {
84+
this.name = name;
85+
}
86+
87+
public String getName() {
88+
return name;
89+
}
90+
91+
public void runOnFinish(Runnable runnable) {
92+
onFinishedCallbacks.add(runnable);
93+
}
94+
95+
@Override
96+
public void update(float tpf){
97+
98+
}
99+
100+
// convenience registration method so users can avoid repeatedly writing this AppState fetching code
101+
public void registerTo(AppStateManager stateManager) {
102+
if(stateManager != null){
103+
VectorEffectManagerState vectorEffectManagerState = stateManager.getState(VectorEffectManagerState.class);
104+
if(vectorEffectManagerState != null){
105+
vectorEffectManagerState.playVectorEffect(this);
106+
}
107+
}
108+
}
109+
}
110+
111+
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
package com.jme3.vectoreffect;
34+
35+
import com.jme3.math.EaseFunction;
36+
import com.jme3.math.Easing;
37+
import com.jme3.math.Vector4f;
38+
39+
/**
40+
*
41+
* @author yaRnMcDonuts
42+
*/
43+
public class EaseVectorEffect extends AbstractVectorEffect{
44+
45+
private VectorGroup targetVectors;
46+
private VectorGroup startVectors;
47+
48+
private float duration = 0f;
49+
private float easeTimer = 0f;
50+
private float delay = 0f;
51+
private float delayTimer = 0f;
52+
53+
private EaseFunction easeFunction = Easing.linear;
54+
55+
private final Vector4f tempTargetVec = new Vector4f();
56+
private final Vector4f tempStartVec = new Vector4f();
57+
58+
public EaseVectorEffect(VectorGroup vectorToModify) {
59+
super(vectorToModify);
60+
}
61+
62+
public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration) {
63+
this(vectorToModify);
64+
setEaseToValueOverDuration(duration, targetVector);
65+
}
66+
67+
public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
68+
float delay) {
69+
this(vectorToModify);
70+
setEaseToValueOverDuration(duration, targetVector);
71+
setDelayTime(delay);
72+
}
73+
74+
public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
75+
EaseFunction easeFunction) {
76+
this(vectorToModify, targetVector, duration);
77+
setEaseFunction(easeFunction);
78+
}
79+
80+
public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
81+
EaseFunction easeFunction, float delay) {
82+
this(vectorToModify, targetVector, duration);
83+
setEaseFunction(easeFunction);
84+
setDelayTime(delay);
85+
}
86+
87+
88+
@Override
89+
public void update(float tpf) {
90+
super.update(tpf);
91+
92+
if (delayTimer <= delay) {
93+
delayTimer += tpf;
94+
return;
95+
}
96+
97+
if (startVectors == null) {
98+
startVectors = vectorsToModify.clone();
99+
}
100+
101+
easeTimer += tpf;
102+
float t = Math.min(easeTimer / duration, 1f);
103+
104+
float easedT = easeFunction.apply(t);
105+
106+
for(int v = 0; v < vectorsToModify.getSize(); v++){
107+
108+
int targetIndex = Math.min(v, targetVectors.getSize() - 1); //allows multiple vectors to share a lesser number of targets if desired
109+
targetVectors.getAsVector4(targetIndex, tempTargetVec);
110+
startVectors.getAsVector4(v, tempStartVec);
111+
tempTargetVec.subtractLocal(tempStartVec);
112+
tempTargetVec.multLocal(easedT);
113+
114+
vectorsToModify.updateVectorObject(tempStartVec.addLocal(tempTargetVec), v);
115+
}
116+
117+
if (t >= 1f) {
118+
super.setIsFinished(true);
119+
}
120+
}
121+
122+
public EaseVectorEffect setEaseToValueOverDuration(float dur, VectorGroup targetVector) {
123+
this.targetVectors = targetVector;
124+
duration = dur;
125+
startVectors = null;
126+
return this;
127+
}
128+
129+
public void setDelayTime(float delay) {
130+
this.delay = delay;
131+
}
132+
133+
public void setEaseFunction(EaseFunction func) {
134+
this.easeFunction = func;
135+
}
136+
137+
@Override
138+
public void reset() {
139+
delayTimer = 0;
140+
easeTimer = 0;
141+
startVectors = null;
142+
super.reset();
143+
}
144+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.vectoreffect;
33+
34+
import com.jme3.math.Vector4f;
35+
import com.jme3.math.FastNoiseLite;
36+
import com.jme3.math.FastNoiseLite.NoiseType;
37+
38+
/**
39+
*
40+
* @author yaRnMcDonuts
41+
*/
42+
public class NoiseVectorEffect extends AbstractVectorEffect {
43+
44+
private FastNoiseLite noiseGenerator;
45+
46+
private VectorGroup noiseMagnitudes;
47+
private VectorGroup originalVectorValues;
48+
49+
private final Vector4f tempNoiseVariationVec = new Vector4f();
50+
private final Vector4f tempOriginalVec = new Vector4f();
51+
52+
public float speed = 1;
53+
private float timeAccrued = 0;
54+
55+
public NoiseVectorEffect(VectorGroup vectorObject, VectorGroup noiseMagnitude) {
56+
this(vectorObject, noiseMagnitude, NoiseType.OpenSimplex2, 0.5f);
57+
}
58+
59+
public NoiseVectorEffect(VectorGroup vectorObject, VectorGroup noiseMagnitude, NoiseType noiseType,
60+
float frequency) {
61+
super(vectorObject);
62+
63+
this.noiseMagnitudes = noiseMagnitude;
64+
noiseGenerator = new FastNoiseLite();
65+
noiseGenerator.SetFrequency(frequency);
66+
noiseGenerator.SetNoiseType(noiseType);
67+
68+
}
69+
70+
@Override
71+
public void update(float tpf) {
72+
super.update(tpf);
73+
74+
if(originalVectorValues == null){
75+
originalVectorValues = vectorsToModify.clone();
76+
}
77+
78+
timeAccrued += tpf;
79+
float noiseReturnVal = noiseGenerator.GetNoise(timeAccrued * speed, 12.671f + timeAccrued * speed * 0.92173f, 19.54f + timeAccrued * speed * 0.68913f);
80+
81+
for(int v = 0; v < vectorsToModify.getSize(); v++){
82+
int magnitudeIndex = Math.min(v, noiseMagnitudes.getSize() - 1); //allows multiple vectors to share the same magnitude if desired
83+
noiseMagnitudes.getAsVector4(magnitudeIndex, tempNoiseVariationVec);
84+
85+
tempNoiseVariationVec.multLocal(noiseReturnVal);
86+
originalVectorValues.getAsVector4(v, tempOriginalVec);
87+
88+
vectorsToModify.updateVectorObject(tempOriginalVec.addLocal(tempNoiseVariationVec), v);
89+
}
90+
}
91+
92+
public FastNoiseLite getNoiseGenerator() {
93+
return noiseGenerator;
94+
}
95+
96+
public void setSpeed(float speed) {
97+
this.speed = speed;
98+
}
99+
100+
@Override
101+
public void reset() {
102+
super.reset();
103+
timeAccrued = 0.0f;
104+
originalVectorValues = null;
105+
}
106+
}

0 commit comments

Comments
 (0)