public async Task Render_SimpleBitmap_WithTransparency() { await TestUtilities.InitializeWithGraphicsAsync(); using (var solidBrush = new SolidBrushResource(Color4.LightGray)) using (var bitmap = new StandardBitmapResource(new AssemblyResourceLink(this.GetType(), "Resources.Bitmaps.Logo.png"))) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync(graphics => { // 2D rendering is made here graphics.FillRectangle(graphics.ScreenBounds, solidBrush); graphics.DrawBitmap(bitmap, new Vector2(100f, 100f)); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var diff = BitmapComparison.CalculatePercentageDifference( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "SimpleBitmap_Transparency.png")); Assert.IsTrue(diff < 0.02, "Difference to reference image is to big!"); } }
public async Task Render_SimpleText_SimpleSingleColor() { await TestUtilities.InitializeWithGraphicsAsync(); using (var solidBrush = new SolidBrushResource(Color4.RedColor)) using (var textFormat = new TextFormatResource("Arial", 70)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync(graphics => { // 2D rendering is made here graphics.DrawText( $"Just a dummy text ;){Environment.NewLine}Just a dummy text ;)", textFormat, new RectangleF(10, 10, 512, 512), solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var diff = BitmapComparison.CalculatePercentageDifference( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "SimpleText_SingleColor.png")); Assert.IsTrue(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleRoundedRect_Filled_Solid() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.Gray)) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.FillRoundedRectangle( new RectangleF(10, 10, 512, 512), 30, 30, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_SimpleRoundedRectFilled); Assert.True(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleObject_D2D_Texture() { await TestUtilities.InitializeWithGraphicsAsync(); using (var solidBrush = new SolidBrushResource(Color4.Gray)) using (var textFormat = new TextFormatResource("Arial", 36)) using (var textBrush = new SolidBrushResource(Color4.RedColor)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { memRenderTarget.ClearColor = Color4.CornflowerBlue; // Get and configure the camera var camera = (PerspectiveCamera3D)memRenderTarget.Camera; camera.Position = new Vector3(0f, 5f, -7f); camera.Target = new Vector3(0f, 0f, 0f); camera.UpdateCamera(); // 2D rendering is made here var d2dDrawingLayer = new Custom2DDrawingLayer(graphics => { var d2dRectangle = new RectangleF(10, 10, 236, 236); graphics.Clear(Color4.LightBlue); graphics.FillRoundedRectangle( d2dRectangle, 30, 30, solidBrush); d2dRectangle.Inflate(-10, -10); graphics.DrawText("Hello Direct2D!", textFormat, d2dRectangle, textBrush); }); // Define scene await memRenderTarget.Scene.ManipulateSceneAsync(manipulator => { var resD2DTexture = manipulator.AddResource( _ => new Direct2DTextureResource(d2dDrawingLayer, 256, 256)); var resD2DMaterial = manipulator.AddStandardMaterialResource(resD2DTexture); var geoResource = manipulator.AddResource( _ => new GeometryResource(new CubeGeometryFactory())); var newMesh = manipulator.AddMeshObject(geoResource, resD2DMaterial); newMesh.RotationEuler = new Vector3(0f, EngineMath.RAD_90DEG / 2f, 0f); newMesh.Scaling = new Vector3(2f, 2f, 2f); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var isNearEqual = BitmapComparison.IsNearEqual( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "SimpleObject_D2DTexture.png")); Assert.IsTrue(isNearEqual, "Difference to reference image is to big!"); } // Finishing checks Assert.IsTrue(GraphicsCore.Current.MainLoop !.RegisteredRenderLoopCount == 0, "RenderLoops where not disposed correctly!"); }
public async Task Render_SimpleGeometry_Ellipse() { await TestUtilities.InitializeWithGraphicsAsync(); using (var solidBrush = new SolidBrushResource(Color4.LightGray)) using (var solidBrushBorder = new SolidBrushResource(Color4.Gray)) using (var ellipseGeometry = new EllipseGeometryResource(new Vector2(512, 512), 400f, 300f)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync(graphics => { // 2D rendering is made here graphics.DrawGeometry(ellipseGeometry, solidBrushBorder, 3f); graphics.FillGeometry(ellipseGeometry, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var diff = BitmapComparison.CalculatePercentageDifference( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "SimpleGeometry2D_Ellipse.png")); Assert.IsTrue(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleBitmap_WithTransparency() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.LightGray)) using (StandardBitmapResource bitmap = new StandardBitmapResource(new AssemblyResourceLink(this.GetType(), "Ressources.Bitmaps.Logo.png"))) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.FillRectangle(graphics.ScreenBounds, solidBrush); graphics.DrawBitmap(bitmap, new Vector2(100f, 100f)); }); //await AsyncResourceLoader.Current.WaitForAllFinishedAsync(); await memRenderTarget.AwaitRenderAsync(); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_SimpleBitmap_Transparency); Assert.True(diff < 0.02, "Difference to reference image is to big!"); } }
public async Task Render_SimpleGeometry_Ellipse() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.LightGray)) using (SolidBrushResource solidBrushBorder = new SolidBrushResource(Color4.Gray)) using (EllipseGeometryResource ellipseGeometry = new EllipseGeometryResource(new Vector2(512, 512), 400f, 300f)) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.DrawGeometry(ellipseGeometry, solidBrushBorder, 3f); graphics.FillGeometry(ellipseGeometry, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_SimpleGeometry2D_Ellipse); Assert.True(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleText_SimpleSingleColor() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.RedColor)) using (TextFormatResource textFormat = new TextFormatResource("Arial", 70)) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.DrawText( string.Format("Just a dummy text ;){0}Just a dummy text ;)", Environment.NewLine), textFormat, new RectangleF(10, 10, 512, 512), solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_SimpleText_SingleColor); Assert.True(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleRoundedRect_Filled_Solid() { await TestUtilities.InitializeWithGraphicsAsync(); using (var solidBrush = new SolidBrushResource(Color4.Gray)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync(graphics => { // 2D rendering is made here graphics.FillRoundedRectangle( new RectangleF(10, 10, 512, 512), 30, 30, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var diff = BitmapComparison.CalculatePercentageDifference( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "SimpleRoundedRectFilled.png")); Assert.IsTrue(diff < 0.2, "Difference to reference image is to big!"); } }
public async Task Render_SimpleRoundedRect_Filled_Over3D() { await TestUtilities.InitializeWithGraphicsAsync(); var brushColor = Color4.Gray; brushColor.ChangeAlphaTo(0.5f); using (var solidBrush = new SolidBrushResource(brushColor)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { memRenderTarget.ClearColor = Color4.CornflowerBlue; // Get and configure the camera var camera = (PerspectiveCamera3D)memRenderTarget.Camera; camera.Position = new Vector3(0f, 5f, -5f); camera.Target = new Vector3(0f, 1f, 0f); camera.UpdateCamera(); // Define scene await memRenderTarget.Scene.ManipulateSceneAsync(manipulator => { // Define object var resGeometry = manipulator.AddResource( _ => new GeometryResource(new CubeGeometryFactory())); var resMaterial = manipulator.AddResource( _ => new StandardMaterialResource(enableShaderGeneratedBorder: true)); var newMesh = manipulator.AddMeshObject(resGeometry, resMaterial); newMesh.RotationEuler = new Vector3(0f, EngineMath.RAD_90DEG / 2f, 0f); newMesh.Scaling = new Vector3(2f, 2f, 2f); newMesh.Color = Color4.Goldenrod; }); // Define 2D overlay await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync(graphics => { // 2D rendering is made here graphics.FillRoundedRectangle( new RectangleF(10, 10, 512, 512), 30, 30, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot var screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); // Calculate and check difference var diff = BitmapComparison.CalculatePercentageDifference( screenshot, TestUtilities.LoadBitmapFromResource("Drawing2D", "RoundedRectOver3D.png")); Assert.IsTrue(diff < 0.2, "Difference to reference image is to big!"); } // Finishing checks Assert.IsTrue(GraphicsCore.Current.MainLoop !.RegisteredRenderLoopCount == 0, "RenderLoops where not disposed correctly!"); }
public async Task Render_SimpleRoundedRect_Filled_Over3D() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.Gray.ChangeAlphaTo(0.5f))) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { memRenderTarget.ClearColor = Color4.CornflowerBlue; // Get and configure the camera PerspectiveCamera3D camera = memRenderTarget.Camera as PerspectiveCamera3D; camera.Position = new Vector3(0f, 5f, -5f); camera.Target = new Vector3(0f, 1f, 0f); camera.UpdateCamera(); // Define scene await memRenderTarget.Scene.ManipulateSceneAsync((manipulator) => { // Define object NamedOrGenericKey geoResource = manipulator.AddResource <GeometryResource>( () => new GeometryResource(new PalletStackType( NamedOrGenericKey.Empty, 10))); var newObject = manipulator.AddGeneric(geoResource); newObject.RotationEuler = new Vector3(0f, EngineMath.RAD_90DEG / 2f, 0f); newObject.Scaling = new Vector3(2f, 2f, 2f); newObject.Color = Color4.Goldenrod; newObject.EnableShaderGeneratedBorder(); }); // Define 2D overlay await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.FillRoundedRectangle( new RectangleF(10, 10, 512, 512), 30, 30, solidBrush); }); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_RoundedRectOver3D); Assert.True(diff < 0.2, "Difference to reference image is to big!"); } // Finishing checks Assert.True(GraphicsCore.Current.MainLoop.RegisteredRenderLoopCount == 0, "RenderLoops where not disposed correctly!"); }
public async Task MemoryRenderTarget_2DFallbackRendering() { await UnitTestHelper.InitializeWithGrahicsAsync(); using (GraphicsCore.AutomatedTest_NewTestEnviornment()) { GraphicsCore.Initialize( enableDebug: false, force2DFallback: true); Assert.True(GraphicsCore.IsInitialized); Polygon2D polygon = new Polygon2D(new Vector2[] { new Vector2(10, 10), new Vector2(900, 100), new Vector2(800, 924), new Vector2(50, 1014), new Vector2(10, 10) }); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.LightGray)) using (SolidBrushResource solidBrushBorder = new SolidBrushResource(Color4.Gray)) using (PolygonGeometryResource polygonGeometry = new PolygonGeometryResource(polygon)) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { // Perform rendering memRenderTarget.ClearColor = Color4.CornflowerBlue; await memRenderTarget.RenderLoop.Register2DDrawingLayerAsync((graphics) => { // 2D rendering is made here graphics.DrawGeometry(polygonGeometry, solidBrushBorder, 3f); graphics.FillGeometry(polygonGeometry, solidBrush); }); await memRenderTarget.AwaitRenderAsync(); // Take screenshot GDI.Bitmap screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); // Calculate and check difference float diff = BitmapComparison.CalculatePercentageDifference( screenshot, Properties.Resources.ReferenceImage_SimpleGeometry2D); Assert.True(diff < 0.2, "Difference to reference image is to big!"); } } }
public PerformanceMeasureDrawingLayer(float verticalPadding, ViewInformation view) { _view = view; _verticalPadding = verticalPadding; _lastTimeSpans = new List <TimeSpan>(); _lastRender = DateTime.UtcNow; // Define drawing resources _textFormatCentered = new TextFormatResource("Arial", 18f); _textFormatCentered.TextAlignment = TextAlignment.Center; _textFormatLeftAligned = new TextFormatResource("Arial", 18f); _textFormatLeftAligned.TextAlignment = TextAlignment.Leading; _backBrush = new SolidBrushResource(Color4.LightGray); _foreBrush = new SolidBrushResource(Color4.Black); _borderBrush = new SolidBrushResource(Color4.DarkGray); _enabled = true; _currentType = PerformanceDrawingLayerType.FramesPerSecond; _keysDown = new List <WinVirtualKey>(12); }
public async Task MemoryRenderTarget_2DInitError() { await TestUtilities.InitializeWithGraphicsAsync(); // Ensure that any async disposal is done before we create a new GraphicsCore await GraphicsCore.Current.MainLoop !.WaitForNextPassedLoopAsync(); await GraphicsCore.Current.MainLoop !.WaitForNextPassedLoopAsync(); GDI.Bitmap?screenshot = null; using (TestUtilities.FailTestOnInternalExceptions()) using (GraphicsCore.AutomatedTest_NewTestEnvironment()) { await GraphicsCore.Loader .ConfigureLoading(settings => settings.ThrowD2DInitDeviceError = true) .LoadAsync(); Assert.IsTrue(GraphicsCore.IsLoaded); Assert.IsFalse(GraphicsCore.Current.DefaultDevice !.Supports2D); using (var solidBrush = new SolidBrushResource(Color4.Gray)) using (var textFormat = new TextFormatResource("Arial", 36)) using (var textBrush = new SolidBrushResource(Color4.RedColor)) using (var memRenderTarget = new MemoryRenderTarget(1024, 1024)) { memRenderTarget.ClearColor = Color4.CornflowerBlue; // Get and configure the camera var camera = (PerspectiveCamera3D)memRenderTarget.Camera; camera.Position = new Vector3(0f, 5f, -7f); camera.Target = new Vector3(0f, 0f, 0f); camera.UpdateCamera(); // 2D rendering is made here var d2dDrawingLayer = new Custom2DDrawingLayer(graphics => { var d2dRectangle = new GDI.RectangleF(10, 10, 236, 236); graphics.Clear(Color4.LightBlue); graphics.FillRoundedRectangle( d2dRectangle, 30, 30, solidBrush); d2dRectangle.Inflate(-10, -10); graphics.DrawText("Hello Direct2D!", textFormat, d2dRectangle, textBrush); }); // Define scene await memRenderTarget.Scene.ManipulateSceneAsync(manipulator => { var resD2DTexture = manipulator.AddResource( _ => new Direct2DTextureResource(d2dDrawingLayer, 256, 256)); var resD2DMaterial = manipulator.AddStandardMaterialResource(resD2DTexture); var resGeometry = manipulator.AddResource( _ => new GeometryResource(new CubeGeometryFactory())); var newMesh = manipulator.AddMeshObject(resGeometry, resD2DMaterial); newMesh.RotationEuler = new Vector3(0f, EngineMath.RAD_90DEG / 2f, 0f); newMesh.Scaling = new Vector3(2f, 2f, 2f); }); // Take screenshot await memRenderTarget.AwaitRenderAsync(); screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); // TestUtilities.DumpToDesktop(screenshot, "Blub.png"); } } // Calculate and check difference Assert.IsNotNull(screenshot); var isNearEqual = BitmapComparison.IsNearEqual( screenshot, TestUtilities.LoadBitmapFromResource("ErrorHandling", "SimpleObject.png")); Assert.IsTrue(isNearEqual, "Difference to reference image is to big!"); }
/// <summary> /// Called when the sample has to startup. /// </summary> /// <param name="targetRenderLoop">The target render loop.</param> public override async Task OnStartupAsync(RenderLoop targetRenderLoop) { targetRenderLoop.EnsureNotNull(nameof(targetRenderLoop)); // Build dummy scene Scene scene = targetRenderLoop.Scene; Camera3DBase camera = targetRenderLoop.Camera as Camera3DBase; // Define 2D overlay m_solidBrush = new SolidBrushResource(Color4.LightSteelBlue.ChangeAlphaTo(0.7f)); m_textFormat = new TextFormatResource("Arial", 36); m_textBrush = new SolidBrushResource(Color4.RedColor); Action <Graphics2D> draw2DAction = (graphics) => { // 2D rendering is made here RectangleF d2dRectangle = new RectangleF( 10, 10, graphics.ScreenSize.Width - 20, graphics.ScreenSize.Height - 20); if (d2dRectangle.Width < 100) { return; } if (d2dRectangle.Height < 100) { return; } // Draw background rectangle graphics.FillRoundedRectangle( d2dRectangle, 30, 30, m_solidBrush); // Draw the text d2dRectangle.Inflate(-10, -10); d2dRectangle.Y = d2dRectangle.Y + 15f; graphics.DrawText("Hello Direct2D!", m_textFormat, d2dRectangle, m_textBrush); }; await targetRenderLoop.Scene.ManipulateSceneAsync((manipulator) => { // Add the 2D layer to the scene manipulator.AddDrawingLayer(draw2DAction); // Create floor SampleSceneBuilder.BuildStandardFloor( manipulator, Scene.DEFAULT_LAYER_NAME); // Create pallet geometry resource PalletType pType = new PalletType(); pType.ContentColor = Color4.Transparent; var resPalletGeometry = manipulator.AddResource <GeometryResource>( () => new GeometryResource(pType)); // Create pallet object GenericObject palletObject = manipulator.AddGeneric(resPalletGeometry); palletObject.Color = Color4.GreenColor; palletObject.EnableShaderGeneratedBorder(); palletObject.BuildAnimationSequence() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_180DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_360DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .CallAction(() => palletObject.RotationEuler = Vector3.Zero) .ApplyAndRewind(); }); // Configure camera camera.Position = new Vector3(2f, 2f, 2f); camera.Target = new Vector3(0f, 0.5f, 0f); camera.UpdateCamera(); }
/// <summary> /// Called when the sample has to startup. /// </summary> /// <param name="targetRenderLoop">The target render loop.</param> public override async Task OnStartupAsync(RenderLoop targetRenderLoop) { targetRenderLoop.EnsureNotNull(nameof(targetRenderLoop)); // Build dummy scene Scene scene = targetRenderLoop.Scene; Camera3DBase camera = targetRenderLoop.Camera as Camera3DBase; // 2D rendering is made here m_solidBrush = new SolidBrushResource(Color4.Gray); m_textFormat = new TextFormatResource("Arial", 36); m_textBrush = new SolidBrushResource(Color4.RedColor); Custom2DDrawingLayer d2dDrawingLayer = new Custom2DDrawingLayer((graphics) => { RectangleF d2dRectangle = new RectangleF(10, 10, 236, 236); graphics.Clear(Color4.LightBlue); graphics.FillRoundedRectangle( d2dRectangle, 30, 30, m_solidBrush); d2dRectangle.Inflate(-10, -10); graphics.DrawText("Hello Direct2D!", m_textFormat, d2dRectangle, m_textBrush); }); // Build 3D scene await targetRenderLoop.Scene.ManipulateSceneAsync((manipulator) => { // Create floor SampleSceneBuilder.BuildStandardFloor( manipulator, Scene.DEFAULT_LAYER_NAME); // Define Direct2D texture resource var resD2DTexture = manipulator.AddResource <Direct2DTextureResource>( () => new Direct2DTextureResource(d2dDrawingLayer, 256, 256)); var resD2DMaterial = manipulator.AddSimpleColoredMaterial(resD2DTexture); // Create pallet geometry resource PalletType pType = new PalletType(); pType.PalletMaterial = NamedOrGenericKey.Empty; pType.ContentMaterial = resD2DMaterial; var resPalletGeometry = manipulator.AddResource <GeometryResource>( () => new GeometryResource(pType)); // Create pallet object GenericObject palletObject = manipulator.AddGeneric(resPalletGeometry); palletObject.Color = Color4.GreenColor; palletObject.EnableShaderGeneratedBorder(); palletObject.BuildAnimationSequence() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_180DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_360DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .CallAction(() => palletObject.RotationEuler = Vector3.Zero) .ApplyAndRewind(); }); // Configure camera camera.Position = new Vector3(2f, 2f, 2f); camera.Target = new Vector3(0f, 0.5f, 0f); camera.UpdateCamera(); }
public async Task MemoryRenderTarget_2DInitError() { await UnitTestHelper.InitializeWithGrahicsAsync(); GDI.Bitmap screenshot = null; using (UnitTestHelper.FailTestOnInternalExceptions()) using (GraphicsCore.AutomatedTest_NewTestEnviornment()) using (GraphicsCore.AutomatedTest_ForceD2DInitError()) { GraphicsCore.Initialize(); Assert.True(GraphicsCore.IsInitialized); Assert.False(GraphicsCore.Current.DefaultDevice.Supports2D); using (SolidBrushResource solidBrush = new SolidBrushResource(Color4.Gray)) using (TextFormatResource textFormat = new TextFormatResource("Arial", 36)) using (SolidBrushResource textBrush = new SolidBrushResource(Color4.RedColor)) using (MemoryRenderTarget memRenderTarget = new MemoryRenderTarget(1024, 1024)) { memRenderTarget.ClearColor = Color4.CornflowerBlue; // Get and configure the camera PerspectiveCamera3D camera = memRenderTarget.Camera as PerspectiveCamera3D; camera.Position = new Vector3(0f, 5f, -7f); camera.Target = new Vector3(0f, 0f, 0f); camera.UpdateCamera(); // 2D rendering is made here Custom2DDrawingLayer d2dDrawingLayer = new Custom2DDrawingLayer((graphics) => { RectangleF d2dRectangle = new RectangleF(10, 10, 236, 236); graphics.Clear(Color4.LightBlue); graphics.FillRoundedRectangle( d2dRectangle, 30, 30, solidBrush); d2dRectangle.Inflate(-10, -10); graphics.DrawText("Hello Direct2D!", textFormat, d2dRectangle, textBrush); }); // Define scene await memRenderTarget.Scene.ManipulateSceneAsync((manipulator) => { var resD2DTexture = manipulator.AddResource <Direct2DTextureResource>( () => new Direct2DTextureResource(d2dDrawingLayer, 256, 256)); var resD2DMaterial = manipulator.AddSimpleColoredMaterial(resD2DTexture); var geoResource = manipulator.AddResource <GeometryResource>( () => new GeometryResource(new PalletType( palletMaterial: NamedOrGenericKey.Empty, contentMaterial: resD2DMaterial))); GenericObject newObject = manipulator.AddGeneric(geoResource); newObject.RotationEuler = new Vector3(0f, EngineMath.RAD_90DEG / 2f, 0f); newObject.Scaling = new Vector3(2f, 2f, 2f); }); // Take screenshot await memRenderTarget.AwaitRenderAsync(); screenshot = await memRenderTarget.RenderLoop.GetScreenshotGdiAsync(); //screenshot.DumpToDesktop("Blub.png"); } } // Calculate and check difference Assert.NotNull(screenshot); bool isNearEqual = BitmapComparison.IsNearEqual( screenshot, Properties.Resources.ReferenceImage_SimpleObject); Assert.True(isNearEqual, "Difference to reference image is to big!"); }
/// <summary> /// Called when the sample has to startup. /// </summary> /// <param name="targetRenderLoop">The target render loop.</param> public override async Task OnStartupAsync(RenderLoop targetRenderLoop) { targetRenderLoop.EnsureNotNull(nameof(targetRenderLoop)); // Build dummy scene Scene scene = targetRenderLoop.Scene; Camera3DBase camera = targetRenderLoop.Camera as Camera3DBase; // Create all objects for animation List <Vector2> starLocations = new List <Vector2>(); m_starBitmap = new StandardBitmapResource( new AssemblyResourceLink( typeof(SeeingSharpSampleResources), "Bitmaps.StarColored_128x128.png")); m_borderBrush = new SolidBrushResource(Color4.SteelBlue); Random starCreateRandomizer = new Random(); // 2D rendering is made here Custom2DDrawingLayer d2dDrawingLayer = new Custom2DDrawingLayer((graphics) => { // Draw background RectangleF d2dRectangle = new RectangleF(10, 10, 236, 236); graphics.Clear(Color4.LightBlue); // Dynamically create new stars if ((starLocations.Count < 50) && (starCreateRandomizer.Next(0, 100) <= 70)) { starLocations.Add(new Vector2( (float)starCreateRandomizer.Next(0, 256), -32f)); } // Update and draw all stars for (int loopStar = 0; loopStar < starLocations.Count; loopStar++) { Vector2 actLocation = starLocations[loopStar]; if (actLocation.Y > 270f) { starLocations.RemoveAt(loopStar); loopStar--; continue; } actLocation.Y = actLocation.Y + 4f; starLocations[loopStar] = actLocation; graphics.DrawBitmap( m_starBitmap, new RectangleF( actLocation.X - 16f, actLocation.Y - 16f, 32f, 32f), 0.6f, BitmapInterpolationMode.Linear); } // Draw a simple border graphics.DrawRectangle(graphics.ScreenBounds, m_borderBrush, 2f); }); // Build 3D scene await targetRenderLoop.Scene.ManipulateSceneAsync((manipulator) => { // Create floor SampleSceneBuilder.BuildStandardFloor( manipulator, Scene.DEFAULT_LAYER_NAME); // Define Direct2D texture resource var resD2DTexture = manipulator.AddResource <Direct2DTextureResource>( () => new Direct2DTextureResource(d2dDrawingLayer, 256, 256)); var resD2DMaterial = manipulator.AddSimpleColoredMaterial(resD2DTexture); // Create pallet geometry resource PalletType pType = new PalletType(); pType.PalletMaterial = NamedOrGenericKey.Empty; pType.ContentMaterial = resD2DMaterial; var resPalletGeometry = manipulator.AddResource <GeometryResource>( () => new GeometryResource(pType)); // Create pallet object GenericObject palletObject = manipulator.AddGeneric(resPalletGeometry); palletObject.Color = Color4.GreenColor; palletObject.EnableShaderGeneratedBorder(); palletObject.BuildAnimationSequence() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_180DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .RotateEulerAnglesTo(new Vector3(0f, EngineMath.RAD_360DEG, 0f), TimeSpan.FromSeconds(2.0)) .WaitFinished() .CallAction(() => palletObject.RotationEuler = Vector3.Zero) .ApplyAndRewind(); }); // Configure camera camera.Position = new Vector3(2f, 2f, 2f); camera.Target = new Vector3(0f, 0.5f, 0f); camera.UpdateCamera(); }