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);
}
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;
}
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();
}
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);
}
}
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);
}
Aggregations