📄 splineanim.java
字号:
sceneTransformGroup.addChild(light1TransformGroup); sceneTransformGroup.addChild(light2TransformGroup); // Create lights AmbientLight ambLight = new AmbientLight(aColor); Light dirLight1; Light dirLight2; Vector3f lightDir1 = new Vector3f(lightPos1); Vector3f lightDir2 = new Vector3f(lightPos2); lightDir1.negate(); lightDir2.negate(); dirLight1 = new DirectionalLight(lightColor, lightDir1); dirLight2 = new DirectionalLight(lightColor, lightDir2); // Set the influencing bounds ambLight.setInfluencingBounds(bounds); dirLight1.setInfluencingBounds(bounds); dirLight2.setInfluencingBounds(bounds); // Add the lights into the scene graph sceneTransformGroup.addChild(ambLight); sceneTransformGroup.addChild(dirLight1); sceneTransformGroup.addChild(dirLight2); // Create a cone and add it to the scene graph. objTransform = new Transform3D(); objTransformGroup = new TransformGroup(objTransform); objTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); sceneTransformGroup.addChild(objTransformGroup); Material m = new Material(coneColor, eColor, coneColor, sColor, 100.0f); Appearance a = new Appearance(); m.setLightingEnable(true); a.setMaterial(m); Cone cone = new Cone(0.4f, 1.0f); cone.setAppearance(a); objTransformGroup.addChild(cone); // Create transform groups for each knot point // knot point 0 Transform3D t3dKnot = new Transform3D(); t3dKnot.set (pos0); TransformGroup k0TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k0TransformGroup); // knot point 1 t3dKnot = new Transform3D(); t3dKnot.set (pos1); TransformGroup k1TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k1TransformGroup); // knot point 2 t3dKnot = new Transform3D(); t3dKnot.set (pos2); TransformGroup k2TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k2TransformGroup); // knot point 3 t3dKnot = new Transform3D(); t3dKnot.set (pos3); TransformGroup k3TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k3TransformGroup); // knot point 4 t3dKnot = new Transform3D(); t3dKnot.set (pos4); TransformGroup k4TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k4TransformGroup); // knot point 5 t3dKnot = new Transform3D(); t3dKnot.set (pos5); TransformGroup k5TransformGroup = new TransformGroup(t3dKnot); sceneTransformGroup.addChild(k5TransformGroup); // Create spheres for each knot point's transform group ColoringAttributes sphereColorAttr = new ColoringAttributes(); sphereColorAttr.setColor(sphereColor); Appearance sphereAppearance = new Appearance(); sphereAppearance.setColoringAttributes(sphereColorAttr); k0TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); k1TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); k2TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); k3TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); k4TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); k5TransformGroup.addChild(new Sphere(0.10f, sphereAppearance)); return root; } /* * This sets up the key frame data for the spline interpolator. Each knot * point has a scale and rotation component specified. The second argument * to KBKeyFrame (in this case 0) tells the interpolator that this is * to be interpolated using splines. The last three arguments to * KBKeyFrame are Tension, Continuity, and Bias components for each * key frame. */ private void setupSplineKeyFrames () { // Prepare spline keyframe data Point3f p = new Point3f (pos0); // position float head = (float)Math.PI/2.0f; // heading float pitch = 0.0f; // pitch float bank = 0.0f; // bank Point3f s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale splineKeyFrames[0] = new KBKeyFrame(0.0f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos1); head = 0.0f; // heading pitch = 0.0f; // pitch bank = (float)-Math.PI/2.0f; // bank s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale splineKeyFrames[1] = new KBKeyFrame(0.2f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos2); head = 0.0f; // heading pitch = 0.0f; // pitch bank = 0.0f; // bank s = new Point3f(0.7f, 0.7f, 0.7f); // uniform scale splineKeyFrames[2] = new KBKeyFrame(0.4f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos3); head = (float)Math.PI/2.0f; // heading pitch = 0.0f; // pitch bank = (float)Math.PI/2.0f; // bank s = new Point3f(0.5f, 0.5f, 0.5f); // uniform scale splineKeyFrames[3] = new KBKeyFrame(0.6f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos4); head = (float)-Math.PI/2.0f; // heading pitch = (float)-Math.PI/2.0f; // pitch bank = (float)Math.PI/2.0f; // bank s = new Point3f(0.4f, 0.4f, 0.4f); // uniform scale splineKeyFrames[4] = new KBKeyFrame(0.8f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos5); head = 0.0f; // heading pitch = 0.0f; // pitch bank = 0.0f; // bank s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale splineKeyFrames[5] = new KBKeyFrame(1.0f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); } /* * This sets up the key frame data for the linear interpolator. Each knot * point has a scale and rotation component specified. The second argument * to KBKeyFrame (in this case 1) tells the interpolator that this is * to be interpolated linearly. The last three arguments to TCBKeyFrame * are Tension, Continuity, and Bias components for each key frame. */ private void setupLinearKeyFrames () { // Prepare linear keyframe data Point3f p = new Point3f (pos0); float head = 0.0f; // heading float pitch = 0.0f; // pitch float bank = 0.0f; // bank Point3f s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale linearKeyFrames[0] = new KBKeyFrame(0.0f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos1); linearKeyFrames[1] = new KBKeyFrame(0.2f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos2); linearKeyFrames[2] = new KBKeyFrame(0.4f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos3); linearKeyFrames[3] = new KBKeyFrame(0.6f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos4); linearKeyFrames[4] = new KBKeyFrame(0.8f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); p = new Point3f (pos5); linearKeyFrames[5] = new KBKeyFrame(1.0f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f); } /* * This sets up alpha for the interpolator */ private void setupAnimationData () { yAxis = new Transform3D(); animAlpha = new Alpha (-1,Alpha.INCREASING_ENABLE,0,0,duration,0,0,0,0,0); } /* * create a spline and a linear interpolator, but we will activate only * one in startInterpolator() */ private void createInterpolators () { behaviorBranch = new BranchGroup(); // create spline interpolator splineInterpolator = new KBRotPosScaleSplinePathInterpolator(animAlpha, objTransformGroup, yAxis, splineKeyFrames); splineInterpolator.setSchedulingBounds(bounds); behaviorBranch.addChild(splineInterpolator); // create linear interpolator linearInterpolator = new KBRotPosScaleSplinePathInterpolator(animAlpha, objTransformGroup, yAxis, linearKeyFrames); linearInterpolator.setSchedulingBounds(bounds); behaviorBranch.addChild(linearInterpolator); objTransformGroup.addChild(behaviorBranch); } /* * This activates one of the interpolators depending on the state of the * linear boolean flag which may be toggled by the user using the choice * menu. */ public void startInterpolator () { if (animationOn) { if (linear) { splineInterpolator.setEnable(false); linearInterpolator.setEnable(true); } else { linearInterpolator.setEnable(false); splineInterpolator.setEnable(true); } } } /* * Toggle animation */ public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == animateButton) { try { // toggle animation if (animationOn) { animationOn = false; splineInterpolator.setEnable(false); linearInterpolator.setEnable(false); animateButton.setLabel("Start Animation"); } else { animationOn = true; startInterpolator(); animateButton.setLabel("Stop Animation"); } } catch (Exception e) { System.err.println ("Exception " + e); } } } /* * Toggle the interpolators */ public void itemStateChanged (ItemEvent event) { Object source = event.getSource(); ItemSelectable ie = event.getItemSelectable(); if (source == interpChoice) { try { if (ie.getSelectedObjects()[0] == "Spline") { linear = false; } if (ie.getSelectedObjects()[0] == "Linear") { linear = true; } startInterpolator(); } catch (Exception e) { System.err.println ("Exception " + e); } } } /* * Adjust the speed of the animations */ public void adjustmentValueChanged (AdjustmentEvent e) { int value = e.getValue(); duration = 6000 - (500 * value); animAlpha.setIncreasingAlphaDuration(duration); } public static void main(String[] args) { Frame frame = new MainFrame(new SplineAnim(), 500, 600); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -