private void Viewport_OnStartRendering(object sender, EventArgs e) { bool isGoingOut = true; bool isAddingNode = false; RenderLoop.Run(window, () => { if (resizeRequested) { viewport.Resize(window.ClientSize.Width, window.ClientSize.Height); resizeRequested = false; return; } var pos = camera.Position; var t = Stopwatch.GetTimestamp(); var elapse = t - previousTime; previousTime = t; cameraController.OnTimeStep(); viewport.Render(); #if TESTADDREMOVE if (groupSphere.Items.Count > 0 && !isAddingNode) { groupSphere.RemoveChildNode(groupSphere.Items.First()); if (groupSphere.Items.Count == 0) { isAddingNode = true; Console.WriteLine($"{effectsManager.GetResourceCountSummary()}"); groupSphere.AddChildNode(groupBox); groupSphere.AddChildNode(groupPoints); groupPoints.AddChildNode(groupLines); } } else { var materialCount = materialList.Length; var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50))); groupSphere.AddChildNode(new MeshNode() { Geometry = box, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back }); transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20))); groupSphere.AddChildNode(new MeshNode() { Geometry = sphere, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back }); if (groupSphere.Items.Count > NumItems) { isAddingNode = false; } } #endif }); }
/// <summary> /// Can be called by different thread. /// </summary> /// <returns></returns> public BitmapSource Render() { lock (viewport) { // Remove existing scene and create an new scene currentScene?.RemoveSelf(); currentScene = new GroupNode(); GenerateSomeMesh(currentScene); viewport.Items.AddChildNode(currentScene); viewport.Render(); using var bitmapStream = viewport.RenderToBitmapStream(); bitmapStream.Position = 0; var frame = BitmapFrame.Create(bitmapStream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad); return(frame); } }
private void Viewport_OnStartRendering(object sender, EventArgs e) { bool isGoingOut = true; RenderLoop.Run(window, () => { if (resizeRequested) { viewport.Resize(window.Width, window.Height); resizeRequested = false; return; } var pos = camera.Position; var t = Stopwatch.GetTimestamp(); var elapse = t - previousTime; previousTime = t; var angle = ((double)elapse / Stopwatch.Frequency) * 0.05; var camRotate = Matrix.RotationAxis(Vector3.UnitY, (float)(angle * Math.PI)); camera.Position = Vector3.TransformCoordinate(pos, camRotate); camera.LookDirection = -camera.Position; if (isGoingOut) { camera.Position += 0.05f * Vector3.Normalize(camera.Position); if (camera.Position.LengthSquared() > 10000) { isGoingOut = false; } } else { camera.Position -= 0.05f * Vector3.Normalize(camera.Position); if (camera.Position.LengthSquared() < 2500) { isGoingOut = true; } } viewport.Render(); viewport.InvalidateRender(); }); }
private void Viewport_OnStartRendering(object sender, EventArgs e) { bool isGoingOut = true; bool isAddingNode = false; RenderLoop.Run(window, () => { if (resizeRequested) { viewport.Resize(window.Width, window.Height); resizeRequested = false; return; } var pos = camera.Position; var t = Stopwatch.GetTimestamp(); var elapse = t - previousTime; previousTime = t; var angle = ((double)elapse / Stopwatch.Frequency) * 0.05; var camRotate = Matrix.RotationAxis(Vector3.UnitY, (float)(angle * Math.PI)); camera.Position = Vector3.TransformCoordinate(pos, camRotate); camera.LookDirection = -camera.Position; if (isGoingOut) { camera.Position += 0.05f * Vector3.Normalize(camera.Position); if (camera.Position.LengthSquared() > 10000) { isGoingOut = false; } } else { camera.Position -= 0.05f * Vector3.Normalize(camera.Position); if (camera.Position.LengthSquared() < 2500) { isGoingOut = true; } } viewport.Render(); viewport.InvalidateRender(); #if TESTADDREMOVE if (groupSphere.Items.Count > 0 && !isAddingNode) { groupSphere.RemoveChildNode(groupSphere.Items.First()); if (groupSphere.Items.Count == 0) { isAddingNode = true; Console.WriteLine($"{effectsManager.GetResourceCountSummary()}"); groupSphere.AddChildNode(groupBox); groupSphere.AddChildNode(groupPoints); groupPoints.AddChildNode(groupLines); } } else { var materialCount = materialList.Length; var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50))); groupSphere.AddChildNode(new MeshNode() { Geometry = box, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back }); transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20))); groupSphere.AddChildNode(new MeshNode() { Geometry = sphere, Material = materialList[groupSphere.Items.Count % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back }); if (groupSphere.Items.Count > NumItems) { isAddingNode = false; } } #endif }); }