commit
54ef6ec280
@ -0,0 +1,99 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.util.SafeArrayList; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 20/12/2017. |
||||||
|
*/ |
||||||
|
public class AnimClip implements JmeCloneable, Savable { |
||||||
|
|
||||||
|
private String name; |
||||||
|
private double length; |
||||||
|
|
||||||
|
private AnimTrack[] tracks; |
||||||
|
|
||||||
|
public AnimClip() { |
||||||
|
} |
||||||
|
|
||||||
|
public AnimClip(String name) { |
||||||
|
this.name = name; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTracks(AnimTrack[] tracks) { |
||||||
|
this.tracks = tracks; |
||||||
|
for (AnimTrack track : tracks) { |
||||||
|
if (track.getLength() > length) { |
||||||
|
length = track.getLength(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public String getName() { |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public AnimTrack[] getTracks() { |
||||||
|
return tracks; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
return super.clone(); |
||||||
|
} catch (CloneNotSupportedException e) { |
||||||
|
throw new RuntimeException("Error cloning", e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
AnimTrack[] newTracks = new AnimTrack[tracks.length]; |
||||||
|
for (int i = 0; i < tracks.length; i++) { |
||||||
|
newTracks[i] = (cloner.clone(tracks[i])); |
||||||
|
} |
||||||
|
this.tracks = newTracks; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "Clip " + name + ", " + length + 's'; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(name, "name", null); |
||||||
|
oc.write(tracks, "tracks", null); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
name = ic.readString("name", null); |
||||||
|
Savable[] arr = ic.readSavableArray("tracks", null); |
||||||
|
if (arr != null) { |
||||||
|
tracks = new AnimTrack[arr.length]; |
||||||
|
for (int i = 0; i < arr.length; i++) { |
||||||
|
AnimTrack t = (AnimTrack) arr[i]; |
||||||
|
tracks[i] = t; |
||||||
|
if (t.getLength() > length) { |
||||||
|
length = t.getLength(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,250 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
import com.jme3.anim.tween.Tweens; |
||||||
|
import com.jme3.anim.tween.action.*; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.renderer.RenderManager; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.scene.control.AbstractControl; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 20/12/2017. |
||||||
|
*/ |
||||||
|
public class AnimComposer extends AbstractControl { |
||||||
|
|
||||||
|
public static final String DEFAULT_LAYER = "Default"; |
||||||
|
private Map<String, AnimClip> animClipMap = new HashMap<>(); |
||||||
|
|
||||||
|
private Map<String, Action> actions = new HashMap<>(); |
||||||
|
private float globalSpeed = 1f; |
||||||
|
private Map<String, Layer> layers = new LinkedHashMap<>(); |
||||||
|
|
||||||
|
public AnimComposer() { |
||||||
|
layers.put(DEFAULT_LAYER, new Layer()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve an animation from the list of animations. |
||||||
|
* |
||||||
|
* @param name The name of the animation to retrieve. |
||||||
|
* @return The animation corresponding to the given name, or null, if no |
||||||
|
* such named animation exists. |
||||||
|
*/ |
||||||
|
public AnimClip getAnimClip(String name) { |
||||||
|
return animClipMap.get(name); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Adds an animation to be available for playing to this |
||||||
|
* <code>AnimControl</code>. |
||||||
|
* |
||||||
|
* @param anim The animation to add. |
||||||
|
*/ |
||||||
|
public void addAnimClip(AnimClip anim) { |
||||||
|
animClipMap.put(anim.getName(), anim); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Remove an animation so that it is no longer available for playing. |
||||||
|
* |
||||||
|
* @param anim The animation to remove. |
||||||
|
*/ |
||||||
|
public void removeAnimClip(AnimClip anim) { |
||||||
|
if (!animClipMap.containsKey(anim.getName())) { |
||||||
|
throw new IllegalArgumentException("Given animation does not exist " |
||||||
|
+ "in this AnimControl"); |
||||||
|
} |
||||||
|
|
||||||
|
animClipMap.remove(anim.getName()); |
||||||
|
} |
||||||
|
|
||||||
|
public Action setCurrentAction(String name) { |
||||||
|
return setCurrentAction(name, DEFAULT_LAYER); |
||||||
|
} |
||||||
|
|
||||||
|
public Action setCurrentAction(String actionName, String layerName) { |
||||||
|
Layer l = layers.get(layerName); |
||||||
|
if (l == null) { |
||||||
|
throw new IllegalArgumentException("Unknown layer " + layerName); |
||||||
|
} |
||||||
|
Action currentAction = action(actionName); |
||||||
|
l.time = 0; |
||||||
|
l.currentAction = currentAction; |
||||||
|
return currentAction; |
||||||
|
} |
||||||
|
|
||||||
|
public Action action(String name) { |
||||||
|
Action action = actions.get(name); |
||||||
|
if (action == null) { |
||||||
|
action = makeAction(name); |
||||||
|
actions.put(name, action); |
||||||
|
} |
||||||
|
return action; |
||||||
|
} |
||||||
|
|
||||||
|
public Action makeAction(String name) { |
||||||
|
Action action; |
||||||
|
AnimClip clip = animClipMap.get(name); |
||||||
|
if (clip == null) { |
||||||
|
throw new IllegalArgumentException("Cannot find clip named " + name); |
||||||
|
} |
||||||
|
action = new ClipAction(clip); |
||||||
|
return action; |
||||||
|
} |
||||||
|
|
||||||
|
public void makeLayer(String name, AnimationMask mask){ |
||||||
|
Layer l = new Layer(); |
||||||
|
l.mask = mask; |
||||||
|
layers.put(name, l); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public BaseAction actionSequence(String name, Tween... tweens) { |
||||||
|
BaseAction action = new BaseAction(Tweens.sequence(tweens)); |
||||||
|
actions.put(name, action); |
||||||
|
return action; |
||||||
|
} |
||||||
|
|
||||||
|
public BlendAction actionBlended(String name, BlendSpace blendSpace, String... clips) { |
||||||
|
BlendableAction[] acts = new BlendableAction[clips.length]; |
||||||
|
for (int i = 0; i < acts.length; i++) { |
||||||
|
BlendableAction ba = (BlendableAction) makeAction(clips[i]); |
||||||
|
acts[i] = ba; |
||||||
|
} |
||||||
|
BlendAction action = new BlendAction(blendSpace, acts); |
||||||
|
actions.put(name, action); |
||||||
|
return action; |
||||||
|
} |
||||||
|
|
||||||
|
public void reset() { |
||||||
|
for (Layer layer : layers.values()) { |
||||||
|
layer.currentAction = null; |
||||||
|
layer.time = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public Collection<AnimClip> getAnimClips() { |
||||||
|
return Collections.unmodifiableCollection(animClipMap.values()); |
||||||
|
} |
||||||
|
|
||||||
|
public Collection<String> getAnimClipsNames() { |
||||||
|
return Collections.unmodifiableCollection(animClipMap.keySet()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlUpdate(float tpf) { |
||||||
|
for (Layer layer : layers.values()) { |
||||||
|
Action currentAction = layer.currentAction; |
||||||
|
if (currentAction == null) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
layer.advance(tpf); |
||||||
|
|
||||||
|
currentAction.setMask(layer.mask); |
||||||
|
boolean running = currentAction.interpolate(layer.time); |
||||||
|
currentAction.setMask(null); |
||||||
|
|
||||||
|
if (!running) { |
||||||
|
layer.time = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public float getGlobalSpeed() { |
||||||
|
return globalSpeed; |
||||||
|
} |
||||||
|
|
||||||
|
public void setGlobalSpeed(float globalSpeed) { |
||||||
|
this.globalSpeed = globalSpeed; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
AnimComposer clone = (AnimComposer) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
super.cloneFields(cloner, original); |
||||||
|
Map<String, AnimClip> clips = new HashMap<>(); |
||||||
|
for (String key : animClipMap.keySet()) { |
||||||
|
clips.put(key, cloner.clone(animClipMap.get(key))); |
||||||
|
} |
||||||
|
Map<String, Action> act = new HashMap<>(); |
||||||
|
for (String key : actions.keySet()) { |
||||||
|
act.put(key, cloner.clone(actions.get(key))); |
||||||
|
} |
||||||
|
actions = act; |
||||||
|
animClipMap = clips; |
||||||
|
|
||||||
|
Map<String, Layer> newLayers = new LinkedHashMap<>(); |
||||||
|
for (String key : layers.keySet()) { |
||||||
|
newLayers.put(key, cloner.clone(layers.get(key))); |
||||||
|
} |
||||||
|
|
||||||
|
layers = newLayers; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
super.read(im); |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
super.write(ex); |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.writeStringSavableMap(animClipMap, "animClipMap", new HashMap<String, AnimClip>()); |
||||||
|
} |
||||||
|
|
||||||
|
private class Layer implements JmeCloneable { |
||||||
|
private Action currentAction; |
||||||
|
private AnimationMask mask; |
||||||
|
private float weight; |
||||||
|
private double time; |
||||||
|
|
||||||
|
public void advance(float tpf) { |
||||||
|
time += tpf * currentAction.getSpeed() * globalSpeed; |
||||||
|
// make sure negative time is in [0, length] range
|
||||||
|
if (time < 0) { |
||||||
|
double length = currentAction.getLength(); |
||||||
|
time = (time % length + length) % length; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
Layer clone = (Layer) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
currentAction = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.export.Savable; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
public interface AnimTrack<T> extends Savable, JmeCloneable { |
||||||
|
|
||||||
|
public void getDataAtTime(double time, T store); |
||||||
|
public double getLength(); |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon |
||||||
|
* An AnimationMask is defining a subset of elements on which an animation will be applied. |
||||||
|
* Most used implementation is the ArmatureMask that defines a subset of joints in an Armature. |
||||||
|
*/ |
||||||
|
public interface AnimationMask { |
||||||
|
|
||||||
|
boolean contains(Object target); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,300 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.util.JointModelTransform; |
||||||
|
import com.jme3.asset.AssetLoadException; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 15/12/2017. |
||||||
|
*/ |
||||||
|
public class Armature implements JmeCloneable, Savable { |
||||||
|
|
||||||
|
private Joint[] rootJoints; |
||||||
|
private Joint[] jointList; |
||||||
|
|
||||||
|
/** |
||||||
|
* Contains the skinning matrices, multiplying it by a vertex effected by a bone |
||||||
|
* will cause it to go to the animated position. |
||||||
|
*/ |
||||||
|
private transient Matrix4f[] skinningMatrixes; |
||||||
|
private Class<? extends JointModelTransform> modelTransformClass = SeparateJointModelTransform.class; |
||||||
|
|
||||||
|
/** |
||||||
|
* Serialization only |
||||||
|
*/ |
||||||
|
public Armature() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates an armature from a joint list. |
||||||
|
* The root joints are found automatically. |
||||||
|
* <p> |
||||||
|
* Note that using this constructor will cause the joints in the list |
||||||
|
* to have their bind pose recomputed based on their local transforms. |
||||||
|
* |
||||||
|
* @param jointList The list of joints to manage by this Armature |
||||||
|
*/ |
||||||
|
public Armature(Joint[] jointList) { |
||||||
|
this.jointList = jointList; |
||||||
|
|
||||||
|
List<Joint> rootJointList = new ArrayList<>(); |
||||||
|
for (int i = jointList.length - 1; i >= 0; i--) { |
||||||
|
Joint joint = jointList[i]; |
||||||
|
joint.setId(i); |
||||||
|
instanciateJointModelTransform(joint); |
||||||
|
if (joint.getParent() == null) { |
||||||
|
rootJointList.add(joint); |
||||||
|
} |
||||||
|
} |
||||||
|
rootJoints = rootJointList.toArray(new Joint[rootJointList.size()]); |
||||||
|
|
||||||
|
createSkinningMatrices(); |
||||||
|
|
||||||
|
for (int i = rootJoints.length - 1; i >= 0; i--) { |
||||||
|
Joint rootJoint = rootJoints[i]; |
||||||
|
rootJoint.update(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update all joints sin this Amature. |
||||||
|
*/ |
||||||
|
public void update() { |
||||||
|
for (Joint rootJoint : rootJoints) { |
||||||
|
rootJoint.update(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void createSkinningMatrices() { |
||||||
|
skinningMatrixes = new Matrix4f[jointList.length]; |
||||||
|
for (int i = 0; i < skinningMatrixes.length; i++) { |
||||||
|
skinningMatrixes[i] = new Matrix4f(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the JointModelTransform implementation |
||||||
|
* Default is {@link MatrixJointModelTransform} |
||||||
|
* |
||||||
|
* @param modelTransformClass |
||||||
|
* @see {@link JointModelTransform},{@link MatrixJointModelTransform},{@link SeparateJointModelTransform}, |
||||||
|
*/ |
||||||
|
public void setModelTransformClass(Class<? extends JointModelTransform> modelTransformClass) { |
||||||
|
this.modelTransformClass = modelTransformClass; |
||||||
|
if (jointList == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
for (Joint joint : jointList) { |
||||||
|
instanciateJointModelTransform(joint); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void instanciateJointModelTransform(Joint joint) { |
||||||
|
try { |
||||||
|
joint.setJointModelTransform(modelTransformClass.newInstance()); |
||||||
|
} catch (InstantiationException | IllegalAccessException e) { |
||||||
|
throw new IllegalArgumentException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the array of all root joints of this Armature |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Joint[] getRoots() { |
||||||
|
return rootJoints; |
||||||
|
} |
||||||
|
|
||||||
|
public List<Joint> getJointList() { |
||||||
|
return Arrays.asList(jointList); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* return a joint for the given index |
||||||
|
* |
||||||
|
* @param index |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Joint getJoint(int index) { |
||||||
|
return jointList[index]; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the joint with the given name |
||||||
|
* |
||||||
|
* @param name |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Joint getJoint(String name) { |
||||||
|
for (int i = 0; i < jointList.length; i++) { |
||||||
|
if (jointList[i].getName().equals(name)) { |
||||||
|
return jointList[i]; |
||||||
|
} |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the bone index of the given bone |
||||||
|
* |
||||||
|
* @param joint |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public int getJointIndex(Joint joint) { |
||||||
|
for (int i = 0; i < jointList.length; i++) { |
||||||
|
if (jointList[i] == joint) { |
||||||
|
return i; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the joint index of the joint that has the given name |
||||||
|
* |
||||||
|
* @param name |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public int getJointIndex(String name) { |
||||||
|
for (int i = 0; i < jointList.length; i++) { |
||||||
|
if (jointList[i].getName().equals(name)) { |
||||||
|
return i; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Saves the current Armature state as its bind pose. |
||||||
|
* Note that the bind pose is supposed to be the one where the armature is aligned with the mesh to deform. |
||||||
|
* Saving this pose will affect how skinning works. |
||||||
|
*/ |
||||||
|
public void saveBindPose() { |
||||||
|
//make sure all bones are updated
|
||||||
|
update(); |
||||||
|
//Save the current pose as bind pose
|
||||||
|
for (Joint joint : jointList) { |
||||||
|
joint.saveBindPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This methods sets this armature in its bind pose (aligned with the mesh to deform) |
||||||
|
* Note that this is only useful for debugging purpose. |
||||||
|
*/ |
||||||
|
public void applyBindPose() { |
||||||
|
for (Joint joint : rootJoints) { |
||||||
|
joint.applyBindPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Saves the current local transform as the initial transform. |
||||||
|
* Initial transform is the one applied to the armature when loaded. |
||||||
|
*/ |
||||||
|
public void saveInitialPose() { |
||||||
|
for (Joint joint : jointList) { |
||||||
|
joint.saveInitialPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Applies the initial pose to this armature |
||||||
|
*/ |
||||||
|
public void applyInitialPose() { |
||||||
|
for (Joint rootJoint : rootJoints) { |
||||||
|
rootJoint.applyInitialPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Compute the skinning matrices for each bone of the armature that would be used to transform vertices of associated meshes |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Matrix4f[] computeSkinningMatrices() { |
||||||
|
for (int i = 0; i < jointList.length; i++) { |
||||||
|
jointList[i].getOffsetTransform(skinningMatrixes[i]); |
||||||
|
} |
||||||
|
return skinningMatrixes; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the number of joints of this armature |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public int getJointCount() { |
||||||
|
return jointList.length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
Armature clone = (Armature) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
this.rootJoints = cloner.clone(rootJoints); |
||||||
|
this.jointList = cloner.clone(jointList); |
||||||
|
this.skinningMatrixes = cloner.clone(skinningMatrixes); |
||||||
|
for (Joint joint : jointList) { |
||||||
|
instanciateJointModelTransform(joint); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule input = im.getCapsule(this); |
||||||
|
|
||||||
|
Savable[] jointRootsAsSavable = input.readSavableArray("rootJoints", null); |
||||||
|
rootJoints = new Joint[jointRootsAsSavable.length]; |
||||||
|
System.arraycopy(jointRootsAsSavable, 0, rootJoints, 0, jointRootsAsSavable.length); |
||||||
|
|
||||||
|
Savable[] jointListAsSavable = input.readSavableArray("jointList", null); |
||||||
|
jointList = new Joint[jointListAsSavable.length]; |
||||||
|
System.arraycopy(jointListAsSavable, 0, jointList, 0, jointListAsSavable.length); |
||||||
|
|
||||||
|
String className = input.readString("modelTransformClass", MatrixJointModelTransform.class.getCanonicalName()); |
||||||
|
try { |
||||||
|
modelTransformClass = (Class<? extends JointModelTransform>) Class.forName(className); |
||||||
|
} catch (ClassNotFoundException e) { |
||||||
|
throw new AssetLoadException("Cannnot find class for name " + className); |
||||||
|
} |
||||||
|
|
||||||
|
int i = 0; |
||||||
|
for (Joint joint : jointList) { |
||||||
|
joint.setId(i++); |
||||||
|
instanciateJointModelTransform(joint); |
||||||
|
} |
||||||
|
createSkinningMatrices(); |
||||||
|
|
||||||
|
for (Joint rootJoint : rootJoints) { |
||||||
|
rootJoint.update(); |
||||||
|
} |
||||||
|
applyInitialPose(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule output = ex.getCapsule(this); |
||||||
|
output.write(rootJoints, "rootJoints", null); |
||||||
|
output.write(jointList, "jointList", null); |
||||||
|
output.write(modelTransformClass.getCanonicalName(), "modelTransformClass", MatrixJointModelTransform.class.getCanonicalName()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,62 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import java.util.BitSet; |
||||||
|
|
||||||
|
public class ArmatureMask implements AnimationMask { |
||||||
|
|
||||||
|
private BitSet affectedJoints = new BitSet(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean contains(Object target) { |
||||||
|
return affectedJoints.get(((Joint) target).getId()); |
||||||
|
} |
||||||
|
|
||||||
|
public static ArmatureMask createMask(Armature armature, String fromJoint) { |
||||||
|
ArmatureMask mask = new ArmatureMask(); |
||||||
|
mask.addFromJoint(armature, fromJoint); |
||||||
|
return mask; |
||||||
|
} |
||||||
|
|
||||||
|
public static ArmatureMask createMask(Armature armature, String... joints) { |
||||||
|
ArmatureMask mask = new ArmatureMask(); |
||||||
|
mask.addBones(armature, joints); |
||||||
|
for (String joint : joints) { |
||||||
|
mask.affectedJoints.set(armature.getJoint(joint).getId()); |
||||||
|
} |
||||||
|
return mask; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Add joints to be influenced by this animation mask. |
||||||
|
*/ |
||||||
|
public void addBones(Armature armature, String... jointNames) { |
||||||
|
for (String jointName : jointNames) { |
||||||
|
Joint joint = findJoint(armature, jointName); |
||||||
|
affectedJoints.set(joint.getId()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private Joint findJoint(Armature armature, String jointName) { |
||||||
|
Joint joint = armature.getJoint(jointName); |
||||||
|
if (joint == null) { |
||||||
|
throw new IllegalArgumentException("Cannot find joint " + jointName); |
||||||
|
} |
||||||
|
return joint; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a joint and all its sub armature joints to be influenced by this animation mask. |
||||||
|
*/ |
||||||
|
public void addFromJoint(Armature armature, String jointName) { |
||||||
|
Joint joint = findJoint(armature, jointName); |
||||||
|
recurseAddJoint(joint); |
||||||
|
} |
||||||
|
|
||||||
|
private void recurseAddJoint(Joint joint) { |
||||||
|
affectedJoints.set(joint.getId()); |
||||||
|
for (Joint j : joint.getChildren()) { |
||||||
|
recurseAddJoint(j); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,342 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.util.HasLocalTransform; |
||||||
|
import com.jme3.anim.util.JointModelTransform; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.material.MatParamOverride; |
||||||
|
import com.jme3.math.*; |
||||||
|
import com.jme3.scene.*; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import com.jme3.util.SafeArrayList; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* A Joint is the basic component of an armature designed to perform skeletal animation |
||||||
|
* Created by Nehon on 15/12/2017. |
||||||
|
*/ |
||||||
|
public class Joint implements Savable, JmeCloneable, HasLocalTransform { |
||||||
|
|
||||||
|
private String name; |
||||||
|
private int id; |
||||||
|
private Joint parent; |
||||||
|
private SafeArrayList<Joint> children = new SafeArrayList<>(Joint.class); |
||||||
|
private Geometry targetGeometry; |
||||||
|
|
||||||
|
/** |
||||||
|
* The attachment node. |
||||||
|
*/ |
||||||
|
private Node attachedNode; |
||||||
|
|
||||||
|
/** |
||||||
|
* The transform of the joint in local space. Relative to its parent. |
||||||
|
* Or relative to the model's origin for the root joint. |
||||||
|
*/ |
||||||
|
private Transform localTransform = new Transform(); |
||||||
|
|
||||||
|
/** |
||||||
|
* The initial transform of the joint in local space. Relative to its parent. |
||||||
|
* Or relative to the model's origin for the root joint. |
||||||
|
* this transform is the transform applied when the armature is loaded. |
||||||
|
*/ |
||||||
|
private Transform initialTransform = new Transform(); |
||||||
|
|
||||||
|
/** |
||||||
|
* The transform of the joint in model space. Relative to the origin of the model. |
||||||
|
* this is either a MatrixJointModelTransform or a SeparateJointModelTransform |
||||||
|
*/ |
||||||
|
private JointModelTransform jointModelTransform; |
||||||
|
|
||||||
|
/** |
||||||
|
* The matrix used to transform affected vertices position into the joint model space. |
||||||
|
* Used for skinning. |
||||||
|
*/ |
||||||
|
private Matrix4f inverseModelBindMatrix = new Matrix4f(); |
||||||
|
|
||||||
|
|
||||||
|
public Joint() { |
||||||
|
} |
||||||
|
|
||||||
|
public Joint(String name) { |
||||||
|
this.name = name; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates world transforms for this bone and it's children. |
||||||
|
*/ |
||||||
|
public final void update() { |
||||||
|
this.updateModelTransforms(); |
||||||
|
|
||||||
|
for (Joint child : children.getArray()) { |
||||||
|
child.update(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates the model transforms for this bone, and, possibly the attach node |
||||||
|
* if not null. |
||||||
|
* <p> |
||||||
|
* The model transform of this bone is computed by combining the parent's |
||||||
|
* model transform with this bones' local transform. |
||||||
|
*/ |
||||||
|
public final void updateModelTransforms() { |
||||||
|
jointModelTransform.updateModelTransform(localTransform, parent); |
||||||
|
updateAttachNode(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the local transform of the attachments node. |
||||||
|
*/ |
||||||
|
private void updateAttachNode() { |
||||||
|
if (attachedNode == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
Node attachParent = attachedNode.getParent(); |
||||||
|
if (attachParent == null || targetGeometry == null |
||||||
|
|| targetGeometry.getParent() == attachParent |
||||||
|
&& targetGeometry.getLocalTransform().isIdentity()) { |
||||||
|
/* |
||||||
|
* The animated meshes are in the same coordinate system as the |
||||||
|
* attachments node: no further transforms are needed. |
||||||
|
*/ |
||||||
|
attachedNode.setLocalTransform(getModelTransform()); |
||||||
|
|
||||||
|
} else { |
||||||
|
Spatial loopSpatial = targetGeometry; |
||||||
|
Transform combined = getModelTransform().clone(); |
||||||
|
/* |
||||||
|
* Climb the scene graph applying local transforms until the |
||||||
|
* attachments node's parent is reached. |
||||||
|
*/ |
||||||
|
while (loopSpatial != attachParent && loopSpatial != null) { |
||||||
|
Transform localTransform = loopSpatial.getLocalTransform(); |
||||||
|
combined.combineWithParent(localTransform); |
||||||
|
loopSpatial = loopSpatial.getParent(); |
||||||
|
} |
||||||
|
attachedNode.setLocalTransform(combined); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Stores the skinning transform in the specified Matrix4f. |
||||||
|
* The skinning transform applies the animation of the bone to a vertex. |
||||||
|
* <p> |
||||||
|
* This assumes that the world transforms for the entire bone hierarchy |
||||||
|
* have already been computed, otherwise this method will return undefined |
||||||
|
* results. |
||||||
|
* |
||||||
|
* @param outTransform |
||||||
|
*/ |
||||||
|
void getOffsetTransform(Matrix4f outTransform) { |
||||||
|
jointModelTransform.getOffsetTransform(outTransform, inverseModelBindMatrix); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the current localTransform as the Bind transform. |
||||||
|
*/ |
||||||
|
protected void saveBindPose() { |
||||||
|
//Note that the whole Armature must be updated before calling this method.
|
||||||
|
getModelTransform().toTransformMatrix(inverseModelBindMatrix); |
||||||
|
inverseModelBindMatrix.invertLocal(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the current local transforms as the initial transform. |
||||||
|
*/ |
||||||
|
protected void saveInitialPose() { |
||||||
|
initialTransform.set(localTransform); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the local transform with the bind transforms |
||||||
|
*/ |
||||||
|
protected void applyBindPose() { |
||||||
|
jointModelTransform.applyBindPose(localTransform, inverseModelBindMatrix, parent); |
||||||
|
updateModelTransforms(); |
||||||
|
|
||||||
|
for (Joint child : children.getArray()) { |
||||||
|
child.applyBindPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the local transform with the initial transform |
||||||
|
*/ |
||||||
|
protected void applyInitialPose() { |
||||||
|
setLocalTransform(initialTransform); |
||||||
|
updateModelTransforms(); |
||||||
|
|
||||||
|
for (Joint child : children.getArray()) { |
||||||
|
child.applyInitialPose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected JointModelTransform getJointModelTransform() { |
||||||
|
return jointModelTransform; |
||||||
|
} |
||||||
|
|
||||||
|
protected void setJointModelTransform(JointModelTransform jointModelTransform) { |
||||||
|
this.jointModelTransform = jointModelTransform; |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getLocalTranslation() { |
||||||
|
return localTransform.getTranslation(); |
||||||
|
} |
||||||
|
|
||||||
|
public Quaternion getLocalRotation() { |
||||||
|
return localTransform.getRotation(); |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getLocalScale() { |
||||||
|
return localTransform.getScale(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setLocalTranslation(Vector3f translation) { |
||||||
|
localTransform.setTranslation(translation); |
||||||
|
} |
||||||
|
|
||||||
|
public void setLocalRotation(Quaternion rotation) { |
||||||
|
localTransform.setRotation(rotation); |
||||||
|
} |
||||||
|
|
||||||
|
public void setLocalScale(Vector3f scale) { |
||||||
|
localTransform.setScale(scale); |
||||||
|
} |
||||||
|
|
||||||
|
public void addChild(Joint child) { |
||||||
|
children.add(child); |
||||||
|
child.parent = this; |
||||||
|
} |
||||||
|
|
||||||
|
public void setName(String name) { |
||||||
|
this.name = name; |
||||||
|
} |
||||||
|
|
||||||
|
public void setLocalTransform(Transform localTransform) { |
||||||
|
this.localTransform.set(localTransform); |
||||||
|
} |
||||||
|
|
||||||
|
public void setInverseModelBindMatrix(Matrix4f inverseModelBindMatrix) { |
||||||
|
this.inverseModelBindMatrix = inverseModelBindMatrix; |
||||||
|
} |
||||||
|
|
||||||
|
public String getName() { |
||||||
|
return name; |
||||||
|
} |
||||||
|
|
||||||
|
public Joint getParent() { |
||||||
|
return parent; |
||||||
|
} |
||||||
|
|
||||||
|
public List<Joint> getChildren() { |
||||||
|
return children; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Access the attachments node of this joint. If this joint doesn't already |
||||||
|
* have an attachments node, create one. Models and effects attached to the |
||||||
|
* attachments node will follow this bone's motions. |
||||||
|
* |
||||||
|
* @param jointIndex this bone's index in its armature (≥0) |
||||||
|
* @param targets a list of geometries animated by this bone's skeleton (not |
||||||
|
* null, unaffected) |
||||||
|
*/ |
||||||
|
Node getAttachmentsNode(int jointIndex, SafeArrayList<Geometry> targets) { |
||||||
|
targetGeometry = null; |
||||||
|
/* |
||||||
|
* Search for a geometry animated by this particular bone. |
||||||
|
*/ |
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
if (mesh != null && mesh.isAnimatedByJoint(jointIndex)) { |
||||||
|
targetGeometry = geometry; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (attachedNode == null) { |
||||||
|
attachedNode = new Node(name + "_attachnode"); |
||||||
|
attachedNode.setUserData("AttachedBone", this); |
||||||
|
//We don't want the node to have a numBone set by a parent node so we force it to null
|
||||||
|
attachedNode.addMatParamOverride(new MatParamOverride(VarType.Int, "NumberOfBones", null)); |
||||||
|
} |
||||||
|
|
||||||
|
return attachedNode; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public Transform getLocalTransform() { |
||||||
|
return localTransform; |
||||||
|
} |
||||||
|
|
||||||
|
public Transform getModelTransform() { |
||||||
|
return jointModelTransform.getModelTransform(); |
||||||
|
} |
||||||
|
|
||||||
|
public Matrix4f getInverseModelBindMatrix() { |
||||||
|
return inverseModelBindMatrix; |
||||||
|
} |
||||||
|
|
||||||
|
public int getId() { |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
public void setId(int id) { |
||||||
|
this.id = id; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
Joint clone = (Joint) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
this.children = cloner.clone(children); |
||||||
|
this.parent = cloner.clone(parent); |
||||||
|
this.attachedNode = cloner.clone(attachedNode); |
||||||
|
this.targetGeometry = cloner.clone(targetGeometry); |
||||||
|
this.localTransform = cloner.clone(localTransform); |
||||||
|
this.inverseModelBindMatrix = cloner.clone(inverseModelBindMatrix); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule input = im.getCapsule(this); |
||||||
|
|
||||||
|
name = input.readString("name", null); |
||||||
|
attachedNode = (Node) input.readSavable("attachedNode", null); |
||||||
|
targetGeometry = (Geometry) input.readSavable("targetGeometry", null); |
||||||
|
initialTransform = (Transform) input.readSavable("initialTransform", new Transform()); |
||||||
|
inverseModelBindMatrix = (Matrix4f) input.readSavable("inverseModelBindMatrix", inverseModelBindMatrix); |
||||||
|
|
||||||
|
ArrayList<Joint> childList = input.readSavableArrayList("children", null); |
||||||
|
for (int i = childList.size() - 1; i >= 0; i--) { |
||||||
|
this.addChild(childList.get(i)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule output = ex.getCapsule(this); |
||||||
|
|
||||||
|
output.write(name, "name", null); |
||||||
|
output.write(attachedNode, "attachedNode", null); |
||||||
|
output.write(targetGeometry, "targetGeometry", null); |
||||||
|
output.write(initialTransform, "initialTransform", new Transform()); |
||||||
|
output.write(inverseModelBindMatrix, "inverseModelBindMatrix", new Matrix4f()); |
||||||
|
output.writeSavableArrayList(new ArrayList(children), "children", null); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.util.JointModelTransform; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
/** |
||||||
|
* This JointModelTransform implementation accumulate joints transforms in a Matrix4f to properly |
||||||
|
* support non uniform scaling in an armature hierarchy |
||||||
|
*/ |
||||||
|
public class MatrixJointModelTransform implements JointModelTransform { |
||||||
|
|
||||||
|
private Matrix4f modelTransformMatrix = new Matrix4f(); |
||||||
|
private Transform modelTransform = new Transform(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void updateModelTransform(Transform localTransform, Joint parent) { |
||||||
|
localTransform.toTransformMatrix(modelTransformMatrix); |
||||||
|
if (parent != null) { |
||||||
|
((MatrixJointModelTransform) parent.getJointModelTransform()).getModelTransformMatrix().mult(modelTransformMatrix, modelTransformMatrix); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void getOffsetTransform(Matrix4f outTransform, Matrix4f inverseModelBindMatrix) { |
||||||
|
modelTransformMatrix.mult(inverseModelBindMatrix, outTransform); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void applyBindPose(Transform localTransform, Matrix4f inverseModelBindMatrix, Joint parent) { |
||||||
|
modelTransformMatrix.set(inverseModelBindMatrix).invertLocal(); // model transform = model bind
|
||||||
|
if (parent != null) { |
||||||
|
((MatrixJointModelTransform) parent.getJointModelTransform()).getModelTransformMatrix().invert().mult(modelTransformMatrix, modelTransformMatrix); |
||||||
|
} |
||||||
|
localTransform.fromTransformMatrix(modelTransformMatrix); |
||||||
|
} |
||||||
|
|
||||||
|
public Matrix4f getModelTransformMatrix() { |
||||||
|
return modelTransformMatrix; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Transform getModelTransform() { |
||||||
|
modelTransform.fromTransformMatrix(modelTransformMatrix); |
||||||
|
return modelTransform; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,353 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.export.Savable; |
||||||
|
import com.jme3.material.*; |
||||||
|
import com.jme3.renderer.*; |
||||||
|
import com.jme3.scene.*; |
||||||
|
import com.jme3.scene.control.AbstractControl; |
||||||
|
import com.jme3.scene.mesh.MorphTarget; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import com.jme3.util.BufferUtils; |
||||||
|
import com.jme3.util.SafeArrayList; |
||||||
|
|
||||||
|
import java.nio.FloatBuffer; |
||||||
|
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* A control that handle morph animation for Position, Normal and Tangent buffers. |
||||||
|
* All stock shaders only support morphing these 3 buffers, but note that MorphTargets can have any type of buffers. |
||||||
|
* If you want to use other types of buffers you will need a custom MorphControl and a custom shader. |
||||||
|
* |
||||||
|
* @author Rémy Bouquet |
||||||
|
*/ |
||||||
|
public class MorphControl extends AbstractControl implements Savable { |
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(MorphControl.class.getName()); |
||||||
|
|
||||||
|
private static final int MAX_MORPH_BUFFERS = 14; |
||||||
|
private final static float MIN_WEIGHT = 0.005f; |
||||||
|
|
||||||
|
private SafeArrayList<Geometry> targets = new SafeArrayList<>(Geometry.class); |
||||||
|
private TargetLocator targetLocator = new TargetLocator(); |
||||||
|
|
||||||
|
private boolean approximateTangents = true; |
||||||
|
private MatParamOverride nullNumberOfBones = new MatParamOverride(VarType.Int, "NumberOfBones", null); |
||||||
|
|
||||||
|
private float[] tmpPosArray; |
||||||
|
private float[] tmpNormArray; |
||||||
|
private float[] tmpTanArray; |
||||||
|
|
||||||
|
private static final VertexBuffer.Type bufferTypes[] = VertexBuffer.Type.values(); |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlUpdate(float tpf) { |
||||||
|
if (!enabled) { |
||||||
|
return; |
||||||
|
} |
||||||
|
// gathering geometries in the sub graph.
|
||||||
|
// This must be done in the update phase as the gathering might add a matparam override
|
||||||
|
targets.clear(); |
||||||
|
this.spatial.depthFirstTraversal(targetLocator); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) { |
||||||
|
if (!enabled) { |
||||||
|
return; |
||||||
|
} |
||||||
|
for (Geometry geom : targets) { |
||||||
|
Mesh mesh = geom.getMesh(); |
||||||
|
if (!geom.isDirtyMorph()) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
Material m = geom.getMaterial(); |
||||||
|
float weights[] = geom.getMorphState(); |
||||||
|
MorphTarget morphTargets[] = mesh.getMorphTargets(); |
||||||
|
float matWeights[]; |
||||||
|
//Number of buffer to handle for each morph target
|
||||||
|
int targetNumBuffers = getTargetNumBuffers(morphTargets[0]); |
||||||
|
|
||||||
|
int maxGPUTargets = getMaxGPUTargets(rm, geom, m, targetNumBuffers); |
||||||
|
|
||||||
|
MatParam param2 = m.getParam("MorphWeights"); |
||||||
|
matWeights = (float[]) param2.getValue(); |
||||||
|
|
||||||
|
int nbGPUTargets = 0; |
||||||
|
int lastGpuTargetIndex = 0; |
||||||
|
int boundBufferIdx = 0; |
||||||
|
float cpuWeightSum = 0; |
||||||
|
// binding the morphTargets buffer to the mesh morph buffers
|
||||||
|
for (int i = 0; i < morphTargets.length; i++) { |
||||||
|
// discard weights below the threshold
|
||||||
|
if (weights[i] < MIN_WEIGHT) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
if (nbGPUTargets >= maxGPUTargets) { |
||||||
|
// we already bound all the available gpu slots we need to merge the remaining morph targets.
|
||||||
|
cpuWeightSum += weights[i]; |
||||||
|
continue; |
||||||
|
} |
||||||
|
lastGpuTargetIndex = i; |
||||||
|
// binding the morph target's buffers to the mesh morph buffers.
|
||||||
|
MorphTarget t = morphTargets[i]; |
||||||
|
boundBufferIdx = bindMorphtargetBuffer(mesh, targetNumBuffers, boundBufferIdx, t); |
||||||
|
// setting the weight in the mat param array
|
||||||
|
matWeights[nbGPUTargets] = weights[i]; |
||||||
|
nbGPUTargets++; |
||||||
|
} |
||||||
|
|
||||||
|
if (nbGPUTargets < matWeights.length) { |
||||||
|
// if we have less simultaneous GPU targets than the length of the weight array, the array is padded with 0
|
||||||
|
for (int i = nbGPUTargets; i < matWeights.length; i++) { |
||||||
|
matWeights[i] = 0; |
||||||
|
} |
||||||
|
} else if (cpuWeightSum > 0) { |
||||||
|
// we have more simultaneous morph targets than available gpu slots,
|
||||||
|
// we merge the additional morph targets and bind them to the last gpu slot
|
||||||
|
MorphTarget mt = geom.getFallbackMorphTarget(); |
||||||
|
if (mt == null) { |
||||||
|
mt = initCpuMorphTarget(geom); |
||||||
|
geom.setFallbackMorphTarget(mt); |
||||||
|
} |
||||||
|
// adding the last Gpu target weight
|
||||||
|
cpuWeightSum += matWeights[nbGPUTargets - 1]; |
||||||
|
ensureTmpArraysCapacity(geom.getVertexCount() * 3, targetNumBuffers); |
||||||
|
|
||||||
|
// merging all remaining targets in tmp arrays
|
||||||
|
for (int i = lastGpuTargetIndex; i < morphTargets.length; i++) { |
||||||
|
if (weights[i] < MIN_WEIGHT) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
float weight = weights[i] / cpuWeightSum; |
||||||
|
MorphTarget t = geom.getMesh().getMorphTargets()[i]; |
||||||
|
mergeMorphTargets(targetNumBuffers, weight, t, i == lastGpuTargetIndex); |
||||||
|
} |
||||||
|
|
||||||
|
// writing the tmp arrays to the float buffer
|
||||||
|
writeCpuBuffer(targetNumBuffers, mt); |
||||||
|
|
||||||
|
// binding the merged morph target
|
||||||
|
bindMorphtargetBuffer(mesh, targetNumBuffers, (nbGPUTargets - 1) * targetNumBuffers, mt); |
||||||
|
|
||||||
|
// setting the eight of the merged targets
|
||||||
|
matWeights[nbGPUTargets - 1] = cpuWeightSum; |
||||||
|
} |
||||||
|
geom.setDirtyMorph(false); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private int getMaxGPUTargets(RenderManager rm, Geometry geom, Material mat, int targetNumBuffers) { |
||||||
|
if (geom.getNbSimultaneousGPUMorph() > -1) { |
||||||
|
return geom.getNbSimultaneousGPUMorph(); |
||||||
|
} |
||||||
|
|
||||||
|
// Evaluate the number of CPU slots remaining for morph buffers.
|
||||||
|
int nbMaxBuffers = getRemainingBuffers(geom.getMesh(), rm.getRenderer()); |
||||||
|
|
||||||
|
int realNumTargetsBuffers = geom.getMesh().getMorphTargets().length * targetNumBuffers; |
||||||
|
|
||||||
|
// compute the max number of targets to send to the GPU
|
||||||
|
int maxGPUTargets = Math.min(realNumTargetsBuffers, Math.min(nbMaxBuffers, MAX_MORPH_BUFFERS)) / targetNumBuffers; |
||||||
|
|
||||||
|
MatParam param = mat.getParam("MorphWeights"); |
||||||
|
if (param == null) { |
||||||
|
// init the mat param if it doesn't exists.
|
||||||
|
float[] wts = new float[maxGPUTargets]; |
||||||
|
mat.setParam("MorphWeights", VarType.FloatArray, wts); |
||||||
|
} |
||||||
|
|
||||||
|
mat.setInt("NumberOfTargetsBuffers", targetNumBuffers); |
||||||
|
|
||||||
|
// test compile the shader to find the accurate number of remaining attributes slots
|
||||||
|
boolean compilationOk = false; |
||||||
|
// Note that if ever the shader has an unrelated issue we want to break at some point, hence the maxGPUTargets > 0
|
||||||
|
while (!compilationOk && maxGPUTargets > 0) { |
||||||
|
// setting the maximum number as the real number may change every frame and trigger a shader recompilation since it's bound to a define.
|
||||||
|
mat.setInt("NumberOfMorphTargets", maxGPUTargets); |
||||||
|
try { |
||||||
|
// preload the spatial. this will trigger a shader compilation that will fail if the number of attributes is over the limit.
|
||||||
|
rm.preloadScene(spatial); |
||||||
|
compilationOk = true; |
||||||
|
} catch (RendererException e) { |
||||||
|
logger.log(Level.FINE, geom.getName() + ": failed at " + maxGPUTargets); |
||||||
|
// the compilation failed let's decrement the number of targets an try again.
|
||||||
|
maxGPUTargets--; |
||||||
|
} |
||||||
|
} |
||||||
|
logger.log(Level.FINE, geom.getName() + ": " + maxGPUTargets); |
||||||
|
// set the number of GPU morph on the geom to not have to recompute it next frame.
|
||||||
|
geom.setNbSimultaneousGPUMorph(maxGPUTargets); |
||||||
|
return maxGPUTargets; |
||||||
|
} |
||||||
|
|
||||||
|
private int bindMorphtargetBuffer(Mesh mesh, int targetNumBuffers, int boundBufferIdx, MorphTarget t) { |
||||||
|
int start = VertexBuffer.Type.MorphTarget0.ordinal(); |
||||||
|
if (targetNumBuffers >= 1) { |
||||||
|
activateBuffer(mesh, boundBufferIdx, start, t.getBuffer(VertexBuffer.Type.Position)); |
||||||
|
boundBufferIdx++; |
||||||
|
} |
||||||
|
if (targetNumBuffers >= 2) { |
||||||
|
activateBuffer(mesh, boundBufferIdx, start, t.getBuffer(VertexBuffer.Type.Normal)); |
||||||
|
boundBufferIdx++; |
||||||
|
} |
||||||
|
if (!approximateTangents && targetNumBuffers == 3) { |
||||||
|
activateBuffer(mesh, boundBufferIdx, start, t.getBuffer(VertexBuffer.Type.Tangent)); |
||||||
|
boundBufferIdx++; |
||||||
|
} |
||||||
|
return boundBufferIdx; |
||||||
|
} |
||||||
|
|
||||||
|
private void writeCpuBuffer(int targetNumBuffers, MorphTarget mt) { |
||||||
|
if (targetNumBuffers >= 1) { |
||||||
|
FloatBuffer dest = mt.getBuffer(VertexBuffer.Type.Position); |
||||||
|
dest.rewind(); |
||||||
|
dest.put(tmpPosArray, 0, dest.capacity()); |
||||||
|
} |
||||||
|
if (targetNumBuffers >= 2) { |
||||||
|
FloatBuffer dest = mt.getBuffer(VertexBuffer.Type.Normal); |
||||||
|
dest.rewind(); |
||||||
|
dest.put(tmpNormArray, 0, dest.capacity()); |
||||||
|
} |
||||||
|
if (!approximateTangents && targetNumBuffers == 3) { |
||||||
|
FloatBuffer dest = mt.getBuffer(VertexBuffer.Type.Tangent); |
||||||
|
dest.rewind(); |
||||||
|
dest.put(tmpTanArray, 0, dest.capacity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void mergeMorphTargets(int targetNumBuffers, float weight, MorphTarget t, boolean init) { |
||||||
|
if (targetNumBuffers >= 1) { |
||||||
|
mergeTargetBuffer(tmpPosArray, weight, t.getBuffer(VertexBuffer.Type.Position), init); |
||||||
|
} |
||||||
|
if (targetNumBuffers >= 2) { |
||||||
|
mergeTargetBuffer(tmpNormArray, weight, t.getBuffer(VertexBuffer.Type.Normal), init); |
||||||
|
} |
||||||
|
if (!approximateTangents && targetNumBuffers == 3) { |
||||||
|
mergeTargetBuffer(tmpTanArray, weight, t.getBuffer(VertexBuffer.Type.Tangent), init); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void ensureTmpArraysCapacity(int capacity, int targetNumBuffers) { |
||||||
|
if (targetNumBuffers >= 1) { |
||||||
|
tmpPosArray = ensureCapacity(tmpPosArray, capacity); |
||||||
|
} |
||||||
|
if (targetNumBuffers >= 2) { |
||||||
|
tmpNormArray = ensureCapacity(tmpNormArray, capacity); |
||||||
|
} |
||||||
|
if (!approximateTangents && targetNumBuffers == 3) { |
||||||
|
tmpTanArray = ensureCapacity(tmpTanArray, capacity); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void mergeTargetBuffer(float[] array, float weight, FloatBuffer src, boolean init) { |
||||||
|
src.rewind(); |
||||||
|
for (int j = 0; j < src.capacity(); j++) { |
||||||
|
if (init) { |
||||||
|
array[j] = 0; |
||||||
|
} |
||||||
|
array[j] += weight * src.get(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void activateBuffer(Mesh mesh, int idx, int start, FloatBuffer b) { |
||||||
|
VertexBuffer.Type t = bufferTypes[start + idx]; |
||||||
|
VertexBuffer vb = mesh.getBuffer(t); |
||||||
|
// only set the buffer if it's different
|
||||||
|
if (vb == null || vb.getData() != b) { |
||||||
|
mesh.setBuffer(t, 3, b); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private float[] ensureCapacity(float[] tmpArray, int size) { |
||||||
|
if (tmpArray == null || tmpArray.length < size) { |
||||||
|
return new float[size]; |
||||||
|
} |
||||||
|
return tmpArray; |
||||||
|
} |
||||||
|
|
||||||
|
private MorphTarget initCpuMorphTarget(Geometry geom) { |
||||||
|
MorphTarget res = new MorphTarget(); |
||||||
|
MorphTarget mt = geom.getMesh().getMorphTargets()[0]; |
||||||
|
FloatBuffer b = mt.getBuffer(VertexBuffer.Type.Position); |
||||||
|
if (b != null) { |
||||||
|
res.setBuffer(VertexBuffer.Type.Position, BufferUtils.createFloatBuffer(b.capacity())); |
||||||
|
} |
||||||
|
b = mt.getBuffer(VertexBuffer.Type.Normal); |
||||||
|
if (b != null) { |
||||||
|
res.setBuffer(VertexBuffer.Type.Normal, BufferUtils.createFloatBuffer(b.capacity())); |
||||||
|
} |
||||||
|
if (!approximateTangents) { |
||||||
|
b = mt.getBuffer(VertexBuffer.Type.Tangent); |
||||||
|
if (b != null) { |
||||||
|
res.setBuffer(VertexBuffer.Type.Tangent, BufferUtils.createFloatBuffer(b.capacity())); |
||||||
|
} |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
|
||||||
|
private int getTargetNumBuffers(MorphTarget morphTarget) { |
||||||
|
int num = 0; |
||||||
|
if (morphTarget.getBuffer(VertexBuffer.Type.Position) != null) num++; |
||||||
|
if (morphTarget.getBuffer(VertexBuffer.Type.Normal) != null) num++; |
||||||
|
|
||||||
|
// if tangents are not needed we don't count the tangent buffer
|
||||||
|
if (!approximateTangents && morphTarget.getBuffer(VertexBuffer.Type.Tangent) != null) { |
||||||
|
num++; |
||||||
|
} |
||||||
|
return num; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Computes the number of remaining buffers on this mesh. |
||||||
|
* This is supposed to give a hint on how many attributes will be used in the material and computes the remaining available slots for the morph attributes. |
||||||
|
* However, the shader can declare attributes that are not used and not bound to a real buffer. |
||||||
|
* That's why we attempt to compile the shader later on to avoid any compilation crash. |
||||||
|
* This method is here to avoid too much render test iteration. |
||||||
|
* |
||||||
|
* @param mesh |
||||||
|
* @param renderer |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private int getRemainingBuffers(Mesh mesh, Renderer renderer) { |
||||||
|
int nbUsedBuffers = 0; |
||||||
|
for (VertexBuffer vb : mesh.getBufferList().getArray()) { |
||||||
|
boolean isMorphBuffer = vb.getBufferType().ordinal() >= VertexBuffer.Type.MorphTarget0.ordinal() && vb.getBufferType().ordinal() <= VertexBuffer.Type.MorphTarget9.ordinal(); |
||||||
|
if (vb.getBufferType() == VertexBuffer.Type.Index || isMorphBuffer) continue; |
||||||
|
if (vb.getUsage() != VertexBuffer.Usage.CpuOnly) { |
||||||
|
nbUsedBuffers++; |
||||||
|
} |
||||||
|
} |
||||||
|
return renderer.getLimits().get(Limits.VertexAttributes) - nbUsedBuffers; |
||||||
|
} |
||||||
|
|
||||||
|
public void setApproximateTangents(boolean approximateTangents) { |
||||||
|
this.approximateTangents = approximateTangents; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isApproximateTangents() { |
||||||
|
return approximateTangents; |
||||||
|
} |
||||||
|
|
||||||
|
private class TargetLocator extends SceneGraphVisitorAdapter { |
||||||
|
@Override |
||||||
|
public void visit(Geometry geom) { |
||||||
|
MatParam p = geom.getMaterial().getMaterialDef().getMaterialParam("MorphWeights"); |
||||||
|
if (p == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
Mesh mesh = geom.getMesh(); |
||||||
|
if (mesh != null && mesh.hasMorphTargets()) { |
||||||
|
targets.add(geom); |
||||||
|
// If the mesh is in a subgraph of a node with a SkinningControl it might have hardware skinning activated through mat param override even if it's not skinned.
|
||||||
|
// this code makes sure that if the mesh has no hardware skinning buffers hardware skinning won't be activated.
|
||||||
|
// this is important, because if HW skinning is activated the shader will declare 2 additional useless attributes,
|
||||||
|
// and we desperately need all the attributes we can find for Morph animation.
|
||||||
|
if (mesh.getBuffer(VertexBuffer.Type.HWBoneIndex) == null && !geom.getLocalMatParamOverrides().contains(nullNumberOfBones)) { |
||||||
|
geom.addMatParamOverride(nullNumberOfBones); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,219 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2012 jMonkeyEngine |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* * Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in the |
||||||
|
* documentation and/or other materials provided with the distribution. |
||||||
|
* |
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
|
* may be used to endorse or promote products derived from this software |
||||||
|
* without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.interpolator.FrameInterpolator; |
||||||
|
import com.jme3.animation.*; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Contains a list of weights and times for each keyframe. |
||||||
|
* |
||||||
|
* @author Rémy Bouquet |
||||||
|
*/ |
||||||
|
public class MorphTrack implements AnimTrack<float[]> { |
||||||
|
|
||||||
|
private double length; |
||||||
|
private Geometry target; |
||||||
|
|
||||||
|
/** |
||||||
|
* Weights and times for track. |
||||||
|
*/ |
||||||
|
private float[] weights; |
||||||
|
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT; |
||||||
|
private float[] times; |
||||||
|
private int nbMorphTargets; |
||||||
|
|
||||||
|
/** |
||||||
|
* Serialization-only. Do not use. |
||||||
|
*/ |
||||||
|
public MorphTrack() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a morph track with the given Geometry as a target |
||||||
|
* |
||||||
|
* @param times a float array with the time of each frame |
||||||
|
* @param weights the morphs for each frames |
||||||
|
*/ |
||||||
|
public MorphTrack(Geometry target, float[] times, float[] weights, int nbMorphTargets) { |
||||||
|
this.target = target; |
||||||
|
this.nbMorphTargets = nbMorphTargets; |
||||||
|
this.setKeyframes(times, weights); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* return the array of weights of this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public float[] getWeights() { |
||||||
|
return weights; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the arrays of time for this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public float[] getTimes() { |
||||||
|
return times; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the keyframes times for this Joint track |
||||||
|
* |
||||||
|
* @param times the keyframes times |
||||||
|
*/ |
||||||
|
public void setTimes(float[] times) { |
||||||
|
if (times.length == 0) { |
||||||
|
throw new RuntimeException("TransformTrack with no keyframes!"); |
||||||
|
} |
||||||
|
this.times = times; |
||||||
|
length = times[times.length - 1] - times[0]; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Set the weight for this morph track |
||||||
|
* |
||||||
|
* @param times a float array with the time of each frame |
||||||
|
* @param weights the weights of the morphs for each frame |
||||||
|
|
||||||
|
*/ |
||||||
|
public void setKeyframes(float[] times, float[] weights) { |
||||||
|
setTimes(times); |
||||||
|
if (weights != null) { |
||||||
|
if (times == null) { |
||||||
|
throw new RuntimeException("MorphTrack doesn't have any time for key frames, please call setTimes first"); |
||||||
|
} |
||||||
|
|
||||||
|
this.weights = weights; |
||||||
|
|
||||||
|
assert times != null && times.length == weights.length; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void getDataAtTime(double t, float[] store) { |
||||||
|
float time = (float) t; |
||||||
|
|
||||||
|
int lastFrame = times.length - 1; |
||||||
|
if (time < 0 || lastFrame == 0) { |
||||||
|
if (weights != null) { |
||||||
|
System.arraycopy(weights,0,store,0, nbMorphTargets); |
||||||
|
} |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
int startFrame = 0; |
||||||
|
int endFrame = 1; |
||||||
|
float blend = 0; |
||||||
|
if (time >= times[lastFrame]) { |
||||||
|
startFrame = lastFrame; |
||||||
|
|
||||||
|
time = time - times[startFrame] + times[startFrame - 1]; |
||||||
|
blend = (time - times[startFrame - 1]) |
||||||
|
/ (times[startFrame] - times[startFrame - 1]); |
||||||
|
|
||||||
|
} else { |
||||||
|
// use lastFrame so we never overflow the array
|
||||||
|
int i; |
||||||
|
for (i = 0; i < lastFrame && times[i] < time; i++) { |
||||||
|
startFrame = i; |
||||||
|
endFrame = i + 1; |
||||||
|
} |
||||||
|
blend = (time - times[startFrame]) |
||||||
|
/ (times[endFrame] - times[startFrame]); |
||||||
|
} |
||||||
|
|
||||||
|
interpolator.interpolateWeights(blend, startFrame, weights, nbMorphTargets, store); |
||||||
|
} |
||||||
|
|
||||||
|
public void setFrameInterpolator(FrameInterpolator interpolator) { |
||||||
|
this.interpolator = interpolator; |
||||||
|
} |
||||||
|
|
||||||
|
public Geometry getTarget() { |
||||||
|
return target; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTarget(Geometry target) { |
||||||
|
this.target = target; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(weights, "weights", null); |
||||||
|
oc.write(times, "times", null); |
||||||
|
oc.write(target, "target", null); |
||||||
|
oc.write(nbMorphTargets, "nbMorphTargets", 0); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
weights = ic.readFloatArray("weights", null); |
||||||
|
times = ic.readFloatArray("times", null); |
||||||
|
target = (Geometry) ic.readSavable("target", null); |
||||||
|
nbMorphTargets = ic.readInt("nbMorphTargets", 0); |
||||||
|
setTimes(times); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
MorphTrack clone = (MorphTrack) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
this.target = cloner.clone(target); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.util.JointModelTransform; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
/** |
||||||
|
* This JointModelTransform implementation accumulates model transform in a Transform class
|
||||||
|
* This does NOT support proper non uniform scale in the armature hierarchy. |
||||||
|
* But the effect might be useful in some circumstances. |
||||||
|
* Note that this is how the old animation system was working, so you might want to use this |
||||||
|
* if your model has non uniform scale and was migrated from old j3o model. |
||||||
|
*/ |
||||||
|
public class SeparateJointModelTransform implements JointModelTransform { |
||||||
|
|
||||||
|
private Transform modelTransform = new Transform(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void updateModelTransform(Transform localTransform, Joint parent) { |
||||||
|
modelTransform.set(localTransform); |
||||||
|
if (parent != null) { |
||||||
|
modelTransform.combineWithParent(parent.getModelTransform()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void getOffsetTransform(Matrix4f outTransform, Matrix4f inverseModelBindMatrix) { |
||||||
|
modelTransform.toTransformMatrix(outTransform).mult(inverseModelBindMatrix, outTransform); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void applyBindPose(Transform localTransform, Matrix4f inverseModelBindMatrix, Joint parent) { |
||||||
|
localTransform.fromTransformMatrix(inverseModelBindMatrix.invert()); |
||||||
|
if (parent != null) { |
||||||
|
localTransform.combineWithParent(parent.getModelTransform().invert()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Transform getModelTransform() { |
||||||
|
return modelTransform; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,744 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2017 jMonkeyEngine |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* * Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in the |
||||||
|
* documentation and/or other materials provided with the distribution. |
||||||
|
* |
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
|
* may be used to endorse or promote products derived from this software |
||||||
|
* without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.material.MatParamOverride; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.renderer.*; |
||||||
|
import com.jme3.scene.*; |
||||||
|
import com.jme3.scene.VertexBuffer.Type; |
||||||
|
import com.jme3.scene.control.AbstractControl; |
||||||
|
import com.jme3.scene.mesh.IndexBuffer; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import com.jme3.util.SafeArrayList; |
||||||
|
import com.jme3.util.TempVars; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.nio.Buffer; |
||||||
|
import java.nio.FloatBuffer; |
||||||
|
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* The Skinning control deforms a model according to an armature, It handles the |
||||||
|
* computation of the deformation matrices and performs the transformations on |
||||||
|
* the mesh |
||||||
|
* <p> |
||||||
|
* It can perform software skinning or Hardware skinning |
||||||
|
* |
||||||
|
* @author Rémy Bouquet Based on SkeletonControl by Kirill Vainer |
||||||
|
*/ |
||||||
|
public class SkinningControl extends AbstractControl implements Cloneable, JmeCloneable { |
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(SkinningControl.class.getName()); |
||||||
|
|
||||||
|
/** |
||||||
|
* The armature of the model. |
||||||
|
*/ |
||||||
|
private Armature armature; |
||||||
|
|
||||||
|
/** |
||||||
|
* List of geometries affected by this control. |
||||||
|
*/ |
||||||
|
private SafeArrayList<Geometry> targets = new SafeArrayList<>(Geometry.class); |
||||||
|
|
||||||
|
/** |
||||||
|
* Used to track when a mesh was updated. Meshes are only updated if they |
||||||
|
* are visible in at least one camera. |
||||||
|
*/ |
||||||
|
private boolean wasMeshUpdated = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* User wishes to use hardware skinning if available. |
||||||
|
*/ |
||||||
|
private transient boolean hwSkinningDesired = true; |
||||||
|
|
||||||
|
/** |
||||||
|
* Hardware skinning is currently being used. |
||||||
|
*/ |
||||||
|
private transient boolean hwSkinningEnabled = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* Hardware skinning was tested on this GPU, results |
||||||
|
* are stored in {@link #hwSkinningSupported} variable. |
||||||
|
*/ |
||||||
|
private transient boolean hwSkinningTested = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* If hardware skinning was {@link #hwSkinningTested tested}, then |
||||||
|
* this variable will be set to true if supported, and false if otherwise. |
||||||
|
*/ |
||||||
|
private transient boolean hwSkinningSupported = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* Bone offset matrices, recreated each frame |
||||||
|
*/ |
||||||
|
private transient Matrix4f[] offsetMatrices; |
||||||
|
|
||||||
|
|
||||||
|
private MatParamOverride numberOfJointsParam; |
||||||
|
private MatParamOverride jointMatricesParam; |
||||||
|
|
||||||
|
/** |
||||||
|
* Serialization only. Do not use. |
||||||
|
*/ |
||||||
|
public SkinningControl() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a armature control. The list of targets will be acquired |
||||||
|
* automatically when the control is attached to a node. |
||||||
|
* |
||||||
|
* @param armature the armature |
||||||
|
*/ |
||||||
|
public SkinningControl(Armature armature) { |
||||||
|
if (armature == null) { |
||||||
|
throw new IllegalArgumentException("armature cannot be null"); |
||||||
|
} |
||||||
|
this.armature = armature; |
||||||
|
this.numberOfJointsParam = new MatParamOverride(VarType.Int, "NumberOfBones", null); |
||||||
|
this.jointMatricesParam = new MatParamOverride(VarType.Matrix4Array, "BoneMatrices", null); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private void switchToHardware() { |
||||||
|
numberOfJointsParam.setEnabled(true); |
||||||
|
jointMatricesParam.setEnabled(true); |
||||||
|
|
||||||
|
// Next full 10 bones (e.g. 30 on 24 bones)
|
||||||
|
int numBones = ((armature.getJointCount() / 10) + 1) * 10; |
||||||
|
numberOfJointsParam.setValue(numBones); |
||||||
|
|
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
if (mesh != null && mesh.isAnimated()) { |
||||||
|
mesh.prepareForAnim(false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void switchToSoftware() { |
||||||
|
numberOfJointsParam.setEnabled(false); |
||||||
|
jointMatricesParam.setEnabled(false); |
||||||
|
|
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
if (mesh != null && mesh.isAnimated()) { |
||||||
|
mesh.prepareForAnim(true); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private boolean testHardwareSupported(RenderManager rm) { |
||||||
|
|
||||||
|
//Only 255 bones max supported with hardware skinning
|
||||||
|
if (armature.getJointCount() > 255) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
switchToHardware(); |
||||||
|
|
||||||
|
try { |
||||||
|
rm.preloadScene(spatial); |
||||||
|
return true; |
||||||
|
} catch (RendererException e) { |
||||||
|
logger.log(Level.WARNING, "Could not enable HW skinning due to shader compile error:", e); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Specifies if hardware skinning is preferred. If it is preferred and |
||||||
|
* supported by GPU, it shall be enabled, if its not preferred, or not |
||||||
|
* supported by GPU, then it shall be disabled. |
||||||
|
* |
||||||
|
* @param preferred |
||||||
|
* @see #isHardwareSkinningUsed() |
||||||
|
*/ |
||||||
|
public void setHardwareSkinningPreferred(boolean preferred) { |
||||||
|
hwSkinningDesired = preferred; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return True if hardware skinning is preferable to software skinning. |
||||||
|
* Set to false by default. |
||||||
|
* @see #setHardwareSkinningPreferred(boolean) |
||||||
|
*/ |
||||||
|
public boolean isHardwareSkinningPreferred() { |
||||||
|
return hwSkinningDesired; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return True is hardware skinning is activated and is currently used, false otherwise. |
||||||
|
*/ |
||||||
|
public boolean isHardwareSkinningUsed() { |
||||||
|
return hwSkinningEnabled; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* If specified the geometry has an animated mesh, add its mesh and material |
||||||
|
* to the lists of animation targets. |
||||||
|
*/ |
||||||
|
private void findTargets(Geometry geometry) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
if (mesh != null && mesh.isAnimated()) { |
||||||
|
targets.add(geometry); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void findTargets(Node node) { |
||||||
|
for (Spatial child : node.getChildren()) { |
||||||
|
if (child instanceof Geometry) { |
||||||
|
findTargets((Geometry) child); |
||||||
|
} else if (child instanceof Node) { |
||||||
|
findTargets((Node) child); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setSpatial(Spatial spatial) { |
||||||
|
Spatial oldSpatial = this.spatial; |
||||||
|
super.setSpatial(spatial); |
||||||
|
updateTargetsAndMaterials(spatial); |
||||||
|
|
||||||
|
if (oldSpatial != null) { |
||||||
|
oldSpatial.removeMatParamOverride(numberOfJointsParam); |
||||||
|
oldSpatial.removeMatParamOverride(jointMatricesParam); |
||||||
|
} |
||||||
|
|
||||||
|
if (spatial != null) { |
||||||
|
spatial.removeMatParamOverride(numberOfJointsParam); |
||||||
|
spatial.removeMatParamOverride(jointMatricesParam); |
||||||
|
spatial.addMatParamOverride(numberOfJointsParam); |
||||||
|
spatial.addMatParamOverride(jointMatricesParam); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void controlRenderSoftware() { |
||||||
|
resetToBind(); // reset morph meshes to bind pose
|
||||||
|
|
||||||
|
offsetMatrices = armature.computeSkinningMatrices(); |
||||||
|
|
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
// NOTE: This assumes code higher up has
|
||||||
|
// already ensured this mesh is animated.
|
||||||
|
// Otherwise a crash will happen in skin update.
|
||||||
|
softwareSkinUpdate(mesh, offsetMatrices); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void controlRenderHardware() { |
||||||
|
offsetMatrices = armature.computeSkinningMatrices(); |
||||||
|
jointMatricesParam.setValue(offsetMatrices); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlRender(RenderManager rm, ViewPort vp) { |
||||||
|
if (!wasMeshUpdated) { |
||||||
|
updateTargetsAndMaterials(spatial); |
||||||
|
|
||||||
|
// Prevent illegal cases. These should never happen.
|
||||||
|
assert hwSkinningTested || (!hwSkinningTested && !hwSkinningSupported && !hwSkinningEnabled); |
||||||
|
assert !hwSkinningEnabled || (hwSkinningEnabled && hwSkinningTested && hwSkinningSupported); |
||||||
|
|
||||||
|
if (hwSkinningDesired && !hwSkinningTested) { |
||||||
|
hwSkinningTested = true; |
||||||
|
hwSkinningSupported = testHardwareSupported(rm); |
||||||
|
|
||||||
|
if (hwSkinningSupported) { |
||||||
|
hwSkinningEnabled = true; |
||||||
|
|
||||||
|
Logger.getLogger(SkinningControl.class.getName()).log(Level.INFO, "Hardware skinning engaged for {0}", spatial); |
||||||
|
} else { |
||||||
|
switchToSoftware(); |
||||||
|
} |
||||||
|
} else if (hwSkinningDesired && hwSkinningSupported && !hwSkinningEnabled) { |
||||||
|
switchToHardware(); |
||||||
|
hwSkinningEnabled = true; |
||||||
|
} else if (!hwSkinningDesired && hwSkinningEnabled) { |
||||||
|
switchToSoftware(); |
||||||
|
hwSkinningEnabled = false; |
||||||
|
} |
||||||
|
|
||||||
|
if (hwSkinningEnabled) { |
||||||
|
controlRenderHardware(); |
||||||
|
} else { |
||||||
|
controlRenderSoftware(); |
||||||
|
} |
||||||
|
|
||||||
|
wasMeshUpdated = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void controlUpdate(float tpf) { |
||||||
|
wasMeshUpdated = false; |
||||||
|
armature.update(); |
||||||
|
} |
||||||
|
|
||||||
|
//only do this for software updates
|
||||||
|
void resetToBind() { |
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
if (mesh != null && mesh.isAnimated()) { |
||||||
|
Buffer bwBuff = mesh.getBuffer(Type.BoneWeight).getData(); |
||||||
|
Buffer biBuff = mesh.getBuffer(Type.BoneIndex).getData(); |
||||||
|
if (!biBuff.hasArray() || !bwBuff.hasArray()) { |
||||||
|
mesh.prepareForAnim(true); // prepare for software animation
|
||||||
|
} |
||||||
|
VertexBuffer bindPos = mesh.getBuffer(Type.BindPosePosition); |
||||||
|
VertexBuffer bindNorm = mesh.getBuffer(Type.BindPoseNormal); |
||||||
|
VertexBuffer pos = mesh.getBuffer(Type.Position); |
||||||
|
VertexBuffer norm = mesh.getBuffer(Type.Normal); |
||||||
|
FloatBuffer pb = (FloatBuffer) pos.getData(); |
||||||
|
FloatBuffer nb = (FloatBuffer) norm.getData(); |
||||||
|
FloatBuffer bpb = (FloatBuffer) bindPos.getData(); |
||||||
|
FloatBuffer bnb = (FloatBuffer) bindNorm.getData(); |
||||||
|
pb.clear(); |
||||||
|
nb.clear(); |
||||||
|
bpb.clear(); |
||||||
|
bnb.clear(); |
||||||
|
|
||||||
|
//reseting bind tangents if there is a bind tangent buffer
|
||||||
|
VertexBuffer bindTangents = mesh.getBuffer(Type.BindPoseTangent); |
||||||
|
if (bindTangents != null) { |
||||||
|
VertexBuffer tangents = mesh.getBuffer(Type.Tangent); |
||||||
|
FloatBuffer tb = (FloatBuffer) tangents.getData(); |
||||||
|
FloatBuffer btb = (FloatBuffer) bindTangents.getData(); |
||||||
|
tb.clear(); |
||||||
|
btb.clear(); |
||||||
|
tb.put(btb).clear(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
pb.put(bpb).clear(); |
||||||
|
nb.put(bnb).clear(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
return super.jmeClone(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
super.cloneFields(cloner, original); |
||||||
|
|
||||||
|
this.armature = cloner.clone(armature); |
||||||
|
|
||||||
|
// If the targets were cloned then this will clone them. If the targets
|
||||||
|
// were shared then this will share them.
|
||||||
|
this.targets = cloner.clone(targets); |
||||||
|
|
||||||
|
this.numberOfJointsParam = cloner.clone(numberOfJointsParam); |
||||||
|
this.jointMatricesParam = cloner.clone(jointMatricesParam); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Access the attachments node of the named bone. If the bone doesn't |
||||||
|
* already have an attachments node, create one and attach it to the scene |
||||||
|
* graph. Models and effects attached to the attachments node will follow |
||||||
|
* the bone's motions. |
||||||
|
* |
||||||
|
* @param jointName the name of the joint |
||||||
|
* @return the attachments node of the joint |
||||||
|
*/ |
||||||
|
public Node getAttachmentsNode(String jointName) { |
||||||
|
Joint b = armature.getJoint(jointName); |
||||||
|
if (b == null) { |
||||||
|
throw new IllegalArgumentException("Given bone name does not exist " |
||||||
|
+ "in the armature."); |
||||||
|
} |
||||||
|
|
||||||
|
updateTargetsAndMaterials(spatial); |
||||||
|
int boneIndex = armature.getJointIndex(b); |
||||||
|
Node n = b.getAttachmentsNode(boneIndex, targets); |
||||||
|
/* |
||||||
|
* Select a node to parent the attachments node. |
||||||
|
*/ |
||||||
|
Node parent; |
||||||
|
if (spatial instanceof Node) { |
||||||
|
parent = (Node) spatial; // the usual case
|
||||||
|
} else { |
||||||
|
parent = spatial.getParent(); |
||||||
|
} |
||||||
|
parent.attachChild(n); |
||||||
|
|
||||||
|
return n; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the armature of this control |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Armature getArmature() { |
||||||
|
return armature; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Enumerate the target meshes of this control. |
||||||
|
* |
||||||
|
* @return a new array |
||||||
|
*/ |
||||||
|
public Mesh[] getTargets() { |
||||||
|
Mesh[] result = new Mesh[targets.size()]; |
||||||
|
int i = 0; |
||||||
|
for (Geometry geometry : targets) { |
||||||
|
Mesh mesh = geometry.getMesh(); |
||||||
|
result[i] = mesh; |
||||||
|
i++; |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the mesh according to the given transformation matrices |
||||||
|
* |
||||||
|
* @param mesh then mesh |
||||||
|
* @param offsetMatrices the transformation matrices to apply |
||||||
|
*/ |
||||||
|
private void softwareSkinUpdate(Mesh mesh, Matrix4f[] offsetMatrices) { |
||||||
|
|
||||||
|
VertexBuffer tb = mesh.getBuffer(Type.Tangent); |
||||||
|
if (tb == null) { |
||||||
|
//if there are no tangents use the classic skinning
|
||||||
|
applySkinning(mesh, offsetMatrices); |
||||||
|
} else { |
||||||
|
//if there are tangents use the skinning with tangents
|
||||||
|
applySkinningTangents(mesh, offsetMatrices, tb); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Method to apply skinning transforms to a mesh's buffers |
||||||
|
* |
||||||
|
* @param mesh the mesh |
||||||
|
* @param offsetMatrices the offset matices to apply |
||||||
|
*/ |
||||||
|
private void applySkinning(Mesh mesh, Matrix4f[] offsetMatrices) { |
||||||
|
int maxWeightsPerVert = mesh.getMaxNumWeights(); |
||||||
|
if (maxWeightsPerVert <= 0) { |
||||||
|
throw new IllegalStateException("Max weights per vert is incorrectly set!"); |
||||||
|
} |
||||||
|
int fourMinusMaxWeights = 4 - maxWeightsPerVert; |
||||||
|
|
||||||
|
// NOTE: This code assumes the vertex buffer is in bind pose
|
||||||
|
// resetToBind() has been called this frame
|
||||||
|
VertexBuffer vb = mesh.getBuffer(Type.Position); |
||||||
|
FloatBuffer fvb = (FloatBuffer) vb.getData(); |
||||||
|
fvb.rewind(); |
||||||
|
|
||||||
|
VertexBuffer nb = mesh.getBuffer(Type.Normal); |
||||||
|
FloatBuffer fnb = (FloatBuffer) nb.getData(); |
||||||
|
fnb.rewind(); |
||||||
|
|
||||||
|
// get boneIndexes and weights for mesh
|
||||||
|
IndexBuffer ib = IndexBuffer.wrapIndexBuffer(mesh.getBuffer(Type.BoneIndex).getData()); |
||||||
|
FloatBuffer wb = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData(); |
||||||
|
|
||||||
|
wb.rewind(); |
||||||
|
|
||||||
|
float[] weights = wb.array(); |
||||||
|
int idxWeights = 0; |
||||||
|
|
||||||
|
TempVars vars = TempVars.get(); |
||||||
|
|
||||||
|
float[] posBuf = vars.skinPositions; |
||||||
|
float[] normBuf = vars.skinNormals; |
||||||
|
|
||||||
|
int iterations = (int) FastMath.ceil(fvb.limit() / ((float) posBuf.length)); |
||||||
|
int bufLength = posBuf.length; |
||||||
|
for (int i = iterations - 1; i >= 0; i--) { |
||||||
|
// read next set of positions and normals from native buffer
|
||||||
|
bufLength = Math.min(posBuf.length, fvb.remaining()); |
||||||
|
fvb.get(posBuf, 0, bufLength); |
||||||
|
fnb.get(normBuf, 0, bufLength); |
||||||
|
int verts = bufLength / 3; |
||||||
|
int idxPositions = 0; |
||||||
|
|
||||||
|
// iterate vertices and apply skinning transform for each effecting bone
|
||||||
|
for (int vert = verts - 1; vert >= 0; vert--) { |
||||||
|
// Skip this vertex if the first weight is zero.
|
||||||
|
if (weights[idxWeights] == 0) { |
||||||
|
idxPositions += 3; |
||||||
|
idxWeights += 4; |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
float nmx = normBuf[idxPositions]; |
||||||
|
float vtx = posBuf[idxPositions++]; |
||||||
|
float nmy = normBuf[idxPositions]; |
||||||
|
float vty = posBuf[idxPositions++]; |
||||||
|
float nmz = normBuf[idxPositions]; |
||||||
|
float vtz = posBuf[idxPositions++]; |
||||||
|
|
||||||
|
float rx = 0, ry = 0, rz = 0, rnx = 0, rny = 0, rnz = 0; |
||||||
|
|
||||||
|
for (int w = maxWeightsPerVert - 1; w >= 0; w--) { |
||||||
|
float weight = weights[idxWeights]; |
||||||
|
Matrix4f mat = offsetMatrices[ib.get(idxWeights++)]; |
||||||
|
|
||||||
|
rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight; |
||||||
|
ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight; |
||||||
|
rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight; |
||||||
|
|
||||||
|
rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight; |
||||||
|
rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight; |
||||||
|
rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight; |
||||||
|
} |
||||||
|
|
||||||
|
idxWeights += fourMinusMaxWeights; |
||||||
|
|
||||||
|
idxPositions -= 3; |
||||||
|
normBuf[idxPositions] = rnx; |
||||||
|
posBuf[idxPositions++] = rx; |
||||||
|
normBuf[idxPositions] = rny; |
||||||
|
posBuf[idxPositions++] = ry; |
||||||
|
normBuf[idxPositions] = rnz; |
||||||
|
posBuf[idxPositions++] = rz; |
||||||
|
} |
||||||
|
|
||||||
|
fvb.position(fvb.position() - bufLength); |
||||||
|
fvb.put(posBuf, 0, bufLength); |
||||||
|
fnb.position(fnb.position() - bufLength); |
||||||
|
fnb.put(normBuf, 0, bufLength); |
||||||
|
} |
||||||
|
|
||||||
|
vars.release(); |
||||||
|
|
||||||
|
vb.updateData(fvb); |
||||||
|
nb.updateData(fnb); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Specific method for skinning with tangents to avoid cluttering the |
||||||
|
* classic skinning calculation with null checks that would slow down the |
||||||
|
* process even if tangents don't have to be computed. Also the iteration |
||||||
|
* has additional indexes since tangent has 4 components instead of 3 for |
||||||
|
* pos and norm |
||||||
|
* |
||||||
|
* @param mesh the mesh |
||||||
|
* @param offsetMatrices the offsetMatrices to apply |
||||||
|
* @param tb the tangent vertexBuffer |
||||||
|
*/ |
||||||
|
private void applySkinningTangents(Mesh mesh, Matrix4f[] offsetMatrices, VertexBuffer tb) { |
||||||
|
int maxWeightsPerVert = mesh.getMaxNumWeights(); |
||||||
|
|
||||||
|
if (maxWeightsPerVert <= 0) { |
||||||
|
throw new IllegalStateException("Max weights per vert is incorrectly set!"); |
||||||
|
} |
||||||
|
|
||||||
|
int fourMinusMaxWeights = 4 - maxWeightsPerVert; |
||||||
|
|
||||||
|
// NOTE: This code assumes the vertex buffer is in bind pose
|
||||||
|
// resetToBind() has been called this frame
|
||||||
|
VertexBuffer vb = mesh.getBuffer(Type.Position); |
||||||
|
FloatBuffer fvb = (FloatBuffer) vb.getData(); |
||||||
|
fvb.rewind(); |
||||||
|
|
||||||
|
VertexBuffer nb = mesh.getBuffer(Type.Normal); |
||||||
|
|
||||||
|
FloatBuffer fnb = (FloatBuffer) nb.getData(); |
||||||
|
fnb.rewind(); |
||||||
|
|
||||||
|
|
||||||
|
FloatBuffer ftb = (FloatBuffer) tb.getData(); |
||||||
|
ftb.rewind(); |
||||||
|
|
||||||
|
|
||||||
|
// get boneIndexes and weights for mesh
|
||||||
|
IndexBuffer ib = IndexBuffer.wrapIndexBuffer(mesh.getBuffer(Type.BoneIndex).getData()); |
||||||
|
FloatBuffer wb = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData(); |
||||||
|
|
||||||
|
wb.rewind(); |
||||||
|
|
||||||
|
float[] weights = wb.array(); |
||||||
|
int idxWeights = 0; |
||||||
|
|
||||||
|
TempVars vars = TempVars.get(); |
||||||
|
|
||||||
|
|
||||||
|
float[] posBuf = vars.skinPositions; |
||||||
|
float[] normBuf = vars.skinNormals; |
||||||
|
float[] tanBuf = vars.skinTangents; |
||||||
|
|
||||||
|
int iterations = (int) FastMath.ceil(fvb.limit() / ((float) posBuf.length)); |
||||||
|
int bufLength = 0; |
||||||
|
int tanLength = 0; |
||||||
|
for (int i = iterations - 1; i >= 0; i--) { |
||||||
|
// read next set of positions and normals from native buffer
|
||||||
|
bufLength = Math.min(posBuf.length, fvb.remaining()); |
||||||
|
tanLength = Math.min(tanBuf.length, ftb.remaining()); |
||||||
|
fvb.get(posBuf, 0, bufLength); |
||||||
|
fnb.get(normBuf, 0, bufLength); |
||||||
|
ftb.get(tanBuf, 0, tanLength); |
||||||
|
int verts = bufLength / 3; |
||||||
|
int idxPositions = 0; |
||||||
|
//tangents has their own index because of the 4 components
|
||||||
|
int idxTangents = 0; |
||||||
|
|
||||||
|
// iterate vertices and apply skinning transform for each effecting bone
|
||||||
|
for (int vert = verts - 1; vert >= 0; vert--) { |
||||||
|
// Skip this vertex if the first weight is zero.
|
||||||
|
if (weights[idxWeights] == 0) { |
||||||
|
idxTangents += 4; |
||||||
|
idxPositions += 3; |
||||||
|
idxWeights += 4; |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
float nmx = normBuf[idxPositions]; |
||||||
|
float vtx = posBuf[idxPositions++]; |
||||||
|
float nmy = normBuf[idxPositions]; |
||||||
|
float vty = posBuf[idxPositions++]; |
||||||
|
float nmz = normBuf[idxPositions]; |
||||||
|
float vtz = posBuf[idxPositions++]; |
||||||
|
|
||||||
|
float tnx = tanBuf[idxTangents++]; |
||||||
|
float tny = tanBuf[idxTangents++]; |
||||||
|
float tnz = tanBuf[idxTangents++]; |
||||||
|
|
||||||
|
// skipping the 4th component of the tangent since it doesn't have to be transformed
|
||||||
|
idxTangents++; |
||||||
|
|
||||||
|
float rx = 0, ry = 0, rz = 0, rnx = 0, rny = 0, rnz = 0, rtx = 0, rty = 0, rtz = 0; |
||||||
|
|
||||||
|
for (int w = maxWeightsPerVert - 1; w >= 0; w--) { |
||||||
|
float weight = weights[idxWeights]; |
||||||
|
Matrix4f mat = offsetMatrices[ib.get(idxWeights++)]; |
||||||
|
|
||||||
|
rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight; |
||||||
|
ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight; |
||||||
|
rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight; |
||||||
|
|
||||||
|
rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight; |
||||||
|
rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight; |
||||||
|
rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight; |
||||||
|
|
||||||
|
rtx += (tnx * mat.m00 + tny * mat.m01 + tnz * mat.m02) * weight; |
||||||
|
rty += (tnx * mat.m10 + tny * mat.m11 + tnz * mat.m12) * weight; |
||||||
|
rtz += (tnx * mat.m20 + tny * mat.m21 + tnz * mat.m22) * weight; |
||||||
|
} |
||||||
|
|
||||||
|
idxWeights += fourMinusMaxWeights; |
||||||
|
|
||||||
|
idxPositions -= 3; |
||||||
|
|
||||||
|
normBuf[idxPositions] = rnx; |
||||||
|
posBuf[idxPositions++] = rx; |
||||||
|
normBuf[idxPositions] = rny; |
||||||
|
posBuf[idxPositions++] = ry; |
||||||
|
normBuf[idxPositions] = rnz; |
||||||
|
posBuf[idxPositions++] = rz; |
||||||
|
|
||||||
|
idxTangents -= 4; |
||||||
|
|
||||||
|
tanBuf[idxTangents++] = rtx; |
||||||
|
tanBuf[idxTangents++] = rty; |
||||||
|
tanBuf[idxTangents++] = rtz; |
||||||
|
|
||||||
|
//once again skipping the 4th component of the tangent
|
||||||
|
idxTangents++; |
||||||
|
} |
||||||
|
|
||||||
|
fvb.position(fvb.position() - bufLength); |
||||||
|
fvb.put(posBuf, 0, bufLength); |
||||||
|
fnb.position(fnb.position() - bufLength); |
||||||
|
fnb.put(normBuf, 0, bufLength); |
||||||
|
ftb.position(ftb.position() - tanLength); |
||||||
|
ftb.put(tanBuf, 0, tanLength); |
||||||
|
} |
||||||
|
|
||||||
|
vars.release(); |
||||||
|
|
||||||
|
vb.updateData(fvb); |
||||||
|
nb.updateData(fnb); |
||||||
|
tb.updateData(ftb); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
super.write(ex); |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(armature, "armature", null); |
||||||
|
|
||||||
|
oc.write(numberOfJointsParam, "numberOfBonesParam", null); |
||||||
|
oc.write(jointMatricesParam, "boneMatricesParam", null); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
super.read(im); |
||||||
|
InputCapsule in = im.getCapsule(this); |
||||||
|
armature = (Armature) in.readSavable("armature", null); |
||||||
|
|
||||||
|
numberOfJointsParam = (MatParamOverride) in.readSavable("numberOfBonesParam", null); |
||||||
|
jointMatricesParam = (MatParamOverride) in.readSavable("boneMatricesParam", null); |
||||||
|
|
||||||
|
if (numberOfJointsParam == null) { |
||||||
|
numberOfJointsParam = new MatParamOverride(VarType.Int, "NumberOfBones", null); |
||||||
|
jointMatricesParam = new MatParamOverride(VarType.Matrix4Array, "BoneMatrices", null); |
||||||
|
getSpatial().addMatParamOverride(numberOfJointsParam); |
||||||
|
getSpatial().addMatParamOverride(jointMatricesParam); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the lists of animation targets. |
||||||
|
* |
||||||
|
* @param spatial the controlled spatial |
||||||
|
*/ |
||||||
|
private void updateTargetsAndMaterials(Spatial spatial) { |
||||||
|
targets.clear(); |
||||||
|
|
||||||
|
if (spatial instanceof Node) { |
||||||
|
findTargets((Node) spatial); |
||||||
|
} else if (spatial instanceof Geometry) { |
||||||
|
findTargets((Geometry) spatial); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,315 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2012 jMonkeyEngine |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* * Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in the |
||||||
|
* documentation and/or other materials provided with the distribution. |
||||||
|
* |
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
|
* may be used to endorse or promote products derived from this software |
||||||
|
* without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import com.jme3.anim.interpolator.FrameInterpolator; |
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
import com.jme3.anim.util.HasLocalTransform; |
||||||
|
import com.jme3.animation.CompactQuaternionArray; |
||||||
|
import com.jme3.animation.CompactVector3Array; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.math.*; |
||||||
|
import com.jme3.util.clone.Cloner; |
||||||
|
import com.jme3.util.clone.JmeCloneable; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Contains a list of transforms and times for each keyframe. |
||||||
|
* |
||||||
|
* @author Rémy Bouquet |
||||||
|
*/ |
||||||
|
public class TransformTrack implements AnimTrack<Transform> { |
||||||
|
|
||||||
|
private double length; |
||||||
|
private HasLocalTransform target; |
||||||
|
|
||||||
|
/** |
||||||
|
* Transforms and times for track. |
||||||
|
*/ |
||||||
|
private CompactVector3Array translations; |
||||||
|
private CompactQuaternionArray rotations; |
||||||
|
private CompactVector3Array scales; |
||||||
|
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT; |
||||||
|
private float[] times; |
||||||
|
|
||||||
|
/** |
||||||
|
* Serialization-only. Do not use. |
||||||
|
*/ |
||||||
|
public TransformTrack() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a transform track for the given bone index |
||||||
|
* |
||||||
|
* @param times a float array with the time of each frame |
||||||
|
* @param translations the translation of the bone for each frame |
||||||
|
* @param rotations the rotation of the bone for each frame |
||||||
|
* @param scales the scale of the bone for each frame |
||||||
|
*/ |
||||||
|
public TransformTrack(HasLocalTransform target, float[] times, Vector3f[] translations, Quaternion[] rotations, Vector3f[] scales) { |
||||||
|
this.target = target; |
||||||
|
this.setKeyframes(times, translations, rotations, scales); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* return the array of rotations of this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Quaternion[] getRotations() { |
||||||
|
return rotations.toObjectArray(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the array of scales for this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Vector3f[] getScales() { |
||||||
|
return scales == null ? null : scales.toObjectArray(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the arrays of time for this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public float[] getTimes() { |
||||||
|
return times; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* returns the array of translations of this track |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public Vector3f[] getTranslations() { |
||||||
|
return translations.toObjectArray(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the keyframes times for this Joint track |
||||||
|
* |
||||||
|
* @param times the keyframes times |
||||||
|
*/ |
||||||
|
public void setTimes(float[] times) { |
||||||
|
if (times.length == 0) { |
||||||
|
throw new RuntimeException("TransformTrack with no keyframes!"); |
||||||
|
} |
||||||
|
this.times = times; |
||||||
|
length = times[times.length - 1] - times[0]; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the translations for this joint track |
||||||
|
* |
||||||
|
* @param translations the translation of the bone for each frame |
||||||
|
*/ |
||||||
|
public void setKeyframesTranslation(Vector3f[] translations) { |
||||||
|
if (times == null) { |
||||||
|
throw new RuntimeException("TransformTrack doesn't have any time for key frames, please call setTimes first"); |
||||||
|
} |
||||||
|
if (translations.length == 0) { |
||||||
|
throw new RuntimeException("TransformTrack with no translation keyframes!"); |
||||||
|
} |
||||||
|
this.translations = new CompactVector3Array(); |
||||||
|
this.translations.add(translations); |
||||||
|
this.translations.freeze(); |
||||||
|
|
||||||
|
assert times != null && times.length == translations.length; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the scales for this joint track |
||||||
|
* |
||||||
|
* @param scales the scales of the bone for each frame |
||||||
|
*/ |
||||||
|
public void setKeyframesScale(Vector3f[] scales) { |
||||||
|
if (times == null) { |
||||||
|
throw new RuntimeException("TransformTrack doesn't have any time for key frames, please call setTimes first"); |
||||||
|
} |
||||||
|
if (scales.length == 0) { |
||||||
|
throw new RuntimeException("TransformTrack with no scale keyframes!"); |
||||||
|
} |
||||||
|
this.scales = new CompactVector3Array(); |
||||||
|
this.scales.add(scales); |
||||||
|
this.scales.freeze(); |
||||||
|
|
||||||
|
assert times != null && times.length == scales.length; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Set the rotations for this joint track |
||||||
|
* |
||||||
|
* @param rotations the rotations of the bone for each frame |
||||||
|
*/ |
||||||
|
public void setKeyframesRotation(Quaternion[] rotations) { |
||||||
|
if (times == null) { |
||||||
|
throw new RuntimeException("TransformTrack doesn't have any time for key frames, please call setTimes first"); |
||||||
|
} |
||||||
|
if (rotations.length == 0) { |
||||||
|
throw new RuntimeException("TransformTrack with no rotation keyframes!"); |
||||||
|
} |
||||||
|
this.rotations = new CompactQuaternionArray(); |
||||||
|
this.rotations.add(rotations); |
||||||
|
this.rotations.freeze(); |
||||||
|
|
||||||
|
assert times != null && times.length == rotations.length; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Set the translations, rotations and scales for this bone track |
||||||
|
* |
||||||
|
* @param times a float array with the time of each frame |
||||||
|
* @param translations the translation of the bone for each frame |
||||||
|
* @param rotations the rotation of the bone for each frame |
||||||
|
* @param scales the scale of the bone for each frame |
||||||
|
*/ |
||||||
|
public void setKeyframes(float[] times, Vector3f[] translations, Quaternion[] rotations, Vector3f[] scales) { |
||||||
|
setTimes(times); |
||||||
|
if (translations != null) { |
||||||
|
setKeyframesTranslation(translations); |
||||||
|
} |
||||||
|
if (rotations != null) { |
||||||
|
setKeyframesRotation(rotations); |
||||||
|
} |
||||||
|
if (scales != null) { |
||||||
|
setKeyframesScale(scales); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
public void getDataAtTime(double t, Transform transform) { |
||||||
|
float time = (float) t; |
||||||
|
|
||||||
|
int lastFrame = times.length - 1; |
||||||
|
if (time < 0 || lastFrame == 0) { |
||||||
|
if (translations != null) { |
||||||
|
translations.get(0, transform.getTranslation()); |
||||||
|
} |
||||||
|
if (rotations != null) { |
||||||
|
rotations.get(0, transform.getRotation()); |
||||||
|
} |
||||||
|
if (scales != null) { |
||||||
|
scales.get(0, transform.getScale()); |
||||||
|
} |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
int startFrame = 0; |
||||||
|
int endFrame = 1; |
||||||
|
float blend = 0; |
||||||
|
if (time >= times[lastFrame]) { |
||||||
|
startFrame = lastFrame; |
||||||
|
|
||||||
|
time = time - times[startFrame] + times[startFrame - 1]; |
||||||
|
blend = (time - times[startFrame - 1]) |
||||||
|
/ (times[startFrame] - times[startFrame - 1]); |
||||||
|
|
||||||
|
} else { |
||||||
|
// use lastFrame so we never overflow the array
|
||||||
|
int i; |
||||||
|
for (i = 0; i < lastFrame && times[i] < time; i++) { |
||||||
|
startFrame = i; |
||||||
|
endFrame = i + 1; |
||||||
|
} |
||||||
|
blend = (time - times[startFrame]) |
||||||
|
/ (times[endFrame] - times[startFrame]); |
||||||
|
} |
||||||
|
|
||||||
|
Transform interpolated = interpolator.interpolate(blend, startFrame, translations, rotations, scales, times); |
||||||
|
|
||||||
|
if (translations != null) { |
||||||
|
transform.setTranslation(interpolated.getTranslation()); |
||||||
|
} |
||||||
|
if (rotations != null) { |
||||||
|
transform.setRotation(interpolated.getRotation()); |
||||||
|
} |
||||||
|
if (scales != null) { |
||||||
|
transform.setScale(interpolated.getScale()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void setFrameInterpolator(FrameInterpolator interpolator) { |
||||||
|
this.interpolator = interpolator; |
||||||
|
} |
||||||
|
|
||||||
|
public HasLocalTransform getTarget() { |
||||||
|
return target; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTarget(HasLocalTransform target) { |
||||||
|
this.target = target; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(translations, "translations", null); |
||||||
|
oc.write(rotations, "rotations", null); |
||||||
|
oc.write(times, "times", null); |
||||||
|
oc.write(scales, "scales", null); |
||||||
|
oc.write(target, "target", null); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
translations = (CompactVector3Array) ic.readSavable("translations", null); |
||||||
|
rotations = (CompactQuaternionArray) ic.readSavable("rotations", null); |
||||||
|
times = ic.readFloatArray("times", null); |
||||||
|
scales = (CompactVector3Array) ic.readSavable("scales", null); |
||||||
|
target = (HasLocalTransform) ic.readSavable("target", null); |
||||||
|
setTimes(times); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object jmeClone() { |
||||||
|
try { |
||||||
|
TransformTrack clone = (TransformTrack) super.clone(); |
||||||
|
return clone; |
||||||
|
} catch (CloneNotSupportedException ex) { |
||||||
|
throw new AssertionError(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cloneFields(Cloner cloner, Object original) { |
||||||
|
this.target = cloner.clone(target); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,95 @@ |
|||||||
|
package com.jme3.anim; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
|
||||||
|
public class Weights {//} extends Savable, JmeCloneable{
|
||||||
|
|
||||||
|
|
||||||
|
private final static float MIN_WEIGHT = 0.005f; |
||||||
|
|
||||||
|
private int[] indices; |
||||||
|
private float[] data; |
||||||
|
private int size; |
||||||
|
|
||||||
|
public Weights(float[] array, int start, int length) { |
||||||
|
ArrayList<Float> list = new ArrayList<>(); |
||||||
|
ArrayList<Integer> idx = new ArrayList<>(); |
||||||
|
|
||||||
|
for (int i = start; i < length; i++) { |
||||||
|
float val = array[i]; |
||||||
|
if (val > MIN_WEIGHT) { |
||||||
|
list.add(val); |
||||||
|
idx.add(i); |
||||||
|
} |
||||||
|
} |
||||||
|
size = list.size(); |
||||||
|
data = new float[size]; |
||||||
|
indices = new int[size]; |
||||||
|
for (int i = 0; i < size; i++) { |
||||||
|
data[i] = list.get(i); |
||||||
|
indices[i] = idx.get(i); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public int getSize() { |
||||||
|
return size; |
||||||
|
} |
||||||
|
|
||||||
|
// public Weights(float[] array, int start, int length) {
|
||||||
|
// LinkedList<Float> list = new LinkedList<>();
|
||||||
|
// LinkedList<Integer> idx = new LinkedList<>();
|
||||||
|
// for (int i = start; i < length; i++) {
|
||||||
|
// float val = array[i];
|
||||||
|
// if (val > MIN_WEIGHT) {
|
||||||
|
// int index = insert(list, val);
|
||||||
|
// if (idx.size() < index) {
|
||||||
|
// idx.add(i);
|
||||||
|
// } else {
|
||||||
|
// idx.add(index, i);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// data = new float[list.size()];
|
||||||
|
// for (int i = 0; i < data.length; i++) {
|
||||||
|
// data[i] = list.get(i);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// indices = new int[idx.size()];
|
||||||
|
// for (int i = 0; i < indices.length; i++) {
|
||||||
|
// indices[i] = idx.get(i);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private int insert(LinkedList<Float> list, float value) {
|
||||||
|
// for (int i = 0; i < list.size(); i++) {
|
||||||
|
// float w = list.get(i);
|
||||||
|
// if (value > w) {
|
||||||
|
// list.add(i, value);
|
||||||
|
// return i;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// list.add(value);
|
||||||
|
// return list.size();
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
StringBuilder b = new StringBuilder(); |
||||||
|
for (int i = 0; i < indices.length; i++) { |
||||||
|
b.append(indices[i]).append(","); |
||||||
|
} |
||||||
|
b.append("\n"); |
||||||
|
for (int i = 0; i < data.length; i++) { |
||||||
|
b.append(data[i]).append(","); |
||||||
|
} |
||||||
|
return b.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
public static void main(String... args) { |
||||||
|
// 6 7 4 8
|
||||||
|
float values[] = {0, 0, 0, 0, 0.5f, 0.001f, 0.7f, 0.6f, 0.2f, 0, 0, 0}; |
||||||
|
Weights w = new Weights(values, 0, values.length); |
||||||
|
System.err.println(w); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
package com.jme3.anim.interpolator; |
||||||
|
|
||||||
|
import static com.jme3.anim.interpolator.FrameInterpolator.TrackDataReader; |
||||||
|
import static com.jme3.anim.interpolator.FrameInterpolator.TrackTimeReader; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by nehon on 15/04/17. |
||||||
|
*/ |
||||||
|
public abstract class AnimInterpolator<T> { |
||||||
|
|
||||||
|
public abstract T interpolate(float t, int currentIndex, TrackDataReader<T> data, TrackTimeReader times, T store); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,149 @@ |
|||||||
|
package com.jme3.anim.interpolator; |
||||||
|
|
||||||
|
import com.jme3.math.*; |
||||||
|
|
||||||
|
import static com.jme3.anim.interpolator.FrameInterpolator.TrackDataReader; |
||||||
|
import static com.jme3.anim.interpolator.FrameInterpolator.TrackTimeReader; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by nehon on 15/04/17. |
||||||
|
*/ |
||||||
|
public class AnimInterpolators { |
||||||
|
|
||||||
|
//Rotation interpolators
|
||||||
|
|
||||||
|
public static final AnimInterpolator<Quaternion> NLerp = new AnimInterpolator<Quaternion>() { |
||||||
|
private Quaternion next = new Quaternion(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Quaternion interpolate(float t, int currentIndex, TrackDataReader<Quaternion> data, TrackTimeReader times, Quaternion store) { |
||||||
|
data.getEntryClamp(currentIndex, store); |
||||||
|
data.getEntryClamp(currentIndex + 1, next); |
||||||
|
store.nlerp(next, t); |
||||||
|
return store; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static final AnimInterpolator<Quaternion> SLerp = new AnimInterpolator<Quaternion>() { |
||||||
|
private Quaternion next = new Quaternion(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Quaternion interpolate(float t, int currentIndex, TrackDataReader<Quaternion> data, TrackTimeReader times, Quaternion store) { |
||||||
|
data.getEntryClamp(currentIndex, store); |
||||||
|
data.getEntryClamp(currentIndex + 1, next); |
||||||
|
//MathUtils.slerpNoInvert(store, next, t, store);
|
||||||
|
MathUtils.slerp(store, next, t, store); |
||||||
|
return store; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static final AnimInterpolator<Quaternion> SQuad = new AnimInterpolator<Quaternion>() { |
||||||
|
private Quaternion a = new Quaternion(); |
||||||
|
private Quaternion b = new Quaternion(); |
||||||
|
|
||||||
|
private Quaternion q0 = new Quaternion(); |
||||||
|
private Quaternion q1 = new Quaternion(); |
||||||
|
private Quaternion q2 = new Quaternion(); |
||||||
|
private Quaternion q3 = new Quaternion(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Quaternion interpolate(float t, int currentIndex, TrackDataReader<Quaternion> data, TrackTimeReader times, Quaternion store) { |
||||||
|
data.getEntryModSkip(currentIndex - 1, q0); |
||||||
|
data.getEntryModSkip(currentIndex, q1); |
||||||
|
data.getEntryModSkip(currentIndex + 1, q2); |
||||||
|
data.getEntryModSkip(currentIndex + 2, q3); |
||||||
|
MathUtils.squad(q0, q1, q2, q3, a, b, t, store); |
||||||
|
return store; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
//Position / Scale interpolators
|
||||||
|
public static final AnimInterpolator<Vector3f> LinearVec3f = new AnimInterpolator<Vector3f>() { |
||||||
|
private Vector3f next = new Vector3f(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Vector3f interpolate(float t, int currentIndex, TrackDataReader<Vector3f> data, TrackTimeReader times, Vector3f store) { |
||||||
|
data.getEntryClamp(currentIndex, store); |
||||||
|
data.getEntryClamp(currentIndex + 1, next); |
||||||
|
store.interpolateLocal(next, t); |
||||||
|
return store; |
||||||
|
} |
||||||
|
}; |
||||||
|
/** |
||||||
|
* CatmullRom interpolation |
||||||
|
*/ |
||||||
|
public static final CatmullRomInterpolator CatmullRom = new CatmullRomInterpolator(); |
||||||
|
|
||||||
|
public static class CatmullRomInterpolator extends AnimInterpolator<Vector3f> { |
||||||
|
private Vector3f p0 = new Vector3f(); |
||||||
|
private Vector3f p1 = new Vector3f(); |
||||||
|
private Vector3f p2 = new Vector3f(); |
||||||
|
private Vector3f p3 = new Vector3f(); |
||||||
|
private float tension = 0.7f; |
||||||
|
|
||||||
|
public CatmullRomInterpolator(float tension) { |
||||||
|
this.tension = tension; |
||||||
|
} |
||||||
|
|
||||||
|
public CatmullRomInterpolator() { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Vector3f interpolate(float t, int currentIndex, TrackDataReader<Vector3f> data, TrackTimeReader times, Vector3f store) { |
||||||
|
data.getEntryModSkip(currentIndex - 1, p0); |
||||||
|
data.getEntryModSkip(currentIndex, p1); |
||||||
|
data.getEntryModSkip(currentIndex + 1, p2); |
||||||
|
data.getEntryModSkip(currentIndex + 2, p3); |
||||||
|
|
||||||
|
FastMath.interpolateCatmullRom(t, tension, p0, p1, p2, p3, store); |
||||||
|
return store; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//Time Interpolators
|
||||||
|
|
||||||
|
public static class TimeInterpolator extends AnimInterpolator<Float> { |
||||||
|
private EaseFunction ease; |
||||||
|
|
||||||
|
public TimeInterpolator(EaseFunction ease) { |
||||||
|
this.ease = ease; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Float interpolate(float t, int currentIndex, TrackDataReader<Float> data, TrackTimeReader times, Float store) { |
||||||
|
return ease.apply(t); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//in
|
||||||
|
public static final TimeInterpolator easeInQuad = new TimeInterpolator(Easing.inQuad); |
||||||
|
public static final TimeInterpolator easeInCubic = new TimeInterpolator(Easing.inCubic); |
||||||
|
public static final TimeInterpolator easeInQuart = new TimeInterpolator(Easing.inQuart); |
||||||
|
public static final TimeInterpolator easeInQuint = new TimeInterpolator(Easing.inQuint); |
||||||
|
public static final TimeInterpolator easeInBounce = new TimeInterpolator(Easing.inBounce); |
||||||
|
public static final TimeInterpolator easeInElastic = new TimeInterpolator(Easing.inElastic); |
||||||
|
|
||||||
|
//out
|
||||||
|
public static final TimeInterpolator easeOutQuad = new TimeInterpolator(Easing.outQuad); |
||||||
|
public static final TimeInterpolator easeOutCubic = new TimeInterpolator(Easing.outCubic); |
||||||
|
public static final TimeInterpolator easeOutQuart = new TimeInterpolator(Easing.outQuart); |
||||||
|
public static final TimeInterpolator easeOutQuint = new TimeInterpolator(Easing.outQuint); |
||||||
|
public static final TimeInterpolator easeOutBounce = new TimeInterpolator(Easing.outBounce); |
||||||
|
public static final TimeInterpolator easeOutElastic = new TimeInterpolator(Easing.outElastic); |
||||||
|
|
||||||
|
//inout
|
||||||
|
public static final TimeInterpolator easeInOutQuad = new TimeInterpolator(Easing.inOutQuad); |
||||||
|
public static final TimeInterpolator easeInOutCubic = new TimeInterpolator(Easing.inOutCubic); |
||||||
|
public static final TimeInterpolator easeInOutQuart = new TimeInterpolator(Easing.inOutQuart); |
||||||
|
public static final TimeInterpolator easeInOutQuint = new TimeInterpolator(Easing.inOutQuint); |
||||||
|
public static final TimeInterpolator easeInOutBounce = new TimeInterpolator(Easing.inOutBounce); |
||||||
|
public static final TimeInterpolator easeInOutElastic = new TimeInterpolator(Easing.inOutElastic); |
||||||
|
|
||||||
|
//extra
|
||||||
|
public static final TimeInterpolator smoothStep = new TimeInterpolator(Easing.smoothStep); |
||||||
|
public static final TimeInterpolator smootherStep = new TimeInterpolator(Easing.smootherStep); |
||||||
|
|
||||||
|
public static final TimeInterpolator constant = new TimeInterpolator(Easing.constant); |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,136 @@ |
|||||||
|
package com.jme3.anim.interpolator; |
||||||
|
|
||||||
|
import com.jme3.animation.*; |
||||||
|
import com.jme3.math.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by nehon on 15/04/17. |
||||||
|
*/ |
||||||
|
public class FrameInterpolator { |
||||||
|
|
||||||
|
public static final FrameInterpolator DEFAULT = new FrameInterpolator(); |
||||||
|
|
||||||
|
private AnimInterpolator<Float> timeInterpolator; |
||||||
|
private AnimInterpolator<Vector3f> translationInterpolator = AnimInterpolators.LinearVec3f; |
||||||
|
private AnimInterpolator<Quaternion> rotationInterpolator = AnimInterpolators.NLerp; |
||||||
|
private AnimInterpolator<Vector3f> scaleInterpolator = AnimInterpolators.LinearVec3f; |
||||||
|
|
||||||
|
private TrackDataReader<Vector3f> translationReader = new TrackDataReader<>(); |
||||||
|
private TrackDataReader<Quaternion> rotationReader = new TrackDataReader<>(); |
||||||
|
private TrackDataReader<Vector3f> scaleReader = new TrackDataReader<>(); |
||||||
|
private TrackTimeReader timesReader = new TrackTimeReader(); |
||||||
|
|
||||||
|
|
||||||
|
private Transform transforms = new Transform(); |
||||||
|
|
||||||
|
public Transform interpolate(float t, int currentIndex, CompactVector3Array translations, CompactQuaternionArray rotations, CompactVector3Array scales, float[] times){ |
||||||
|
timesReader.setData(times); |
||||||
|
if( timeInterpolator != null){ |
||||||
|
t = timeInterpolator.interpolate(t,currentIndex, null, timesReader, null ); |
||||||
|
} |
||||||
|
if(translations != null) { |
||||||
|
translationReader.setData(translations); |
||||||
|
translationInterpolator.interpolate(t, currentIndex, translationReader, timesReader, transforms.getTranslation()); |
||||||
|
} |
||||||
|
if(rotations != null) { |
||||||
|
rotationReader.setData(rotations); |
||||||
|
rotationInterpolator.interpolate(t, currentIndex, rotationReader, timesReader, transforms.getRotation()); |
||||||
|
} |
||||||
|
if(scales != null){ |
||||||
|
scaleReader.setData(scales); |
||||||
|
scaleInterpolator.interpolate(t, currentIndex, scaleReader, timesReader, transforms.getScale()); |
||||||
|
} |
||||||
|
return transforms; |
||||||
|
} |
||||||
|
|
||||||
|
public void interpolateWeights(float t, int currentIndex, float[] weights, int nbMorphTargets, float[] store) { |
||||||
|
int start = currentIndex * nbMorphTargets; |
||||||
|
for (int i = 0; i < nbMorphTargets; i++) { |
||||||
|
int current = start + i; |
||||||
|
int next = current + nbMorphTargets; |
||||||
|
if (next >= weights.length) { |
||||||
|
next = current; |
||||||
|
} |
||||||
|
|
||||||
|
float val = FastMath.interpolateLinear(t, weights[current], weights[next]); |
||||||
|
store[i] = val; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void setTimeInterpolator(AnimInterpolator<Float> timeInterpolator) { |
||||||
|
this.timeInterpolator = timeInterpolator; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTranslationInterpolator(AnimInterpolator<Vector3f> translationInterpolator) { |
||||||
|
this.translationInterpolator = translationInterpolator; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRotationInterpolator(AnimInterpolator<Quaternion> rotationInterpolator) { |
||||||
|
this.rotationInterpolator = rotationInterpolator; |
||||||
|
} |
||||||
|
|
||||||
|
public void setScaleInterpolator(AnimInterpolator<Vector3f> scaleInterpolator) { |
||||||
|
this.scaleInterpolator = scaleInterpolator; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static class TrackTimeReader { |
||||||
|
private float[] data; |
||||||
|
|
||||||
|
protected void setData(float[] data) { |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
|
||||||
|
public float getEntry(int index) { |
||||||
|
return data[mod(index, data.length)]; |
||||||
|
} |
||||||
|
|
||||||
|
public int getLength() { |
||||||
|
return data.length; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static class TrackDataReader<T> { |
||||||
|
|
||||||
|
private CompactArray<T> data; |
||||||
|
|
||||||
|
protected void setData(CompactArray<T> data) { |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
|
||||||
|
public T getEntryMod(int index, T store) { |
||||||
|
return data.get(mod(index, data.getTotalObjectSize()), store); |
||||||
|
} |
||||||
|
|
||||||
|
public T getEntryClamp(int index, T store) { |
||||||
|
index = (int) FastMath.clamp(index, 0, data.getTotalObjectSize() - 1); |
||||||
|
return data.get(index, store); |
||||||
|
} |
||||||
|
|
||||||
|
public T getEntryModSkip(int index, T store) { |
||||||
|
int total = data.getTotalObjectSize(); |
||||||
|
if (index == -1) { |
||||||
|
index--; |
||||||
|
} else if (index >= total) { |
||||||
|
index++; |
||||||
|
} |
||||||
|
|
||||||
|
index = mod(index, total); |
||||||
|
|
||||||
|
|
||||||
|
return data.get(index, store); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Euclidean modulo (cycle on 0,n instead of -n,0; 0,n) |
||||||
|
* |
||||||
|
* @param val |
||||||
|
* @param n |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private static int mod(int val, int n) { |
||||||
|
return ((val % n) + n) % n; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,97 @@ |
|||||||
|
/* |
||||||
|
* $Id$ |
||||||
|
* |
||||||
|
* Copyright (c) 2015, Simsilica, LLC |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions |
||||||
|
* are met: |
||||||
|
* |
||||||
|
* 1. Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in |
||||||
|
* the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* |
||||||
|
* 3. Neither the name of the copyright holder nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived |
||||||
|
* from this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||||||
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
||||||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jme3.anim.tween; |
||||||
|
|
||||||
|
|
||||||
|
import com.jme3.export.*; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Base implementation of the Tween interface that provides |
||||||
|
* default implementations of the getLength() and interopolate() |
||||||
|
* methods that provide common tween clamping and bounds checking. |
||||||
|
* Subclasses need only override the doInterpolate() method and |
||||||
|
* the rest is handled for them. |
||||||
|
* |
||||||
|
* @author Paul Speed |
||||||
|
*/ |
||||||
|
public abstract class AbstractTween implements Tween { |
||||||
|
|
||||||
|
private double length; |
||||||
|
|
||||||
|
protected AbstractTween(double length) { |
||||||
|
this.length = length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
public void setLength(double length) { |
||||||
|
this.length = length; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Default implementation clamps the time value, converts |
||||||
|
* it to 0 to 1.0 based on getLength(), and calls doInterpolate(). |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
// Scale t to be between 0 and 1 for our length
|
||||||
|
if (length == 0) { |
||||||
|
t = 1; |
||||||
|
} else { |
||||||
|
t = t / length; |
||||||
|
} |
||||||
|
|
||||||
|
boolean done = false; |
||||||
|
if (t >= 1.0) { |
||||||
|
t = 1.0; |
||||||
|
done = true; |
||||||
|
} |
||||||
|
doInterpolate(t); |
||||||
|
return !done; |
||||||
|
} |
||||||
|
|
||||||
|
protected abstract void doInterpolate(double t); |
||||||
|
} |
@ -0,0 +1,6 @@ |
|||||||
|
package com.jme3.anim.tween; |
||||||
|
|
||||||
|
public interface ContainsTweens { |
||||||
|
|
||||||
|
public Tween[] getTweens(); |
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* $Id$ |
||||||
|
* |
||||||
|
* Copyright (c) 2015, Simsilica, LLC |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions |
||||||
|
* are met: |
||||||
|
* |
||||||
|
* 1. Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in |
||||||
|
* the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* |
||||||
|
* 3. Neither the name of the copyright holder nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived |
||||||
|
* from this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||||||
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
||||||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jme3.anim.tween; |
||||||
|
|
||||||
|
|
||||||
|
import com.jme3.export.Savable; |
||||||
|
|
||||||
|
/** |
||||||
|
* Represents some action that interpolates across input between 0 |
||||||
|
* and some length value. (For example, movement, rotation, fading.) |
||||||
|
* It's also possible to have zero length 'instant' tweens. |
||||||
|
* |
||||||
|
* @author Paul Speed |
||||||
|
*/ |
||||||
|
public interface Tween extends Cloneable { |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the length of the tween. If 't' represents time in |
||||||
|
* seconds then this is the notional time in seconds that the tween |
||||||
|
* will run. Note: all of the caveats are because tweens may be |
||||||
|
* externally scaled in such a way that 't' no longer represents |
||||||
|
* actual time. |
||||||
|
*/ |
||||||
|
public double getLength(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the implementation specific interpolation to the |
||||||
|
* specified 'tween' value as a value in the range from 0 to |
||||||
|
* getLength(). If the value is greater or equal to getLength() |
||||||
|
* then it is internally clamped and the method returns false. |
||||||
|
* If 't' is still in the tween's range then this method returns |
||||||
|
* true. |
||||||
|
*/ |
||||||
|
public boolean interpolate(double t); |
||||||
|
|
||||||
|
} |
||||||
|
|
@ -0,0 +1,619 @@ |
|||||||
|
/* |
||||||
|
* $Id$ |
||||||
|
* |
||||||
|
* Copyright (c) 2015, Simsilica, LLC |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions |
||||||
|
* are met: |
||||||
|
* |
||||||
|
* 1. Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in |
||||||
|
* the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* |
||||||
|
* 3. Neither the name of the copyright holder nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived |
||||||
|
* from this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||||||
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
||||||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.jme3.anim.tween; |
||||||
|
|
||||||
|
import com.jme3.anim.util.Primitives; |
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException; |
||||||
|
import java.lang.reflect.Method; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.Objects; |
||||||
|
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* Static utility methods for creating common generic Tween objects. |
||||||
|
* |
||||||
|
* @author Paul Speed |
||||||
|
*/ |
||||||
|
public class Tweens { |
||||||
|
|
||||||
|
static Logger log = Logger.getLogger(Tweens.class.getName()); |
||||||
|
|
||||||
|
private static final CurveFunction SMOOTH = new SmoothStep(); |
||||||
|
private static final CurveFunction SINE = new Sine(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that will interpolate over an entire sequence |
||||||
|
* of tweens in order. |
||||||
|
*/ |
||||||
|
public static Tween sequence(Tween... delegates) { |
||||||
|
return new Sequence(delegates); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that will interpolate over an entire list |
||||||
|
* of tweens in parallel, ie: all tweens will be run at the same |
||||||
|
* time. |
||||||
|
*/ |
||||||
|
public static Tween parallel(Tween... delegates) { |
||||||
|
return new Parallel(delegates); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that will perform a no-op until the length |
||||||
|
* has expired. |
||||||
|
*/ |
||||||
|
public static Tween delay(double length) { |
||||||
|
return new Delay(length); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that scales the specified delegate tween or tweens |
||||||
|
* to the desired length. If more than one tween is specified then they |
||||||
|
* are wrapped in a sequence using the sequence() method. |
||||||
|
*/ |
||||||
|
public static Tween stretch(double desiredLength, Tween... delegates) { |
||||||
|
if (delegates.length == 1) { |
||||||
|
return new Stretch(delegates[0], desiredLength); |
||||||
|
} |
||||||
|
return new Stretch(sequence(delegates), desiredLength); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that uses a sine function to smooth step the time value |
||||||
|
* for the specified delegate tween or tweens. These 'curved' wrappers |
||||||
|
* can be used to smooth the interpolation of another tween. |
||||||
|
*/ |
||||||
|
public static Tween sineStep(Tween... delegates) { |
||||||
|
if (delegates.length == 1) { |
||||||
|
return new Curve(delegates[0], SINE); |
||||||
|
} |
||||||
|
return new Curve(sequence(delegates), SINE); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a tween that uses a hermite function to smooth step the time value |
||||||
|
* for the specified delegate tween or tweens. This is similar to GLSL's |
||||||
|
* smoothstep(). These 'curved' wrappers can be used to smooth the interpolation |
||||||
|
* of another tween. |
||||||
|
*/ |
||||||
|
public static Tween smoothStep(Tween... delegates) { |
||||||
|
if (delegates.length == 1) { |
||||||
|
return new Curve(delegates[0], SMOOTH); |
||||||
|
} |
||||||
|
return new Curve(sequence(delegates), SMOOTH); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a Tween that will call the specified method and optional arguments |
||||||
|
* whenever supplied a time value greater than or equal to 0. This creates |
||||||
|
* an "instant" tween of length 0. |
||||||
|
*/ |
||||||
|
public static Tween callMethod(Object target, String method, Object... args) { |
||||||
|
return new CallMethod(target, method, args); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a Tween that will call the specified method and optional arguments, |
||||||
|
* including the time value scaled between 0 and 1. The method must take |
||||||
|
* a float or double value as its first or last argument, in addition to whatever |
||||||
|
* optional arguments are specified. |
||||||
|
* <p> |
||||||
|
* <p>For example:</p> |
||||||
|
* <pre>Tweens.callTweenMethod(1, myObject, "foo", "bar")</pre> |
||||||
|
* <p>Would work for any of the following method signatures:</p> |
||||||
|
* <pre> |
||||||
|
* void foo( float t, String arg ) |
||||||
|
* void foo( double t, String arg ) |
||||||
|
* void foo( String arg, float t ) |
||||||
|
* void foo( String arg, double t ) |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
public static Tween callTweenMethod(double length, Object target, String method, Object... args) { |
||||||
|
return new CallTweenMethod(length, target, method, args); |
||||||
|
} |
||||||
|
|
||||||
|
private static interface CurveFunction { |
||||||
|
public double curve(double input); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Curve function for Hermite interpolation ala GLSL smoothstep(). |
||||||
|
*/ |
||||||
|
private static class SmoothStep implements CurveFunction { |
||||||
|
|
||||||
|
@Override |
||||||
|
public double curve(double t) { |
||||||
|
if (t < 0) { |
||||||
|
return 0; |
||||||
|
} else if (t > 1) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
return t * t * (3 - 2 * t); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Sine implements CurveFunction { |
||||||
|
|
||||||
|
@Override |
||||||
|
public double curve(double t) { |
||||||
|
if (t < 0) { |
||||||
|
return 0; |
||||||
|
} else if (t > 1) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
// Sine starting at -90 will go from -1 to 1 through 0
|
||||||
|
double result = Math.sin(t * Math.PI - Math.PI * 0.5); |
||||||
|
return (result + 1) * 0.5; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Curve implements Tween { |
||||||
|
private final Tween delegate; |
||||||
|
private final CurveFunction func; |
||||||
|
private final double length; |
||||||
|
|
||||||
|
public Curve(Tween delegate, CurveFunction func) { |
||||||
|
this.delegate = delegate; |
||||||
|
this.func = func; |
||||||
|
this.length = delegate.getLength(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
// Sanity check the inputs
|
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (length == 0) { |
||||||
|
// Caller did something strange but we'll allow it
|
||||||
|
return delegate.interpolate(t); |
||||||
|
} |
||||||
|
|
||||||
|
t = func.curve(t / length); |
||||||
|
return delegate.interpolate(t * length); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[delegate=" + delegate + ", func=" + func + "]"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Sequence implements Tween, ContainsTweens { |
||||||
|
private final Tween[] delegates; |
||||||
|
private int current = 0; |
||||||
|
private double baseTime; |
||||||
|
private double length; |
||||||
|
|
||||||
|
public Sequence(Tween... delegates) { |
||||||
|
this.delegates = delegates; |
||||||
|
for (Tween t : delegates) { |
||||||
|
length += t.getLength(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
|
||||||
|
// Sanity check the inputs
|
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (t < baseTime) { |
||||||
|
// We've rolled back before the current sequence step
|
||||||
|
// which means we need to reset and start forward
|
||||||
|
// again. We have no idea how to 'roll back' and
|
||||||
|
// this is the only way to maintain consistency.
|
||||||
|
// The only 'normal' case where this happens is when looping
|
||||||
|
// in which case a full rollback is appropriate.
|
||||||
|
current = 0; |
||||||
|
baseTime = 0; |
||||||
|
} |
||||||
|
|
||||||
|
if (current >= delegates.length) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
// Skip any that are done
|
||||||
|
while (!delegates[current].interpolate(t - baseTime)) { |
||||||
|
// Time to go to the next one
|
||||||
|
baseTime += delegates[current].getLength(); |
||||||
|
current++; |
||||||
|
if (current >= delegates.length) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[delegates=" + Arrays.asList(delegates) + "]"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Tween[] getTweens() { |
||||||
|
return delegates; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Parallel implements Tween, ContainsTweens { |
||||||
|
private final Tween[] delegates; |
||||||
|
private final boolean[] done; |
||||||
|
private double length; |
||||||
|
private double lastTime; |
||||||
|
|
||||||
|
public Parallel(Tween... delegates) { |
||||||
|
this.delegates = delegates; |
||||||
|
done = new boolean[delegates.length]; |
||||||
|
|
||||||
|
for (Tween t : delegates) { |
||||||
|
if (t.getLength() > length) { |
||||||
|
length = t.getLength(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
protected void reset() { |
||||||
|
for (int i = 0; i < done.length; i++) { |
||||||
|
done[i] = false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
// Sanity check the inputs
|
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (t < lastTime) { |
||||||
|
// We've rolled back before the last time we were given.
|
||||||
|
// This means we may have 'done'ed a few tasks that now
|
||||||
|
// need to be run again. Better to just reset and start
|
||||||
|
// over. As mentioned in the Sequence task, the only 'normal'
|
||||||
|
// use-case for time rolling backwards is when looping. And
|
||||||
|
// in that case, we want to start from the beginning anyway.
|
||||||
|
reset(); |
||||||
|
} |
||||||
|
lastTime = t; |
||||||
|
|
||||||
|
int runningCount = delegates.length; |
||||||
|
for (int i = 0; i < delegates.length; i++) { |
||||||
|
if (!done[i]) { |
||||||
|
done[i] = !delegates[i].interpolate(t); |
||||||
|
} |
||||||
|
if (done[i]) { |
||||||
|
runningCount--; |
||||||
|
} |
||||||
|
} |
||||||
|
return runningCount > 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[delegates=" + Arrays.asList(delegates) + "]"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Tween[] getTweens() { |
||||||
|
return delegates; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Delay extends AbstractTween { |
||||||
|
|
||||||
|
public Delay(double length) { |
||||||
|
super(length); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doInterpolate(double t) { |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Stretch implements Tween, ContainsTweens { |
||||||
|
|
||||||
|
private final Tween[] delegate = new Tween[1]; |
||||||
|
private final double length; |
||||||
|
private final double scale; |
||||||
|
|
||||||
|
public Stretch(Tween delegate, double length) { |
||||||
|
this.delegate[0] = delegate; |
||||||
|
|
||||||
|
this.length = length; |
||||||
|
|
||||||
|
// Caller desires delegate to be 'length' instead of
|
||||||
|
// it's actual length so we will calculate a time scale
|
||||||
|
// If the desired length is longer than delegate's then
|
||||||
|
// we need to feed time in slower, ie: scale < 1
|
||||||
|
if (length != 0) { |
||||||
|
this.scale = delegate.getLength() / length; |
||||||
|
} else { |
||||||
|
this.scale = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Tween[] getTweens() { |
||||||
|
return delegate; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
if (length > 0) { |
||||||
|
t *= scale; |
||||||
|
} else { |
||||||
|
t = length; |
||||||
|
} |
||||||
|
return delegate[0].interpolate(t); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[delegate=" + delegate[0] + ", length=" + length + "]"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class CallMethod extends AbstractTween { |
||||||
|
|
||||||
|
private Object target; |
||||||
|
private Method method; |
||||||
|
private Object[] args; |
||||||
|
|
||||||
|
public CallMethod(Object target, String methodName, Object... args) { |
||||||
|
super(0); |
||||||
|
if (target == null) { |
||||||
|
throw new IllegalArgumentException("Target cannot be null."); |
||||||
|
} |
||||||
|
this.target = target; |
||||||
|
this.args = args; |
||||||
|
|
||||||
|
// Lookup the method
|
||||||
|
if (args == null) { |
||||||
|
this.method = findMethod(target.getClass(), methodName); |
||||||
|
} else { |
||||||
|
this.method = findMethod(target.getClass(), methodName, args); |
||||||
|
} |
||||||
|
if (this.method == null) { |
||||||
|
throw new IllegalArgumentException("Method not found for:" + methodName + " on type:" + target.getClass()); |
||||||
|
} |
||||||
|
this.method.setAccessible(true); |
||||||
|
} |
||||||
|
|
||||||
|
private static Method findMethod(Class type, String name, Object... args) { |
||||||
|
for (Method m : type.getDeclaredMethods()) { |
||||||
|
if (!Objects.equals(m.getName(), name)) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
Class[] paramTypes = m.getParameterTypes(); |
||||||
|
if (paramTypes.length != args.length) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
int matches = 0; |
||||||
|
for (int i = 0; i < args.length; i++) { |
||||||
|
if (paramTypes[i].isInstance(args[i]) |
||||||
|
|| Primitives.wrap(paramTypes[i]).isInstance(args[i])) { |
||||||
|
matches++; |
||||||
|
} |
||||||
|
} |
||||||
|
if (matches == args.length) { |
||||||
|
return m; |
||||||
|
} |
||||||
|
} |
||||||
|
if (type.getSuperclass() != null) { |
||||||
|
return findMethod(type.getSuperclass(), name, args); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doInterpolate(double t) { |
||||||
|
try { |
||||||
|
method.invoke(target, args); |
||||||
|
} catch (IllegalAccessException e) { |
||||||
|
throw new RuntimeException("Error running method:" + method + " for object:" + target, e); |
||||||
|
} catch (InvocationTargetException e) { |
||||||
|
throw new RuntimeException("Error running method:" + method + " for object:" + target, e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[method=" + method + ", parms=" + Arrays.asList(args) + "]"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class CallTweenMethod extends AbstractTween { |
||||||
|
|
||||||
|
private Object target; |
||||||
|
private Method method; |
||||||
|
private Object[] args; |
||||||
|
private int tIndex = -1; |
||||||
|
private boolean isFloat = false; |
||||||
|
|
||||||
|
public CallTweenMethod(double length, Object target, String methodName, Object... args) { |
||||||
|
super(length); |
||||||
|
if (target == null) { |
||||||
|
throw new IllegalArgumentException("Target cannot be null."); |
||||||
|
} |
||||||
|
this.target = target; |
||||||
|
|
||||||
|
// Lookup the method
|
||||||
|
this.method = findMethod(target.getClass(), methodName, args); |
||||||
|
if (this.method == null) { |
||||||
|
throw new IllegalArgumentException("Method not found for:" + methodName + " on type:" + target.getClass()); |
||||||
|
} |
||||||
|
this.method.setAccessible(true); |
||||||
|
|
||||||
|
// So now setup the real args list
|
||||||
|
this.args = new Object[args.length + 1]; |
||||||
|
if (tIndex == 0) { |
||||||
|
for (int i = 0; i < args.length; i++) { |
||||||
|
this.args[i + 1] = args[i]; |
||||||
|
} |
||||||
|
} else { |
||||||
|
for (int i = 0; i < args.length; i++) { |
||||||
|
this.args[i] = args[i]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static boolean isFloatType(Class type) { |
||||||
|
return type == Float.TYPE || type == Float.class; |
||||||
|
} |
||||||
|
|
||||||
|
private static boolean isDoubleType(Class type) { |
||||||
|
return type == Double.TYPE || type == Double.class; |
||||||
|
} |
||||||
|
|
||||||
|
private Method findMethod(Class type, String name, Object... args) { |
||||||
|
for (Method m : type.getDeclaredMethods()) { |
||||||
|
if (!Objects.equals(m.getName(), name)) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
Class[] paramTypes = m.getParameterTypes(); |
||||||
|
if (paramTypes.length != args.length + 1) { |
||||||
|
if (log.isLoggable(Level.FINE)) { |
||||||
|
log.log(Level.FINE, "Param lengths of [" + m + "] differ. method arg count:" + paramTypes.length + " lookging for:" + (args.length + 1)); |
||||||
|
} |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
// We accept the 't' parameter as either first or last
|
||||||
|
// so we'll see which one matches.
|
||||||
|
if (isFloatType(paramTypes[0]) || isDoubleType(paramTypes[0])) { |
||||||
|
// Try it as the first parameter
|
||||||
|
int matches = 0; |
||||||
|
|
||||||
|
for (int i = 1; i < paramTypes.length; i++) { |
||||||
|
if (paramTypes[i].isInstance(args[i - 1])) { |
||||||
|
matches++; |
||||||
|
} |
||||||
|
} |
||||||
|
if (matches == args.length) { |
||||||
|
// Then this is our method and this is how we are configured
|
||||||
|
tIndex = 0; |
||||||
|
isFloat = isFloatType(paramTypes[0]); |
||||||
|
} else { |
||||||
|
if (log.isLoggable(Level.FINE)) { |
||||||
|
log.log(Level.FINE, m + " Leading float check failed because of type mismatches, for:" + m); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (tIndex >= 0) { |
||||||
|
return m; |
||||||
|
} |
||||||
|
|
||||||
|
// Else try it at the end
|
||||||
|
int last = paramTypes.length - 1; |
||||||
|
if (isFloatType(paramTypes[last]) || isDoubleType(paramTypes[last])) { |
||||||
|
int matches = 0; |
||||||
|
|
||||||
|
for (int i = 0; i < last; i++) { |
||||||
|
if (paramTypes[i].isInstance(args[i])) { |
||||||
|
matches++; |
||||||
|
} |
||||||
|
} |
||||||
|
if (matches == args.length) { |
||||||
|
// Then this is our method and this is how we are configured
|
||||||
|
tIndex = last; |
||||||
|
isFloat = isFloatType(paramTypes[last]); |
||||||
|
return m; |
||||||
|
} else { |
||||||
|
if (log.isLoggable(Level.FINE)) { |
||||||
|
log.log(Level.FINE, "Trailing float check failed because of type mismatches, for:" + m); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (type.getSuperclass() != null) { |
||||||
|
return findMethod(type.getSuperclass(), name, args); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doInterpolate(double t) { |
||||||
|
try { |
||||||
|
if (isFloat) { |
||||||
|
args[tIndex] = (float) t; |
||||||
|
} else { |
||||||
|
args[tIndex] = t; |
||||||
|
} |
||||||
|
method.invoke(target, args); |
||||||
|
} catch (IllegalAccessException e) { |
||||||
|
throw new RuntimeException("Error running method:" + method + " for object:" + target, e); |
||||||
|
} catch (InvocationTargetException e) { |
||||||
|
throw new RuntimeException("Error running method:" + method + " for object:" + target, e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return getClass().getSimpleName() + "[method=" + method + ", parms=" + Arrays.asList(args) + "]"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,70 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
import com.jme3.anim.AnimationMask; |
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
|
||||||
|
public abstract class Action implements Tween { |
||||||
|
|
||||||
|
protected Action[] actions; |
||||||
|
private double length; |
||||||
|
private double speed = 1; |
||||||
|
private AnimationMask mask; |
||||||
|
private boolean forward = true; |
||||||
|
|
||||||
|
protected Action(Tween... tweens) { |
||||||
|
this.actions = new Action[tweens.length]; |
||||||
|
for (int i = 0; i < tweens.length; i++) { |
||||||
|
Tween tween = tweens[i]; |
||||||
|
if (tween instanceof Action) { |
||||||
|
this.actions[i] = (Action) tween; |
||||||
|
} else { |
||||||
|
this.actions[i] = new BaseAction(tween); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public double getLength() { |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
protected void setLength(double length) { |
||||||
|
this.length = length; |
||||||
|
} |
||||||
|
|
||||||
|
public double getSpeed() { |
||||||
|
return speed; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSpeed(double speed) { |
||||||
|
this.speed = speed; |
||||||
|
if( speed < 0){ |
||||||
|
setForward(false); |
||||||
|
} else { |
||||||
|
setForward(true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public AnimationMask getMask() { |
||||||
|
return mask; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMask(AnimationMask mask) { |
||||||
|
this.mask = mask; |
||||||
|
} |
||||||
|
|
||||||
|
protected boolean isForward() { |
||||||
|
return forward; |
||||||
|
} |
||||||
|
|
||||||
|
protected void setForward(boolean forward) { |
||||||
|
if(this.forward == forward){ |
||||||
|
return; |
||||||
|
} |
||||||
|
this.forward = forward; |
||||||
|
for (Action action : actions) { |
||||||
|
action.setForward(forward); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
import com.jme3.anim.tween.ContainsTweens; |
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
import com.jme3.util.SafeArrayList; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class BaseAction extends Action { |
||||||
|
|
||||||
|
private Tween tween; |
||||||
|
|
||||||
|
public BaseAction(Tween tween) { |
||||||
|
this.tween = tween; |
||||||
|
setLength(tween.getLength()); |
||||||
|
List<Action> subActions = new SafeArrayList<>(Action.class); |
||||||
|
gatherActions(tween, subActions); |
||||||
|
actions = new Action[subActions.size()]; |
||||||
|
subActions.toArray(actions); |
||||||
|
} |
||||||
|
|
||||||
|
private void gatherActions(Tween tween, List<Action> subActions) { |
||||||
|
if (tween instanceof Action) { |
||||||
|
subActions.add((Action) tween); |
||||||
|
} else if (tween instanceof ContainsTweens) { |
||||||
|
Tween[] tweens = ((ContainsTweens) tween).getTweens(); |
||||||
|
for (Tween t : tweens) { |
||||||
|
gatherActions(t, subActions); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
return tween.interpolate(t); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,129 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
import com.jme3.anim.util.HasLocalTransform; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
public class BlendAction extends BlendableAction { |
||||||
|
|
||||||
|
private int firstActiveIndex; |
||||||
|
private int secondActiveIndex; |
||||||
|
private BlendSpace blendSpace; |
||||||
|
private float blendWeight; |
||||||
|
private double[] timeFactor; |
||||||
|
private Map<HasLocalTransform, Transform> targetMap = new HashMap<>(); |
||||||
|
|
||||||
|
public BlendAction(BlendSpace blendSpace, BlendableAction... actions) { |
||||||
|
super(actions); |
||||||
|
timeFactor = new double[actions.length]; |
||||||
|
this.blendSpace = blendSpace; |
||||||
|
blendSpace.setBlendAction(this); |
||||||
|
|
||||||
|
for (BlendableAction action : actions) { |
||||||
|
if (action.getLength() > getLength()) { |
||||||
|
setLength(action.getLength()); |
||||||
|
} |
||||||
|
Collection<HasLocalTransform> targets = action.getTargets(); |
||||||
|
for (HasLocalTransform target : targets) { |
||||||
|
Transform t = targetMap.get(target); |
||||||
|
if (t == null) { |
||||||
|
t = new Transform(); |
||||||
|
targetMap.put(target, t); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//Blending effect maybe unexpected when blended animation don't have the same length
|
||||||
|
//Stretching any action that doesn't have the same length.
|
||||||
|
for (int i = 0; i < this.actions.length; i++) { |
||||||
|
this.timeFactor[i] = 1; |
||||||
|
if (this.actions[i].getLength() != getLength()) { |
||||||
|
double actionLength = this.actions[i].getLength(); |
||||||
|
if (actionLength > 0 && getLength() > 0) { |
||||||
|
this.timeFactor[i] = this.actions[i].getLength() / getLength(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void doInterpolate(double t) { |
||||||
|
blendWeight = blendSpace.getWeight(); |
||||||
|
BlendableAction firstActiveAction = (BlendableAction) actions[firstActiveIndex]; |
||||||
|
BlendableAction secondActiveAction = (BlendableAction) actions[secondActiveIndex]; |
||||||
|
firstActiveAction.setCollectTransformDelegate(this); |
||||||
|
secondActiveAction.setCollectTransformDelegate(this); |
||||||
|
|
||||||
|
//only interpolate the first action if the weight if below 1.
|
||||||
|
if (blendWeight < 1f) { |
||||||
|
firstActiveAction.setWeight(1f); |
||||||
|
firstActiveAction.interpolate(t * timeFactor[firstActiveIndex]); |
||||||
|
if (blendWeight == 0) { |
||||||
|
for (HasLocalTransform target : targetMap.keySet()) { |
||||||
|
collect(target, targetMap.get(target)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//Second action should be interpolated
|
||||||
|
secondActiveAction.setWeight(blendWeight); |
||||||
|
secondActiveAction.interpolate(t * timeFactor[secondActiveIndex]); |
||||||
|
|
||||||
|
firstActiveAction.setCollectTransformDelegate(null); |
||||||
|
secondActiveAction.setCollectTransformDelegate(null); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
protected Action[] getActions() { |
||||||
|
return actions; |
||||||
|
} |
||||||
|
|
||||||
|
public BlendSpace getBlendSpace() { |
||||||
|
return blendSpace; |
||||||
|
} |
||||||
|
|
||||||
|
protected void setFirstActiveIndex(int index) { |
||||||
|
this.firstActiveIndex = index; |
||||||
|
} |
||||||
|
|
||||||
|
protected void setSecondActiveIndex(int index) { |
||||||
|
this.secondActiveIndex = index; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Collection<HasLocalTransform> getTargets() { |
||||||
|
return targetMap.keySet(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void collectTransform(HasLocalTransform target, Transform t, float weight, BlendableAction source) { |
||||||
|
|
||||||
|
Transform tr = targetMap.get(target); |
||||||
|
if (weight == 1) { |
||||||
|
tr.set(t); |
||||||
|
} else if (weight > 0) { |
||||||
|
tr.interpolateTransforms(tr, t, weight); |
||||||
|
} |
||||||
|
|
||||||
|
if (source == actions[secondActiveIndex]) { |
||||||
|
collect(target, tr); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void collect(HasLocalTransform target, Transform tr) { |
||||||
|
if (collectTransformDelegate != null) { |
||||||
|
collectTransformDelegate.collectTransform(target, tr, this.getWeight(), this); |
||||||
|
} else { |
||||||
|
if (getTransitionWeight() == 1) { |
||||||
|
target.setLocalTransform(tr); |
||||||
|
} else { |
||||||
|
Transform trans = target.getLocalTransform(); |
||||||
|
trans.interpolateTransforms(trans, tr, getTransitionWeight()); |
||||||
|
target.setLocalTransform(trans); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
public interface BlendSpace { |
||||||
|
|
||||||
|
public void setBlendAction(BlendAction action); |
||||||
|
|
||||||
|
public float getWeight(); |
||||||
|
|
||||||
|
public void setValue(float value); |
||||||
|
} |
@ -0,0 +1,97 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
import com.jme3.anim.tween.AbstractTween; |
||||||
|
import com.jme3.anim.tween.Tween; |
||||||
|
import com.jme3.anim.util.HasLocalTransform; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
import java.util.Collection; |
||||||
|
|
||||||
|
public abstract class BlendableAction extends Action { |
||||||
|
|
||||||
|
protected BlendableAction collectTransformDelegate; |
||||||
|
private float transitionWeight = 1.0f; |
||||||
|
private double transitionLength = 0.4f; |
||||||
|
private float weight = 1f; |
||||||
|
private TransitionTween transition = new TransitionTween(transitionLength); |
||||||
|
|
||||||
|
public BlendableAction(Tween... tweens) { |
||||||
|
super(tweens); |
||||||
|
} |
||||||
|
|
||||||
|
public void setCollectTransformDelegate(BlendableAction delegate) { |
||||||
|
this.collectTransformDelegate = delegate; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean interpolate(double t) { |
||||||
|
// Sanity check the inputs
|
||||||
|
if (t < 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
if (collectTransformDelegate == null) { |
||||||
|
if (transition.getLength() > getLength()) { |
||||||
|
transition.setLength(getLength()); |
||||||
|
} |
||||||
|
if(isForward()) { |
||||||
|
transition.interpolate(t); |
||||||
|
} else { |
||||||
|
float v = Math.max((float)(getLength() - t), 0f); |
||||||
|
transition.interpolate(v); |
||||||
|
} |
||||||
|
} else { |
||||||
|
transitionWeight = 1f; |
||||||
|
} |
||||||
|
|
||||||
|
if (weight == 0) { |
||||||
|
//weight is 0 let's not interpolate
|
||||||
|
return t < getLength(); |
||||||
|
} |
||||||
|
|
||||||
|
doInterpolate(t); |
||||||
|
|
||||||
|
return t < getLength(); |
||||||
|
} |
||||||
|
|
||||||
|
public float getWeight() { |
||||||
|
return weight; |
||||||
|
} |
||||||
|
|
||||||
|
public void setWeight(float weight) { |
||||||
|
this.weight = weight; |
||||||
|
} |
||||||
|
|
||||||
|
protected abstract void doInterpolate(double t); |
||||||
|
|
||||||
|
public abstract Collection<HasLocalTransform> getTargets(); |
||||||
|
|
||||||
|
public abstract void collectTransform(HasLocalTransform target, Transform t, float weight, BlendableAction source); |
||||||
|
|
||||||
|
public double getTransitionLength() { |
||||||
|
return transitionLength; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTransitionLength(double transitionLength) { |
||||||
|
this.transitionLength = transitionLength; |
||||||
|
} |
||||||
|
|
||||||
|
protected float getTransitionWeight() { |
||||||
|
return transitionWeight; |
||||||
|
} |
||||||
|
|
||||||
|
private class TransitionTween extends AbstractTween { |
||||||
|
|
||||||
|
|
||||||
|
public TransitionTween(double length) { |
||||||
|
super(length); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doInterpolate(double t) { |
||||||
|
transitionWeight = (float) t; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,94 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
import com.jme3.anim.*; |
||||||
|
import com.jme3.anim.tween.AbstractTween; |
||||||
|
import com.jme3.anim.util.HasLocalTransform; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class ClipAction extends BlendableAction { |
||||||
|
|
||||||
|
private AnimClip clip; |
||||||
|
private Transform transform = new Transform(); |
||||||
|
|
||||||
|
public ClipAction(AnimClip clip) { |
||||||
|
this.clip = clip; |
||||||
|
setLength(clip.getLength()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doInterpolate(double t) { |
||||||
|
AnimTrack[] tracks = clip.getTracks(); |
||||||
|
for (AnimTrack track : tracks) { |
||||||
|
if (track instanceof TransformTrack) { |
||||||
|
TransformTrack tt = (TransformTrack) track; |
||||||
|
if(getMask() != null && !getMask().contains(tt.getTarget())){ |
||||||
|
continue; |
||||||
|
} |
||||||
|
interpolateTransformTrack(t, tt); |
||||||
|
} else if (track instanceof MorphTrack) { |
||||||
|
interpolateMorphTrack(t, (MorphTrack) track); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void interpolateTransformTrack(double t, TransformTrack track) { |
||||||
|
HasLocalTransform target = track.getTarget(); |
||||||
|
transform.set(target.getLocalTransform()); |
||||||
|
track.getDataAtTime(t, transform); |
||||||
|
|
||||||
|
if (collectTransformDelegate != null) { |
||||||
|
collectTransformDelegate.collectTransform(target, transform, getWeight(), this); |
||||||
|
} else { |
||||||
|
this.collectTransform(target, transform, getTransitionWeight(), this); |
||||||
|
} |
||||||
|
} |
||||||
|
private void interpolateMorphTrack(double t, MorphTrack track) { |
||||||
|
Geometry target = track.getTarget(); |
||||||
|
float[] weights = target.getMorphState(); |
||||||
|
track.getDataAtTime(t, weights); |
||||||
|
target.setMorphState(weights); |
||||||
|
|
||||||
|
// if (collectTransformDelegate != null) {
|
||||||
|
// collectTransformDelegate.collectTransform(target, transform, getWeight(), this);
|
||||||
|
// } else {
|
||||||
|
// this.collectTransform(target, transform, getTransitionWeight(), this);
|
||||||
|
// }
|
||||||
|
} |
||||||
|
|
||||||
|
public void reset() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public String toString() { |
||||||
|
return clip.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Collection<HasLocalTransform> getTargets() { |
||||||
|
List<HasLocalTransform> targets = new ArrayList<>(clip.getTracks().length); |
||||||
|
for (AnimTrack track : clip.getTracks()) { |
||||||
|
if (track instanceof TransformTrack) { |
||||||
|
targets.add(((TransformTrack) track).getTarget()); |
||||||
|
} |
||||||
|
} |
||||||
|
return targets; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void collectTransform(HasLocalTransform target, Transform t, float weight, BlendableAction source) { |
||||||
|
if (weight == 1f) { |
||||||
|
target.setLocalTransform(t); |
||||||
|
} else { |
||||||
|
Transform tr = target.getLocalTransform(); |
||||||
|
tr.interpolateTransforms(tr, t, weight); |
||||||
|
target.setLocalTransform(tr); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
package com.jme3.anim.tween.action; |
||||||
|
|
||||||
|
public class LinearBlendSpace implements BlendSpace { |
||||||
|
|
||||||
|
private BlendAction action; |
||||||
|
private float value; |
||||||
|
private float maxValue; |
||||||
|
private float minValue; |
||||||
|
private float step; |
||||||
|
|
||||||
|
public LinearBlendSpace(float minValue, float maxValue) { |
||||||
|
this.maxValue = maxValue; |
||||||
|
this.minValue = minValue; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setBlendAction(BlendAction action) { |
||||||
|
this.action = action; |
||||||
|
Action[] actions = action.getActions(); |
||||||
|
step = (maxValue - minValue) / (float) (actions.length - 1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float getWeight() { |
||||||
|
Action[] actions = action.getActions(); |
||||||
|
float lowStep = minValue, highStep = minValue; |
||||||
|
int lowIndex = 0, highIndex = 0; |
||||||
|
for (int i = 0; i < actions.length && highStep < value; i++) { |
||||||
|
lowStep = highStep; |
||||||
|
lowIndex = i; |
||||||
|
highStep += step; |
||||||
|
} |
||||||
|
highIndex = lowIndex + 1; |
||||||
|
|
||||||
|
action.setFirstActiveIndex(lowIndex); |
||||||
|
action.setSecondActiveIndex(highIndex); |
||||||
|
|
||||||
|
if (highStep == lowStep) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
return (value - lowStep) / (highStep - lowStep); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setValue(float value) { |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,200 @@ |
|||||||
|
package com.jme3.anim.util; |
||||||
|
|
||||||
|
import com.jme3.anim.*; |
||||||
|
import com.jme3.animation.*; |
||||||
|
import com.jme3.math.Quaternion; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.scene.*; |
||||||
|
|
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
public class AnimMigrationUtils { |
||||||
|
|
||||||
|
private static AnimControlVisitor animControlVisitor = new AnimControlVisitor(); |
||||||
|
private static SkeletonControlVisitor skeletonControlVisitor = new SkeletonControlVisitor(); |
||||||
|
|
||||||
|
|
||||||
|
public static Spatial migrate(Spatial source) { |
||||||
|
Map<Skeleton, Armature> skeletonArmatureMap = new HashMap<>(); |
||||||
|
animControlVisitor.setMappings(skeletonArmatureMap); |
||||||
|
source.depthFirstTraversal(animControlVisitor); |
||||||
|
skeletonControlVisitor.setMappings(skeletonArmatureMap); |
||||||
|
source.depthFirstTraversal(skeletonControlVisitor); |
||||||
|
return source; |
||||||
|
} |
||||||
|
|
||||||
|
private static class AnimControlVisitor implements SceneGraphVisitor { |
||||||
|
|
||||||
|
Map<Skeleton, Armature> skeletonArmatureMap; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void visit(Spatial spatial) { |
||||||
|
AnimControl control = spatial.getControl(AnimControl.class); |
||||||
|
if (control != null) { |
||||||
|
AnimComposer composer = new AnimComposer(); |
||||||
|
Skeleton skeleton = control.getSkeleton(); |
||||||
|
if (skeleton == null) { |
||||||
|
//only bone anim for now
|
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
Joint[] joints = new Joint[skeleton.getBoneCount()]; |
||||||
|
for (int i = 0; i < skeleton.getBoneCount(); i++) { |
||||||
|
Bone b = skeleton.getBone(i); |
||||||
|
Joint j = joints[i]; |
||||||
|
if (j == null) { |
||||||
|
j = fromBone(b); |
||||||
|
joints[i] = j; |
||||||
|
} |
||||||
|
for (Bone bone : b.getChildren()) { |
||||||
|
int index = skeleton.getBoneIndex(bone); |
||||||
|
Joint joint = joints[index]; |
||||||
|
if (joint == null) { |
||||||
|
joint = fromBone(bone); |
||||||
|
} |
||||||
|
j.addChild(joint); |
||||||
|
joints[index] = joint; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Armature armature = new Armature(joints); |
||||||
|
armature.saveBindPose(); |
||||||
|
skeletonArmatureMap.put(skeleton, armature); |
||||||
|
|
||||||
|
List<TransformTrack> tracks = new ArrayList<>(); |
||||||
|
|
||||||
|
for (String animName : control.getAnimationNames()) { |
||||||
|
tracks.clear(); |
||||||
|
Animation anim = control.getAnim(animName); |
||||||
|
AnimClip clip = new AnimClip(animName); |
||||||
|
Joint[] staticJoints = new Joint[joints.length]; |
||||||
|
|
||||||
|
System.arraycopy(joints, 0, staticJoints, 0, joints.length); |
||||||
|
for (Track track : anim.getTracks()) { |
||||||
|
if (track instanceof BoneTrack) { |
||||||
|
BoneTrack boneTrack = (BoneTrack) track; |
||||||
|
int index = boneTrack.getTargetBoneIndex(); |
||||||
|
Bone bone = skeleton.getBone(index); |
||||||
|
Joint joint = joints[index]; |
||||||
|
TransformTrack jointTrack = fromBoneTrack(boneTrack, bone, joint); |
||||||
|
tracks.add(jointTrack); |
||||||
|
//this joint is animated let's remove it from the static joints
|
||||||
|
staticJoints[index] = null; |
||||||
|
} |
||||||
|
//TODO spatial tracks , Effect tracks, Audio tracks
|
||||||
|
} |
||||||
|
|
||||||
|
for (int i = 0; i < staticJoints.length; i++) { |
||||||
|
padJointTracks(tracks, staticJoints[i]); |
||||||
|
} |
||||||
|
|
||||||
|
clip.setTracks(tracks.toArray(new TransformTrack[tracks.size()])); |
||||||
|
|
||||||
|
composer.addAnimClip(clip); |
||||||
|
} |
||||||
|
spatial.removeControl(control); |
||||||
|
spatial.addControl(composer); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void setMappings(Map<Skeleton, Armature> skeletonArmatureMap) { |
||||||
|
this.skeletonArmatureMap = skeletonArmatureMap; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void padJointTracks(List<TransformTrack> tracks, Joint staticJoint) { |
||||||
|
Joint j = staticJoint; |
||||||
|
if (j != null) { |
||||||
|
// joint has no track , we create one with the default pose
|
||||||
|
float[] times = new float[]{0}; |
||||||
|
Vector3f[] translations = new Vector3f[]{j.getLocalTranslation()}; |
||||||
|
Quaternion[] rotations = new Quaternion[]{j.getLocalRotation()}; |
||||||
|
Vector3f[] scales = new Vector3f[]{j.getLocalScale()}; |
||||||
|
TransformTrack track = new TransformTrack(j, times, translations, rotations, scales); |
||||||
|
tracks.add(track); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class SkeletonControlVisitor implements SceneGraphVisitor { |
||||||
|
|
||||||
|
Map<Skeleton, Armature> skeletonArmatureMap; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void visit(Spatial spatial) { |
||||||
|
SkeletonControl control = spatial.getControl(SkeletonControl.class); |
||||||
|
if (control != null) { |
||||||
|
Armature armature = skeletonArmatureMap.get(control.getSkeleton()); |
||||||
|
SkinningControl skinningControl = new SkinningControl(armature); |
||||||
|
Map<String, List<Spatial>> attachedSpatials = new HashMap<>(); |
||||||
|
for (int i = 0; i < control.getSkeleton().getBoneCount(); i++) { |
||||||
|
Bone b = control.getSkeleton().getBone(i); |
||||||
|
Node n = control.getAttachmentsNode(b.getName()); |
||||||
|
n.removeFromParent(); |
||||||
|
if (!n.getChildren().isEmpty()) { |
||||||
|
attachedSpatials.put(b.getName(), n.getChildren()); |
||||||
|
} |
||||||
|
} |
||||||
|
spatial.removeControl(control); |
||||||
|
spatial.addControl(skinningControl); |
||||||
|
for (String name : attachedSpatials.keySet()) { |
||||||
|
List<Spatial> spatials = attachedSpatials.get(name); |
||||||
|
for (Spatial child : spatials) { |
||||||
|
skinningControl.getAttachmentsNode(name).attachChild(child); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void setMappings(Map<Skeleton, Armature> skeletonArmatureMap) { |
||||||
|
this.skeletonArmatureMap = skeletonArmatureMap; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static TransformTrack fromBoneTrack(BoneTrack boneTrack, Bone bone, Joint joint) { |
||||||
|
float[] times = new float[boneTrack.getTimes().length]; |
||||||
|
int length = times.length; |
||||||
|
System.arraycopy(boneTrack.getTimes(), 0, times, 0, length); |
||||||
|
//translation
|
||||||
|
Vector3f[] translations = new Vector3f[length]; |
||||||
|
if (boneTrack.getTranslations() != null) { |
||||||
|
for (int i = 0; i < boneTrack.getTranslations().length; i++) { |
||||||
|
Vector3f oldTrans = boneTrack.getTranslations()[i]; |
||||||
|
Vector3f newTrans = new Vector3f(); |
||||||
|
newTrans.set(bone.getBindPosition()).addLocal(oldTrans); |
||||||
|
translations[i] = newTrans; |
||||||
|
} |
||||||
|
} |
||||||
|
//rotation
|
||||||
|
Quaternion[] rotations = new Quaternion[length]; |
||||||
|
if (boneTrack.getRotations() != null) { |
||||||
|
for (int i = 0; i < boneTrack.getRotations().length; i++) { |
||||||
|
Quaternion oldRot = boneTrack.getRotations()[i]; |
||||||
|
Quaternion newRot = new Quaternion(); |
||||||
|
newRot.set(bone.getBindRotation()).multLocal(oldRot); |
||||||
|
rotations[i] = newRot; |
||||||
|
} |
||||||
|
} |
||||||
|
//scale
|
||||||
|
Vector3f[] scales = new Vector3f[length]; |
||||||
|
if (boneTrack.getScales() != null) { |
||||||
|
for (int i = 0; i < boneTrack.getScales().length; i++) { |
||||||
|
Vector3f oldScale = boneTrack.getScales()[i]; |
||||||
|
Vector3f newScale = new Vector3f(); |
||||||
|
newScale.set(bone.getBindScale()).multLocal(oldScale); |
||||||
|
scales[i] = newScale; |
||||||
|
} |
||||||
|
} |
||||||
|
TransformTrack t = new TransformTrack(joint, times, translations, rotations, scales); |
||||||
|
return t; |
||||||
|
} |
||||||
|
|
||||||
|
private static Joint fromBone(Bone b) { |
||||||
|
Joint j = new Joint(b.getName()); |
||||||
|
j.setLocalTranslation(b.getBindPosition()); |
||||||
|
j.setLocalRotation(b.getBindRotation()); |
||||||
|
j.setLocalScale(b.getBindScale()); |
||||||
|
return j; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
package com.jme3.anim.util; |
||||||
|
|
||||||
|
import com.jme3.export.Savable; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
public interface HasLocalTransform extends Savable { |
||||||
|
public void setLocalTransform(Transform transform); |
||||||
|
|
||||||
|
public Transform getLocalTransform(); |
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
package com.jme3.anim.util; |
||||||
|
|
||||||
|
import com.jme3.anim.Joint; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
/** |
||||||
|
* Implementations of this interface holds accumulated model transform of a Joint. |
||||||
|
* Implementation might choose different accumulation strategy. |
||||||
|
*/ |
||||||
|
public interface JointModelTransform { |
||||||
|
|
||||||
|
void updateModelTransform(Transform localTransform, Joint parent); |
||||||
|
|
||||||
|
void getOffsetTransform(Matrix4f outTransform, Matrix4f inverseModelBindMatrix); |
||||||
|
|
||||||
|
void applyBindPose(Transform localTransform, Matrix4f inverseModelBindMatrix, Joint parent); |
||||||
|
|
||||||
|
Transform getModelTransform(); |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
package com.jme3.anim.util; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* This is a guava method used in {@link com.jme3.anim.tween.Tweens} class. |
||||||
|
* Maybe we should just add guava as a dependency in the engine... |
||||||
|
* //TODO do something about this.
|
||||||
|
*/ |
||||||
|
public class Primitives { |
||||||
|
|
||||||
|
/** |
||||||
|
* A map from primitive types to their corresponding wrapper types. |
||||||
|
*/ |
||||||
|
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE; |
||||||
|
|
||||||
|
static { |
||||||
|
Map<Class<?>, Class<?>> primToWrap = new HashMap<>(16); |
||||||
|
|
||||||
|
primToWrap.put(boolean.class, Boolean.class); |
||||||
|
primToWrap.put(byte.class, Byte.class); |
||||||
|
primToWrap.put(char.class, Character.class); |
||||||
|
primToWrap.put(double.class, Double.class); |
||||||
|
primToWrap.put(float.class, Float.class); |
||||||
|
primToWrap.put(int.class, Integer.class); |
||||||
|
primToWrap.put(long.class, Long.class); |
||||||
|
primToWrap.put(short.class, Short.class); |
||||||
|
primToWrap.put(void.class, Void.class); |
||||||
|
|
||||||
|
PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the corresponding wrapper type of {@code type} if it is a primitive type; otherwise |
||||||
|
* returns {@code type} itself. Idempotent. |
||||||
|
* <p> |
||||||
|
* <pre> |
||||||
|
* wrap(int.class) == Integer.class |
||||||
|
* wrap(Integer.class) == Integer.class |
||||||
|
* wrap(String.class) == String.class |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
public static <T> Class<T> wrap(Class<T> type) { |
||||||
|
if (type == null) { |
||||||
|
throw new IllegalArgumentException("type is null"); |
||||||
|
} |
||||||
|
|
||||||
|
// cast is safe: long.class and Long.class are both of type Class<Long>
|
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(type); |
||||||
|
return (wrapped == null) ? type : wrapped; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
package com.jme3.anim.util; |
||||||
|
|
||||||
|
import com.jme3.anim.tween.action.Action; |
||||||
|
import com.jme3.math.Transform; |
||||||
|
|
||||||
|
public interface Weighted { |
||||||
|
|
||||||
|
// public void setWeight(float weight);
|
||||||
|
|
||||||
|
public void setParentAction(Action action); |
||||||
|
} |
@ -0,0 +1,100 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2012 jMonkeyEngine |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* * Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in the |
||||||
|
* documentation and/or other materials provided with the distribution. |
||||||
|
* |
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
|
* may be used to endorse or promote products derived from this software |
||||||
|
* without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package com.jme3.animation; |
||||||
|
|
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Serialize and compress Float by indexing similar values |
||||||
|
* @author Lim, YongHoon |
||||||
|
*/ |
||||||
|
public class CompactFloatArray extends CompactArray<Float> implements Savable { |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a compact vector array |
||||||
|
*/ |
||||||
|
public CompactFloatArray() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* creates a compact vector array |
||||||
|
* @param dataArray the data array |
||||||
|
* @param index the indices |
||||||
|
*/ |
||||||
|
public CompactFloatArray(float[] dataArray, int[] index) { |
||||||
|
super(dataArray, index); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected final int getTupleSize() { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected final Class<Float> getElementClass() { |
||||||
|
return Float.class; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
serialize(); |
||||||
|
OutputCapsule out = ex.getCapsule(this); |
||||||
|
out.write(array, "array", null); |
||||||
|
out.write(index, "index", null); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule in = im.getCapsule(this); |
||||||
|
array = in.readFloatArray("array", null); |
||||||
|
index = in.readIntArray("index", null); |
||||||
|
} |
||||||
|
|
||||||
|
public void fill(int startIndex, float[] store ){ |
||||||
|
for (int i = 0; i < store.length; i++) { |
||||||
|
store[i] = get(startIndex + i, null); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void serialize(int i, Float data) { |
||||||
|
array[i] = data; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected Float deserialize(int i, Float store) { |
||||||
|
return array[i]; |
||||||
|
} |
||||||
|
} |
@ -1,214 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2009-2015 jMonkeyEngine |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* |
|
||||||
* * Redistributions in binary form must reproduce the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer in the |
|
||||||
* documentation and/or other materials provided with the distribution. |
|
||||||
* |
|
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
|
||||||
* may be used to endorse or promote products derived from this software |
|
||||||
* without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
*/ |
|
||||||
package com.jme3.light; |
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingSphere; |
|
||||||
import com.jme3.post.SceneProcessor; |
|
||||||
import com.jme3.profile.AppProfiler; |
|
||||||
import com.jme3.renderer.RenderManager; |
|
||||||
import com.jme3.renderer.ViewPort; |
|
||||||
import com.jme3.renderer.queue.RenderQueue; |
|
||||||
import com.jme3.scene.Spatial; |
|
||||||
import com.jme3.texture.FrameBuffer; |
|
||||||
import com.jme3.util.TempVars; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.Collections; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
/** |
|
||||||
* this processor allows to blend several light probes maps together according to a Point of Interest. |
|
||||||
* This is all based on this article by Sebastien lagarde |
|
||||||
* https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
|
|
||||||
* @author Nehon |
|
||||||
*/ |
|
||||||
public class LightProbeBlendingProcessor implements SceneProcessor { |
|
||||||
|
|
||||||
private ViewPort viewPort; |
|
||||||
private LightFilter prevFilter; |
|
||||||
private RenderManager renderManager; |
|
||||||
private LightProbe probe = new LightProbe(); |
|
||||||
private Spatial poi; |
|
||||||
private AppProfiler prof; |
|
||||||
|
|
||||||
public LightProbeBlendingProcessor(Spatial poi) { |
|
||||||
this.poi = poi; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void initialize(RenderManager rm, ViewPort vp) { |
|
||||||
viewPort = vp; |
|
||||||
renderManager = rm; |
|
||||||
prevFilter = rm.getLightFilter(); |
|
||||||
rm.setLightFilter(new PoiLightProbeLightFilter(this)); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void reshape(ViewPort vp, int w, int h) { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean isInitialized() { |
|
||||||
return viewPort != null; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void preFrame(float tpf) { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/** 1. For POI take a spatial in the constructor and make all calculation against its world pos |
|
||||||
* - Alternatively compute an arbitrary POI by casting rays from the camera |
|
||||||
* (one in the center and one for each corner and take the median point) |
|
||||||
* 2. Take the 4 most weighted probes for default. Maybe allow the user to change this |
|
||||||
* 3. For the inner influence radius take half of the radius for a start we'll see then how to change this. |
|
||||||
* |
|
||||||
*/ |
|
||||||
@Override |
|
||||||
public void postQueue(RenderQueue rq) { |
|
||||||
List<BlendFactor> blendFactors = new ArrayList<BlendFactor>(); |
|
||||||
float sumBlendFactors = computeBlendFactors(blendFactors); |
|
||||||
|
|
||||||
//Sort blend factors according to their weight
|
|
||||||
Collections.sort(blendFactors); |
|
||||||
|
|
||||||
//normalize blend factors;
|
|
||||||
float normalizer = 1f / sumBlendFactors; |
|
||||||
for (BlendFactor blendFactor : blendFactors) { |
|
||||||
blendFactor.ndf *= normalizer; |
|
||||||
// System.err.println(blendFactor);
|
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
//for now just pick the first probe.
|
|
||||||
if(!blendFactors.isEmpty()){ |
|
||||||
probe = blendFactors.get(0).lightProbe; |
|
||||||
}else{ |
|
||||||
probe = null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private float computeBlendFactors(List<BlendFactor> blendFactors) { |
|
||||||
float sumBlendFactors = 0; |
|
||||||
for (Spatial scene : viewPort.getScenes()) { |
|
||||||
for (Light light : scene.getWorldLightList()) { |
|
||||||
if(light.getType() == Light.Type.Probe){ |
|
||||||
LightProbe p = (LightProbe)light; |
|
||||||
TempVars vars = TempVars.get(); |
|
||||||
boolean intersect = p.intersectsFrustum(viewPort.getCamera(), vars); |
|
||||||
vars.release(); |
|
||||||
//check if the probe is inside the camera frustum
|
|
||||||
if(intersect){ |
|
||||||
|
|
||||||
//is the poi inside the bounds of this probe
|
|
||||||
if(poi.getWorldBound().intersects(p.getBounds())){ |
|
||||||
|
|
||||||
//computing the distance as we need it to check if th epoi in in the inner radius and later to compute the weight
|
|
||||||
float outerRadius = ((BoundingSphere)p.getBounds()).getRadius(); |
|
||||||
float innerRadius = outerRadius * 0.5f; |
|
||||||
float distance = p.getBounds().getCenter().distance(poi.getWorldTranslation()); |
|
||||||
|
|
||||||
// if the poi in inside the inner range of this probe, then this probe is the only one that matters.
|
|
||||||
if( distance < innerRadius ){ |
|
||||||
blendFactors.clear(); |
|
||||||
blendFactors.add(new BlendFactor(p, 1.0f)); |
|
||||||
return 1.0f; |
|
||||||
} |
|
||||||
//else we need to compute the weight of this probe and collect it for blending
|
|
||||||
float ndf = (distance - innerRadius) / (outerRadius - innerRadius); |
|
||||||
sumBlendFactors += ndf; |
|
||||||
blendFactors.add(new BlendFactor(p, ndf)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
return sumBlendFactors; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void postFrame(FrameBuffer out) { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void cleanup() { |
|
||||||
viewPort = null; |
|
||||||
renderManager.setLightFilter(prevFilter); |
|
||||||
} |
|
||||||
|
|
||||||
public void populateProbe(LightList lightList){ |
|
||||||
if(probe != null && probe.isReady()){ |
|
||||||
lightList.add(probe); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public Spatial getPoi() { |
|
||||||
return poi; |
|
||||||
} |
|
||||||
|
|
||||||
public void setPoi(Spatial poi) { |
|
||||||
this.poi = poi; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void setProfiler(AppProfiler profiler) { |
|
||||||
this.prof = profiler; |
|
||||||
} |
|
||||||
|
|
||||||
private class BlendFactor implements Comparable<BlendFactor>{ |
|
||||||
|
|
||||||
LightProbe lightProbe; |
|
||||||
float ndf; |
|
||||||
|
|
||||||
public BlendFactor(LightProbe lightProbe, float ndf) { |
|
||||||
this.lightProbe = lightProbe; |
|
||||||
this.ndf = ndf; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "BlendFactor{" + "lightProbe=" + lightProbe + ", ndf=" + ndf + '}'; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int compareTo(BlendFactor o) { |
|
||||||
if(o.ndf > ndf){ |
|
||||||
return -1; |
|
||||||
}else if(o.ndf < ndf){ |
|
||||||
return 1; |
|
||||||
} |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,254 @@ |
|||||||
|
package com.jme3.light; |
||||||
|
|
||||||
|
import com.jme3.bounding.BoundingBox; |
||||||
|
import com.jme3.bounding.BoundingSphere; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.math.*; |
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.util.TempVars; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
public class OrientedBoxProbeArea implements ProbeArea { |
||||||
|
private Transform transform = new Transform(); |
||||||
|
|
||||||
|
/** |
||||||
|
* @see LightProbe#getUniformMatrix() |
||||||
|
* for this Area type, the matrix is updated when the probe is transformed, |
||||||
|
* and its data is used for bound checks in the light culling process. |
||||||
|
*/ |
||||||
|
private Matrix4f uniformMatrix = new Matrix4f(); |
||||||
|
|
||||||
|
public OrientedBoxProbeArea() { |
||||||
|
} |
||||||
|
|
||||||
|
public OrientedBoxProbeArea(Transform transform) { |
||||||
|
transform.set(transform); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsBox(BoundingBox box, TempVars vars) { |
||||||
|
|
||||||
|
Vector3f axis1 = getScaledAxis(0, vars.vect1); |
||||||
|
Vector3f axis2 = getScaledAxis(1, vars.vect2); |
||||||
|
Vector3f axis3 = getScaledAxis(2, vars.vect3); |
||||||
|
|
||||||
|
Vector3f tn = vars.vect4; |
||||||
|
Plane p = vars.plane; |
||||||
|
Vector3f c = box.getCenter(); |
||||||
|
|
||||||
|
p.setNormal(0, 0, -1); |
||||||
|
p.setConstant(-(c.z + box.getZExtent())); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
p.setNormal(0, 0, 1); |
||||||
|
p.setConstant(c.z - box.getZExtent()); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
|
||||||
|
p.setNormal(0, -1, 0); |
||||||
|
p.setConstant(-(c.y + box.getYExtent())); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
p.setNormal(0, 1, 0); |
||||||
|
p.setConstant(c.y - box.getYExtent()); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
p.setNormal(-1, 0, 0); |
||||||
|
p.setConstant(-(c.x + box.getXExtent())); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
p.setNormal(1, 0, 0); |
||||||
|
p.setConstant(c.x - box.getXExtent()); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
|
||||||
|
return true; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float getRadius() { |
||||||
|
return Math.max(Math.max(transform.getScale().x, transform.getScale().y), transform.getScale().z); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setRadius(float radius) { |
||||||
|
transform.setScale(radius, radius, radius); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) { |
||||||
|
|
||||||
|
Vector3f closestPoint = getClosestPoint(vars, sphere.getCenter()); |
||||||
|
// check if the point intersects with the sphere bound
|
||||||
|
if (sphere.intersects(closestPoint)) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsFrustum(Camera camera, TempVars vars) { |
||||||
|
|
||||||
|
// extract the scaled axis
|
||||||
|
// this allows a small optimization.
|
||||||
|
Vector3f axis1 = getScaledAxis(0, vars.vect1); |
||||||
|
Vector3f axis2 = getScaledAxis(1, vars.vect2); |
||||||
|
Vector3f axis3 = getScaledAxis(2, vars.vect3); |
||||||
|
|
||||||
|
Vector3f tn = vars.vect4; |
||||||
|
|
||||||
|
for (int i = 5; i >= 0; i--) { |
||||||
|
Plane p = camera.getWorldPlane(i); |
||||||
|
if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private Vector3f getScaledAxis(int index, Vector3f store) { |
||||||
|
Matrix4f u = uniformMatrix; |
||||||
|
float x = 0, y = 0, z = 0, s = 1; |
||||||
|
switch (index) { |
||||||
|
case 0: |
||||||
|
x = u.m00; |
||||||
|
y = u.m10; |
||||||
|
z = u.m20; |
||||||
|
s = u.m30; |
||||||
|
break; |
||||||
|
case 1: |
||||||
|
x = u.m01; |
||||||
|
y = u.m11; |
||||||
|
z = u.m21; |
||||||
|
s = u.m31; |
||||||
|
break; |
||||||
|
case 2: |
||||||
|
x = u.m02; |
||||||
|
y = u.m12; |
||||||
|
z = u.m22; |
||||||
|
s = u.m32; |
||||||
|
} |
||||||
|
return store.set(x, y, z).multLocal(s); |
||||||
|
} |
||||||
|
|
||||||
|
private boolean insidePlane(Plane p, Vector3f axis1, Vector3f axis2, Vector3f axis3, Vector3f tn) { |
||||||
|
// transform the plane normal in the box local space.
|
||||||
|
tn.set(axis1.dot(p.getNormal()), axis2.dot(p.getNormal()), axis3.dot(p.getNormal())); |
||||||
|
|
||||||
|
// distance check
|
||||||
|
float radius = FastMath.abs(tn.x) + |
||||||
|
FastMath.abs(tn.y) + |
||||||
|
FastMath.abs(tn.z); |
||||||
|
|
||||||
|
float distance = p.pseudoDistance(transform.getTranslation()); |
||||||
|
|
||||||
|
if (distance < -radius) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private Vector3f getClosestPoint(TempVars vars, Vector3f point) { |
||||||
|
// non normalized direction
|
||||||
|
Vector3f dir = vars.vect2.set(point).subtractLocal(transform.getTranslation()); |
||||||
|
// initialize the closest point with box center
|
||||||
|
Vector3f closestPoint = vars.vect3.set(transform.getTranslation()); |
||||||
|
|
||||||
|
//store extent in an array
|
||||||
|
float[] r = vars.fWdU; |
||||||
|
r[0] = transform.getScale().x; |
||||||
|
r[1] = transform.getScale().y; |
||||||
|
r[2] = transform.getScale().z; |
||||||
|
|
||||||
|
// computing closest point to sphere center
|
||||||
|
for (int i = 0; i < 3; i++) { |
||||||
|
// extract the axis from the 3x3 matrix
|
||||||
|
Vector3f axis = getScaledAxis(i, vars.vect1); |
||||||
|
// nomalize (here we just divide by the extent
|
||||||
|
axis.divideLocal(r[i]); |
||||||
|
// distance to the closest point on this axis.
|
||||||
|
float d = FastMath.clamp(dir.dot(axis), -r[i], r[i]); |
||||||
|
closestPoint.addLocal(vars.vect4.set(axis).multLocal(d)); |
||||||
|
} |
||||||
|
return closestPoint; |
||||||
|
} |
||||||
|
|
||||||
|
private void updateMatrix() { |
||||||
|
TempVars vars = TempVars.get(); |
||||||
|
Matrix3f r = vars.tempMat3; |
||||||
|
Matrix4f u = uniformMatrix; |
||||||
|
transform.getRotation().toRotationMatrix(r); |
||||||
|
|
||||||
|
u.m00 = r.get(0,0); |
||||||
|
u.m10 = r.get(1,0); |
||||||
|
u.m20 = r.get(2,0); |
||||||
|
u.m01 = r.get(0,1); |
||||||
|
u.m11 = r.get(1,1); |
||||||
|
u.m21 = r.get(2,1); |
||||||
|
u.m02 = r.get(0,2); |
||||||
|
u.m12 = r.get(1,2); |
||||||
|
u.m22 = r.get(2,2); |
||||||
|
|
||||||
|
//scale
|
||||||
|
u.m30 = transform.getScale().x; |
||||||
|
u.m31 = transform.getScale().y; |
||||||
|
u.m32 = transform.getScale().z; |
||||||
|
|
||||||
|
//position
|
||||||
|
u.m03 = transform.getTranslation().x; |
||||||
|
u.m13 = transform.getTranslation().y; |
||||||
|
u.m23 = transform.getTranslation().z; |
||||||
|
|
||||||
|
vars.release(); |
||||||
|
} |
||||||
|
|
||||||
|
public Matrix4f getUniformMatrix() { |
||||||
|
return uniformMatrix; |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getExtent() { |
||||||
|
return transform.getScale(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setExtent(Vector3f extent) { |
||||||
|
transform.setScale(extent); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getCenter() { |
||||||
|
return transform.getTranslation(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setCenter(Vector3f center) { |
||||||
|
transform.setTranslation(center); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
public Quaternion getRotation() { |
||||||
|
return transform.getRotation(); |
||||||
|
} |
||||||
|
|
||||||
|
public void setRotation(Quaternion rotation) { |
||||||
|
transform.setRotation(rotation); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected OrientedBoxProbeArea clone() throws CloneNotSupportedException { |
||||||
|
return new OrientedBoxProbeArea(transform); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter e) throws IOException { |
||||||
|
OutputCapsule oc = e.getCapsule(this); |
||||||
|
oc.write(transform, "transform", new Transform()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter i) throws IOException { |
||||||
|
InputCapsule ic = i.getCapsule(this); |
||||||
|
transform = (Transform) ic.readSavable("transform", new Transform()); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,107 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright (c) 2009-2015 jMonkeyEngine |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* |
|
||||||
* * Redistributions in binary form must reproduce the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer in the |
|
||||||
* documentation and/or other materials provided with the distribution. |
|
||||||
* |
|
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
|
||||||
* may be used to endorse or promote products derived from this software |
|
||||||
* without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
*/ |
|
||||||
package com.jme3.light; |
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox; |
|
||||||
import com.jme3.bounding.BoundingSphere; |
|
||||||
import com.jme3.bounding.BoundingVolume; |
|
||||||
import com.jme3.renderer.Camera; |
|
||||||
import com.jme3.scene.Geometry; |
|
||||||
import com.jme3.util.TempVars; |
|
||||||
import java.util.HashSet; |
|
||||||
|
|
||||||
public final class PoiLightProbeLightFilter implements LightFilter { |
|
||||||
|
|
||||||
private Camera camera; |
|
||||||
private final HashSet<Light> processedLights = new HashSet<Light>(); |
|
||||||
private final LightProbeBlendingProcessor processor; |
|
||||||
|
|
||||||
public PoiLightProbeLightFilter(LightProbeBlendingProcessor processor) { |
|
||||||
this.processor = processor; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void setCamera(Camera camera) { |
|
||||||
this.camera = camera; |
|
||||||
for (Light light : processedLights) { |
|
||||||
light.frustumCheckNeeded = true; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void filterLights(Geometry geometry, LightList filteredLightList) { |
|
||||||
TempVars vars = TempVars.get(); |
|
||||||
try { |
|
||||||
LightList worldLights = geometry.getWorldLightList(); |
|
||||||
|
|
||||||
for (int i = 0; i < worldLights.size(); i++) { |
|
||||||
Light light = worldLights.get(i); |
|
||||||
|
|
||||||
if (light.getType() == Light.Type.Probe) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
if (light.frustumCheckNeeded) { |
|
||||||
processedLights.add(light); |
|
||||||
light.frustumCheckNeeded = false; |
|
||||||
light.intersectsFrustum = light.intersectsFrustum(camera, vars); |
|
||||||
} |
|
||||||
|
|
||||||
if (!light.intersectsFrustum) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
BoundingVolume bv = geometry.getWorldBound(); |
|
||||||
|
|
||||||
if (bv instanceof BoundingBox) { |
|
||||||
if (!light.intersectsBox((BoundingBox) bv, vars)) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
} else if (bv instanceof BoundingSphere) { |
|
||||||
if (!Float.isInfinite(((BoundingSphere) bv).getRadius())) { |
|
||||||
if (!light.intersectsSphere((BoundingSphere) bv, vars)) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
filteredLightList.add(light); |
|
||||||
} |
|
||||||
|
|
||||||
processor.populateProbe(filteredLightList); |
|
||||||
|
|
||||||
} finally { |
|
||||||
vars.release(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,35 @@ |
|||||||
|
package com.jme3.light; |
||||||
|
|
||||||
|
import com.jme3.bounding.BoundingBox; |
||||||
|
import com.jme3.bounding.BoundingSphere; |
||||||
|
import com.jme3.export.Savable; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.util.TempVars; |
||||||
|
|
||||||
|
public interface ProbeArea extends Savable, Cloneable{ |
||||||
|
|
||||||
|
public void setCenter(Vector3f center); |
||||||
|
|
||||||
|
public float getRadius(); |
||||||
|
|
||||||
|
public void setRadius(float radius); |
||||||
|
|
||||||
|
public Matrix4f getUniformMatrix(); |
||||||
|
|
||||||
|
/** |
||||||
|
* @see Light#intersectsBox(BoundingBox, TempVars) |
||||||
|
*/ |
||||||
|
public boolean intersectsBox(BoundingBox box, TempVars vars); |
||||||
|
|
||||||
|
/** |
||||||
|
* @see Light#intersectsSphere(BoundingSphere, TempVars) |
||||||
|
*/ |
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars); |
||||||
|
|
||||||
|
/** |
||||||
|
* @see Light#intersectsFrustum(Camera, TempVars) |
||||||
|
*/ |
||||||
|
public abstract boolean intersectsFrustum(Camera camera, TempVars vars); |
||||||
|
} |
@ -0,0 +1,103 @@ |
|||||||
|
package com.jme3.light; |
||||||
|
|
||||||
|
import com.jme3.bounding.*; |
||||||
|
import com.jme3.export.*; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.util.TempVars; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.logging.Level; |
||||||
|
|
||||||
|
public class SphereProbeArea implements ProbeArea { |
||||||
|
|
||||||
|
private Vector3f center = new Vector3f(); |
||||||
|
private float radius = 1; |
||||||
|
private Matrix4f uniformMatrix = new Matrix4f(); |
||||||
|
|
||||||
|
public SphereProbeArea() { |
||||||
|
} |
||||||
|
|
||||||
|
public SphereProbeArea(Vector3f center, float radius) { |
||||||
|
this.center.set(center); |
||||||
|
this.radius = radius; |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
public Vector3f getCenter() { |
||||||
|
return center; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCenter(Vector3f center) { |
||||||
|
this.center.set(center); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
public float getRadius() { |
||||||
|
return radius; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setRadius(float radius) { |
||||||
|
this.radius = radius; |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Matrix4f getUniformMatrix() { |
||||||
|
return uniformMatrix; |
||||||
|
} |
||||||
|
|
||||||
|
private void updateMatrix(){ |
||||||
|
//position
|
||||||
|
uniformMatrix.m03 = center.x; |
||||||
|
uniformMatrix.m13 = center.y; |
||||||
|
uniformMatrix.m23 = center.z; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsBox(BoundingBox box, TempVars vars) { |
||||||
|
return Intersection.intersect(box, center, radius); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) { |
||||||
|
return Intersection.intersect(sphere, center, radius); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean intersectsFrustum(Camera camera, TempVars vars) { |
||||||
|
return Intersection.intersect(camera, center, radius); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "SphereProbeArea{" + |
||||||
|
"center=" + center + |
||||||
|
", radius=" + radius + |
||||||
|
'}'; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected SphereProbeArea clone() throws CloneNotSupportedException { |
||||||
|
return new SphereProbeArea(center, radius); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter e) throws IOException { |
||||||
|
OutputCapsule oc = e.getCapsule(this); |
||||||
|
oc.write(center, "center", new Vector3f()); |
||||||
|
oc.write(radius, "radius", 1); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter i) throws IOException { |
||||||
|
InputCapsule ic = i.getCapsule(this); |
||||||
|
center = (Vector3f) ic.readSavable("center", new Vector3f()); |
||||||
|
radius = ic.readFloat("radius", 1); |
||||||
|
updateMatrix(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,77 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2015 jMonkeyEngine |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* * Redistributions in binary form must reproduce the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer in the |
||||||
|
* documentation and/or other materials provided with the distribution. |
||||||
|
* |
||||||
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
|
* may be used to endorse or promote products derived from this software |
||||||
|
* without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package com.jme3.light; |
||||||
|
|
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* This strategy returns the 3 closest probe from the rendered object. |
||||||
|
* <p> |
||||||
|
* Image based lighting will be blended between those probes in the shader according to their distance and range. |
||||||
|
* |
||||||
|
* @author Nehon |
||||||
|
*/ |
||||||
|
public class WeightedProbeBlendingStrategy implements LightProbeBlendingStrategy { |
||||||
|
|
||||||
|
private final static int MAX_PROBES = 3; |
||||||
|
List<LightProbe> lightProbes = new ArrayList<LightProbe>(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void registerProbe(LightProbe probe) { |
||||||
|
lightProbes.add(probe); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void populateProbes(Geometry g, LightList lightList) { |
||||||
|
if (!lightProbes.isEmpty()) { |
||||||
|
//The 3 first probes are the closest to the geometry since the
|
||||||
|
//light list is sorted according to the distance to the geom.
|
||||||
|
int addedProbes = 0; |
||||||
|
for (LightProbe p : lightProbes) { |
||||||
|
if (p.isReady() && p.isEnabled()) { |
||||||
|
lightList.add(p); |
||||||
|
addedProbes ++; |
||||||
|
} |
||||||
|
if (addedProbes == MAX_PROBES) { |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//clearing the list for next pass.
|
||||||
|
lightProbes.clear(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
package com.jme3.math; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 26/03/2017. |
||||||
|
*/ |
||||||
|
public interface EaseFunction { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param value a value from 0 to 1. Passing a value out of this range will have unexpected behavior. |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
float apply(float value); |
||||||
|
} |
@ -0,0 +1,163 @@ |
|||||||
|
package com.jme3.math; |
||||||
|
|
||||||
|
/** |
||||||
|
* Expose several Easing function from Robert Penner |
||||||
|
* Created by Nehon on 26/03/2017. |
||||||
|
*/ |
||||||
|
public class Easing { |
||||||
|
|
||||||
|
|
||||||
|
public static EaseFunction constant = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
}; |
||||||
|
/** |
||||||
|
* In |
||||||
|
*/ |
||||||
|
public static EaseFunction linear = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return value; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction inQuad = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return value * value; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction inCubic = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return value * value * value; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction inQuart = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return value * value * value * value; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction inQuint = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return value * value * value * value * value; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Out Elastic and bounce |
||||||
|
*/ |
||||||
|
public static EaseFunction outElastic = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return FastMath.pow(2f, -10f * value) * FastMath.sin((value - 0.3f / 4f) * (2f * FastMath.PI) / 0.3f) + 1f; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction outBounce = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
if (value < (1f / 2.75f)) { |
||||||
|
return (7.5625f * value * value); |
||||||
|
} else if (value < (2f / 2.75f)) { |
||||||
|
return (7.5625f * (value -= (1.5f / 2.75f)) * value + 0.75f); |
||||||
|
} else if (value < (2.5 / 2.75)) { |
||||||
|
return (7.5625f * (value -= (2.25f / 2.75f)) * value + 0.9375f); |
||||||
|
} else { |
||||||
|
return (7.5625f * (value -= (2.625f / 2.75f)) * value + 0.984375f); |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
/** |
||||||
|
* In Elastic and bounce |
||||||
|
*/ |
||||||
|
public static EaseFunction inElastic = new Invert(outElastic); |
||||||
|
public static EaseFunction inBounce = new Invert(outBounce); |
||||||
|
|
||||||
|
/** |
||||||
|
* Out |
||||||
|
*/ |
||||||
|
public static EaseFunction outQuad = new Invert(inQuad); |
||||||
|
public static EaseFunction outCubic = new Invert(inCubic); |
||||||
|
public static EaseFunction outQuart = new Invert(inQuart); |
||||||
|
public static EaseFunction outQuint = new Invert(inQuint); |
||||||
|
|
||||||
|
/** |
||||||
|
* inOut |
||||||
|
*/ |
||||||
|
public static EaseFunction inOutQuad = new InOut(inQuad, outQuad); |
||||||
|
public static EaseFunction inOutCubic = new InOut(inCubic, outCubic); |
||||||
|
public static EaseFunction inOutQuart = new InOut(inQuart, outQuart); |
||||||
|
public static EaseFunction inOutQuint = new InOut(inQuint, outQuint); |
||||||
|
public static EaseFunction inOutElastic = new InOut(inElastic, outElastic); |
||||||
|
public static EaseFunction inOutBounce = new InOut(inBounce, outBounce); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Extra functions |
||||||
|
*/ |
||||||
|
|
||||||
|
public static EaseFunction smoothStep = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float t) { |
||||||
|
return t * t * (3f - 2f * t); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
public static EaseFunction smootherStep = new EaseFunction() { |
||||||
|
@Override |
||||||
|
public float apply(float t) { |
||||||
|
return t * t * t * (t * (t * 6f - 15f) + 10f); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
/** |
||||||
|
* An Ease function composed of 2 sb function for custom in and out easing |
||||||
|
*/ |
||||||
|
public static class InOut implements EaseFunction { |
||||||
|
|
||||||
|
private EaseFunction in; |
||||||
|
private EaseFunction out; |
||||||
|
|
||||||
|
public InOut(EaseFunction in, EaseFunction out) { |
||||||
|
this.in = in; |
||||||
|
this.out = out; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
if (value < 0.5) { |
||||||
|
value = value * 2; |
||||||
|
return inQuad.apply(value) / 2; |
||||||
|
} else { |
||||||
|
value = (value - 0.5f) * 2; |
||||||
|
return outQuad.apply(value) / 2 + 0.5f; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static class Invert implements EaseFunction { |
||||||
|
|
||||||
|
private EaseFunction func; |
||||||
|
|
||||||
|
public Invert(EaseFunction func) { |
||||||
|
this.func = func; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float apply(float value) { |
||||||
|
return 1f - func.apply(1f - value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,247 @@ |
|||||||
|
package com.jme3.math; |
||||||
|
|
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.util.TempVars; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 23/04/2017. |
||||||
|
*/ |
||||||
|
public class MathUtils { |
||||||
|
|
||||||
|
public static Quaternion log(Quaternion q, Quaternion store) { |
||||||
|
float a = FastMath.acos(q.w); |
||||||
|
float sina = FastMath.sin(a); |
||||||
|
|
||||||
|
store.w = 0; |
||||||
|
if (sina > 0) { |
||||||
|
store.x = a * q.x / sina; |
||||||
|
store.y = a * q.y / sina; |
||||||
|
store.z = a * q.z / sina; |
||||||
|
} else { |
||||||
|
store.x = 0; |
||||||
|
store.y = 0; |
||||||
|
store.z = 0; |
||||||
|
} |
||||||
|
return store; |
||||||
|
} |
||||||
|
|
||||||
|
public static Quaternion exp(Quaternion q, Quaternion store) { |
||||||
|
|
||||||
|
float len = FastMath.sqrt(q.x * q.x + q.y * q.y + q.z * q.z); |
||||||
|
float sinLen = FastMath.sin(len); |
||||||
|
float cosLen = FastMath.cos(len); |
||||||
|
|
||||||
|
store.w = cosLen; |
||||||
|
if (len > 0) { |
||||||
|
store.x = sinLen * q.x / len; |
||||||
|
store.y = sinLen * q.y / len; |
||||||
|
store.z = sinLen * q.z / len; |
||||||
|
} else { |
||||||
|
store.x = 0; |
||||||
|
store.y = 0; |
||||||
|
store.z = 0; |
||||||
|
} |
||||||
|
return store; |
||||||
|
} |
||||||
|
|
||||||
|
//! This version of slerp, used by squad, does not check for theta > 90.
|
||||||
|
public static Quaternion slerpNoInvert(Quaternion q1, Quaternion q2, float t, Quaternion store) { |
||||||
|
float dot = q1.dot(q2); |
||||||
|
|
||||||
|
if (dot > -0.95f && dot < 0.95f) { |
||||||
|
float angle = FastMath.acos(dot); |
||||||
|
float sin1 = FastMath.sin(angle * (1 - t)); |
||||||
|
float sin2 = FastMath.sin(angle * t); |
||||||
|
float sin3 = FastMath.sin(angle); |
||||||
|
store.x = (q1.x * sin1 + q2.x * sin2) / sin3; |
||||||
|
store.y = (q1.y * sin1 + q2.y * sin2) / sin3; |
||||||
|
store.z = (q1.z * sin1 + q2.z * sin2) / sin3; |
||||||
|
store.w = (q1.w * sin1 + q2.w * sin2) / sin3; |
||||||
|
System.err.println("real slerp"); |
||||||
|
} else { |
||||||
|
// if the angle is small, use linear interpolation
|
||||||
|
store.set(q1).nlerp(q2, t); |
||||||
|
System.err.println("nlerp"); |
||||||
|
} |
||||||
|
return store; |
||||||
|
} |
||||||
|
|
||||||
|
public static Quaternion slerp(Quaternion q1, Quaternion q2, float t, Quaternion store) { |
||||||
|
|
||||||
|
float dot = (q1.x * q2.x) + (q1.y * q2.y) + (q1.z * q2.z) |
||||||
|
+ (q1.w * q2.w); |
||||||
|
|
||||||
|
if (dot < 0.0f) { |
||||||
|
// Negate the second quaternion and the result of the dot product
|
||||||
|
q2.x = -q2.x; |
||||||
|
q2.y = -q2.y; |
||||||
|
q2.z = -q2.z; |
||||||
|
q2.w = -q2.w; |
||||||
|
dot = -dot; |
||||||
|
} |
||||||
|
|
||||||
|
// Set the first and second scale for the interpolation
|
||||||
|
float scale0 = 1 - t; |
||||||
|
float scale1 = t; |
||||||
|
|
||||||
|
// Check if the angle between the 2 quaternions was big enough to
|
||||||
|
// warrant such calculations
|
||||||
|
if (dot < 0.9f) {// Get the angle between the 2 quaternions,
|
||||||
|
// and then store the sin() of that angle
|
||||||
|
float theta = FastMath.acos(dot); |
||||||
|
float invSinTheta = 1f / FastMath.sin(theta); |
||||||
|
|
||||||
|
// Calculate the scale for q1 and q2, according to the angle and
|
||||||
|
// it's sine value
|
||||||
|
scale0 = FastMath.sin((1 - t) * theta) * invSinTheta; |
||||||
|
scale1 = FastMath.sin((t * theta)) * invSinTheta; |
||||||
|
|
||||||
|
// Calculate the x, y, z and w values for the quaternion by using a
|
||||||
|
// special
|
||||||
|
// form of linear interpolation for quaternions.
|
||||||
|
store.x = (scale0 * q1.x) + (scale1 * q2.x); |
||||||
|
store.y = (scale0 * q1.y) + (scale1 * q2.y); |
||||||
|
store.z = (scale0 * q1.z) + (scale1 * q2.z); |
||||||
|
store.w = (scale0 * q1.w) + (scale1 * q2.w); |
||||||
|
} else { |
||||||
|
store.x = (scale0 * q1.x) + (scale1 * q2.x); |
||||||
|
store.y = (scale0 * q1.y) + (scale1 * q2.y); |
||||||
|
store.z = (scale0 * q1.z) + (scale1 * q2.z); |
||||||
|
store.w = (scale0 * q1.w) + (scale1 * q2.w); |
||||||
|
store.normalizeLocal(); |
||||||
|
} |
||||||
|
// Return the interpolated quaternion
|
||||||
|
return store; |
||||||
|
} |
||||||
|
|
||||||
|
// //! Given 3 quaternions, qn-1,qn and qn+1, calculate a control point to be used in spline interpolation
|
||||||
|
// private static Quaternion spline(Quaternion qnm1, Quaternion qn, Quaternion qnp1, Quaternion store, Quaternion tmp) {
|
||||||
|
// store.set(-qn.x, -qn.y, -qn.z, qn.w);
|
||||||
|
// //store.set(qn).inverseLocal();
|
||||||
|
// tmp.set(store);
|
||||||
|
//
|
||||||
|
// log(store.multLocal(qnm1), store);
|
||||||
|
// log(tmp.multLocal(qnp1), tmp);
|
||||||
|
// store.addLocal(tmp).multLocal(1f / -4f);
|
||||||
|
// exp(store, tmp);
|
||||||
|
// store.set(tmp).multLocal(qn);
|
||||||
|
//
|
||||||
|
// return store.normalizeLocal();
|
||||||
|
// //return qn * (((qni * qnm1).log() + (qni * qnp1).log()) / -4).exp();
|
||||||
|
// }
|
||||||
|
|
||||||
|
//! Given 3 quaternions, qn-1,qn and qn+1, calculate a control point to be used in spline interpolation
|
||||||
|
private static Quaternion spline(Quaternion qnm1, Quaternion qn, Quaternion qnp1, Quaternion store, Quaternion tmp) { |
||||||
|
Quaternion invQn = new Quaternion(-qn.x, -qn.y, -qn.z, qn.w); |
||||||
|
|
||||||
|
|
||||||
|
log(invQn.mult(qnp1), tmp); |
||||||
|
log(invQn.mult(qnm1), store); |
||||||
|
store.addLocal(tmp).multLocal(-1f / 4f); |
||||||
|
exp(store, tmp); |
||||||
|
store.set(qn).multLocal(tmp); |
||||||
|
|
||||||
|
return store.normalizeLocal(); |
||||||
|
//return qn * (((qni * qnm1).log() + (qni * qnp1).log()) / -4).exp();
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//! spherical cubic interpolation
|
||||||
|
public static Quaternion squad(Quaternion q0, Quaternion q1, Quaternion q2, Quaternion q3, Quaternion a, Quaternion b, float t, Quaternion store) { |
||||||
|
|
||||||
|
spline(q0, q1, q2, a, store); |
||||||
|
spline(q1, q2, q3, b, store); |
||||||
|
|
||||||
|
slerp(a, b, t, store); |
||||||
|
slerp(q1, q2, t, a); |
||||||
|
return slerp(a, store, 2 * t * (1 - t), b); |
||||||
|
//slerpNoInvert(a, b, t, store);
|
||||||
|
//slerpNoInvert(q1, q2, t, a);
|
||||||
|
//return slerpNoInvert(a, store, 2 * t * (1 - t), b);
|
||||||
|
|
||||||
|
// quaternion c = slerpNoInvert(q1, q2, t),
|
||||||
|
// d = slerpNoInvert(a, b, t);
|
||||||
|
// return slerpNoInvert(c, d, 2 * t * (1 - t));
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the shortest distance between a Ray and a segment. |
||||||
|
* The segment is defined by a start position and an end position in world space |
||||||
|
* The distance returned will be in world space (world units). |
||||||
|
* If the camera parameter is not null the distance will be returned in screen space (pixels) |
||||||
|
* |
||||||
|
* @param ray The ray |
||||||
|
* @param segStart The start position of the segment in world space |
||||||
|
* @param segEnd The end position of the segment in world space |
||||||
|
* @param camera The renderer camera if the distance is required in screen space. Null if the distance is required in world space |
||||||
|
* @return the shortest distance between the ray and the segment or -1 if no solution is found. |
||||||
|
*/ |
||||||
|
public static float raySegmentShortestDistance(Ray ray, Vector3f segStart, Vector3f segEnd, Camera camera) { |
||||||
|
// Algorithm is ported from the C algorithm of
|
||||||
|
// Paul Bourke at http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/
|
||||||
|
TempVars vars = TempVars.get(); |
||||||
|
Vector3f resultSegmentPoint1 = vars.vect1; |
||||||
|
Vector3f resultSegmentPoint2 = vars.vect2; |
||||||
|
|
||||||
|
Vector3f p1 = segStart; |
||||||
|
Vector3f p2 = segEnd; |
||||||
|
Vector3f p3 = ray.origin; |
||||||
|
Vector3f p4 = vars.vect3.set(ray.getDirection()).multLocal(Math.min(ray.getLimit(), 1000)).addLocal(ray.getOrigin()); |
||||||
|
Vector3f p13 = vars.vect4.set(p1).subtractLocal(p3); |
||||||
|
Vector3f p43 = vars.vect5.set(p4).subtractLocal(p3); |
||||||
|
|
||||||
|
if (p43.lengthSquared() < 0.0001) { |
||||||
|
vars.release(); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
Vector3f p21 = vars.vect6.set(p2).subtractLocal(p1); |
||||||
|
if (p21.lengthSquared() < 0.0001) { |
||||||
|
vars.release(); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
double d1343 = p13.x * (double) p43.x + (double) p13.y * p43.y + (double) p13.z * p43.z; |
||||||
|
double d4321 = p43.x * (double) p21.x + (double) p43.y * p21.y + (double) p43.z * p21.z; |
||||||
|
double d1321 = p13.x * (double) p21.x + (double) p13.y * p21.y + (double) p13.z * p21.z; |
||||||
|
double d4343 = p43.x * (double) p43.x + (double) p43.y * p43.y + (double) p43.z * p43.z; |
||||||
|
double d2121 = p21.x * (double) p21.x + (double) p21.y * p21.y + (double) p21.z * p21.z; |
||||||
|
|
||||||
|
double denom = d2121 * d4343 - d4321 * d4321; |
||||||
|
if (Math.abs(denom) < 0.0001) { |
||||||
|
vars.release(); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
double numer = d1343 * d4321 - d1321 * d4343; |
||||||
|
|
||||||
|
double mua = numer / denom; |
||||||
|
double mub = (d1343 + d4321 * (mua)) / d4343; |
||||||
|
|
||||||
|
resultSegmentPoint1.x = (float) (p1.x + mua * p21.x); |
||||||
|
resultSegmentPoint1.y = (float) (p1.y + mua * p21.y); |
||||||
|
resultSegmentPoint1.z = (float) (p1.z + mua * p21.z); |
||||||
|
resultSegmentPoint2.x = (float) (p3.x + mub * p43.x); |
||||||
|
resultSegmentPoint2.y = (float) (p3.y + mub * p43.y); |
||||||
|
resultSegmentPoint2.z = (float) (p3.z + mub * p43.z); |
||||||
|
|
||||||
|
//check if result 1 is in the segment section.
|
||||||
|
float startToPoint = vars.vect3.set(resultSegmentPoint1).subtractLocal(segStart).lengthSquared(); |
||||||
|
float endToPoint = vars.vect3.set(resultSegmentPoint1).subtractLocal(segEnd).lengthSquared(); |
||||||
|
float segLength = vars.vect3.set(segEnd).subtractLocal(segStart).lengthSquared(); |
||||||
|
if (startToPoint > segLength || endToPoint > segLength) { |
||||||
|
vars.release(); |
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
if (camera != null) { |
||||||
|
//camera is not null let's convert the points in screen space
|
||||||
|
camera.getScreenCoordinates(resultSegmentPoint1, resultSegmentPoint1); |
||||||
|
camera.getScreenCoordinates(resultSegmentPoint2, resultSegmentPoint2); |
||||||
|
} |
||||||
|
|
||||||
|
float length = resultSegmentPoint1.subtractLocal(resultSegmentPoint2).length(); |
||||||
|
vars.release(); |
||||||
|
return length; |
||||||
|
} |
||||||
|
|
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,208 @@ |
|||||||
|
/* |
||||||
|
* To change this template, choose Tools | Templates |
||||||
|
* and open the template in the editor. |
||||||
|
*/ |
||||||
|
package com.jme3.scene.debug.custom; |
||||||
|
|
||||||
|
import com.jme3.anim.*; |
||||||
|
import com.jme3.app.Application; |
||||||
|
import com.jme3.app.state.BaseAppState; |
||||||
|
import com.jme3.collision.CollisionResults; |
||||||
|
import com.jme3.input.KeyInput; |
||||||
|
import com.jme3.input.MouseInput; |
||||||
|
import com.jme3.input.controls.*; |
||||||
|
import com.jme3.light.DirectionalLight; |
||||||
|
import com.jme3.math.*; |
||||||
|
import com.jme3.renderer.ViewPort; |
||||||
|
import com.jme3.scene.*; |
||||||
|
|
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Nehon |
||||||
|
*/ |
||||||
|
public class ArmatureDebugAppState extends BaseAppState { |
||||||
|
|
||||||
|
public static final float CLICK_MAX_DELAY = 0.2f; |
||||||
|
private Node debugNode = new Node("debugNode"); |
||||||
|
private Map<Armature, ArmatureDebugger> armatures = new HashMap<>(); |
||||||
|
private Map<Armature, Joint> selectedBones = new HashMap<>(); |
||||||
|
private Application app; |
||||||
|
private boolean displayAllJoints = false; |
||||||
|
private float clickDelay = -1; |
||||||
|
Vector3f tmp = new Vector3f(); |
||||||
|
Vector3f tmp2 = new Vector3f(); |
||||||
|
ViewPort vp; |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void initialize(Application app) { |
||||||
|
vp = app.getRenderManager().createMainView("debug", app.getCamera()); |
||||||
|
vp.attachScene(debugNode); |
||||||
|
vp.setClearDepth(true); |
||||||
|
this.app = app; |
||||||
|
for (ArmatureDebugger armatureDebugger : armatures.values()) { |
||||||
|
armatureDebugger.initialize(app.getAssetManager(), app.getCamera()); |
||||||
|
} |
||||||
|
app.getInputManager().addListener(actionListener, "shoot", "toggleJoints"); |
||||||
|
app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT), new MouseButtonTrigger(MouseInput.BUTTON_RIGHT)); |
||||||
|
app.getInputManager().addMapping("toggleJoints", new KeyTrigger(KeyInput.KEY_F10)); |
||||||
|
|
||||||
|
debugNode.addLight(new DirectionalLight(new Vector3f(-1f, -1f, -1f).normalizeLocal())); |
||||||
|
|
||||||
|
debugNode.addLight(new DirectionalLight(new Vector3f(1f, 1f, 1f).normalizeLocal(), new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f))); |
||||||
|
vp.setEnabled(false); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void cleanup(Application app) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void onEnable() { |
||||||
|
vp.setEnabled(true); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void onDisable() { |
||||||
|
vp.setEnabled(false); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void update(float tpf) { |
||||||
|
if (clickDelay > -1) { |
||||||
|
clickDelay += tpf; |
||||||
|
} |
||||||
|
debugNode.updateLogicalState(tpf); |
||||||
|
debugNode.updateGeometricState(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public ArmatureDebugger addArmatureFrom(SkinningControl skinningControl) { |
||||||
|
Armature armature = skinningControl.getArmature(); |
||||||
|
Spatial forSpatial = skinningControl.getSpatial(); |
||||||
|
return addArmatureFrom(armature, forSpatial); |
||||||
|
} |
||||||
|
|
||||||
|
public ArmatureDebugger addArmatureFrom(Armature armature, Spatial forSpatial) { |
||||||
|
|
||||||
|
ArmatureDebugger ad = armatures.get(armature); |
||||||
|
if(ad != null){ |
||||||
|
return ad; |
||||||
|
} |
||||||
|
|
||||||
|
JointInfoVisitor visitor = new JointInfoVisitor(armature); |
||||||
|
forSpatial.depthFirstTraversal(visitor); |
||||||
|
|
||||||
|
ad = new ArmatureDebugger(forSpatial.getName() + "_Armature", armature, visitor.deformingJoints); |
||||||
|
ad.setLocalTransform(forSpatial.getWorldTransform()); |
||||||
|
if (forSpatial instanceof Node) { |
||||||
|
List<Geometry> geoms = new ArrayList<>(); |
||||||
|
findGeoms((Node) forSpatial, geoms); |
||||||
|
if (geoms.size() == 1) { |
||||||
|
ad.setLocalTransform(geoms.get(0).getWorldTransform()); |
||||||
|
} |
||||||
|
} |
||||||
|
armatures.put(armature, ad); |
||||||
|
debugNode.attachChild(ad); |
||||||
|
if (isInitialized()) { |
||||||
|
ad.initialize(app.getAssetManager(), app.getCamera()); |
||||||
|
} |
||||||
|
return ad; |
||||||
|
} |
||||||
|
|
||||||
|
private void findGeoms(Node node, List<Geometry> geoms) { |
||||||
|
for (Spatial spatial : node.getChildren()) { |
||||||
|
if (spatial instanceof Geometry) { |
||||||
|
geoms.add((Geometry) spatial); |
||||||
|
} else if (spatial instanceof Node) { |
||||||
|
findGeoms((Node) spatial, geoms); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private ActionListener actionListener = new ActionListener() { |
||||||
|
public void onAction(String name, boolean isPressed, float tpf) { |
||||||
|
if (name.equals("shoot") && isPressed) { |
||||||
|
clickDelay = 0; |
||||||
|
} |
||||||
|
if (name.equals("shoot") && !isPressed && clickDelay < CLICK_MAX_DELAY) { |
||||||
|
Vector2f click2d = app.getInputManager().getCursorPosition(); |
||||||
|
CollisionResults results = new CollisionResults(); |
||||||
|
|
||||||
|
Vector3f click3d = app.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f, tmp); |
||||||
|
Vector3f dir = app.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f, tmp2).subtractLocal(click3d); |
||||||
|
Ray ray = new Ray(click3d, dir); |
||||||
|
debugNode.collideWith(ray, results); |
||||||
|
|
||||||
|
if (results.size() == 0) { |
||||||
|
for (ArmatureDebugger ad : armatures.values()) { |
||||||
|
ad.select(null); |
||||||
|
} |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// The closest result is the target that the player picked:
|
||||||
|
Geometry target = results.getClosestCollision().getGeometry(); |
||||||
|
for (ArmatureDebugger ad : armatures.values()) { |
||||||
|
Joint selectedjoint = ad.select(target); |
||||||
|
if (selectedjoint != null) { |
||||||
|
selectedBones.put(ad.getArmature(), selectedjoint); |
||||||
|
System.err.println("-----------------------"); |
||||||
|
System.err.println("Selected Joint : " + selectedjoint.getName() + " in armature " + ad.getName()); |
||||||
|
System.err.println("Root Bone : " + (selectedjoint.getParent() == null)); |
||||||
|
System.err.println("-----------------------"); |
||||||
|
System.err.println("Local translation: " + selectedjoint.getLocalTranslation()); |
||||||
|
System.err.println("Local rotation: " + selectedjoint.getLocalRotation()); |
||||||
|
System.err.println("Local scale: " + selectedjoint.getLocalScale()); |
||||||
|
System.err.println("---"); |
||||||
|
System.err.println("Model translation: " + selectedjoint.getModelTransform().getTranslation()); |
||||||
|
System.err.println("Model rotation: " + selectedjoint.getModelTransform().getRotation()); |
||||||
|
System.err.println("Model scale: " + selectedjoint.getModelTransform().getScale()); |
||||||
|
System.err.println("---"); |
||||||
|
System.err.println("Bind inverse Transform: "); |
||||||
|
System.err.println(selectedjoint.getInverseModelBindMatrix()); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (name.equals("toggleJoints") && isPressed) { |
||||||
|
displayAllJoints = !displayAllJoints; |
||||||
|
for (ArmatureDebugger ad : armatures.values()) { |
||||||
|
ad.displayNonDeformingJoint(displayAllJoints); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
// public Map<Skeleton, Bone> getSelectedBones() {
|
||||||
|
// return selectedBones;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public Node getDebugNode() { |
||||||
|
return debugNode; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDebugNode(Node debugNode) { |
||||||
|
this.debugNode = debugNode; |
||||||
|
} |
||||||
|
|
||||||
|
private class JointInfoVisitor extends SceneGraphVisitorAdapter { |
||||||
|
|
||||||
|
List<Joint> deformingJoints = new ArrayList<>(); |
||||||
|
Armature armature; |
||||||
|
|
||||||
|
public JointInfoVisitor(Armature armature) { |
||||||
|
this.armature = armature; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void visit(Geometry g) { |
||||||
|
for (Joint joint : armature.getJointList()) { |
||||||
|
if (g.getMesh().isAnimatedByJoint(armature.getJointIndex(joint))) { |
||||||
|
deformingJoints.add(joint); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue