private static Matrix ComputeProjViewMatrix(Size clientSize) { var verticalFov = (float)Math.PI / 4; var aspect = clientSize.Width / (float)clientSize.Height; var proj = MatrixCM.PerspectiveFovRH(verticalFov, aspect, 500.0f, 3000.0f); var view = MatrixCM.ViewLookAtRH(cameraTarget + cameraOffset, cameraTarget, cameraUp); return(proj * view); }
public static Canvas3DDebugMultiCanvasHost CreateAndShowCanvas(Size size) { var sceneQueue = new ConcurrentQueue <IScene>(); var scenes = new List <IScene>(); var activeSceneIndex = -1; GraphicsLoop graphicsLoop = null; IPresetsStore presets = null; var initLatch = new ManualResetEvent(false); var thread = new Thread(() => { graphicsLoop = GraphicsLoop.CreateWithNewWindow(size, InitFlags.EnableDebugStats); presets = graphicsLoop.Presets; initLatch.Set(); var rotation = 80 * Math.PI / 180.0; var lookat = new DoubleVector3(0, 0, 0); //var lookat = new DoubleVector3(0, 0, 0); // originally offset -10, -100, 70) //var offset = new DoubleVector3(-100, 100, 200) * 7;// DoubleVector3.FromRadiusAngleAroundXAxis(400, rotation) + new DoubleVector3(100, -50, -100); // var offset = new DoubleVector3(-10, -100, 70) * 30;// DoubleVector3.FromRadiusAngleAroundXAxis(400, rotation) + new DoubleVector3(100, -50, -100); // var offset = new DoubleVector3(-10, -100, 30) * 30;// DoubleVector3.FromRadiusAngleAroundXAxis(400, rotation) + new DoubleVector3(100, -50, -100); var offset = new DoubleVector3(0, -30, 200) * 5; // DoubleVector3.FromRadiusAngleAroundXAxis(400, rotation) + new DoubleVector3(100, -50, -100); var up = new DoubleVector3(-1, 0, 0).Cross(offset).ToUnit(); //DoubleVector3.FromRadiusAngleAroundXAxis(1, rotation - Math.PI / 2); Console.WriteLine(offset); IScene lastScene = null; while (graphicsLoop.IsRunning(out var renderer, out var input)) { while (sceneQueue.TryDequeue(out var res)) { res.SetCamera(Vector3.Zero, Matrix.Identity); scenes.Add(res); if (activeSceneIndex == scenes.Count - 2) { activeSceneIndex = scenes.Count - 1; } } var scene = activeSceneIndex == -1 ? new Scene() : scenes[activeSceneIndex]; lock (scene) { var view = MatrixCM.ViewLookAtRH(ToV3(lookat + offset), ToV3(lookat), ToV3(up)); var verticalFov = 105.0f * (float)Math.PI / 180.0f; var aspect = size.Width / (float)size.Height; var proj = MatrixCM.PerspectiveFovRH(verticalFov, aspect, 1.0f, 10000.0f); void DrawAxes(Matrix transform, float scale = 1.0f) { float length = 1.0f * scale; float thickness = 0.06f * scale; scene.AddRenderable( graphicsLoop.Presets.UnitCube, transform * MatrixCM.Translation(length / 2, 0, 0) * MatrixCM.Scaling(length, thickness, thickness), new MaterialDescription { Resources = { BaseColor = Color.Red }, Properties = { Metallic = 0.0f, Roughness = 0.04f }, }); scene.AddRenderable( graphicsLoop.Presets.UnitCube, transform * MatrixCM.Translation(0, length / 2, 0) * MatrixCM.Scaling(thickness, length, thickness), new MaterialDescription { Resources = { BaseColor = Color.Lime }, Properties = { Metallic = 0.0f, Roughness = 0.04f }, }); scene.AddRenderable( graphicsLoop.Presets.UnitCube, transform * MatrixCM.Translation(0, 0, length / 2) * MatrixCM.Scaling(thickness, thickness, length), new MaterialDescription { Resources = { BaseColor = Color.Blue }, Properties = { Metallic = 0.0f, Roughness = 0.04f }, }); } if (scene != lastScene) { lastScene = scene; scene.SetCamera(ToV3(lookat + offset), Matrix.Multiply(proj, view)); scene.AddSpotlight( ToV3(lookat + offset * 3), ToV3(lookat), ToV3(up), (float)Math.PI * 0.49f, Color.White, 100f, 100000.0f, 1, 1, 1000); //DrawAxes(Matrix.Identity, 200); DrawAxes(MatrixCM.Translation((float)lookat.X, (float)lookat.Y, (float)lookat.Z), 200); //scene.AddRenderable( // graphicsLoop.Presets.UnitCube, // MatrixCM.Translation(1200, 500, 0) * MatrixCM.Scaling(100.0f), // new MaterialDescription { // Resources = { BaseColor = Color.White }, // Properties = { Metallic = 0.0f, Roughness = 0.04f }, // }); } renderer.RenderScene(scene.ExportSnapshot()); } } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); initLatch.WaitOne(); return(new Canvas3DDebugMultiCanvasHost(sceneQueue, graphicsLoop.GraphicsFacade, presets)); }
private static void Render(Scene scene, IRenderContext renderer) { scene.SetCamera(cameraTarget + cameraOffset, ComputeProjViewMatrix(graphicsLoop.Form.ClientSize)); scene.AddRenderable( graphicsLoop.Presets.UnitCube, Matrix.Identity, SomewhatRough, SDXColor.White); var terrainOverlayNetwork = game.TerrainService.CompileSnapshot().OverlayNetworkManager.CompileTerrainOverlayNetwork(0); foreach (var node in terrainOverlayNetwork.TerrainNodes) { if (!lgvMeshesByLgvGuid.TryGetValue(node.LocalGeometryView.Guid, out var mesh)) { VertexPositionNormalColorTexture[] F(Triangle3 triangle) => triangle.Points.Map( p => new VertexPositionNormalColorTexture( new SDXVector3((float)p.X, (float)p.Y, 0), -SDXVector3.UnitZ, SDXColor.White, new SDXVector2(0, 0))); var triangleList = node.LocalGeometryView.Triangulation.Islands .SelectMany(i => i.Triangles) .SelectMany(triangle => F(triangle).Concat(F(triangle).Reverse())) .ToArray(); mesh = graphicsLoop.GraphicsFacade.CreateMesh(triangleList); lgvMeshesByLgvGuid.Add(node.LocalGeometryView.Guid, mesh); } scene.AddRenderable( mesh, ConvertSystemNumericsToSharpDX(Matrix4x4.Transpose(node.SectorNodeDescription.WorldTransform)), SomewhatRough, SDXColor.White); } if (terrainOverlayNetwork.TryFindTerrainOverlayNode(baddie.MovementComponent.WorldPosition, out var n)) { debugCanvas.DrawCrossSectorVisibilityPolygon(n, baddie.MovementComponent.LocalPositionIv2); } else { Console.WriteLine("Err, can't find pos?"); } debugCanvas.Transform = Matrix4x4.Identity; debugCanvas.DrawLineStrip(fred, StrokeStyle.RedThick5Solid); scene.AddRenderable( graphicsLoop.Presets.UnitSphere, MatrixCM.Translation(player.MovementComponent.WorldPosition.ToSharpDX()) * MatrixCM.Scaling(player.MovementComponent.BaseRadius * 2), SomewhatRough, SDXColor.Lime); scene.AddRenderable( graphicsLoop.Presets.UnitSphere, MatrixCM.Translation(baddie.MovementComponent.WorldPosition.ToSharpDX()) * MatrixCM.Scaling(baddie.MovementComponent.BaseRadius * 2), SomewhatRough, SDXColor.Red); foreach (var rock in rocks) { scene.AddRenderable( graphicsLoop.Presets.UnitSphere, MatrixCM.Translation(rock.MovementComponent.WorldPosition.ToSharpDX()) * MatrixCM.Scaling(rock.MovementComponent.BaseRadius * 2), SomewhatRough, SDXColor.Brown); } debugCanvas.DrawEntityPaths(game.EntityService); var snapshot = scene.ExportSnapshot(); renderer.RenderScene(snapshot); snapshot.ReleaseReference(); }