Search in sources :

Example 6 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class SkeletalAnimationObject3D method setJointsWithInverseBindPoseMatrices.

/*
	 * Takes a joint hierarchy where each SkeletonJoint's matrix is that
	 * joint's inverse bind pose. Assigns joints, bind poses, and inverses.
	 */
public void setJointsWithInverseBindPoseMatrices(SkeletonJoint[] joints) {
    double[][] invbp = new double[joints.length][];
    for (int i = 0; i < joints.length; i++) invbp[i] = Arrays.copyOf(joints[i].getMatrix(), 16);
    setInverseBindPoseMatrices(invbp);
    setJoints(joints);
}
Also used : SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Example 7 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class SkeletalAnimationObject3D method setShaderParams.

public void setShaderParams(Camera camera) {
    if (!mIsPlaying)
        return;
    mBoneMatrices.clear();
    mBoneMatrices.position(0);
    long currentTime = SystemClock.uptimeMillis();
    SkeletalAnimationFrame currentFrame = (SkeletalAnimationFrame) mSequence.getFrame(mCurrentFrameIndex);
    SkeletalAnimationFrame nextFrame = (SkeletalAnimationFrame) mSequence.getFrame((mCurrentFrameIndex + 1) % mSequence.getNumFrames());
    mInterpolation += mFps * (currentTime - mStartTime) / 1000.0;
    boolean isTransitioning = mNextSequence != null;
    double transitionInterpolation = 0;
    if (isTransitioning)
        transitionInterpolation = mTransitionInterpolator.getInterpolation((float) ((currentTime - mTransitionStartTime) / mTransitionDuration));
    for (int i = 0; i < mJoints.length; ++i) {
        SkeletonJoint joint = getJoint(i);
        SkeletonJoint fromJoint = currentFrame.getSkeleton().getJoint(i);
        SkeletonJoint toJoint = nextFrame.getSkeleton().getJoint(i);
        joint.setParentIndex(fromJoint.getParentIndex());
        joint.getPosition().lerpAndSet(fromJoint.getPosition(), toJoint.getPosition(), mInterpolation);
        joint.getOrientation().slerp(fromJoint.getOrientation(), toJoint.getOrientation(), mInterpolation);
        if (isTransitioning) {
            SkeletalAnimationFrame currentTransFrame = mNextSequence.getFrame(mCurrentTransitionFrameIndex % mNextSequence.getNumFrames());
            SkeletalAnimationFrame nextTransFrame = mNextSequence.getFrame((mCurrentTransitionFrameIndex + 1) % mNextSequence.getNumFrames());
            fromJoint = currentTransFrame.getSkeleton().getJoint(i);
            toJoint = nextTransFrame.getSkeleton().getJoint(i);
            mTmpJoint1.getPosition().lerpAndSet(fromJoint.getPosition(), toJoint.getPosition(), mInterpolation);
            mTmpJoint1.getOrientation().slerp(fromJoint.getOrientation(), toJoint.getOrientation(), mInterpolation);
            // blend the two animations
            mTmpJoint2.getPosition().lerpAndSet(joint.getPosition(), mTmpJoint1.getPosition(), transitionInterpolation);
            mTmpJoint2.getOrientation().slerp(joint.getOrientation(), mTmpJoint1.getOrientation(), transitionInterpolation);
            joint.getPosition().setAll(mTmpJoint2.getPosition());
            joint.getOrientation().setAll(mTmpJoint2.getOrientation());
        }
        Matrix.setIdentityM(mBoneTranslation, 0);
        Matrix.setIdentityM(mBoneRotation, 0);
        Matrix.setIdentityM(mBoneMatrix, 0);
        Matrix.setIdentityM(mResultMatrix, 0);
        Vector3 jointPos = joint.getPosition();
        Matrix.translateM(mBoneTranslation, 0, jointPos.x, jointPos.y, jointPos.z);
        joint.getOrientation().toRotationMatrix(mBoneRotation);
        Matrix.multiplyMM(mBoneMatrix, 0, mBoneTranslation, 0, mBoneRotation, 0);
        Matrix.multiplyMM(mResultMatrix, 0, mBoneMatrix, 0, mInverseBindPoseMatrix[i], 0);
        joint.setMatrix(mResultMatrix);
        int index = 16 * i;
        for (int j = 0; j < 16; j++) {
            uBoneMatrix[index + j] = mResultMatrix[j];
            mBoneMatrices.put(mResultMatrix[j]);
        }
    }
    if (isTransitioning && transitionInterpolation >= .99f) {
        isTransitioning = false;
        mCurrentFrameIndex = mCurrentTransitionFrameIndex;
        mSequence = mNextSequence;
        mNextSequence = null;
    }
    mGeometry.changeBufferData(mBoneMatricesBufferInfo, mBoneMatrices, 0);
    if (mInterpolation >= 1) {
        mInterpolation = 0;
        mCurrentFrameIndex++;
        if (mCurrentFrameIndex >= mSequence.getNumFrames())
            mCurrentFrameIndex = 0;
        if (isTransitioning) {
            mCurrentTransitionFrameIndex++;
            if (mCurrentTransitionFrameIndex >= mNextSequence.getNumFrames())
                mCurrentTransitionFrameIndex = 0;
        }
    }
    mStartTime = currentTime;
}
Also used : SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) Vector3(org.rajawali3d.math.vector.Vector3) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Example 8 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class SkeletalAnimationSequence method blendWith.

/**
	 * Blend this {@link SkeletalAnimationSequence} with another {@link SkeletalAnimationSequence}.
	 * The blendFactor parameter is a value between 0 and 1.
	 * 
	 * @param otherSequence
	 * @param blendFactor
	 * @throws SkeletalAnimationException
	 */
public void blendWith(SkeletalAnimationSequence otherSequence, double blendFactor) throws SkeletalAnimationException {
    int numFrames = Math.max(mNumFrames, otherSequence.getNumFrames());
    List<SkeletalAnimationFrame> newFrames = new ArrayList<SkeletalAnimationFrame>();
    for (int i = 0; i < numFrames; i++) {
        if (i >= otherSequence.getNumFrames())
            break;
        else if (i >= mNumFrames) {
            newFrames.add(otherSequence.getFrame(i));
            continue;
        }
        SkeletalAnimationFrame thisFrame = getFrame(i);
        SkeletalAnimationFrame otherFrame = otherSequence.getFrame(i);
        SkeletalAnimationFrame newFrame = new SkeletalAnimationFrame();
        int numJoints = thisFrame.getSkeleton().getJoints().length;
        if (numJoints != otherFrame.getSkeleton().getJoints().length)
            throw new SkeletalAnimationObject3D.SkeletalAnimationException("The animation sequences you want to blend have different skeletons.");
        SkeletonJoint[] newJoints = new SkeletonJoint[numJoints];
        for (int j = 0; j < numJoints; ++j) {
            SkeletonJoint thisJoint = thisFrame.getSkeleton().getJoint(j);
            SkeletonJoint otherJoint = otherFrame.getSkeleton().getJoint(j);
            SkeletonJoint newJoint = new SkeletonJoint();
            newJoint.copyAllFrom(thisJoint);
            newJoint.getPosition().lerpAndSet(thisJoint.getPosition(), otherJoint.getPosition(), blendFactor);
            newJoint.getOrientation().slerp(thisJoint.getOrientation(), otherJoint.getOrientation(), blendFactor);
            newJoints[j] = newJoint;
        }
        newFrame.getSkeleton().setJoints(newJoints);
        newFrames.add(newFrame);
    }
    mFrames = newFrames.toArray(new SkeletalAnimationFrame[0]);
    mNumFrames = newFrames.size();
    newFrames.clear();
}
Also used : SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) SkeletalAnimationException(org.rajawali3d.animation.mesh.SkeletalAnimationObject3D.SkeletalAnimationException) ArrayList(java.util.ArrayList) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Example 9 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class BlockAnimator method buildSkeleton.

