Search in sources :

Example 21 with Animation

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

the class Constraint method apply.

/**
     * Applies the constraint to owner (and in some cases can alter other bones of the skeleton).
     * @param frame
     *            the frame of the animation
     */
public void apply(int frame) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "Applying constraint: {0} for frame {1}", new Object[] { name, frame });
    }
    Transform targetTransform = targetOMA != null ? constraintHelper.getTransform(targetOMA, subtargetName, targetSpace) : null;
    constraintDefinition.bake(ownerSpace, targetSpace, targetTransform, (float) ipo.calculateValue(frame));
}
Also used : Transform(com.jme3.math.Transform)

Example 22 with Animation

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

the class SimulationNode method applyConstraints.

/**
     * Applies constraints to the given bone and its children.
     * The goal is to apply constraint from root bone to the last child.
     * @param bone
     *            the bone whose constraints will be applied
     * @param alteredOmas
     *            the set of OMAS of the altered bones (is populated if necessary)
     * @param frame
     *            the current frame of the animation
     * @param bonesStack
     *            the stack of bones used to avoid infinite loops while applying constraints
     */
private void applyConstraints(Bone bone, Set<Long> alteredOmas, Set<Long> applied, int frame, Stack<Bone> bonesStack) {
    if (!bonesStack.contains(bone)) {
        bonesStack.push(bone);
        BoneContext boneContext = blenderContext.getBoneContext(bone);
        if (!applied.contains(boneContext.getBoneOma())) {
            List<Constraint> constraints = this.findConstraints(boneContext.getBoneOma(), blenderContext);
            if (constraints != null && constraints.size() > 0) {
                for (Constraint constraint : constraints) {
                    if (constraint.getTargetOMA() != null && constraint.getTargetOMA() > 0L) {
                        // first apply constraints of the target bone
                        BoneContext targetBone = blenderContext.getBoneContext(constraint.getTargetOMA());
                        this.applyConstraints(targetBone.getBone(), alteredOmas, applied, frame, bonesStack);
                    }
                    constraint.apply(frame);
                    if (constraint.getAlteredOmas() != null) {
                        alteredOmas.addAll(constraint.getAlteredOmas());
                    }
                    alteredOmas.add(boneContext.getBoneOma());
                }
            }
            applied.add(boneContext.getBoneOma());
        }
        List<Bone> children = bone.getChildren();
        if (children != null && children.size() > 0) {
            for (Bone child : bone.getChildren()) {
                this.applyConstraints(child, alteredOmas, applied, frame, bonesStack);
            }
        }
        bonesStack.pop();
    }
}
Also used : BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Bone(com.jme3.animation.Bone)

Example 23 with Animation

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

the class SimulationNode method simulateSkeleton.

/**
     * Simulates the bone node.
     */
