159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2012 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.animation;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.*;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Quaternion;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.TempVars;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.BitSet;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Contains a list of transforms and times for each keyframe.
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Kirill Vainer
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic final class BoneTrack implements Track {
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Bone index in the skeleton which this track effects.
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private int targetBoneIndex;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Transforms and times for track.
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private CompactVector3Array translations;
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private CompactQuaternionArray rotations;
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private CompactVector3Array scales;
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private float[] times;
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Serialization-only. Do not use.
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public BoneTrack() {
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Creates a bone track for the given bone index
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param targetBoneIndex the bone index
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param times a float array with the time of each frame
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param translations the translation of the bone for each frame
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param rotations the rotation of the bone for each frame
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public BoneTrack(int targetBoneIndex, float[] times, Vector3f[] translations, Quaternion[] rotations) {
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.targetBoneIndex = targetBoneIndex;
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.setKeyframes(times, translations, rotations);
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Creates a bone track for the given bone index
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param targetBoneIndex the bone index
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param times a float array with the time of each frame
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param translations the translation of the bone for each frame
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param rotations the rotation of the bone for each frame
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param scales the scale of the bone for each frame
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public BoneTrack(int targetBoneIndex, float[] times, Vector3f[] translations, Quaternion[] rotations, Vector3f[] scales) {
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    	this.targetBoneIndex = targetBoneIndex;
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.setKeyframes(times, translations, rotations, scales);
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Creates a bone track for the given bone index
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param targetBoneIndex the bone's index
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public BoneTrack(int targetBoneIndex) {
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.targetBoneIndex = targetBoneIndex;
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the bone index of this bone track.
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public int getTargetBoneIndex() {
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return targetBoneIndex;
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * return the array of rotations of this track
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Quaternion[] getRotations() {
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return rotations.toObjectArray();
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the array of scales for this track
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Vector3f[] getScales() {
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return scales == null ? null : scales.toObjectArray();
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the arrays of time for this track
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float[] getTimes() {
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return times;
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the array of translations of this track
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Vector3f[] getTranslations() {
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return translations.toObjectArray();
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Set the translations and rotations for this bone track
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param times a float array with the time of each frame
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param translations the translation of the bone for each frame
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param rotations the rotation of the bone for each frame
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setKeyframes(float[] times, Vector3f[] translations, Quaternion[] rotations) {
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (times.length == 0) {
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new RuntimeException("BoneTrack with no keyframes!");
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        assert times.length == translations.length && times.length == rotations.length;
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.times = times;
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.translations = new CompactVector3Array();
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.translations.add(translations);
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.translations.freeze();
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.rotations = new CompactQuaternionArray();
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.rotations.add(rotations);
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.rotations.freeze();
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Set the translations, rotations and scales for this bone track
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param times a float array with the time of each frame
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param translations the translation of the bone for each frame
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param rotations the rotation of the bone for each frame
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param scales the scale of the bone for each frame
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setKeyframes(float[] times, Vector3f[] translations, Quaternion[] rotations, Vector3f[] scales) {
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.setKeyframes(times, translations, rotations);
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        assert times.length == scales.length;
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (scales != null) {
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.scales = new CompactVector3Array();
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.scales.add(scales);
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.scales.freeze();
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Modify the bone which this track modifies in the skeleton to contain
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * the correct animation transforms for a given time.
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * The transforms can be interpolated in some method from the keyframes.
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param time the current time of the animation
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param weight the weight of the animation
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param control
18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param channel
18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param vars
18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setTime(float time, float weight, AnimControl control, AnimChannel channel, TempVars vars) {
19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        BitSet affectedBones = channel.getAffectedBones();
19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (affectedBones != null && !affectedBones.get(targetBoneIndex)) {
19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return;
19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Bone target = control.getSkeleton().getBone(targetBoneIndex);
19759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f tempV = vars.vect1;
19959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f tempS = vars.vect2;
20059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quaternion tempQ = vars.quat1;
20159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f tempV2 = vars.vect3;
20259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f tempS2 = vars.vect4;
20359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quaternion tempQ2 = vars.quat2;
20459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
20559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int lastFrame = times.length - 1;
20659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (time < 0 || lastFrame == 0) {
20759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rotations.get(0, tempQ);
20859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            translations.get(0, tempV);
20959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (scales != null) {
21059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                scales.get(0, tempS);
21159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
21259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else if (time >= times[lastFrame]) {
21359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rotations.get(lastFrame, tempQ);
21459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            translations.get(lastFrame, tempV);
21559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (scales != null) {
21659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                scales.get(lastFrame, tempS);
21759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
21859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
21959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            int startFrame = 0;
22059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            int endFrame = 1;
22159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            // use lastFrame so we never overflow the array
22259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            int i;
22359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (i = 0; i < lastFrame && times[i] < time; i++) {
22459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                startFrame = i;
22559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                endFrame = i + 1;
22659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
22759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
22859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            float blend = (time - times[startFrame])
22959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    / (times[endFrame] - times[startFrame]);
23059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
23159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rotations.get(startFrame, tempQ);
23259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            translations.get(startFrame, tempV);
23359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (scales != null) {
23459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                scales.get(startFrame, tempS);
23559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
23659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rotations.get(endFrame, tempQ2);
23759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            translations.get(endFrame, tempV2);
23859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (scales != null) {
23959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                scales.get(endFrame, tempS2);
24059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
24159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            tempQ.nlerp(tempQ2, blend);
24259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            tempV.interpolate(tempV2, blend);
24359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            tempS.interpolate(tempS2, blend);
24459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
24559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
24659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (weight != 1f) {
24759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            target.blendAnimTransforms(tempV, tempQ, scales != null ? tempS : null, weight);
24859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
24959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            target.setAnimTransforms(tempV, tempQ, scales != null ? tempS : null);
25059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
25159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
25259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
25359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
25459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the length of the track
25559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
25659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getLength() {
25759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return times == null ? 0 : times[times.length - 1] - times[0];
25859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
25959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
26059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
26159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * This method creates a clone of the current object.
26259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return a clone of the current object
26359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
26459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
26559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public BoneTrack clone() {
26659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int tablesLength = times.length;
26759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
26859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        float[] times = this.times.clone();
26959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f[] sourceTranslations = this.getTranslations();
27059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quaternion[] sourceRotations = this.getRotations();
27159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f[] sourceScales = this.getScales();
27259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
27359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f[] translations = new Vector3f[tablesLength];
27459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quaternion[] rotations = new Quaternion[tablesLength];
27559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f[] scales = new Vector3f[tablesLength];
27659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < tablesLength; ++i) {
27759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            translations[i] = sourceTranslations[i].clone();
27859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rotations[i] = sourceRotations[i].clone();
27959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            scales[i] = sourceScales != null ? sourceScales[i].clone() : new Vector3f(1.0f, 1.0f, 1.0f);
28059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
28159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
28259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Need to use the constructor here because of the final fields used in this class
28359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return new BoneTrack(targetBoneIndex, times, translations, rotations, scales);
28459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
28559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
28659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
28759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void write(JmeExporter ex) throws IOException {
28859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        OutputCapsule oc = ex.getCapsule(this);
28959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(targetBoneIndex, "boneIndex", 0);
29059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(translations, "translations", null);
29159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(rotations, "rotations", null);
29259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(times, "times", null);
29359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(scales, "scales", null);
29459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
29559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
29659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
29759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void read(JmeImporter im) throws IOException {
29859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        InputCapsule ic = im.getCapsule(this);
29959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        targetBoneIndex = ic.readInt("boneIndex", 0);
30059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
30159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        translations = (CompactVector3Array) ic.readSavable("translations", null);
30259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rotations = (CompactQuaternionArray) ic.readSavable("rotations", null);
30359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        times = ic.readFloatArray("times", null);
30459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        scales = (CompactVector3Array) ic.readSavable("scales", null);
30559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
30659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //Backward compatibility for old j3o files generated before revision 6807
30759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (im.getFormatVersion() == 0){
30859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (translations == null) {
30959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Savable[] sav = ic.readSavableArray("translations", null);
31059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (sav != null) {
31159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    translations = new CompactVector3Array();
31259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    Vector3f[] transCopy = new Vector3f[sav.length];
31359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    System.arraycopy(sav, 0, transCopy, 0, sav.length);
31459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    translations.add(transCopy);
31559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    translations.freeze();
31659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
31759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
31859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (rotations == null) {
31959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Savable[] sav = ic.readSavableArray("rotations", null);
32059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (sav != null) {
32159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    rotations = new CompactQuaternionArray();
32259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    Quaternion[] rotCopy = new Quaternion[sav.length];
32359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    System.arraycopy(sav, 0, rotCopy, 0, sav.length);
32459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    rotations.add(rotCopy);
32559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    rotations.freeze();
32659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
32759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
32859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
32959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
33059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
33159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setTime(float time, float weight, AnimControl control, AnimChannel channel) {
33259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        throw new UnsupportedOperationException("Not supported yet.");
33359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
33459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
335