Search in sources :

Example 1 with Skeleton

use of com.jme3.animation.Skeleton in project jmonkeyengine by jMonkeyEngine.

the class BoneTrack method setTime.

/**
     * 
     * Modify the bone which this track modifies in the skeleton to contain
     * the correct animation transforms for a given time.
     * The transforms can be interpolated in some method from the keyframes.
     *
     * @param time the current time of the animation
     * @param weight the weight of the animation
     * @param control
     * @param channel
     * @param vars
     */
public void setTime(float time, float weight, AnimControl control, AnimChannel channel, TempVars vars) {
    BitSet affectedBones = channel.getAffectedBones();
    if (affectedBones != null && !affectedBones.get(targetBoneIndex)) {
        return;
    }
    Bone target = control.getSkeleton().getBone(targetBoneIndex);
    Vector3f tempV = vars.vect1;
    Vector3f tempS = vars.vect2;
    Quaternion tempQ = vars.quat1;
    Vector3f tempV2 = vars.vect3;
    Vector3f tempS2 = vars.vect4;
    Quaternion tempQ2 = vars.quat2;
    int lastFrame = times.length - 1;
    if (time < 0 || lastFrame == 0) {
        rotations.get(0, tempQ);
        translations.get(0, tempV);
        if (scales != null) {
            scales.get(0, tempS);
        }
    } else if (time >= times[lastFrame]) {
        rotations.get(lastFrame, tempQ);
        translations.get(lastFrame, tempV);
        if (scales != null) {
            scales.get(lastFrame, tempS);
        }
    } else {
        int startFrame = 0;
        int endFrame = 1;
        // use lastFrame so we never overflow the array
        int i;
        for (i = 0; i < lastFrame && times[i] < time; i++) {
            startFrame = i;
            endFrame = i + 1;
        }
        float blend = (time - times[startFrame]) / (times[endFrame] - times[startFrame]);
        rotations.get(startFrame, tempQ);
        translations.get(startFrame, tempV);
        if (scales != null) {
            scales.get(startFrame, tempS);
        }
        rotations.get(endFrame, tempQ2);
        translations.get(endFrame, tempV2);
        if (scales != null) {
            scales.get(endFrame, tempS2);
        }
        tempQ.nlerp(tempQ2, blend);
        tempV.interpolateLocal(tempV2, blend);
        tempS.interpolateLocal(tempS2, blend);
    }
    //        if (weight != 1f) {
    target.blendAnimTransforms(tempV, tempQ, scales != null ? tempS : null, weight);
//        } else {
//            target.setAnimTransforms(tempV, tempQ, scales != null ? tempS : null);
//        }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) BitSet(java.util.BitSet)

Example 2 with Skeleton

use of com.jme3.animation.Skeleton in project jmonkeyengine by jMonkeyEngine.

the class SkeletonControl method cloneFields.

@Override
public void cloneFields(Cloner cloner, Object original) {
    super.cloneFields(cloner, original);
    this.skeleton = cloner.clone(skeleton);
    // 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);
    // Not automatic set cloning yet
    Set<Material> newMaterials = new HashSet<Material>();
    for (Material m : this.materials) {
        Material mClone = cloner.clone(m);
        newMaterials.add(mClone);
        if (mClone != m) {
            // Material was really cloned so clear the bone matrices in case
            // this is hardware skinned.  This allows a local version to be
            // used and will be reset on the material.  Really this just avoids
            // the 'safety' check in controlRenderHardware().  Right now material
            // doesn't clone itself with the cloner (and doesn't clone its parameters)
            // else this would be unnecessary.
            MatParam boneMatrices = mClone.getParam("BoneMatrices");
            // parameter.
            if (boneMatrices != null) {
                mClone.clearParam("BoneMatrices");
            }
        }
    }
    this.materials = newMaterials;
}
Also used : MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material) HashSet(java.util.HashSet)

Example 3 with Skeleton

use of com.jme3.animation.Skeleton in project jmonkeyengine by jMonkeyEngine.

the class AnimControl method controlUpdate.

/**
     * Internal use only.
     */
@Override
protected void controlUpdate(float tpf) {
    if (skeleton != null) {
        // reset skeleton to bind pose
        skeleton.reset();
    }
    TempVars vars = TempVars.get();
    for (int i = 0; i < channels.size(); i++) {
        channels.get(i).update(tpf, vars);
    }
    vars.release();
    if (skeleton != null) {
        skeleton.updateWorldVectors();
    }
}
Also used : TempVars(com.jme3.util.TempVars)

Example 4 with Skeleton

use of com.jme3.animation.Skeleton in project jmonkeyengine by jMonkeyEngine.

the class FbxLoader method constructAnimations.