private void simulateSkeleton() {
    LOGGER.fine("Simulating skeleton.");
    Set<Long> alteredOmas = new HashSet<Long>();
    if (animations != null) {
        TempVars vars = TempVars.get();
        AnimChannel animChannel = animControl.createChannel();
        for (Animation animation : animations) {
            float[] animationTimeBoundaries = this.computeAnimationTimeBoundaries(animation);
            int maxFrame = (int) animationTimeBoundaries[0];
            float maxTime = animationTimeBoundaries[1];
            Map<Integer, VirtualTrack> tracks = new HashMap<Integer, VirtualTrack>();
            for (int frame = 0; frame < maxFrame; ++frame) {
                // this MUST be done here, otherwise setting next frame of animation will
                // lead to possible errors
                this.reset();
                // first set proper time for all bones in all the tracks ...
                for (Track track : animation.getTracks()) {
                    float time = ((BoneTrack) track).getTimes()[frame];
                    track.setTime(time, 1, animControl, animChannel, vars);
                    skeleton.updateWorldVectors();
                }
                // ... and then apply constraints from the root bone to the last child ...
                Set<Long> applied = new HashSet<Long>();
                for (Bone rootBone : skeleton.getRoots()) {
                    // ignore the 0-indexed bone
                    if (skeleton.getBoneIndex(rootBone) > 0) {
                        this.applyConstraints(rootBone, alteredOmas, applied, frame, new Stack<Bone>());
                    }
                }
                // ... add virtual tracks if neccessary, for bones that were altered but had no tracks before ...
                for (Long boneOMA : alteredOmas) {
                    BoneContext boneContext = blenderContext.getBoneContext(boneOMA);
                    int boneIndex = skeleton.getBoneIndex(boneContext.getBone());
                    if (!tracks.containsKey(boneIndex)) {
                        tracks.put(boneIndex, new VirtualTrack(boneContext.getBone().getName(), maxFrame, maxTime));
                    }
                }
                alteredOmas.clear();
                // ... and fill in another frame in the result track
                for (Entry<Integer, VirtualTrack> trackEntry : tracks.entrySet()) {
                    Bone bone = skeleton.getBone(trackEntry.getKey());
                    Transform startTransform = boneStartTransforms.get(bone);
                    // track contains differences between the frame position and bind positions of bones/spatials
                    Vector3f bonePositionDifference = bone.getLocalPosition().subtract(startTransform.getTranslation());
                    Quaternion boneRotationDifference = startTransform.getRotation().inverse().mult(bone.getLocalRotation()).normalizeLocal();
                    Vector3f boneScaleDifference = bone.getLocalScale().divide(startTransform.getScale());
                    trackEntry.getValue().setTransform(frame, new Transform(bonePositionDifference, boneRotationDifference, boneScaleDifference));
                }
            }
            for (Entry<Integer, VirtualTrack> trackEntry : tracks.entrySet()) {
                Track newTrack = trackEntry.getValue().getAsBoneTrack(trackEntry.getKey());
                if (newTrack != null) {
                    boolean trackReplaced = false;
                    for (Track track : animation.getTracks()) {
                        if (((BoneTrack) track).getTargetBoneIndex() == trackEntry.getKey().intValue()) {
                            animation.removeTrack(track);
                            animation.addTrack(newTrack);
                            trackReplaced = true;
                            break;
                        }
                    }
                    if (!trackReplaced) {
                        animation.addTrack(newTrack);
                    }
                }
            }
        }
        vars.release();
        animControl.clearChannels();
        this.reset();
    }
}
Also used : HashMap(java.util.HashMap) Quaternion(com.jme3.math.Quaternion) AnimChannel(com.jme3.animation.AnimChannel) TempVars(com.jme3.util.TempVars) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Vector3f(com.jme3.math.Vector3f) Animation(com.jme3.animation.Animation) Bone(com.jme3.animation.Bone) Transform(com.jme3.math.Transform) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track) HashSet(java.util.HashSet)

Example 24 with Animation

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

the class SimulationNode method computeAnimationTimeBoundaries.

/**
     * Computes the maximum frame and time for the animation. Different tracks
     * can have different lengths so here the maximum one is being found.
     * 
     * @param animation
     *            the animation
     * @return maximum frame and time of the animation
     */
private float[] computeAnimationTimeBoundaries(Animation animation) {
    int maxFrame = Integer.MIN_VALUE;
    float maxTime = -Float.MAX_VALUE;
    for (Track track : animation.getTracks()) {
        if (track instanceof BoneTrack) {
            maxFrame = Math.max(maxFrame, ((BoneTrack) track).getTranslations().length);
            maxTime = Math.max(maxTime, ((BoneTrack) track).getTimes()[((BoneTrack) track).getTimes().length - 1]);
        } else if (track instanceof SpatialTrack) {
            maxFrame = Math.max(maxFrame, ((SpatialTrack) track).getTranslations().length);
            maxTime = Math.max(maxTime, ((SpatialTrack) track).getTimes()[((SpatialTrack) track).getTimes().length - 1]);
        } else {
            throw new IllegalStateException("Unsupported track type for simuation: " + track);
        }
    }
    return new float[] { maxFrame, maxTime };
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track)

Example 25 with Animation

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

the class AnimationHelper method getTracks249.

/**
     * This method retuns the bone tracks for animation for blender version 2.49
     * (and probably several lower versions too).
     * 
     * @param actionStructure
     *            the structure containing the tracks
     * @param blenderContext
     *            the blender context
     * @return a list of tracks for the specified animation
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the blend
     *             file
     */
private BlenderAction getTracks249(Structure actionStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Getting tracks!");
    Structure chanbase = (Structure) actionStructure.getFieldValue("chanbase");
    // bActionChannel
    List<Structure> actionChannels = chanbase.evaluateListBase();
    BlenderAction blenderAction = new BlenderAction(actionStructure.getName(), blenderContext.getBlenderKey().getFps());
    int lastFrame = 1;
    for (Structure bActionChannel : actionChannels) {
        String animatedFeatureName = bActionChannel.getFieldValue("name").toString();
        Pointer p = (Pointer) bActionChannel.getFieldValue("ipo");
        if (!p.isNull()) {
            Structure ipoStructure = p.fetchData().get(0);
            Ipo ipo = this.fromIpoStructure(ipoStructure, blenderContext);
            if (ipo != null) {
                // this can happen when ipo with no curves appear in blender file
                lastFrame = Math.max(lastFrame, ipo.getLastFrame());
                blenderAction.featuresTracks.put(animatedFeatureName, ipo);
            }
        }
    }
    blenderAction.stopFrame = lastFrame;
    return blenderAction;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

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