/Rendering/BasicTests/MaterialTests.cs
C# | 1329 lines | 900 code | 121 blank | 308 comment | 27 complexity | aa3bb33a8bf55d6b95d3f798d158aa30 MD5 | raw file
Possible License(s): Apache-2.0
- using System.Diagnostics;
- using Delta.ContentSystem;
- using Delta.ContentSystem.Rendering;
- using Delta.Engine;
- using Delta.Engine.Game;
- using Delta.Graphics.Basics;
- using Delta.InputSystem;
- using Delta.InputSystem.Devices;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Basics.Materials;
- using Delta.Rendering.Cameras;
- using Delta.Rendering.Enums;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Graphics;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.Rendering.BasicTests
- {
- /// <summary>
- /// Material tests
- /// </summary>
- [Category("Visual")]
- internal class MaterialTests
- {
- #region DrawDefaultMaterial
- /// <summary>
- /// Draw default material, has 10-15k fps, pretty good. All dynamic.
- /// </summary>
- [Test]
- public static void DrawDefaultMaterial()
- {
- Application.Start(delegate
- {
- Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
- });
- }
- #endregion
-
- #region DrawDefaultMaterialAutoRotated
- /// <summary>
- /// Draw default material auto rotated.
- /// </summary>
- [Test]
- public static void DrawDefaultMaterialAutoRotated()
- {
- float rotation = 0.0f;
-
- Application.Start(delegate
- {
- rotation += Time.Delta * 10.0f;
-
- Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f),
- rotation);
- });
- }
- #endregion
-
- #region DrawDefaultMaterialColoredFullscreen
- /// <summary>
- /// Draw default material with color on fullscreen. Testing bug #4481
- /// </summary>
- [Test]
- public static void DrawDefaultMaterialColoredFullscreen()
- {
- Material2DColored background = new Material2DColored(
- //Color.Red);
- "DeltaIcon");
- Application.Start(delegate
- {
- background.Draw(Rectangle.One);
- });
- }
- #endregion
-
- #region DrawDefaultMaterialWithLines
- /// <summary>
- /// Draw default material and some lines, can get up to 10k fps.
- /// </summary>
- [Test]
- public static void DrawDefaultMaterialWithLines()
- {
- // The default fallback material texture (4x4) should use no filtering
- Assert.False(Material2D.Default.diffuseMap.UseLinearFiltering);
- Application.BackgroundColor = Color.Unused;
-
- Application.Start(delegate
- {
- Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
-
- Line.Draw(new Point(0, 0.5f), new Point(1, 0.5f), Color.Red);
- Line.Draw(new Point(0.5f, 0), new Point(0.5f, 1), Color.Green);
- Line.Draw(new Point(0, 0), new Point(1, 1), Color.Yellow);
- });
- }
- #endregion
-
- #region DrawManyMaterialsPerformance
- /// <summary>
- /// Draw many materials performance test
- /// </summary>
- [Test]
- public static void DrawManyMaterialsPerformance()
- {
- // With new optimizations on PC we have ~1500 fps for 2500 draws with
- // 5000 polys (or 10000 quad vertices), so we have 7.5m poly/s (in
- // debug mode, in release mode it is almost 10m poly/s).
- // Note: When using 25000 draws per frame (50000 polys), it still
- // comes out to about 7.5m poly/s with ~150 fps.
-
- // New optimizations done 2011-03-20, mostly caching MaterialManager layers in Material now!
- // Performance for the DrawManyMaterialsPerformance unit test is about 4000fps (release, in debug still 2500fps) for 10000 vertices,
- // which is 40 million dynamic vertices with 60 million indices for 20 million dynamic polys
- // per second (not really dynamic anymore because of caching, but you can change them anytime)!
- // Other unit tests are also doing well, almost all simple tests easily reach 10000fps in debug mode!
- //OK: performance for this is horrible, 5000 polys and only 1000fps, it is all CPU limited for all the .Save methods used here (and new Point for each property there), which can be optimized! See MaterialTests.DrawManyMaterialsPerformance()!
- //OK: continue optimizing, first get this as fast as possible (even in debug mode, some checks are just too slow)
- //OK: then try to remember the values from last time and skip updating/adding and even the hash code check in geometry if nothing has changed at all (which is the case in 99.9% of the time for dynamic data).
- //OK: we should get a few thousand fps :)
- //OK: indices are always the same, why not calculate when creating indices?
- //OK: do most checks at initialization time of the render geometry (does not change after that)
- //OK: put in method, always the same, isn't it? same as creation!
- //OK: easy caching of GeometryData or RenderGeometryByAtlas does not work this easily, we need to cache the whole result of the render geometry tree, next time all steps have to be retraced and we need to increase the counters to make rendering work!
- //OK: all GeometryData initialization code is always the same and can be in a method too!
-
- /*performance result in iPad debug mode:
- 12.673 totalCpuTimeMs=5121 (95.36313%)
- 12.674 totalGpuTimeMs=249 (4.636871%)
- 12.675 Last frame OnRenderFrame time: 34ms (CPU: 34ms, GPU: 0ms)
- 12.675 Last frame Time.Delta: 0.03338146
- 12.678 Total Application Time: 12.17727s
- We got 5 sections that took in total: 6.317755s
- Section Time took: 0.007066727s (0.111855%), Calls: 294, first time called: 1.151879s
- Section Application delegate run code took: 5.47965s (86.73415%), Calls: 294, first time called: 1.154123s
- Section ContentManager took: 0.004089594s (0.06473176%), Calls: 294, first time called: 1.581513s
- Section OpenGLES20Graphic took: 0.008611083s (0.1362997%), Calls: 293, first time called: 1.623532s
- Section MaterialManager took: 0.818337s (12.95297%), Calls: 293, first time called: 2.007285s
- */
-
- // 5 layers * 50 * 10 = 2500 draws per frame (which is 5000 polygons or
- // 10000 vertices with 15000 indices being rendered, these are also the
- // limits for MaterialManager right now)!
- int numberOfLayerTypes = EnumHelper.GetCount<RenderLayer>();
- const int HorizontalDrawsPerLayer = 50;
- const int VerticalDrawsPerLayer = 10;
- const float DrawSize = 0.6f / HorizontalDrawsPerLayer;
-
- Material2D material = new Material2D(Content.EmptyName)
- {
- DrawLayer = RenderLayer.Text,
- };
-
- Application.Start(delegate
- {
- for (int layer = 0; layer < numberOfLayerTypes; layer++)
- {
- for (int y = 0; y < VerticalDrawsPerLayer; y++)
- {
- for (int x = 0; x < HorizontalDrawsPerLayer; x++)
- {
- material.Draw(new Rectangle(x / (float)HorizontalDrawsPerLayer,
- 0.1f + 0.15f * (layer + (y / (float)VerticalDrawsPerLayer)),
- DrawSize, DrawSize));
- }
- } // for
- } // for
- });
- }
- #endregion
-
- #region DrawTwoSameMaterials
- /// <summary>
- /// Helper unit test to check if Niko's Bug report about rendering the same
- /// image in a different order does not affect rendering. This test proves
- /// that it all works fine (he had different content caching bugs btw,
- /// non-loaded images were compared to loaded ones, this is already fixed
- /// in v0.8.3.2).
- /// </summary>
- [Test]
- public static void DrawTwoSameMaterials()
- {
- Material2D material1 = new Material2D("DeltaEngineLogo");
- Material2D material2 = new Material2D("DeltaEngineLogo");
- bool material2First = false;
- Application.Start(delegate
- {
- // All the same, just render material2 first if space was pressed,
- // otherwise render material1 first, then material2.
- if (material2First)
- {
- material2.Draw(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f));
- material1.Draw(ScreenSpace.DrawArea);
- }
- else
- {
- material1.Draw(ScreenSpace.DrawArea);
- material2.Draw(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f));
- }
-
- if (Input.Keyboard.SpaceReleased)
- {
- material2First = !material2First;
- }
- });
- }
- #endregion
-
- #region DrawAnimatedAndNormalImagesAlternating
- /// <summary>
- /// Draw Animated and Normal Images Alternating
- /// </summary>
- [Test]
- public static void DrawAnimatedAndNormalImagesAlternating()
- {
- Material2D material = new Material2D("ImageAnimation");
- Material2D staticImage = new Material2D("DeltaEngineLogo");
-
- bool showAnimated = true;
- Application.Start(delegate
- {
- staticImage.Draw(new Rectangle(0f, 0f, 0.2f, 0.2f));
-
- if (showAnimated)
- {
- material.Draw(new Rectangle(0.2f, 0.35f, 0.6f, 0.3f));
- }
- else
- {
- staticImage.Draw(new Rectangle(0.2f, 0.35f, 0.6f, 0.3f));
- }
-
- if (Time.CheckEvery(2))
- {
- showAnimated = !showAnimated;
- }
- });
- }
- #endregion
-
- #region DrawAnimatedImageWithGameTimePause
- /// <summary>
- /// Draw Animated image with game time pause.
- /// </summary>
- [Test]
- public static void DrawAnimatedImageWithGameTimePause()
- {
- Material2D material = new Material2D("ImageAnimation");
-
- Application.Start(delegate
- {
- material.Draw(Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.2f));
-
- if (Input.Keyboard.SpaceReleased)
- {
- if (GameTime.IsRunning)
- {
- GameTime.Pause();
- }
- else
- {
- GameTime.Start();
- }
- }
- });
- }
- #endregion
-
- #region DrawMaterialFlipped
- /// <summary>
- /// This test explains how to draw materials vertically or horizontally
- /// flipped. Internally the UVs are flipped, nothing else changes. You can
- /// combine this with other operations like rotation or coloring as well.
- /// </summary>
- [Test]
- public static void DrawMaterialFlipped()
- {
- Material2DColored logo = new Material2DColored("DeltaEngineLogo");
-
- Application.Start(delegate
- {
- logo.Draw(Rectangle.FromCenter(
- Point.Half + new Size(-0.5f, -0.5f) * logo.Size, logo.Size), 0,
- FlipMode.None);
- logo.Draw(Rectangle.FromCenter(
- Point.Half + new Size(0.5f, -0.5f) * logo.Size, logo.Size), 0,
- FlipMode.Horizontal);
- logo.Draw(Rectangle.FromCenter(
- Point.Half + new Size(-0.5f, 0.5f) * logo.Size, logo.Size), 0,
- FlipMode.Vertical);
- logo.Draw(Rectangle.FromCenter(
- Point.Half + new Size(0.5f, 0.5f) * logo.Size, logo.Size), 0,
- FlipMode.VerticalAndHorizontal);
- });
- }
- #endregion
-
- #region DrawMaterialRotatedAroundPoint
- /// <summary>
- /// Draw default material rotated around a custom point. As requested in
- /// http://forum.deltaengine.net/yaf_postsm1094_Rotate-a-Material2D.aspx
- /// </summary>
- [Test]
- public static void DrawMaterialRotatedAroundPoint()
- {
- float rotation = 0.0f;
-
- Application.Start(delegate
- {
- rotation += Time.Delta * 100.0f;
-
- Material2D.Default.Draw(new Rectangle(0.3f, 0.3f, 0.4f, 0.4f),
- rotation, new Point(0.3f, 0.3f));
- });
- }
- #endregion
-
- #region BlendModes
- /// <summary>
- /// Blend modes. We test the different blend modes with textures.
- /// </summary>
- [Test]
- public static void BlendModes()
- {
- // Test Additive BlendMode
- Material2D additive = new Material2D("DeltaAdditive");
-
- // Test LightEffect BlendMode
- Material2D lightEffect = new Material2D("BlendModesLightEffect");
-
- // Test AlphaTest BlendMode
- Material2D transparent = new Material2D(
- "DeltaIcon");
-
- // Test Substractive BlendMode
- Material2D substractive = new Material2D("DeltaSubtractive");
-
- // And draw them all
- Application.Start(delegate
- {
- Material2D.Default.Draw(Rectangle.FromCenter(Point.Half, 0.25f));
- additive.Draw(new Rectangle(new Point(0.5f - additive.Size.Width,
- 0.5f - additive.Size.Height), additive.Size));
- lightEffect.Draw(new Rectangle(new Point(0.5f,
- 0.5f - lightEffect.Size.Height), lightEffect.Size));
- transparent.Draw(new Rectangle(new Point(0.5f - transparent.Size.Width,
- 0.5f), transparent.Size));
- substractive.Draw(new Rectangle(new Point(0.5f,
- 0.5f), substractive.Size));
- });
- }
- #endregion
-
- #region DrawTransparentMaterial
- /// <summary>
- /// Draw transparent material
- /// </summary>
- [Test]
- public static void DrawTransparentMaterial()
- {
- Material2D material = new Material2D("DeltaIcon");
-
- Application.Start(delegate
- {
- material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
- });
- }
- #endregion
-
- #region DrawOpaqueMaterial
- /// <summary>
- /// Draw opaque material
- /// </summary>
- [Test]
- public static void DrawOpaqueMaterial()
- {
- Material2D opaqueMat = new Material2D("ImageAnimation");
- Application.BackgroundColor = Color.Red;
-
- Application.Start(delegate
- {
- opaqueMat.Draw(ScreenSpace.DrawArea);
- Font.DrawTopLeftInformation("Fps: " + Time.Fps);
- });
- }
- #endregion
-
- #region DrawTwoMaterialsFromSameAtlas
- /// <summary>
- /// Draw two materials from same atlas. Helped fixing bug #3197, now we
- /// got 9000fps with 2 materials and only 1 draw call :)
- /// </summary>
- [Test]
- public static void DrawTwoMaterialsFromSameAtlas()
- {
- Material2D testMat1 = new Material2D("DeltaEngineLogo");
- Material2D testMat2 = new Material2D("DefaultButtonBackground");
-
- Application.Start(delegate
- {
- testMat1.Draw(new Rectangle(0.2f, 0.2f, 0.3f, 0.2f));
- testMat2.Draw(new Rectangle(0.3f, 0.3f, 0.3f, 0.2f));
- // Note: Fonts eat up lots of performance, disable for more fps
- Font.DrawTopLeftInformation("Fps: " + Time.Fps);
- });
- }
- #endregion
-
- #region DrawAdditiveMaterial
- /// <summary>
- /// DrawAdditiveMaterial for the bug #3173
- /// </summary>
- [Test]
- public static void DrawAdditiveMaterial()
- {
- Material2D background = new Material2D("DeltaEngineLogo");
- background.DrawLayer = RenderLayer.Background;
- Material2D material = new Material2D("DeltaAdditive");
- Material2D material2 = new Material2D("SmokeSmallAdditive");
-
- Application.Start(delegate
- {
- background.Draw(Rectangle.FromCenter(Point.Half, background.Size));
- material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
- material2.Draw(Rectangle.One);
- });
- }
- #endregion
-
- #region DrawSubtractiveMaterial
- /// <summary>
- /// DrawSubtractiveMaterial
- /// </summary>
- [Test]
- public static void DrawSubtractiveMaterial()
- {
- Material2D background = new Material2D("DeltaEngineLogo")
- {
- DrawLayer = RenderLayer.Background,
- };
- Material2D material = new Material2D("DeltaSubtractive");
- Application.Start(delegate
- {
- background.Draw(Rectangle.FromCenter(Point.Half, background.Size));
- material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
- });
- }
- #endregion
-
- #region DrawAdditiveMaterialComplex
- /// <summary>
- /// Draw additive materials onto each other.
- /// Move two of the materials back and forth to see the additive effect
- /// at work.
- /// </summary>
- [Test]
- public static void DrawAdditiveMaterialComplex()
- {
- RenderLayer drawLayer = RenderLayer.Normal;
-
- Material2DColored additive = new Material2DColored("DeltaAdditive")
- {
- DrawLayer = drawLayer,
- };
- Material2DColored halfTransparent = new Material2DColored(
- "DeltaAdditive")
- {
- DrawLayer = drawLayer,
- BlendColor = Color.WhiteHalfTransparent,
- };
- Material2DColored transparentGrey = new Material2DColored(
- "DeltaAdditive")
- {
- DrawLayer = drawLayer,
- BlendColor = Color.Grey,
- };
-
- float start = 0.2f;
- float end = 0.6f;
- float offset = start;
- float speed = 0.05f;
- bool direction = true;
-
- Application.Start(delegate
- {
- offset +=
- Time.Delta *
- (direction
- ? speed
- : -speed);
- if (offset > end ||
- offset < start)
- {
- direction = !direction;
- }
-
- additive.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f), 0.0f);
- // WhiteHalfAlpha and Grey should both be the same (50% of original)
- halfTransparent.Draw(new Rectangle(offset, 0.41f, 0.2f, 0.2f));
- transparentGrey.Draw(new Rectangle(0.41f, offset, 0.2f, 0.2f), 0.0f);
- });
- }
- #endregion
-
- #region DrawLightEffectMaterial
- /// <summary>
- /// Draw LightEffect Material
- /// </summary>
- [Test]
- public static void DrawLightEffectMaterial()
- {
- Material2D lightEffect = new Material2D("BlendModesLightEffect");
-
- Application.Start(delegate
- {
- lightEffect.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
- });
- }
- #endregion
-
- #region DrawAnimatedMaterial
- /// <summary>
- /// Test animated material from Content.xml, must exist there and be setup
- /// correctly (either with AnimatedSpeed or with AnimationIndexAndMs).
- /// </summary>
- [Test]
- public static void DrawAnimatedMaterial()
- {
- Material2D animatedMaterial = new Material2D("BigExplosion");
-
- Application.Start(delegate
- {
- // By default all animations play right away
- if (Input.Keyboard.SpaceReleased)
- {
- if (animatedMaterial.IsPlaying)
- {
- animatedMaterial.StopAnimation();
- }
- else
- {
- animatedMaterial.StartAnimation(true);
- }
- }
-
- animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
- });
- }
- #endregion
-
- #region DrawAnimatedMaterialAndOverwritingAnimationSpeed
- /// <summary>
- /// Test animated material from Content and overwriting the AnimationSpeed.
- /// </summary>
- [Test]
- public static void DrawAnimatedMaterialAndOverwritingAnimationSpeed()
- {
- Material2D animatedMaterial = new Material2D("BigExplosion");
-
- animatedMaterial.AnimationSpeed = 5;
-
- Application.Start(delegate
- {
- // With space we switch 2 speed values to see the difference.
- if (Input.Keyboard.SpaceReleased)
- {
- animatedMaterial.AnimationSpeed =
- animatedMaterial.AnimationSpeed == 5
- ? 10
- : 5;
- }
- animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
- });
- }
- #endregion
-
- #region DrawAnimatedMaterialCustomIndicesAndTimes
- /// <summary>
- /// Test animated material from Content.xml and using custom
- /// AnimationIndexAndMs values.
- /// </summary>
- [Test]
- public static void DrawAnimatedMaterialCustomIndicesAndTimes()
- {
- Material2D animatedMaterial = new Material2D("ImageAnimation");
-
- // This is image meta-data and set automatically be the content!
- // We will overwrite it with our own indices and times in ms.
- animatedMaterial.AnimationFrameIndicesAndMs = new[]
- {
- // ImageAnimation has 10 frames (first number = frame, second = ms)
- // Count to 2 in 2 seconds
- 0, 1000,
- 1, 1000,
- // Count to 3 in 2 seconds
- 0, 666,
- 1, 666,
- 2, 666,
- // Count to 4 in 2 seconds
- 0, 500,
- 1, 500,
- 2, 500,
- 3, 500,
- // Count to 5 in 2 seconds
- 0, 400,
- 1, 400,
- 2, 400,
- 3, 400,
- 4, 400,
- // Count to 6 in 2 seconds
- 0, 333,
- 1, 333,
- 2, 333,
- 3, 333,
- 4, 333,
- 5, 333,
- // Count to 7 in 2 seconds
- 0, 285,
- 1, 285,
- 2, 285,
- 3, 285,
- 4, 285,
- 5, 285,
- 6, 285,
- // Count to 8 in 2 seconds
- 0, 250,
- 1, 250,
- 2, 250,
- 3, 250,
- 4, 250,
- 5, 250,
- 6, 250,
- 7, 250,
- // Count to 9 in 2 seconds
- 0, 222,
- 1, 222,
- 2, 222,
- 3, 222,
- 4, 222,
- 5, 222,
- 6, 222,
- 7, 222,
- 8, 222,
- // Count to 10
- 0, 200,
- 1, 200,
- 2, 200,
- 3, 200,
- 4, 200,
- 5, 200,
- 6, 200,
- 7, 200,
- 8, 200,
- 9, 200,
- };
-
- Application.Start(delegate
- {
- if (Input.Keyboard.SpaceReleased)
- {
- if (animatedMaterial.IsPlaying)
- {
- animatedMaterial.StopAnimation();
- }
- else
- {
- animatedMaterial.StartAnimation(true);
- }
- } // if
-
- animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
- });
- }
- #endregion
-
- #region DrawLayeredMaterials
- /// <summary>
- /// Helper to figure out some old bug with texture atlas switching
- /// </summary>
- [Test]
- public static void DrawLayeredMaterials()
- {
- /*iPad debug result:
- 12.427 OnUnload Fps: 31.87856
- 12.709 totalCpuTimeMs=1758 (90.10764%)
- 12.710 totalGpuTimeMs=193 (9.892363%)
- 12.710 Last frame OnRenderFrame time: 29ms (CPU: 29ms, GPU: 0ms)
- 12.710 Last frame Time.Delta: 0.03332901
- 12.713 Total Application Time: 12.07296s
- We got 6 sections that took in total: 3.000518s
- Section Time took: 0.01031613s (0.3438118%), Calls: 549, first time called: 1.075648s
- Section Application delegate run code took: 1.729822s (57.65079%), Calls: 549, first time called: 1.077253s
- Section ContentManager took: 0.00714159s (0.2380119%), Calls: 549, first time called: 2.362464s
- Section OpenGLES20Graphic took: 0.01350093s (0.4499533%), Calls: 548, first time called: 2.397646s
- Section MaterialManager took: 1.128s (37.59352%), Calls: 548, first time called: 2.401425s
- Section Draw took: 0.111737s (3.723924%), Calls: 548, first time called: 2.67375s
- */
-
- // Works fine with this or all default materials!
- Material2DColored[] testMaterials = new[]
- {
- new Material2DColored("SmokeSmallAdditive")
- {
- DrawLayer = RenderLayer.UI,
- BlendColor = Color.Yellow,
- },
- new Material2DColored("DeltaEngineLogo")
- {
- DrawLayer = RenderLayer.Front,
- BlendColor = Color.Red,
- },
- new Material2DColored("LensflareDTransparency")
- {
- DrawLayer = RenderLayer.Normal,
- BlendColor = Color.Blue,
- },
- new Material2DColored("SteamAdditive")
- {
- DrawLayer = RenderLayer.Background,
- },
- new Material2DColored("WaterSplatterAdditive")
- {
- DrawLayer = RenderLayer.Front,
- BlendColor = Color.Green,
- },
- };
-
- Application.Start(delegate
- {
- Font.DrawTopLeftInformation("Fps: " + Time.Fps);
-
- testMaterials[0].Draw(new Rectangle(0.15f, 0.15f, 0.15f, 0.15f));
- testMaterials[1].Draw(new Rectangle(0.2f, 0.2f, 0.34f, 0.34f));
- testMaterials[2].Draw(new Rectangle(0.2f, 0.2f, 0.45f, 0.45f));
- testMaterials[3].Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
- testMaterials[4].Draw(new Rectangle(0.2f, 0.2f, 0.15f, 0.15f));
- });
- }
- #endregion
-
- #region DrawManyMaterialsInSameLayer
- /// <summary>
- /// Draw many materials in same layer
- /// </summary>
- [Test]
- public static void DrawManyMaterialsInSameLayer()
- {
- const int DrawCalls = 10;
- const float DrawOffset = 0.025f;
-
- const string textureName = "DeltaAdditive";
- Material2D material = new Material2D(textureName);
-
- Application.Start(delegate
- {
- float leftStart = ScreenSpace.DrawArea.X + 0.025f;
- float topStart = ScreenSpace.DrawArea.Y + 0.025f;
-
- // First test overlapped drawing when using the same material
- Rectangle drawArea = new Rectangle(leftStart, topStart, 0.225f, 0.15f);
- for (int index = 0; index < DrawCalls; index++)
- {
- material.Draw(drawArea);
-
- drawArea.X += DrawOffset;
- drawArea.Y = ((index + 1) == (DrawCalls / 2))
- ? topStart
- : drawArea.Y + DrawOffset;
- }
-
- // Second test overlapped drawing when using always a new material
- drawArea.X = leftStart + 0.5f;
- drawArea.Y = topStart;
- for (int index = 0; index < DrawCalls; index++)
- {
- new Material2DColored(textureName).Draw(drawArea);
-
- drawArea.X += DrawOffset;
- drawArea.Y = ((index + 1) == (DrawCalls / 2))
- ? topStart
- : drawArea.Y + DrawOffset;
- }
- });
- }
- #endregion
-
- #region DrawMaterialClipped
- /// <summary>
- /// Draw material clipped
- /// </summary>
- [Test]
- public static void DrawMaterialClipped()
- {
- Material2DColored testMaterial =
- new Material2DColored("DeltaEngineLogo");
-
- Rectangle materialArea = Rectangle.FromCenter(0.5f, 0.3f, 0.2f, 0.2f);
- Rectangle strechedArea = Rectangle.FromCenter(0.3f, 0.6f, 0.1f, 0.2f);
- Rectangle clippedArea = Rectangle.FromCenter(0.7f, 0.6f, 0.1f, 0.2f);
-
- float horizontalClipBorder = 0.5f;
- float verticalClipBorder = 0.5f;
- Application.Start(delegate
- {
- BaseMouse mouse = Input.Mouse;
- if (Input.Gestures.IsHorizontalDrag ||
- Input.Gestures.IsVerticalDrag)
- {
- horizontalClipBorder =
- MathHelper.Clamp(horizontalClipBorder - mouse.Movement.X);
- verticalClipBorder =
- MathHelper.Clamp(verticalClipBorder - mouse.Movement.Y);
- }
-
- testMaterial.Draw(materialArea);
- testMaterial.Draw(strechedArea);
- testMaterial.Draw(clippedArea,
- new Rectangle(horizontalClipBorder, verticalClipBorder,
- 1f - horizontalClipBorder, 1f - verticalClipBorder));
- });
- }
- #endregion
-
- #region DrawTwoTexturesSameSizeColorAndUV
- /// <summary>
- /// This is testing two (atlas) materials which has the same size, uv
- /// coordinates and the same blending color.
- /// </summary>
- [Test]
- public static void DrawTwoTexturesSameSizeColorAndUV()
- {
- Material2DColored material1 = new Material2DColored("ImageAnimation01",
- Color.White);
- Material2DColored material2 = new Material2DColored("ImageAnimation02",
- Color.White);
-
- Rectangle drawArea = new Rectangle(0.2f, 0.3f, 0.2f, 0.2f);
-
- Application.Start(delegate
- {
- // Use the space key to toggle the materials
- // -> we should see the image with "2" if the space is pressed
- if (Input.Keyboard.SpaceIsPressed)
- {
- material2.Draw(drawArea);
- }
- // or the image with the "1" if we do nothing
- else
- {
- material1.Draw(drawArea);
- }
- });
- }
- #endregion
-
- #region DrawScreenRectangle
- /// <summary>
- /// Draw the default material in the screen area rectangle.
- /// </summary>
- [Test]
- public static void DrawScreenRectangle()
- {
- Application.Start(delegate
- {
- // There should be a border of 1 pixel around this image!
- Material2D.Default.Draw(ScreenSpace.ToQuadraticSpace(
- new Rectangle(1, 1, Application.Window.ViewportPixelWidth - 2,
- Application.Window.ViewportPixelHeight - 2)));
- });
- }
- #endregion
-
- #region DrawDDS888Image
- /// <summary>
- /// Draw image special unit test for testing DDS textures with the 888
- /// format like for the Shader lut helper textures.
- /// </summary>
- [Test]
- public static void DrawDDS888Image()
- {
- Material2D testMat = new Material2D("Specular10Fresnel4Lut");
-
- Application.Start(delegate
- {
- testMat.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
- });
- }
- #endregion
-
- #region DrawPureColoredMaterial
- /// <summary>
- /// This test shows 3 pure colored materials and a colored one with a
- /// texture.
- /// </summary>
- [Test]
- public static void DrawPureColoredMaterial()
- {
- Material2DColored material1 = new Material2DColored(Color.Red);
- Material2DColored material2 = new Material2DColored(Color.Green);
- Material2DColored material3 = new Material2DColored(Color.Blue);
- Material2DColored materialTexture = new Material2DColored("",
- Color.Yellow);
-
- Application.Start(delegate
- {
- material1.Draw(new Rectangle(0.2f, 0.2f, 0.2f, 0.2f));
- material2.Draw(new Rectangle(0.4f, 0.2f, 0.2f, 0.2f));
- material3.Draw(new Rectangle(0.6f, 0.2f, 0.2f, 0.2f));
- materialTexture.Draw(new Rectangle(0.2f, 0.4f, 0.2f, 0.2f));
- });
- }
- #endregion
-
- #region DrawSimpleBrownMaterial
- /// <summary>
- /// Draws a simple brown material fullscreen. Testing bug report #7482.
- /// </summary>
- [Test]
- public static void DrawSimpleBrownMaterial()
- {
- Material2DColored background = new Material2DColored(Color.Brown);
-
- Application.Start(delegate
- {
- background.Draw(Rectangle.One);
- });
- }
- #endregion
-
- #region ResizeWindowAndDrawBoxAndTexture
- /// <summary>
- /// Helper unit test to fix bug #4472: Different resolution doesn't work
- /// with materials, can even happenin some cases that the resolution in the
- /// settings file is "reset". Materials are drawn at the wrong positition,
- /// DrawManager based drawings works correctly. Tested with a resolution of
- /// 640 * 960 pixels, but happens at any resolution.
- /// </summary>
- [Test]
- public static void ResizeWindowAndDrawBoxAndTexture()
- {
- Material2DColored mat = new Material2DColored("DeltaEngineLogo");
-
- // Note: This is the only way to resize now :)
- Application.Window.Resize(640, 960);
-
- // Restore the old size again when closing!
- Application.ApplicationClosing += delegate
- {
- Application.Window.Resize(1024, 768);
- };
-
- Rectangle drawArea = Rectangle.FromCenter(Point.Half, mat.Size);
-
- // This will resize the already created window to fix the first part of
- // the problem. Then we also have to fix the graphics device, which might
- // also be created already, which actually caused all the render bugs!
- Application.Start(delegate
- {
- Font.DrawTopLeftInformation(
- "Resolution=" + Settings.Resolution + "\n" +
- "Window=" + Application.Window.ViewportPixelWidth + ", " +
- Application.Window.ViewportPixelHeight);
- Line.Draw(Point.Zero, drawArea.Position, Color.Red);
- mat.Draw(drawArea);
- });
- }
- #endregion
-
- #region DrawMultipleColoredMaterials
- /// <summary>
- /// Helper unit test to fix bug #7481: Most of the materials will be
- /// rendered wrong if many different are used - maybe corrupt check for
- /// caching?
- /// </summary>
- [Test]
- public static void DrawMultipleColoredMaterials()
- {
- Material2DColored mat1 = new Material2DColored("DeltaEngineLogo");
- Material2DColored mat2 = new Material2DColored("DeltaIcon");
- Material2DColored mat3 = new Material2DColored("ImageAnimation01");
- Material2DColored mat4 = new Material2DColored("ImageAnimation02");
- Material2DColored mat5 = new Material2DColored("ImageAnimation03");
- Material2DColored mat6 = new Material2DColored("ImageAnimation02");
- Material2DColored mat7 = new Material2DColored("ImageAnimation03");
- Material2DColored mat8 = new Material2DColored("ImageAnimation01");
- Material2DColored mat9 = new Material2DColored("ImageAnimation03");
-
- Application.Start(delegate
- {
- Font.DrawTopLeftInformation(
- "Resolution=" + Settings.Resolution);
- Line.Draw(Point.Zero, Point.One, Color.Red);
- mat1.Draw(new Rectangle(0.3f, 0.3f, 0.1f, 0.1f));
- mat2.Draw(new Rectangle(0.4f, 0.3f, 0.1f, 0.1f));
- mat3.Draw(new Rectangle(0.5f, 0.3f, 0.1f, 0.1f));
- mat4.Draw(new Rectangle(0.3f, 0.4f, 0.1f, 0.1f));
- mat5.Draw(new Rectangle(0.4f, 0.4f, 0.1f, 0.1f));
- mat6.Draw(new Rectangle(0.5f, 0.4f, 0.1f, 0.1f));
- mat7.Draw(new Rectangle(0.3f, 0.5f, 0.1f, 0.1f));
- mat8.Draw(new Rectangle(0.4f, 0.5f, 0.1f, 0.1f));
- mat9.Draw(new Rectangle(0.5f, 0.5f, 0.1f, 0.1f));
- });
-
- /*this part of the test only works with ZooMumbaMobile test content!
- Material2DColored mat1 = new Material2DColored("AnimalTileBlue");
- Material2DColored mat2 = new Material2DColored("AnimalTileCyan");
- Material2DColored mat3 = new Material2DColored("AnimalTileGreen");
- Material2DColored mat4 = new Material2DColored("AnimalTileRed");
- Material2DColored mat5 = new Material2DColored("AnimalTileYellow");
- Material2DColored mat6 = new Material2DColored("IconChimpanze");
- Material2DColored mat7 = new Material2DColored("IconDove");
- Material2DColored mat8 = new Material2DColored("IconElephant");
- Material2DColored mat9 = new Material2DColored("IconFlamingo");
- Material2DColored mat10 = mat1;// new Material2DColored("AnimalTileBlue");
- Material2DColored mat11 = mat2;// new Material2DColored("AnimalTileCyan");
- Material2DColored mat12 = mat2;// new Material2DColored("AnimalTileCyan");
- Material2DColored mat13 = mat2;// new Material2DColored("AnimalTileBlue");
- Material2DColored mat14 = mat1;// new Material2DColored("AnimalTileBlue");
- Material2DColored mat15 = mat2;// new Material2DColored("AnimalTileYellow");
-
- Application.Start(delegate
- {
- //Font.DrawTopLeftInformation(
- // "Resolution=" + Settings.Resolution);
- //Line.Draw(Point.Zero, Point.One, Color.Red);
- mat1.Draw(new Rectangle(0.3f, 0.3f, 0.1f, 0.1f));
- if (Time.Milliseconds > 500)
- mat2.Draw(new Rectangle(0.4f, 0.3f, 0.1f, 0.1f));
- if (Time.Milliseconds > 1000)
- mat3.Draw(new Rectangle(0.5f, 0.3f, 0.1f, 0.1f));
- if (Time.Milliseconds > 1500)
- mat4.Draw(new Rectangle(0.3f, 0.4f, 0.1f, 0.1f));
- if (Time.Milliseconds > 2000)
- mat5.Draw(new Rectangle(0.4f, 0.4f, 0.1f, 0.1f));
- if (Time.Milliseconds > 2500)
- mat6.Draw(new Rectangle(0.5f, 0.4f, 0.1f, 0.1f));
- if (Time.Milliseconds > 3000)
- mat7.Draw(new Rectangle(0.3f, 0.5f, 0.1f, 0.1f));
- if (Time.Milliseconds > 3500)
- mat8.Draw(new Rectangle(0.4f, 0.5f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat9.Draw(new Rectangle(0.5f, 0.5f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat10.Draw(new Rectangle(0.6f, 0.5f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat11.Draw(new Rectangle(0.3f, 0.6f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat12.Draw(new Rectangle(0.4f, 0.6f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat13.Draw(new Rectangle(0.5f, 0.6f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat14.Draw(new Rectangle(0.6f, 0.6f, 0.1f, 0.1f));
- if (Time.Milliseconds > 4000)
- mat15.Draw(new Rectangle(0.3f, 0.7f, 0.1f, 0.1f));
- });
- */
- }
- #endregion
-
- #region DrawMaterial3D
- /// <summary>
- /// Draw a material in 3d by using the geometry class.
- /// </summary>
- [Test]
- public static void DrawMaterial3D()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(1f, -5f, 3f));
- Material material = new Material("DeltaEngineLogo",
- Shader.Create("TexturedShader3D"));
-
- GeometryData data = GeometryData.CreatePlane(
- VertexFormat.Position3DTextured, 20f, 5f);
- Geometry geometry = Geometry.Create(data);
-
- Application.Start(delegate
- {
- Grid.Draw();
-
- material.Draw(geometry);
- });
- }
- #endregion
-
- #region DrawBillboard3D
- /// <summary>
- /// Draw billboard 3D
- /// </summary>
- [Test]
- public static void DrawBillboard3D()
- {
- LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
- // Note: Texture may not look like a particle, but it is good for
- // checking billboard orientation.
- MaterialColored billboard = new MaterialColored("DeltaEngineLogo");
-
- Size size = new Size(2f, 2f);
- float r = 0f;
- Application.Start(delegate
- {
- // Draw a Grid
- Grid.Draw();
-
- // Draw Billboards unrotated in different modes and rotated
- // in different modes
- for (int index = 0; index < 2; index++)
- {
- r += Time.CheckEvery(0.03f)
- ? 0.5f
- : 0f;
- r = (r > 360f)
- ? (r - 360f)
- : r;
-
- float z = index == 0
- ? -1f
- : 1f;
- float rotation = index == 0
- ? 0f
- : r;
-
- billboard.billboardMode = BillboardMode.CameraFacingPrecise;
- BillboardManager.Instance.Draw(billboard, new Vector(-4f, 0f, z),
- size, rotation);
- billboard.billboardMode = BillboardMode.UpAxis;
- BillboardManager.Instance.Draw(billboard, new Vector(-2f, 0f, z),
- size, rotation);
- billboard.billboardMode = BillboardMode.FrontAxis;
- BillboardManager.Instance.Draw(billboard, new Vector(0f, 0f, z),
- size, rotation);
- billboard.billboardMode = BillboardMode.Ground;
- BillboardManager.Instance.Draw(billboard, new Vector(4f, 0f, z),
- size, rotation);
- }
- });
- }
- #endregion
-
- #region CreateTextureDynamically
- /// <summary>
- /// Unit test that shows how to create a texture dynamically from rgb data.
- /// </summary>
- [Test]
- public static void CreateTextureDynamically()
- {
- // Define a 8x8 texture with some colors.
- Color[] textureColors = new[]
- {
- Color.Red, Color.Green, Color.Yellow, Color.Blue,
- Color.Black, Color.White, Color.Brown, Color.Orange,
- Color.Green, Color.Yellow, Color.Blue, Color.Black,
- Color.White, Color.Brown, Color.Orange, Color.Red,
- Color.Yellow, Color.Blue, Color.Black, Color.White,
- Color.Brown, Color.Orange, Color.Red, Color.Green,
- Color.Blue, Color.Black, Color.White, Color.Brown,
- Color.Orange, Color.Red, Color.Green, Color.Yellow,
- Color.Black, Color.White, Color.Brown, Color.Orange,
- Color.Red, Color.Green, Color.Yellow, Color.Blue,
- Color.White, Color.Brown, Color.Orange, Color.Red,
- Color.Green, Color.Yellow, Color.Blue, Color.Black,
- Color.Brown, Color.Orange, Color.Red, Color.Green,
- Color.Yellow, Color.Blue, Color.Black, Color.White,
- Color.Orange, Color.Red, Color.Green, Color.Yellow,
- Color.Blue, Color.Black, Color.White, Color.Brown,
- };
- // Convert the colors to rgb data and create texture out of it plus
- // pass it into a material for rendering.
- Material2D dynamicMaterial = new Material2D(Texture.Create(
- Texture.ConvertColorToRgb(textureColors), new Size(8, 8),
- BlendMode.Opaque, false));
-
- Application.Start(delegate
- {
- dynamicMaterial.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
- });
- }
- #endregion
-
- #region CreateTextureDynamicallyWithAlpha
- /// <summary>
- /// Unit test that shows how to create a texture dynamically from RGBA
- /// data.
- /// </summary>
- [Test]
- public static void CreateTextureDynamicallyWithAlpha()
- {
- // We will use partially transparent Black and Green.
- Color transparentBlack = new Color(Color.Black, 0.5f);
- Color transparentGreen = new Color(Color.Green, 0.2f);
-
- // Define a 8x8 texture with some colors.
- Color[] textureColors = new[]
- {
- Color.Unused, transparentGreen, Color.Yellow, Color.Blue,
- transparentBlack, Color.White, Color.Brown, Color.Orange,
- transparentGreen, Color.Yellow, Color.Blue, transparentBlack,
- Color.White, Color.Brown, Color.Orange, Color.Red,
- Color.Yellow, Color.Blue, transparentBlack, Color.White,
- Color.Brown, Color.Orange, Color.Red, transparentGreen,
- Color.Blue, transparentBlack, Color.White, Color.Brown,
- Color.Orange, Color.Red, transparentGreen, Color.Yellow,
- transparentBlack, Color.White, Color.Brown, Color.Orange,
- Color.Red, transparentGreen, Color.Yellow, Color.Blue,
- Color.White, Color.Brown, Color.Orange, Color.Red,
- transparentGreen, Color.Yellow, Color.Blue, transparentBlack,
- Color.Brown, Color.Orange, Color.Red, transparentGreen,
- Color.Yellow, Color.Blue, transparentBlack, Color.White,
- Color.Orange, Color.Red, transparentGreen, Color.Yellow,
- Color.Blue, transparentBlack, Color.White, Color.Brown,
- };
- // Convert the colors to RGBA data and create texture out of it plus
- // pass it into a material for rendering.
- Material2D dynamicMaterial = new Material2D(Texture.Create(
- Texture.ConvertColorToRgba(textureColors), new Size(8, 8),
- BlendMode.Translucency, false, true));
-
- Application.Start(delegate
- {
- dynamicMaterial.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
-
- Font.DrawTopLeftInformation(
- "Click to change background color to see alpha a bit better");
-
- // Clicking changes the background color
- if (Input.Mouse.LeftButtonReleased)
- {
- Application.BackgroundColor = Color.Random;
- }
- });
- }
- #endregion
-
- #region DrawLineOnTexture
- /// <summary>
- /// Draw line onto render to texture
- /// </summary>
- [Test]
- public static void DrawLineOnTexture()
- {
- RenderToTexture renderToTexture = RenderToTexture.Create(
- new Size(256, 256), false, false);
- Material2D mat = new Material2D(renderToTexture);
-
- Application.Start(delegate
- {
- // Render line into texture.
- renderToTexture.Render(delegate
- {
- // Line from top left up to bottom right in quadratic space
- Line.Draw(new Point(0, 0), new Point(1, 1), Color.Red);
- Line.Draw(new Point(0, 0.3f), new Point(1, 0.3f), Color.Red);
-
- // Also draw a default material.
- //Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
- DrawManager.Instance.Run();
- //MaterialManager.Instance.Run();
- });
-
- // Also draw line and material in normal mode.
- //Line.Draw(new Point(0, 0), new Point(1, 1), Color.Green);
- //Material2D.Default.Draw(new Rectangle(0.7f, 0.3f, 0.1f, 0.1f));
-
- // Show rendered texture with our material.
- mat.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
- });
- }
- #endregion
-
- #region MaskTexture
- /// <summary>
- /// Draws a mask into a stencilbuffer hiding parts of a texture
- /// </summary>
- [Test]
- public static void MaskTexture()
- {
- Application.BackgroundColor = Color.Red;
- RenderToTexture renderToTextureWithStencil = RenderToTexture.Create(
- new Size(256, 256), false, false, true);
- Material2D stencilMask = new Material2D(renderToTextureWithStencil);
-
- Application.Start(delegate
- {
- // Render line into texture.
- renderToTextureWithStencil.Render(delegate
- {
- // Draw a circle mask in the center
- Circle.DrawFilled(new Point(0.5f, 0.5f), 0.3f, Color.Black);
- DrawManager.Instance.Run();
- });
-
- // Show masked texture with our stencilMask material.
- stencilMask.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
- });
- }
- #endregion
-
- #region TakeScreenshot
- /// <summary>
- /// Test the screenshot capturer.
- /// </summary>
- [Test]
- public static void TakeScreenshot()
- {
- Application.Start(delegate
- {
- // Draw dummy image
- Material2D.Default.Draw(new Rectangle(0, 0, 0.5f, 0.5f));
-
- // And some text
- Font.Default.Draw(
- "Click Left to take a screenshot!\n" +
- "The app will then close and open the image automatically.",
- Rectangle.FromCenter(Point.Half, Size.Half));
-
- // Capture when clicking.
- if (Input.Mouse.LeftButtonReleased)
- {
- string screenshotPath = DrawManager.MakeScreenshot();
-
- // Also quit the application and show the screenshot we just made.
- Application.Quit();
- Process.Start(screenshotPath);
- }
- });
- }
- #endregion
-
- }
- }