public CmdResult Execute(ExtendCmdData cmdData, ref string message) { var mainForm = cmdData.MainForm as Form; var viewForm = cmdData.ViewForm as IViewForm; if (viewForm == null) { return(CmdResult.Cancel); } var osgView = viewForm.View as ZfOsgViewCtrl; var osgObj = osgView.OsgObj; // 读取模型 string osgFileName; if (!DialogUtil.OpenOSG(out osgFileName)) { return(CmdResult.Cancel); } var node = OsgDB._.readNodeFile(osgFileName); var sequence = new Sequence(); for (int i = 0; i < 24 * 2; i++) { var matrixd = Matrixf.rotate((float)(Math.PI / 12 * 2) * i, new Vec3d(0, 0, 1)); var matrixTransform = new MatrixTransform(matrixd); matrixTransform.addChild(node); sequence.addChild(matrixTransform, i); } //设置帧动画持续的时间 sequence.setInterval(Sequence.LoopMode.LOOP, 0, -1); //设置播放的速度及重复的次数 sequence.setDuration(1.0f / 24, 10); sequence.setLoopMode(Sequence.LoopMode.LOOP); sequence.setMode(Sequence.SequenceMode.START); sequence.Name = "sequence"; osgObj.AddOrReplaceModel("Models", sequence); return(CmdResult.Succeed); }
public CmdResult Execute(ExtendCmdData cmdData, ref string message) { var mainForm = cmdData.MainForm as Form; var viewForm = cmdData.ViewForm as IViewForm; if (viewForm == null) { return(CmdResult.Cancel); } var osgView = viewForm.View as ZfOsgViewCtrl; var osgObj = osgView.OsgObj; var skelroot = new Skeleton(); // 骨骼动画 skelroot.setDefaultUpdateCallback(); var root = new Bone(); // 骨骼 root.setInvBindMatrixInSkeletonSpace(Matrixd.inverse(Matrixd.translate(-1.0f, 0.0f, 0.0f))); root.setName("root"); var pRootUpdate = new UpdateBone("root"); // 骨骼更新器 pRootUpdate.getStackedTransforms().push_back(new StackedTranslateElement("translate", new Vec3f(-1.0f, 0.0f, 0.0f))); root.setUpdateCallback(pRootUpdate); var right0 = new Bone(); right0.setInvBindMatrixInSkeletonSpace(Matrixd.inverse(Matrixd.translate(0.0f, 0.0f, 0.0f))); right0.setName("right0"); var pRight0Update = new UpdateBone("right0"); pRight0Update.getStackedTransforms().push_back(new StackedTranslateElement("translate", new Vec3f(1.0f, 0.0f, 0.0f))); pRight0Update.getStackedTransforms().push_back(new StackedRotateAxisElement("rotate", new Vec3f(0.0f, 0.0f, 1.0f), 0.0)); right0.setUpdateCallback(pRight0Update); var right1 = new Bone(); right1.setInvBindMatrixInSkeletonSpace(Matrixd.inverse(Matrixd.translate(1.0f, 0.0f, 0.0f))); right1.setName("right1"); var pRight1Update = new UpdateBone("right1"); pRight1Update.getStackedTransforms().push_back(new StackedTranslateElement("translate", new Vec3f(1.0f, 0.0f, 0.0f))); pRight1Update.getStackedTransforms().push_back(new StackedRotateAxisElement("rotate", new Vec3f(0.0f, 0.0f, 1.0f), 0.0)); right1.setUpdateCallback(pRight1Update); root.addChild(right0); right0.addChild(right1); skelroot.addChild(root); Group scene = new Group(); BasicAnimationManager manager = new BasicAnimationManager(); scene.setUpdateCallback(manager); Animation anim = new Animation(); { var keys0 = new FloatKeyframeContainer(); // 关键帧容器 keys0.push_back(new FloatKeyframe(0.0, 0.0f)); keys0.push_back(new FloatKeyframe(3.0, (float)MathUtil.PI_2)); keys0.push_back(new FloatKeyframe(6.0, (float)MathUtil.PI_2)); var sampler = new FloatLinearSampler(); // 线性采样器 sampler.setKeyframeContainer(keys0); var channel = new FloatLinearChannel(sampler); // 通道 channel.setName("rotate"); channel.setTargetName("right0"); anim.addChannel(channel); } { var keys1 = new FloatKeyframeContainer(); // 关键帧容器 keys1.push_back(new FloatKeyframe(0.0, 0.0f)); keys1.push_back(new FloatKeyframe(3.0, 0.0f)); keys1.push_back(new FloatKeyframe(6.0, (float)MathUtil.PI_2)); var sampler = new FloatLinearSampler(); // 线性采样器 sampler.setKeyframeContainer(keys1); var channel = new FloatLinearChannel(sampler); // 通道 channel.setName("rotate"); channel.setTargetName("right1"); anim.addChannel(channel); } manager.registerAnimation(anim); manager.buildTargetReference(); // let's start ! manager.playAnimation(anim); // we will use local data from the skeleton MatrixTransform rootTransform = new MatrixTransform(); rootTransform.setMatrix(Matrixf.rotate((float)MathUtil.PI_2, new Vec3f(1.0f, 0.0f, 0.0f))); right0.addChild(createAxis()); right0.setDataVariance(Osg.Object.DataVariance.DYNAMIC); right1.addChild(createAxis()); right1.setDataVariance(Osg.Object.DataVariance.DYNAMIC); MatrixTransform trueroot = new MatrixTransform(); //trueroot.setMatrix(new Matrixf(root.getMatrixInBoneSpace().ptr())); trueroot.setMatrix(root.getMatrixInBoneSpace()); trueroot.addChild(createAxis()); trueroot.addChild(skelroot); trueroot.setDataVariance(Osg.Object.DataVariance.DYNAMIC); rootTransform.addChild(trueroot); scene.addChild(rootTransform); RigGeometry geom = createTesselatedBox(4, 4.0f); Geode geode = new Geode(); geode.Name = "2"; geode.addDrawable(geom); skelroot.addChild(geode); Vec3Array src = new Vec3Array(geom.getSourceGeometry().getVertexArray()); geom.setDataVariance(Osg.Object.DataVariance.DYNAMIC); initVertexMap(root, right0, right1, geom, src); scene.Name = "1"; osgObj.AddOrReplaceModel("Models", scene); osgObj.SetView(ViewMode.ShowAll); return(CmdResult.Succeed); }
public CmdResult Execute(ExtendCmdData cmdData, ref string message) { var mainForm = cmdData.MainForm as Form; var viewForm = cmdData.ViewForm as IViewForm; if (viewForm == null) { return(CmdResult.Cancel); } var osgView = viewForm.View as ZfOsgViewCtrl; var osgObj = osgView.OsgObj; var geode = new Geode(); geode.addDrawable(new ShapeDrawable(new Box(new Vec3f(0, 0, 0), 0.5f))); var trans = new MatrixTransform(); trans.Name = "AnimatedNode"; trans.setDataVariance(Osg.Object.DataVariance.DYNAMIC); trans.setMatrix(Matrixf.identity()); trans.addChild(geode); var root = new Group(); root.addChild(trans); var grp = new Group() { Name = "1" }; grp.addChild(root); var updatecb = new UpdateMatrixTransform("AnimatedCallback"); updatecb.getStackedTransforms().push_back(new StackedTranslateElement("position")); updatecb.getStackedTransforms().push_back(new StackedRotateAxisElement("euler", new Vec3f(1, 0, 0), 0)); trans.setUpdateCallback(updatecb); var mng = new BasicAnimationManager(); grp.setUpdateCallback(mng); var channelAnimation1 = new Vec3LinearChannel(); //name of the AnimationUpdateCallback channelAnimation1.setTargetName("AnimatedCallback"); //name of the StackedElementTransform for position modification channelAnimation1.setName("position"); //Create keyframes for (in this case linear) interpolation of a Vec3 var keyframeContainer = channelAnimation1.getOrCreateSampler().getOrCreateKeyframeContainer(); keyframeContainer.push_back(new Vec3Keyframe(0, new Vec3f(0, 0, 0))); keyframeContainer.push_back(new Vec3Keyframe(2, new Vec3f(1, 1, 0))); var anim1 = new Animation(); anim1.addChannel(channelAnimation1); anim1.setPlayMode(Animation.PlayMode.PPONG); //define the channel for interpolation of a float angle value var channelAnimation2 = new FloatLinearChannel(); //name of the AnimationUpdateCallback channelAnimation2.setTargetName("AnimatedCallback"); //name of the StackedElementTransform for position modification channelAnimation2.setName("euler"); //Create keyframes for (in this case linear) interpolation of a Vec3 var keyframeContainer2 = channelAnimation2.getOrCreateSampler().getOrCreateKeyframeContainer(); keyframeContainer2.push_back(new FloatKeyframe(0, 0)); keyframeContainer2.push_back(new FloatKeyframe(1.5, (float)(2 * Math.PI))); var anim2 = new Animation(); anim2.addChannel(channelAnimation2); anim2.setPlayMode(Animation.PlayMode.LOOP); // We register all animation inside the scheduler mng.registerAnimation(anim1); mng.registerAnimation(anim2); //start the animation mng.playAnimation(anim1); mng.playAnimation(anim2); osgObj.AddOrReplaceModel("Models", grp); osgObj.SetView(ViewMode.ShowAll); return(CmdResult.Succeed); }