Search in sources :

Example 1 with Bone

use of com.jme3.animation.Bone 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 Bone

use of com.jme3.animation.Bone 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 Bone

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

the class FbxLimbNode method getJmeBone.

public Bone getJmeBone() {
    if (bone == null) {
        bone = new Bone(name);
        bone.setBindTransforms(jmeLocalBindPose.getTranslation(), jmeLocalBindPose.getRotation(), jmeLocalBindPose.getScale());
    }
    return bone;
}
Also used : Bone(com.jme3.animation.Bone)

Example 4 with Bone

use of com.jme3.animation.Bone 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 Bone

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

the class MaskModifier method readBoneNames.

/**
     * Reads the names of the bones from the given bone base.
     * @param boneBase
     *            the list of bone base structures
     * @return a list of bones' names
     * @throws BlenderFileException
     *             is thrown if problems with reading the child bones' bases occur
     */
private List<String> readBoneNames(List<Structure> boneBase) throws BlenderFileException {
    List<String> result = new ArrayList<String>();
    for (Structure boneStructure : boneBase) {
        int flag = ((Number) boneStructure.getFieldValue("flag")).intValue();
        if ((flag & BoneContext.SELECTED) != 0) {
            result.add(boneStructure.getFieldValue("name").toString());
        }
        List<Structure> childbase = ((Structure) boneStructure.getFieldValue("childbase")).evaluateListBase();
        result.addAll(this.readBoneNames(childbase));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Structure(com.jme3.scene.plugins.blender.file.Structure) Point(com.jme3.scene.plugins.blender.meshes.Point)

Aggregations

Bone (com.jme3.animation.Bone)35 Vector3f (com.jme3.math.Vector3f)25 Quaternion (com.jme3.math.Quaternion)17 TempVars (com.jme3.util.TempVars)13 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)10 FloatBuffer (java.nio.FloatBuffer)10 VertexBuffer (com.jme3.scene.VertexBuffer)8 HashMap (java.util.HashMap)8 Skeleton (com.jme3.animation.Skeleton)7 Matrix4f (com.jme3.math.Matrix4f)7 Transform (com.jme3.math.Transform)7 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)7 ByteBuffer (java.nio.ByteBuffer)7 BoneTrack (com.jme3.animation.BoneTrack)6 Structure (com.jme3.scene.plugins.blender.file.Structure)6 ArrayList (java.util.ArrayList)6 AnimControl (com.jme3.animation.AnimControl)5 Spatial (com.jme3.scene.Spatial)5 Map (java.util.Map)5 Animation (com.jme3.animation.Animation)4