Search in sources :

Example 11 with Animation

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

the class AnimationHelper method applyAnimations.

/**
     * The method applies animations to the given node. The names of the animations should be the same as actions names in the blender file.
     * @param node
     *            the node to whom the animations will be applied
     * @param animationMatchMethod
     *            the way animation should be matched with node
     */
public void applyAnimations(Node node, AnimationMatchMethod animationMatchMethod) {
    List<BlenderAction> actions = this.getActions(node, animationMatchMethod);
    if (actions.size() > 0) {
        List<Animation> animations = new ArrayList<Animation>();
        for (BlenderAction action : actions) {
            SpatialTrack[] tracks = action.toTracks(node, blenderContext);
            if (tracks != null && tracks.length > 0) {
                Animation spatialAnimation = new Animation(action.getName(), action.getAnimationTime());
                spatialAnimation.setTracks(tracks);
                animations.add(spatialAnimation);
                blenderContext.addAnimation((Long) node.getUserData(ObjectHelper.OMA_MARKER), spatialAnimation);
            }
        }
        if (animations.size() > 0) {
            AnimControl control = new AnimControl();
            HashMap<String, Animation> anims = new HashMap<String, Animation>(animations.size());
            for (int i = 0; i < animations.size(); ++i) {
                Animation animation = animations.get(i);
                anims.put(animation.getName(), animation);
            }
            control.setAnimations(anims);
            node.addControl(control);
        }
    }
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Animation(com.jme3.animation.Animation) AnimControl(com.jme3.animation.AnimControl)

Example 12 with Animation

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

the class AnimationHelper method applyAnimations.

/**
     * The method applies skeleton animations to the given node.
     * @param node
     *            the node where the animations will be applied
     * @param skeleton
     *            the skeleton of the node
     * @param animationMatchMethod
     *            the way animation should be matched with skeleton
     */
public void applyAnimations(Node node, Skeleton skeleton, AnimationMatchMethod animationMatchMethod) {
    node.addControl(new SkeletonControl(skeleton));
    blenderContext.setNodeForSkeleton(skeleton, node);
    List<BlenderAction> actions = this.getActions(skeleton, animationMatchMethod);
    if (actions.size() > 0) {
        List<Animation> animations = new ArrayList<Animation>();
        for (BlenderAction action : actions) {
            BoneTrack[] tracks = action.toTracks(skeleton, blenderContext);
            if (tracks != null && tracks.length > 0) {
                Animation boneAnimation = new Animation(action.getName(), action.getAnimationTime());
                boneAnimation.setTracks(tracks);
                animations.add(boneAnimation);
                Long animatedNodeOMA = ((Number) blenderContext.getMarkerValue(ObjectHelper.OMA_MARKER, node)).longValue();
                blenderContext.addAnimation(animatedNodeOMA, boneAnimation);
            }
        }
        if (animations.size() > 0) {
            AnimControl control = new AnimControl(skeleton);
            HashMap<String, Animation> anims = new HashMap<String, Animation>(animations.size());
            for (int i = 0; i < animations.size(); ++i) {
                Animation animation = animations.get(i);
                anims.put(animation.getName(), animation);
            }
            control.setAnimations(anims);
            node.addControl(control);
            // make sure that SkeletonControl is added AFTER the AnimControl
            SkeletonControl skeletonControl = node.getControl(SkeletonControl.class);
            if (skeletonControl != null) {
                node.removeControl(SkeletonControl.class);
                node.addControl(skeletonControl);
            }
        }
    }
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SkeletonControl(com.jme3.animation.SkeletonControl) AnimControl(com.jme3.animation.AnimControl) Animation(com.jme3.animation.Animation)

Example 13 with Animation

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

the class SimulationNode method simulateSpatial.

/**
     * Simulates the spatial node.
     */
private void simulateSpatial() {
    List<Constraint> constraints = blenderContext.getConstraints(featureOMA);
    if (constraints != null && constraints.size() > 0) {
        LOGGER.fine("Simulating spatial.");
        boolean applyStaticConstraints = true;
        if (animations != null) {
            for (Animation animation : animations) {
                float[] animationTimeBoundaries = this.computeAnimationTimeBoundaries(animation);
                int maxFrame = (int) animationTimeBoundaries[0];
                float maxTime = animationTimeBoundaries[1];
                VirtualTrack vTrack = new VirtualTrack(spatial.getName(), maxFrame, maxTime);
                for (Track track : animation.getTracks()) {
                    for (int frame = 0; frame < maxFrame; ++frame) {
                        spatial.setLocalTranslation(((SpatialTrack) track).getTranslations()[frame]);
                        spatial.setLocalRotation(((SpatialTrack) track).getRotations()[frame]);
                        spatial.setLocalScale(((SpatialTrack) track).getScales()[frame]);
                        for (Constraint constraint : constraints) {
                            constraint.apply(frame);
                            vTrack.setTransform(frame, spatial.getLocalTransform());
                        }
                    }
                    Track newTrack = vTrack.getAsSpatialTrack();
                    if (newTrack != null) {
                        animation.removeTrack(track);
                        animation.addTrack(newTrack);
                    }
                    applyStaticConstraints = false;
                }
            }
        }
        // object's transformation
        if (applyStaticConstraints) {
            for (Constraint constraint : constraints) {
                constraint.apply(0);
            }
        }
    }
    for (SimulationNode child : children) {
        child.simulate();
    }
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) Animation(com.jme3.animation.Animation) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track)

Example 14 with Animation

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

the class BlenderKey method read.

@Override
public void read(JmeImporter e) throws IOException {
    super.read(e);
    InputCapsule ic = e.getCapsule(this);
    fps = ic.readInt("fps", DEFAULT_FPS);
    featuresToLoad = ic.readInt("features-to-load", FeaturesToLoad.ALL);
    loadUnlinkedAssets = ic.readBoolean("load-unlinked-assets", false);
    assetRootPath = ic.readString("asset-root-path", null);
    fixUpAxis = ic.readBoolean("fix-up-axis", true);
    generatedTexturePPU = ic.readInt("generated-texture-ppu", 128);
    usedWorld = ic.readString("used-world", null);
    defaultMaterial = (Material) ic.readSavable("default-material", null);
    faceCullMode = ic.readEnum("face-cull-mode", FaceCullMode.class, FaceCullMode.Off);
    layersToLoad = ic.readInt("layers-to=load", -1);
    mipmapGenerationMethod = ic.readEnum("mipmap-generation-method", MipmapGenerationMethod.class, MipmapGenerationMethod.GENERATE_WHEN_NEEDED);
    skyGeneratedTextureSize = ic.readInt("sky-generated-texture-size", 1000);
    skyGeneratedTextureRadius = ic.readFloat("sky-generated-texture-radius", 1f);
    skyGeneratedTextureShape = ic.readEnum("sky-generated-texture-shape", SkyGeneratedTextureShape.class, SkyGeneratedTextureShape.SPHERE);
    optimiseTextures = ic.readBoolean("optimise-textures", false);
    animationMatchMethod = ic.readEnum("animation-match-method", AnimationMatchMethod.class, AnimationMatchMethod.AT_LEAST_ONE_NAME_MATCH);
    pointsSize = ic.readFloat("points-size", 1);
    linesWidth = ic.readFloat("lines-width", 1);
}
Also used : InputCapsule(com.jme3.export.InputCapsule) FaceCullMode(com.jme3.material.RenderState.FaceCullMode)

Example 15 with Animation

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

the class KinematicRagdollControl method kinematicUpdate.

protected void kinematicUpdate(float tpf) {
    //the ragdoll does not have the controll, so the keyframed animation updates the physic position of the physic bonces
    TempVars vars = TempVars.get();
    Quaternion tmpRot1 = vars.quat1;
    Quaternion tmpRot2 = vars.quat2;
    Vector3f position = vars.vect1;
    for (PhysicsBoneLink link : boneLinks.values()) {
        //but to allow smooth transition, we blend this transformation with the saved position of the ragdoll
        if (blendedControl) {
            Vector3f position2 = vars.vect2;
            //initializing tmp vars with the start position/rotation of the ragdoll
            position.set(link.startBlendingPos);
            tmpRot1.set(link.startBlendingRot);
            //interpolating between ragdoll position/rotation and keyframed position/rotation
            tmpRot2.set(tmpRot1).nlerp(link.bone.getModelSpaceRotation(), blendStart / blendTime);
            position2.set(position).interpolateLocal(link.bone.getModelSpacePosition(), blendStart / blendTime);
            tmpRot1.set(tmpRot2);
            position.set(position2);
            //updating bones transforms
            if (boneList.isEmpty()) {
                //we ensure we have the control to update the bone
                link.bone.setUserControl(true);
                link.bone.setUserTransformsInModelSpace(position, tmpRot1);
                //we give control back to the key framed animation.
                link.bone.setUserControl(false);
            } else {
                RagdollUtils.setTransform(link.bone, position, tmpRot1, true, boneList);
            }
        }
        //setting skeleton transforms to the ragdoll
        matchPhysicObjectToBone(link, position, tmpRot1);
        modelPosition.set(targetModel.getLocalTranslation());
    }
    //time control for blending
    if (blendedControl) {
        blendStart += tpf;
        if (blendStart > blendTime) {
            blendedControl = false;
        }
    }
    vars.release();
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)18 Animation (com.jme3.animation.Animation)12 Quaternion (com.jme3.math.Quaternion)12 AnimControl (com.jme3.animation.AnimControl)11 BoneTrack (com.jme3.animation.BoneTrack)10 HashMap (java.util.HashMap)9 Bone (com.jme3.animation.Bone)8 SpatialTrack (com.jme3.animation.SpatialTrack)7 TempVars (com.jme3.util.TempVars)6 Material (com.jme3.material.Material)5 Track (com.jme3.animation.Track)4 ParticleEmitter (com.jme3.effect.ParticleEmitter)4 DirectionalLight (com.jme3.light.DirectionalLight)4 Geometry (com.jme3.scene.Geometry)4 Node (com.jme3.scene.Node)4 ArrayList (java.util.ArrayList)4 AmbientLight (com.jme3.light.AmbientLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Transform (com.jme3.math.Transform)3 Spatial (com.jme3.scene.Spatial)3