private void constructAnimations() {
    // In FBX, animation are not attached to any root.
    // They are implicitly global.
    // So, we need to use hueristics to find which node(s) 
    // an animation is associated with, so we can create the AnimControl
    // in the appropriate location in the scene.
    Map<FbxToJmeTrack, FbxToJmeTrack> pairs = new HashMap<FbxToJmeTrack, FbxToJmeTrack>();
    for (FbxAnimStack stack : animStacks) {
        for (FbxAnimLayer layer : stack.getLayers()) {
            for (FbxAnimCurveNode curveNode : layer.getAnimationCurveNodes()) {
                for (Map.Entry<FbxNode, String> nodePropertyEntry : curveNode.getInfluencedNodeProperties().entrySet()) {
                    FbxToJmeTrack lookupPair = new FbxToJmeTrack();
                    lookupPair.animStack = stack;
                    lookupPair.animLayer = layer;
                    lookupPair.node = nodePropertyEntry.getKey();
                    // Find if this pair is already stored
                    FbxToJmeTrack storedPair = pairs.get(lookupPair);
                    if (storedPair == null) {
                        // If not, store it.
                        storedPair = lookupPair;
                        pairs.put(storedPair, storedPair);
                    }
                    String property = nodePropertyEntry.getValue();
                    storedPair.animCurves.put(property, curveNode);
                }
            }
        }
    }
    // At this point we can construct the animation for all pairs ...
    for (FbxToJmeTrack pair : pairs.values()) {
        String animName = pair.animStack.getName();
        float duration = pair.animStack.getDuration();
        System.out.println("ANIMATION: " + animName + ", duration = " + duration);
        System.out.println("NODE: " + pair.node.getName());
        duration = pair.getDuration();
        if (pair.node instanceof FbxLimbNode) {
            // Find the spatial that has the skeleton for this limb.
            FbxLimbNode limbNode = (FbxLimbNode) pair.node;
            Bone bone = limbNode.getJmeBone();
            Spatial jmeSpatial = limbNode.getSkeletonHolder().getJmeObject();
            Skeleton skeleton = limbNode.getSkeletonHolder().getJmeSkeleton();
            // Get the animation control (create if missing).
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl.getSkeleton() != skeleton) {
                throw new UnsupportedOperationException();
            }
            // Get the animation (create if missing).
            Animation anim = animControl.getAnim(animName);
            if (anim == null) {
                anim = new Animation(animName, duration);
                animControl.addAnim(anim);
            }
            // Find the bone index from the spatial's skeleton.
            int boneIndex = skeleton.getBoneIndex(bone);
            // Generate the bone track.
            BoneTrack bt = pair.toJmeBoneTrack(boneIndex, bone.getBindInverseTransform());
            // Add the bone track to the animation.
            anim.addTrack(bt);
        } else {
            // Create the spatial animation
            Animation anim = new Animation(animName, duration);
            anim.setTracks(new Track[] { pair.toJmeSpatialTrack() });
            // Get the animation control (create if missing).
            Spatial jmeSpatial = pair.node.getJmeObject();
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl == null) {
                animControl = new AnimControl(null);
                jmeSpatial.addControl(animControl);
            }
            // Add the spatial animation
            animControl.addAnim(anim);
        }
    }
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) HashMap(java.util.HashMap) FbxAnimCurveNode(com.jme3.scene.plugins.fbx.anim.FbxAnimCurveNode) AnimControl(com.jme3.animation.AnimControl) FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode) Spatial(com.jme3.scene.Spatial) Animation(com.jme3.animation.Animation) FbxToJmeTrack(com.jme3.scene.plugins.fbx.anim.FbxToJmeTrack) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) FbxLimbNode(com.jme3.scene.plugins.fbx.anim.FbxLimbNode) FbxAnimLayer(com.jme3.scene.plugins.fbx.anim.FbxAnimLayer) HashMap(java.util.HashMap) Map(java.util.Map) FbxAnimStack(com.jme3.scene.plugins.fbx.anim.FbxAnimStack)

Example 5 with Skeleton

use of com.jme3.animation.Skeleton in project jmonkeyengine by jMonkeyEngine.

the class ArmatureModifier method postMeshCreationApply.

@Override
public void postMeshCreationApply(Node node, BlenderContext blenderContext) {
    LOGGER.fine("Applying armature modifier after mesh has been created.");
    AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
    animationHelper.applyAnimations(node, skeleton, blenderContext.getBlenderKey().getAnimationMatchMethod());
    node.updateModelBound();
}
Also used : AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper)

Aggregations

Quaternion (com.jme3.math.Quaternion)13 Vector3f (com.jme3.math.Vector3f)13 Bone (com.jme3.animation.Bone)11 Animation (com.jme3.animation.Animation)7 Skeleton (com.jme3.animation.Skeleton)7 AnimControl (com.jme3.animation.AnimControl)6 BoneTrack (com.jme3.animation.BoneTrack)6 TempVars (com.jme3.util.TempVars)6 HashMap (java.util.HashMap)6 SkeletonControl (com.jme3.animation.SkeletonControl)5 Material (com.jme3.material.Material)3 Transform (com.jme3.math.Transform)3 Node (com.jme3.scene.Node)3 Spatial (com.jme3.scene.Spatial)3 AnimChannel (com.jme3.animation.AnimChannel)2 Track (com.jme3.animation.Track)2 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)2 DirectionalLight (com.jme3.light.DirectionalLight)2 Matrix4f (com.jme3.math.Matrix4f)2 Geometry (com.jme3.scene.Geometry)2