bonetransform.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 501 行 · 第 1/2 页

JAVA
501
字号
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.animation;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;

import com.jme.math.Matrix4f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;

/**
 * BoneTransform contains a Bone/Transform array pairing. This pairing defines
 * the bone that will be transformed (translate, rotate), and the
 * transformations for a given frame. The bone is updated during a call to the
 * update method that defines two keyframes and the interpolation value between
 * them.
 */
public class BoneTransform implements Serializable, Savable {
    private static final Logger logger = Logger.getLogger(BoneTransform.class
            .getName());

    private static final long serialVersionUID = -6037680427670917355L;

    private Quaternion[] rotations;
    private Vector3f[] translations;
    private Bone bone;
    private String boneId;

    private static Quaternion tempQuat1 = new Quaternion();
    private static Vector3f tempVec1 = new Vector3f();

    /**
     * Default constructor creates a new BoneTransform with no data set.
     */
    public BoneTransform() {

    }

    /**
     * Constructor defines the bone that will be transformed as well as how many
     * transform keyframes that exist. These keyframes are not set until setXXXX
     * is called.
     * 
     * @param bone
     *            the bone to transform.
     * @param frames
     *            the number of keyframes for this animation.
     */
    public BoneTransform(Bone bone, int frames) {
        this.bone = bone;
        rotations = new Quaternion[frames];
        translations = new Vector3f[frames];
    }

    /**
     * Constructor defines the bone and the list of transforms to use. This
     * constructor builds a complete BoneTransform ready for use.
     * 
     * @param bone
     *            the bone to transform.
     * @param transforms
     *            the transforms to use.
     */
    public BoneTransform(Bone bone, Matrix4f[] transforms) {
        this.bone = bone;
        setTransforms(transforms);
    }

    /**
     * setCurrentFrame will set the current frame from the bone. The frame
     * supplied will define how to transform the bone. It is the responsibility
     * of the caller to insure the frame supplied is valid.
     * 
     * @param frame
     *            the frame to set the bone's transform to.
     */
    public void setCurrentFrame(int frame) {
        setCurrentFrame(frame, null, null, 0, null);
    }

    public void setCurrentFrame(int frame, Bone source, Spatial destination, float diffModifier, AnimationProperties props) {
        
        if (bone != null) {
            if (props != null && bone == source && destination != null) {

                tempVec1.set(translations[frame]);
                if (frame != 0) {
                    tempVec1.subtractLocal(translations[frame - 1]);
                }
                if (props.isLockX()) {
                    tempVec1.x = 0;
                }

                if (props.isLockY()) {
                    tempVec1.y = 0;
                }

                if (props.isLockZ()) {
                    tempVec1.z = 0;
                }

                destination.getLocalTranslation().addLocal(
                        tempVec1.divide(diffModifier));

                if (props.isLockX() || props.isLockY() || props.isLockZ()) {
                    bone.getLocalTranslation().set(translations[frame]);
                    
                    if(!props.isLockX()) {
                        bone.getLocalTranslation().x = 0;
                    }
                    
                    if(!props.isLockY()) {
                        bone.getLocalTranslation().y = 0;
                    }
                    
                    if(!props.isLockZ()) {
                        logger.info("LOCK Z");
                        bone.getLocalTranslation().z = 0;
                    }
                }
                
                bone.getLocalRotation().set(rotations[frame]);
                bone.propogateBoneChange(true);
            } else {
                bone.getLocalRotation().set(rotations[frame]);
                bone.getLocalTranslation().set(translations[frame]);
                bone.propogateBoneChange(true);
            }
        }
    }
    
    public void setCurrentFrame(int frame, float blend) {
        setCurrentFrame(frame, blend, null, null, 0, null);
    }

    /**
     * setCurrentFrame will set the current frame from the bone. The frame
     * supplied will define how to transform the bone. It is the responsibility
     * of the caller to insure the frame supplied is valid.
     * 
     * @param frame
     *            the frame to set the bone's transform to.
     */
    public void setCurrentFrame(int frame, float blend, Bone source, Spatial destination, float diffModifier, AnimationProperties props) {
        if (bone != null) {
            if (props != null && bone == source && destination != null) {
                tempVec1.set(translations[frame]);
                if(frame != 0) {
                    tempVec1.subtractLocal(translations[frame - 1]);
                }
                
                if(props.isLockX()) {
                    tempVec1.x = 0;
                }
                
                if(props.isLockY()) {
                    tempVec1.y = 0;
                }
                
                if(props.isLockZ()) {
                    tempVec1.z = 0;
                }
                tempVec1.divideLocal(diffModifier);
                tempVec1.addLocal(destination.getLocalTranslation());
                destination.getLocalTranslation().interpolate(tempVec1, blend);
                
                bone.getLocalRotation().slerp(rotations[frame], blend);
                bone.propogateBoneChange(true);

            } else {
                bone.getLocalRotation().slerp(rotations[frame], blend);
                bone.getLocalTranslation().interpolate(translations[frame],
                        blend);
                bone.propogateBoneChange(true);
            }
        }
    }

    /**
     * update sets the transform of the bone to a given interpolation between
     * two given frames.
     * 
     * @param prevFrame
     *            the initial frame.
     * @param currentFrame
     *            the goal frame.
     * @param interpType
     *            the type of interpolation
     * @param time
     *            the time between frames
     */
    public void update(int prevFrame, int currentFrame, int interpType,
            float time) {
        if (bone == null) {
            return;
        }
//        logger.info(bone.getName());
        if (bone.getName().equals("Bip01-node")) {
//            logger.info("BIP 01");
        } else {
            interpolateRotation(rotations[prevFrame], rotations[currentFrame],
                    interpType, time, bone.getLocalRotation());
            interpolateTranslation(translations[prevFrame],
                    translations[currentFrame], interpType, time, bone
                            .getLocalTranslation());
            bone.propogateBoneChange(true);
        }
    }

    public void update(int prevFrame, int currentFrame, int interpType,
            float time, float blend) {
        if (bone == null) {
            return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?