// apply joint hierarchy to joint pose frames
private void buildSkeleton(BlockHeader blockHeader, long skelAddr) throws ParsingException {
    SkeletonJoint[] joints = lookupSkeleton(blockHeader, skelAddr);
    SkeletalAnimationSequence[] skelAnims = new SkeletalAnimationSequence[mAnimSet.length];
    for (int i = 0; i < mAnimSet.length; i++) skelAnims[i] = (SkeletalAnimationSequence) mAnimSet[i];
    Matrix4 scratch1 = new Matrix4();
    Matrix4 scratch2 = new Matrix4();
    for (SkeletalAnimationSequence skelSeq : skelAnims) {
        for (SkeletalAnimationFrame frame : skelSeq.getFrames()) {
            SkeletonJoint[] poses = frame.getSkeleton().getJoints();
            // apply parent transforms
            for (int i = 0; i < poses.length; i++) {
                // matrix and index already set, need parent & other attribs
                poses[i].setParentIndex(joints[i].getParentIndex());
                if (// has parent joint
                poses[i].getParentIndex() >= 0) {
                    SkeletonJoint parentPose = poses[poses[i].getParentIndex()];
                    scratch1.setAll(parentPose.getMatrix()).multiply(scratch2.setAll(poses[i].getMatrix()));
                    poses[i].setMatrix(scratch1.getDoubleValues());
                } else
                    scratch1.setAll(poses[i].getMatrix());
                // assign pos + rot from final matrix
                scratch1.getTranslation(poses[i].getPosition());
                poses[i].getOrientation().fromMatrix(scratch1);
                poses[i].getOrientation().computeW();
            }
        }
    }
    for (int i = 0; i < mTargets.length; i++) {
        SkeletalAnimationObject3D obj = (SkeletalAnimationObject3D) mTargets[i];
        // assigns INVBP, builds BP, sets joints
        obj.setJointsWithInverseBindPoseMatrices(joints);
        for (int j = 0; j < obj.getNumChildren(); j++) {
            SkeletalAnimationChildObject3D child = (SkeletalAnimationChildObject3D) obj.getChildAt(j);
            SkeletalAnimationMaterialPlugin plugin = new SkeletalAnimationMaterialPlugin(child.getNumJoints(), child.getMaxBoneWeightsPerVertex());
            child.getMaterial().addPlugin(plugin);
        }
        obj.setAnimationSequences(skelAnims);
        obj.setAnimationSequence(mActive);
        if (mAutoPlay)
            obj.play(true);
    }
}
Also used : SkeletalAnimationSequence(org.rajawali3d.animation.mesh.SkeletalAnimationSequence) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame) SkeletalAnimationChildObject3D(org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D) SkeletalAnimationMaterialPlugin(org.rajawali3d.materials.plugins.SkeletalAnimationMaterialPlugin) SkeletalAnimationObject3D(org.rajawali3d.animation.mesh.SkeletalAnimationObject3D) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) Matrix4(org.rajawali3d.math.Matrix4)

Example 10 with SkeletonJoint

use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.

the class SkeletalAnimationObject3D method setJointsWithBindPoseMatrices.

/*
	 * Takes a joint hierarchy where each SkeletonJoint's matrix is that
	 * joint's bind pose. Assigns joints, bind poses, and inverses.
	 */
public void setJointsWithBindPoseMatrices(SkeletonJoint[] joints) {
    double[] bp = new double[joints.length * 16];
    for (int i = 0; i < joints.length; i++) System.arraycopy(joints[i].getMatrix(), 0, bp, i * 16, 16);
    setBindPoseMatrices(bp);
    setJoints(joints);
}
Also used : SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)

Aggregations

SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)17 FileNotFoundException (java.io.FileNotFoundException)6 ParsingException (org.rajawali3d.loader.ParsingException)6 StringTokenizer (java.util.StringTokenizer)5 Vector3 (org.rajawali3d.math.vector.Vector3)5 SkeletalAnimationFrame (org.rajawali3d.animation.mesh.SkeletalAnimationFrame)4 SkeletalAnimationException (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D.SkeletalAnimationException)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 BoneVertex (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneVertex)2 BoneWeight (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D.BoneWeight)2 SkeletalAnimationSequence (org.rajawali3d.animation.mesh.SkeletalAnimationSequence)2 TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)2 ArrayList (java.util.ArrayList)1 SkeletalAnimationChildObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D)1 Skeleton (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.Skeleton)1 SkeletalAnimationObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D)1 SkeletalAnimationMaterialPlugin (org.rajawali3d.materials.plugins.SkeletalAnimationMaterialPlugin)1