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