📄 lwsmotion.java
字号:
framesVector.addElement(frameHolder); ++index; } } else { double currentNewFrameNumber = -1.0; while (index < numFrames) { currentNewFrameNumber = frames[index].getFrameNum() - newFirstFrame; FrameHolder frameHolder = new FrameHolder(frames[index], currentNewFrameNumber); framesVector.addElement(frameHolder); ++index; } index = 0; while (index <= startIndex && frames[index].getFrameNum() <= newLastFrame) { if (index == 0) { LwsFrame newFrame = new LwsFrame(frames[index], frames[index+1], 1.0/(frames[index+1].getFrameNum() - frames[index].getFrameNum())); FrameHolder frameHolder = new FrameHolder(newFrame, newFrame.getFrameNum() + currentNewFrameNumber); framesVector.addElement(frameHolder); } else { FrameHolder frameHolder = new FrameHolder(frames[index], frames[index].getFrameNum() + currentNewFrameNumber); framesVector.addElement(frameHolder); } ++index; } } } else { int index = 0; while (index < numFrames) { if (frames[index].getFrameNum() >= firstFrame) break; ++index; } int startIndex = index; if (frames[startIndex].getFrameNum() > firstFrame && startIndex > 0) { // Interpolate to first frame double ratio = (double)firstFrame / (frames[startIndex].getFrameNum() - frames[startIndex-1].getFrameNum()); LwsFrame newFrame = new LwsFrame(frames[startIndex-1], frames[startIndex], ratio); FrameHolder frameHolder = new FrameHolder(newFrame, newFrame.getFrameNum() - firstFrame); framesVector.addElement(frameHolder); } index = startIndex; while (index < numFrames && frames[index].getFrameNum() <= totalFrames) { FrameHolder frameHolder = new FrameHolder(frames[index], frames[index].getFrameNum() - firstFrame); framesVector.addElement(frameHolder); ++index; } if (frames[index-1].getFrameNum() < totalFrames) { // Interpolate to last frame double ratio = (double)(totalFrames - frames[index-1].getFrameNum()) / (frames[index].getFrameNum() - frames[index-1].getFrameNum()); LwsFrame newFrame = new LwsFrame(frames[index-1], frames[index], ratio); FrameHolder frameHolder = new FrameHolder(newFrame, totalFrames - firstFrame); framesVector.addElement(frameHolder); } } } /** * Normally, we just create j3d behaviors from the frames. But if the * animation's first frame is after frame number one, then we have to * shuffle things around to account for playing/looping on this subset * of the total frames of the animation */ void createJava3dBehaviorsForFramesSubset(TransformGroup target) { debugOutputLn(TRACE, "createJava3dBehaviorsForFramesSubset"); Vector frameHolders = new Vector(); playWithFrameTimes(frameHolders); long alphaAtOne = 0; // determine looping int loopCount; if (loop) loopCount = -1; else loopCount = 1; loopCount = -1; int numFrames = frameHolders.size(); debugOutputLn(VALUES, "totalTime = " + totalTime); debugOutputLn(VALUES, "loopCount = " + loopCount); FrameHolder lastFrameHolder = (FrameHolder)frameHolders.elementAt(frameHolders.size() - 1); LwsFrame lastFrame = lastFrameHolder.frame; float animTime = 1000.0f * totalTime * (float)(lastFrameHolder.frameNumber/(float)(totalFrames - firstFrame)); debugOutputLn(VALUES, " anim time: " + animTime); debugOutputLn(VALUES, " totalFrames = " + totalFrames); if (!loop) alphaAtOne = (long)(1000.0*totalTime - animTime); Alpha theAlpha = new Alpha(loopCount, Alpha.INCREASING_ENABLE, 0, 0, (long)animTime, 0, alphaAtOne, 0, 0, 0); float knots[] = new float[numFrames]; Point3f[] positions = new Point3f[numFrames]; Quat4f[] quats = new Quat4f[numFrames]; Point3f[] scales = new Point3f[numFrames]; Transform3D yAxis = new Transform3D(); Matrix4d mat = new Matrix4d(); KBKeyFrame[] keyFrames = new KBKeyFrame[numFrames]; for (int i=0; i < numFrames; ++i) { FrameHolder frameHolder = (FrameHolder)frameHolders.elementAt(i); LwsFrame frame = frameHolder.frame; // copy position positions[i] = frame.getPosition(); // copy scale // Used to hardcode no-scale: scales[i] = 1.0f, 1.0f, 1.0f; // Note that we can't do non-uniform scaling in the current Path // interpolators. The interpolator just uses the x scale. // getScale makes sure that we don't have any zero scale component scales[i] = frame.getScale(); // copy rotation information frame.setRotationMatrix(mat); debugOutputLn(VALUES, "LwsMotion::createj3dbeh, mat = " + mat); quats[i] = new Quat4f(); quats[i].set(mat); debugOutputLn(VALUES, " and quat = " + quats[i]); // calculate knot points from frame numbers if (i == 0) knots[i] = 0.0f; else knots[i] = (float)(frameHolder.frameNumber)/ (float)(lastFrameHolder.frameNumber); // Create KB key frames keyFrames[i] = new KBKeyFrame(knots[i], frame.linearValue, positions[i], (float)frame.heading, (float)frame.pitch, (float)frame.bank, scales[i], (float)frame.tension, (float)frame.continuity, (float)frame.bias); debugOutputLn(VALUES, "pos, knots, quat = " + positions[i] + knots[i] + quats[i]); } // Pass the KeyFrames to the interpolator an let it do its thing KBRotPosScaleSplinePathInterpolator b = new KBRotPosScaleSplinePathInterpolator(theAlpha, target, yAxis, keyFrames); if (b != null) { behaviors = b; BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000000.0); b.setSchedulingBounds(bounds); target.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); target.addChild(behaviors); } } /** * Create j3d behaviors for the data stored in this animation. This is * done by creating a RotPosScalePathInterpolator object that contains * all of the position, orientation, scale data for each keyframe. */ void createJava3dBehaviors(TransformGroup target) { if (numFrames <= 1) behaviors = null; else { if (firstFrame > 1) { createJava3dBehaviorsForFramesSubset(target); return; } long alphaAtOne = 0; // determine looping int loopCount; if (loop) loopCount = -1; else loopCount = 1; loopCount = -1; debugOutputLn(VALUES, "totalTime = " + totalTime); debugOutputLn(VALUES, "loopCount = " + loopCount); float animTime = 1000.0f * totalTime * (float)(frames[numFrames-1].getFrameNum()/(float)totalFrames); debugOutputLn(VALUES, " anim time: " + animTime); debugOutputLn(VALUES, " totalFrames = " + totalFrames); debugOutputLn(VALUES, " lastFrame = " + frames[numFrames-1].getFrameNum()); if (!loop) alphaAtOne = (long)(1000.0*totalTime - animTime); Alpha theAlpha = new Alpha(loopCount, Alpha.INCREASING_ENABLE, 0, 0, (long)animTime, 0, alphaAtOne, 0, 0, 0); float knots[] = new float[numFrames]; Point3f[] positions = new Point3f[numFrames]; Quat4f[] quats = new Quat4f[numFrames]; Point3f[] scales = new Point3f[numFrames]; Transform3D yAxis = new Transform3D(); Matrix4d mat = new Matrix4d(); KBKeyFrame[] keyFrames = new KBKeyFrame[numFrames]; for (int i=0; i < numFrames; ++i) { // copy position positions[i] = frames[i].getPosition(); // copy scale // Used to hardcode no-scale: scales[i] = 1.0f, 1.0f, 1.0f; // Note that we can't do non-uniform scaling in the current Path // interpolators. The interpolator just uses the x scale. // getScale makes sure that we don't have any 0 scale component scales[i] = frames[i].getScale(); // copy rotation information frames[i].setRotationMatrix(mat); debugOutputLn(VALUES, "LwsMotion::createj3dbeh, mat = " + mat); quats[i] = new Quat4f(); quats[i].set(mat); debugOutputLn(VALUES, " and quat = " + quats[i]); // calculate knot points from frame numbers if (i == 0) knots[i] = 0.0f; else knots[i] = (float)(frames[i].getFrameNum())/ (float)(frames[numFrames-1].getFrameNum()); // Create KB key frames keyFrames[i] = new KBKeyFrame(knots[i],frames[i].linearValue, positions[i], (float)frames[i].heading, (float)frames[i].pitch, (float)frames[i].bank, scales[i], (float)frames[i].tension, (float)frames[i].continuity, (float)frames[i].bias); debugOutputLn(VALUES, "pos, knots, quat = " + positions[i] + knots[i] + quats[i]); } // Pass the KeyFrames to the interpolator an let it do its thing KBRotPosScaleSplinePathInterpolator b = new KBRotPosScaleSplinePathInterpolator(theAlpha, target, yAxis, keyFrames); if (b != null) { behaviors = b; BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000000.0); b.setSchedulingBounds(bounds); target.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); target.addChild(behaviors); } } } /** * Returns the Behavior object created for this animation */ Behavior getBehaviors() { return behaviors; } /** * Returns the first LwsFrame object (which contains the initial * setup for a given object) */ LwsFrame getFirstFrame() { if (numFrames > 0) return frames[0]; else return null; } /** * Utility function for printing values */ void printVals() { debugOutputLn(VALUES, " motionName = " + motionName); debugOutputLn(VALUES, " numChannels = " + numChannels); debugOutputLn(VALUES, " numFrames = " + numFrames); debugOutputLn(VALUES, " loop = " + loop); for (int i = 0; i < numFrames; ++i) { debugOutputLn(VALUES, " FRAME " + i); frames[i].printVals(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -