public void TestPlanarReflection() { using (OpenFileDialog d = new OpenFileDialog()) { if (d.ShowDialog() == DialogResult.OK) { SceneTests.InitializeScene(); var content = ContentImporter.Import(SceneManager.Scene, d.FileName); ReflectiveNodeTechnique technique = SceneManager.Scene.EnumerateNodesInPreOrden().Where(x => x.Technique is ReflectiveNodeTechnique).Select(x => (ReflectiveNodeTechnique)x.Technique).FirstOrDefault(); Engine.Presenter.Rendering += () => { var untranformed = Service.Require <RenderQuadEffect>(); var sprite = Service.Require <Sprite>(); GraphicDeviceFactory.Device.Ps.SetResource(0, technique.ReflectionTexture); untranformed.U.alpha = 1; untranformed.Technique = 1; sprite.Begin(); sprite.SetTrasform(untranformed, new Rectangle(0, 0, 256, 256), Matrix.Identity); sprite.DrawQuad(untranformed); sprite.End(); }; } } }
public void BlendAnimation() { SceneTests.InitializeScene(); ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\Lighting\armed.DAE") .OnSceneAttach(SceneManager.Scene); ContentImporter.ImportAnimation(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\Lighting\walk.DAE") .OnSceneAttach(SceneManager.Scene); var armed = SceneManager.Scene.AnimManager.Animations[0]; var walk = SceneManager.Scene.AnimManager.Animations[1]; Frame root = SceneManager.Scene.EnumerateNodesPosOrden().First(x => x.Type == FrameType.Bone); var resetter = new BonesResetter(root); var translation = root.LocalPosition; var cursorArmed = armed.GetCursor(0); var cursorWalk = walk.GetCursor(0); cursorArmed.Looping = AnimationLooping.Loop; cursorWalk.Looping = AnimationLooping.Loop; SceneManager.Scene.Dynamics.Add( new Dynamic(deltaT => { resetter.Reset(); walk.Update(deltaT, 0, 1, true); armed.Update(deltaT, 0, 1, true); root.LocalPosition = translation; root.CommitChanges(); }) ); }
public void AutomaticShadowMapping() { SceneTests.InitializeScene(); var content = ImportContent(); content.OnSceneAttach(SceneManager.Scene); //ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\nissan2.DAE"); //ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\talia.DAE"); if (SceneManager.Scene.Physics != null) { SceneManager.Scene.Physics.Enable = true; } if (SceneManager.Scene.Lights.Count == 0) { var light = new Light() { Diffuse = new Vector3(1, 1, 1), Specular = new Vector3(1, 1, 1), Type = LightType.Directional, Enable = true }; SceneManager.Scene.Create("DirectionalLight0", new FrameLight(light), localRotationEuler: new Euler(0, Numerics.ToRadians(70), 0)); } FrameLight.CreateShadowMapForAllLights(SceneManager.Scene); _technique = SceneManager.Scene.Lights.Where(x => x.Node.Technique is ShadowMapTechnique).Select(x => (ShadowMapTechnique)x.Node.Technique).FirstOrDefault(); _targetCamera = _technique.Camera; _technique.Bias = 0.9e-2f; SceneManager.Scene.AmbientLight.GroundColor = new Vector3(0, 0, 0); SceneManager.Scene.AmbientLight.SkyColor = new Vector3(0.2f, 0.2f, 0.2f); Engine.Presenter.Rendering += Presenter_Rendering; SceneManager.Scene.Dynamics.Add(new Dynamic(x => { if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D1)) { _technique.KernelSize = 3; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D2)) { _technique.KernelSize = 5; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D3)) { _technique.KernelSize = 7; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D4)) { _technique.KernelSize = 1; } })); }
ContentPackage ImportContent() { ContentPackage content = null; using (OpenFileDialog d = new OpenFileDialog()) { if (d.ShowDialog() == DialogResult.OK) { SceneTests.InitializeScene(); content = ContentImporter.Import(SceneManager.Scene, d.FileName); } } return(content); }
public void TestEnvironmentMap() { using (OpenFileDialog d = new OpenFileDialog()) { if (d.ShowDialog() == DialogResult.OK) { SceneTests.InitializeScene(); var content = ContentImporter.Import(SceneManager.Scene, d.FileName); var technique = SceneManager.Scene.EnumerateNodesInPreOrden().Where(x => x.Technique is EnvironmentMapTechnique).Select(x => (EnvironmentMapTechnique)x.Technique).FirstOrDefault(); Engine.Presenter.Rendering += () => { if (_rastState == null) { _rastState = GraphicDeviceFactory.Device.CreateRasterizerState(new RasterizerDesc(true) { Fill = FillMode.Wireframe, Cull = CullMode.None }); } var device = GraphicDeviceFactory.Device; var effect = Service.Require <RenderMeshColorEffect>(); effect.U.ViewProj = SceneManager.Scene.ActiveCamera.ViewProj; effect.U.Color = new Vector4(1); device.RasterizerStack.Push(_rastState); device.PrimitiveTopology = IaPrimitive.TriangleList; device.SetVertexBuffer(0, _vb, 0); var oldtech = effect.Technique; foreach (var pass in effect.Passes(1)) { effect.Apply(pass); foreach (var camera in technique.Cameras) { effect.U.World = camera.InvViewProjection; device.Draw(_boxBuilder.Vertices.Length, 0); //device.DrawIndexedUser(0, boxBuilder.Vertices.Length, boxBuilder.Indices.Length / 3, boxBuilder.Indices, boxBuilder.Vertices); } } effect.EndPasses(); effect.Technique = oldtech; device.RasterizerStack.Pop(); }; } } }
public void ShowImage() { SceneTests.InitializeScene(); Engine.BackColor = new Color4(Color.Yellow.ToArgb()); ScreenDecal decal = new ScreenDecal(null); decal.Texture = GraphicDeviceFactory.Device.CreateTexture2DFromFile(@"F:\Pictures\Yo\DSC03854.JPG"); //decal.Texture = GraphicDeviceFactory.Device.CreateTexture2DFromFile(@"F:\Pictures\gray.jpg"); Engine.Presenter.RenderBegin += (p) => { ScreenDecalRender render = (ScreenDecalRender)decal.GetRender(); render.Draw(decal); }; //Import3DContent(); }
public void Import() { EngineState.Lighting.TransparencyEnable = true; TestSettings.UseFrameLines = false; using (OpenFileDialog d = new OpenFileDialog()) { if (d.ShowDialog() == DialogResult.OK) { SceneTests.InitializeScene(); ContentImporter.Import(Engine.Scene, d.FileName); if (Engine.Scene.Physics != null) { Engine.Scene.Physics.Enable = true; } var box = Engine.Scene.Nodes.First().BoundingBox; var boxMesh = Mesh.CreateBox(2, 2, 2); var rastState = GraphicDeviceFactory.Device.CreateRasterizerState(new RasterizerDesc(true) { Fill = FillMode.Wireframe, Cull = CullMode.None }); //Engine.Presenter.Rendering += () => // { // var device = GraphicDeviceFactory.Device; // var effect = Service.Require<RenderMeshIdEffect>(); // effect.Constants.World = Matrix.Scale(box.Extends) * box.GlobalPose; // effect.Constants.ViewProj = SceneManager.Scene.ActiveCamera.ViewProj; // effect.Constants.gId = new Vector4(1); // device.PushGraphicState<RasterizerState>(rastState); // boxMesh.Draw(device, effect); // device.PopGraphicState<RasterizerState>(); // }; } } }
public void PlayFileAnimation() { using (System.Windows.Forms.OpenFileDialog d = new System.Windows.Forms.OpenFileDialog()) { if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK) { SceneTests.InitializeScene(); var pk = ContentImporter.Import(SceneManager.Scene, d.FileName); pk.OnSceneAttach(SceneManager.Scene); var animation = SceneManager.Scene.AnimManager.Animations[0]; var resetter = new AnimationBonesResetter(animation); // new HeirarchyPoseResetter(root); var state = animation.GetCursor(0); state.Looping = AnimationLooping.Loop; SceneManager.Scene.Dynamics.Add(new Dynamic(deltaT => { resetter.Reset(); animation.Update(deltaT, state, 1, true); resetter.CommitChanges(); })); } } }
public void Import() { TestSettings.UseFrameLines = false; SceneTests.InitializeScene(); SceneManager.Scene.AmbientLight.SkyColor = new Vector3(0.0f, 0.0f, 0.0f); SceneManager.Scene.AmbientLight.GroundColor = new Vector3(0, 0, 0); RenderManager.PushTechnique <DefferedLigthing <DefferedLightingEffect> >(); DrawGBuffers <DefferedLightingEffect>(); SceneManager.Scene.Create("boxMesh", new FrameMesh(new BasicMaterial[] { new BasicMaterial { //Diffuse = new Vector3(0.2f , 0.2f, 0.2f) Diffuse = new Vector3(1f, 1f, 1f) //SpecularIntensity = 1, } }, Mesh.CreateBox(1000f, 0.1f, 1000f)), Matrix.Identity); CreateLights(); //ContentImporter.Import(SceneManager.Scene, @"E:\Modelos\2PINCHA CASA\terreno.DAE"); //ContentImporter.Import(SceneManager.Scene, @"I:\3D Media\Elementalist - Soul of the Ultimate Nation Character\Texture\rabbi.DAE"); ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\nissan2.DAE") .OnSceneAttach(SceneManager.Scene); //ContentImporter.Import(SceneManager.Scene, @"E:\Modelos\CITIZEN EXTRAS_FEMALE 02.dae"); }
public void MultipleImport() { SceneTests.InitializeScene(); ContentImporter.Import(Engine.Scene, @"C:\Users\ansel\Documents\3dsmax\export\talia.DAE"); ContentImporter.Import(Engine.Scene, @"E:\Modelos\2PINCHA CASA\terreno.DAE"); }
public void Import() { SceneTests.InitializeScene(); //RenderManager.PushTechnique<DefferedLigthing<DefferedLightingEffect>>(); //EngineState.Shadow.Enable = true; //EngineState.Shadow.ShadowMapping.Enable = true; //Deffered.DrawGBuffers<DefferedShadowRender>(); // var content = ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\shadowScene.DAE"); var content = ImportContent(); if (content == null) { return; } if (SceneManager.Scene.Physics != null) { SceneManager.Scene.Physics.Enable = true; } if (SceneManager.Scene.Lights.Count == 0) { var light = new Light() { Diffuse = new Vector3(1, 1, 1), Specular = new Vector3(1, 1, 1), Type = LightType.Directional, Enable = true }; SceneManager.Scene.Create("DirectionalLight0", new FrameLight(light), localRotationEuler: new Euler(0, Numerics.ToRadians(70), 0)); FrameLight.CreateShadowMapForAllLights(SceneManager.Scene); } _technique = SceneManager.Scene.Lights.Where(x => x.Node.Technique is ShadowMapTechnique).Select(x => (ShadowMapTechnique)x.Node.Technique).FirstOrDefault(); if (_technique == null) { FrameLight.CreateShadowMapForAllLights(SceneManager.Scene); _technique = SceneManager.Scene.Lights.Where(x => x.Node.Technique is ShadowMapTechnique).Select(x => (ShadowMapTechnique)x.Node.Technique).FirstOrDefault(); } _targetCamera = _technique.Camera; _technique.KernelSize = 3; //targetCamera.ZNear = 1; //targetCamera.ZFar = 1000; //targetCamera.AspectRatio = 1; //targetCamera.FieldOfView = Numerics.PIover6; //targetCamera.Type = ProjectionType.Perspective; //targetCamera.CommitChanges(); _technique.Bias = 0.9e-2f; SceneManager.Scene.AmbientLight.GroundColor = new Vector3(0, 0, 0); SceneManager.Scene.AmbientLight.SkyColor = new Vector3(0.2f, 0.2f, 0.2f); //EngineState.Lighting.Reflection.UseDefaultTechnique = true; Engine.Presenter.Rendering += Presenter_Rendering; SceneManager.Scene.Dynamics.Add(new Dynamic(x => { if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D1)) { _technique.KernelSize = 3; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D2)) { _technique.KernelSize = 5; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D3)) { _technique.KernelSize = 7; } else if (Engine.KeyBoard.IsKeyPressed(Igneel.Input.Keys.D4)) { _technique.KernelSize = 1; } })); // } //} }
public void EdgeFiltering() { SceneTests.InitializeScene(); //var content = ImportContent(); var content = ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\shadowScene.DAE"); EngineState.Shadow.ShadowMapping.PcfBlurSize = 5; content.OnSceneAttach(SceneManager.Scene); //ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\nissan2.DAE"); //ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\talia.DAE"); if (SceneManager.Scene.Physics != null) { SceneManager.Scene.Physics.Enable = true; } //if (SceneManager.Scene.Lights.Count == 0) //{ // var light = new Light() // { // Diffuse = new Vector3(1, 1, 1), // Specular = new Vector3(1, 1, 1), // Type = LightType.Directional, // Enable = true // }; // SceneManager.Scene.Create("DirectionalLight0", new LightInstance(light), // localRotationEuler: new Euler(0, Numerics.ToRadians(70), 0)); //} FrameLight.CreateShadowMapForAllLights(SceneManager.Scene); _technique = SceneManager.Scene.Lights.Where(x => x.Node.Technique is ShadowMapTechnique).Select(x => (ShadowMapTechnique)x.Node.Technique).FirstOrDefault(); _targetCamera = _technique.Camera; _technique.Bias = 0.9e-2f; SceneManager.Scene.AmbientLight.GroundColor = new Vector3(0, 0, 0); SceneManager.Scene.AmbientLight.SkyColor = new Vector3(0.2f, 0.2f, 0.2f); EngineState.Shadow.ShadowMapping.PcfBlurSize = 3; var edgeTechnique = new EdgeShadowFilteringTechnique(); RenderManager.PushTechnique(edgeTechnique); bool debug = true; if (debug) { Form form = new Form(); form.BackColor = Color.Blue; form.StartPosition = FormStartPosition.CenterScreen; form.Size = new System.Drawing.Size(edgeTechnique.ShadowFactorTex.Width, edgeTechnique.ShadowFactorTex.Height); form.SuspendLayout(); Canvas3D canvas = new Canvas3D() { Width = form.Width, Height = form.Height }; canvas.Dock = DockStyle.Fill; var presenter = canvas.CreateSwapChainPresenter(); form.Controls.Add(canvas); form.ResumeLayout(); Engine.RenderFrame += () => { presenter.Begin(new Color4(Color.Aqua.ToArgb())); var device = GraphicDeviceFactory.Device; device.Ps.SamplerStacks[0].Push(SamplerState.Linear); device.Blend = SceneTechnique.NoBlend; var texture = edgeTechnique.EdgeSrcTexture; RenderTexture(device, texture, width: texture.Width, height: texture.Height); device.Ps.SamplerStacks[0].Pop(); presenter.End(); }; form.Show(); } }
public void Import() { using (OpenFileDialog d = new OpenFileDialog()) { if (d.ShowDialog() == DialogResult.OK) { SceneTests.InitializeScene(); EngineState.Shadow.ShadowMapping.Bias = 0.9e-2f; ContentImporter.Import(SceneManager.Scene, d.FileName); //var content = ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\shadowScene.DAE"); if (SceneManager.Scene.Physics != null) { SceneManager.Scene.Physics.Enable = true; } var light = SceneManager.Scene.Lights.FirstOrDefault(); if (light != null) { light.Instance.Intensity = 3; //light.Instance.Specular = new Vector3(5, 5, 5); } SceneManager.Scene.AmbientLight.GroundColor = new Vector3(0, 0, 0); SceneManager.Scene.AmbientLight.SkyColor = new Vector3(1f, 1f, 1f); EngineState.Lighting.Hdr.Enable = true; EngineState.Lighting.Hdr.EnableBlueShift = false; EngineState.Lighting.Hdr.GlareType = GlareLibType.Disable; EngineState.Lighting.Hdr.MiddleGray = 0.5f; EngineState.Lighting.Hdr.BrightThreshold = 0.8f; EngineState.Lighting.Hdr.GaussianMultiplier = 0.4f; EngineState.Lighting.Hdr.GaussianDeviation = 0.8f; EngineState.Lighting.Hdr.StarBlendFactor = 0.2f; EngineState.Lighting.Hdr.CalculateEyeAdaptation = true; EngineState.Lighting.TransparencyEnable = true; EngineState.Lighting.Hdr.Technique.ComputeSamples(); EngineState.Lighting.Reflection.UseDefaultTechnique = true; EngineState.Lighting.Reflection.Enable = false; Form form = new Form(); form.SuspendLayout(); form.BackColor = Color.Blue; form.StartPosition = FormStartPosition.CenterScreen; form.Size = new System.Drawing.Size(800, 600); Canvas3D canvas = new Canvas3D() { Width = form.Width, Height = form.Height }; canvas.Dock = DockStyle.Fill; form.Controls.Add(canvas); form.ResumeLayout(); int width = form.Width / 3; int height = form.Height / 4; var untranformed = Service.Require <RenderQuadEffect>(); var sprite = Service.Require <Sprite>(); var device = GraphicDeviceFactory.Device; var hdrTechinique = EngineState.Lighting.Hdr.Technique; SwapChainPresenter presenter = canvas.CreateSwapChainPresenter(); Texture2DDesc desc = hdrTechinique.ToneMaps[0].Texture.Description; desc.Usage = ResourceUsage.Staging; desc.CpuAccessFlags = CPUAccessFlags.Read; desc.BindFlags = BindFlags.None; ITexture2D tex = GraphicDeviceFactory.Device.CreateTexture2D(desc); Action renderAction = () => { presenter.Begin(new Color4(Color.Aqua.ToArgb())); sprite.Begin(); var textures = new RenderTexture2D[4, 3] { { hdrTechinique.HdrScene, hdrTechinique.BrightPassFilter, hdrTechinique.Bloom[0] }, { hdrTechinique.ToneMaps[5], hdrTechinique.ToneMaps[4], hdrTechinique.ToneMaps[3] }, { hdrTechinique.ToneMaps[2], hdrTechinique.ToneMaps[1], hdrTechinique.ToneMaps[0] }, { hdrTechinique.StarSource, hdrTechinique.StarFinal, hdrTechinique.StarLines[1] } }; //textures[0, 1].SetTexture(0); //sprite.SetTrasform(untranformed, new Igneel.Rectangle(0, 0, width, height), Matrix.Identity); //sprite.DrawQuad(untranformed); GraphicDeviceFactory.Device.Ps.SetSampler(0, SamplerState.Point); for (int i = 0; i < textures.GetLength(0); i++) { for (int j = 0; j < textures.GetLength(1); j++) { if (textures[i, j] != null) { textures[i, j].SetTexture(0); sprite.SetTrasform(untranformed, new Igneel.Rectangle(width * j, height * i, width, height), Matrix.Identity); sprite.DrawQuad(untranformed); } } } sprite.End(); presenter.End(); //device.CopyTexture(tex, hdrTechinique.ToneMaps[0].Texture); //var map = tex.Map(0, MapType.Read); //unsafe //{ // float* pter = (float*)map.DataPointer; // float[] data = new float[map.RowPitch]; // Marshal.Copy(map.DataPointer, data, 0, data.Length); // float value = *pter; //} //tex.UnMap(0); }; Engine.RenderFrame += renderAction; form.Show(); form.FormClosing += (sender, arg) => { Engine.RenderFrame -= renderAction; presenter.Dispose(); }; } } }
public void Transitions_Idle_Walk() { SceneTests.InitializeScene(); const float startTimeWalk = 34f / 30f; const float endTimeWalk = 63f / 30f; const float durationWalk = endTimeWalk - startTimeWalk; const float startTimeIdle = 0; const float endTimeIdle = 0; const float durationIdle = endTimeIdle; const float blendDuration = 0.25f; Vector3 speedVector = new Vector3(0, 0, -1f); var content = ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\lighting_shadowed.DAE"); content.OnSceneAttach(SceneManager.Scene); Frame cameraNode = SceneManager.Scene.FindNode("camera1"); Frame root = SceneManager.Scene.EnumerateNodesPosOrden().First(x => x.Type == FrameType.Bone); BonesResetter resetter = new BonesResetter(root); Vector3 iniTrasnlation = root.LocalPosition; Vector3 translation = root.LocalPosition; var iniHeading = root.Heading; var animation = SceneManager.Scene.AnimManager.Animations[0]; animation.Sample(startTimeWalk); var iniKeyValue = root.LocalPosition; animation.Sample(endTimeWalk); var lastKeyValue = root.LocalPosition; Vector3 lastAnimTrans = root.LocalPosition; //new Vector3(); bool update = false; float deltaH = 0; Action <SecuenceNode, float> updateAction = (node, deltaT) => { if (!update) { lastAnimTrans = root.LocalPosition; update = true; return; } var cursor = node.Animations[0].Cursor; Vector3 disp; if (!cursor.TimeRestart) { disp = root.LocalPosition - lastAnimTrans; } else { disp = root.LocalPosition - (cursor.PlayDirection > 0 ? iniKeyValue : lastKeyValue); } disp = Vector3.TransformCoordinates(disp, Matrix.RotationY(deltaH)); translation += disp; lastAnimTrans = root.LocalPosition; }; SecuenceStateMachine states = new SecuenceStateMachine() .WithState(new SecuenceNode("idle", animation, startTimeIdle, durationIdle, AnimationLooping.Loop)) .WithState(new SecuenceNode("walk", animation, startTimeWalk, durationWalk, AnimationLooping.Loop) .Deactivating(node => update = false) .AfterUpdate(updateAction)) .WithTransition("idle", "walk", new SecuenceTransition(blendDuration) .FiredWhen(t => { var destNode = t.DestNode; if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow)) { destNode.PlayDirection = 1; update = false; return(true); } else if (Engine.KeyBoard.IsKeyPressed(Keys.Downarrow)) { destNode.PlayDirection = -1; update = false; return(true); } return(false); }) .WhenBlending((t, dt) => { Vector3 disp = new Vector3(); var cursor = t.DestNode.Animations[0].Cursor; if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow)) { disp = speedVector; } else { disp = -speedVector; } translation += Vector3.TransformCoordinates(disp, Matrix.RotationY(deltaH)); })) .WithTransition("walk", "idle", new SecuenceTransition(blendDuration) .FiredWhen(t => { if (!Engine.KeyBoard.IsKeyPressed(Keys.Uparrow) && !Engine.KeyBoard.IsKeyPressed(Keys.Downarrow)) { update = false; return(true); } return(false); }) .WhenBlending((t, dt) => { })); SceneManager.Scene.Dynamics.Add( new Dynamic(deltaT => { var oldposition = root.GlobalPosition; float rotSpeed = Numerics.ToRadians(90f); if (Engine.KeyBoard.IsKeyPressed(Keys.Leftarrow)) { deltaH -= rotSpeed * deltaT; } else if (Engine.KeyBoard.IsKeyPressed(Keys.Rightarrow)) { deltaH += rotSpeed * deltaT; } resetter.Reset(); states.Update(deltaT); root.X = translation.X; root.Z = translation.Z; root.Heading = iniHeading + deltaH; root.ComputeLocalPose(); root.CommitChanges(); var displacement = root.GlobalPosition - oldposition; cameraNode.X += displacement.X; cameraNode.Z += displacement.Z; cameraNode.CommitChanges(); }) ); }
public void Automata_Idle_Walk() { SceneTests.InitializeScene(); const float startTimeWalk = 34f / 30f; const float endTimeWalk = 63f / 30f; const float durationWalk = endTimeWalk - startTimeWalk; const float startTimeIdle = 0; const float endTimeIdle = 0; const float durationIdle = endTimeIdle; const float blendDuration = 0.25f; Vector3 speedVector = new Vector3(0, 0, -1f); ContentImporter.Import(SceneManager.Scene, @"C:\Users\ansel\Documents\3dsmax\export\lighting_shadowed.DAE") .OnSceneAttach(SceneManager.Scene); Frame cameraNode = SceneManager.Scene.FindNode("camera1"); Frame root = SceneManager.Scene.EnumerateNodesPosOrden().First(x => x.Type == FrameType.Bone); BonesResetter resetter = new BonesResetter(root); Vector3 translation = root.LocalPosition; var iniHeading = root.Heading; var animation = SceneManager.Scene.AnimManager.Animations[0]; animation.Sample(startTimeWalk); var iniKeyValue = root.LocalPosition; animation.Sample(endTimeWalk); var lastKeyValue = root.LocalPosition; Vector3 lastAnimTrans = root.LocalPosition; //new Vector3(); bool update = false; float deltaH = 0; KeyFrameAnimationPlayback idle = new KeyFrameAnimationPlayback(animation, startTimeIdle, durationIdle, AnimationLooping.Loop); KeyFrameAnimationPlayback walk = new KeyFrameAnimationPlayback(animation, startTimeWalk, durationWalk, AnimationLooping.Loop); AnimationTransition idleWalk = new AnimationTransition(idle, walk, blendDuration); AnimationTransition walkIdle = new AnimationTransition(walk, idle, blendDuration); var walkCursor = walk.GetCursor(animation); Action <float> updateAction = (deltaT) => { walk.Update(deltaT); if (!update) { lastAnimTrans = root.LocalPosition; update = true; return; } var cursor = walkCursor; if (!cursor.TimeRestart) { _disp = root.LocalPosition - lastAnimTrans; } else { _disp = root.LocalPosition - (cursor.PlayDirection > 0 ? iniKeyValue : lastKeyValue); } _disp = Vector3.TransformCoordinates(_disp, Matrix.RotationY(deltaH)); translation += _disp; lastAnimTrans = root.LocalPosition; }; Automata stateMachine = new Automata() .AddState("idle", x => idle.Update(x)) .AddState("idle-walk", dt => { idleWalk.Update(dt); _disp = new Vector3(); if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow)) { _disp = speedVector; } else { _disp = -speedVector; } _disp = Vector3.TransformCoordinates(_disp, Matrix.RotationY(deltaH)); }) .AddState("walk", updateAction) .AddState("walk-idle", x => walkIdle.Update(x)) .AddTransition("idle", "idle-walk", x => { if (Engine.KeyBoard.IsKeyPressed(Keys.Uparrow)) { walk.FirstPlayback.Cursor.PlayDirection = 1; update = false; return(true); } else if (Engine.KeyBoard.IsKeyPressed(Keys.Downarrow)) { walk.FirstPlayback.Cursor.PlayDirection = -1; update = false; return(true); } return(false); }) .AddTransition("idle-walk", "walk", x => idleWalk.TransitionComplete) .AddTransition("walk", "walk-idle", x => !Engine.KeyBoard.IsKeyPressed(Keys.Uparrow) && !Engine.KeyBoard.IsKeyPressed(Keys.Downarrow)) .AddTransition("walk-idle", "idle", x => walkIdle.TransitionComplete); SceneManager.Scene.Dynamics.Add(new Dynamic(deltaT => { //store position previus animation var localPosition = root.LocalPosition; _disp = new Vector3(); float rotSpeed = Numerics.ToRadians(90f); if (Engine.KeyBoard.IsKeyPressed(Keys.Leftarrow)) { deltaH -= rotSpeed * deltaT; } else if (Engine.KeyBoard.IsKeyPressed(Keys.Rightarrow)) { deltaH += rotSpeed * deltaT; } resetter.Reset(); stateMachine.Update(deltaT); //root.Tx = translation.X; //root.Tz = translation.Z; root.X = localPosition.X + _disp.X; root.Z = localPosition.Z + _disp.Z; root.Heading = iniHeading + deltaH; root.ComputeLocalPose(); root.CommitChanges(); //var displacement = root.GlobalPose.Translation - localPosition; //cameraNode.Tx += displacement.X; //cameraNode.Tz += displacement.Z; cameraNode.X += _disp.X; cameraNode.Z += _disp.Z; cameraNode.CommitChanges(); })); }