PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  1. using System.Diagnostics;
  2. using Delta.ContentSystem;
  3. using Delta.ContentSystem.Rendering;
  4. using Delta.Engine;
  5. using Delta.Engine.Game;
  6. using Delta.Graphics.Basics;
  7. using Delta.InputSystem;
  8. using Delta.InputSystem.Devices;
  9. using Delta.Rendering.Basics.Drawing;
  10. using Delta.Rendering.Basics.Fonts;
  11. using Delta.Rendering.Basics.Materials;
  12. using Delta.Rendering.Cameras;
  13. using Delta.Rendering.Enums;
  14. using Delta.Utilities;
  15. using Delta.Utilities.Datatypes;
  16. using Delta.Utilities.Graphics;
  17. using Delta.Utilities.Helpers;
  18. using NUnit.Framework;
  19. namespace Delta.Rendering.BasicTests
  20. {
  21. /// <summary>
  22. /// Material tests
  23. /// </summary>
  24. [Category("Visual")]
  25. internal class MaterialTests
  26. {
  27. #region DrawDefaultMaterial
  28. /// <summary>
  29. /// Draw default material, has 10-15k fps, pretty good. All dynamic.
  30. /// </summary>
  31. [Test]
  32. public static void DrawDefaultMaterial()
  33. {
  34. Application.Start(delegate
  35. {
  36. Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  37. });
  38. }
  39. #endregion
  40. #region DrawDefaultMaterialAutoRotated
  41. /// <summary>
  42. /// Draw default material auto rotated.
  43. /// </summary>
  44. [Test]
  45. public static void DrawDefaultMaterialAutoRotated()
  46. {
  47. float rotation = 0.0f;
  48. Application.Start(delegate
  49. {
  50. rotation += Time.Delta * 10.0f;
  51. Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f),
  52. rotation);
  53. });
  54. }
  55. #endregion
  56. #region DrawDefaultMaterialColoredFullscreen
  57. /// <summary>
  58. /// Draw default material with color on fullscreen. Testing bug #4481
  59. /// </summary>
  60. [Test]
  61. public static void DrawDefaultMaterialColoredFullscreen()
  62. {
  63. Material2DColored background = new Material2DColored(
  64. //Color.Red);
  65. "DeltaIcon");
  66. Application.Start(delegate
  67. {
  68. background.Draw(Rectangle.One);
  69. });
  70. }
  71. #endregion
  72. #region DrawDefaultMaterialWithLines
  73. /// <summary>
  74. /// Draw default material and some lines, can get up to 10k fps.
  75. /// </summary>
  76. [Test]
  77. public static void DrawDefaultMaterialWithLines()
  78. {
  79. // The default fallback material texture (4x4) should use no filtering
  80. Assert.False(Material2D.Default.diffuseMap.UseLinearFiltering);
  81. Application.BackgroundColor = Color.Unused;
  82. Application.Start(delegate
  83. {
  84. Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  85. Line.Draw(new Point(0, 0.5f), new Point(1, 0.5f), Color.Red);
  86. Line.Draw(new Point(0.5f, 0), new Point(0.5f, 1), Color.Green);
  87. Line.Draw(new Point(0, 0), new Point(1, 1), Color.Yellow);
  88. });
  89. }
  90. #endregion
  91. #region DrawManyMaterialsPerformance
  92. /// <summary>
  93. /// Draw many materials performance test
  94. /// </summary>
  95. [Test]
  96. public static void DrawManyMaterialsPerformance()
  97. {
  98. // With new optimizations on PC we have ~1500 fps for 2500 draws with
  99. // 5000 polys (or 10000 quad vertices), so we have 7.5m poly/s (in
  100. // debug mode, in release mode it is almost 10m poly/s).
  101. // Note: When using 25000 draws per frame (50000 polys), it still
  102. // comes out to about 7.5m poly/s with ~150 fps.
  103. // New optimizations done 2011-03-20, mostly caching MaterialManager layers in Material now!
  104. // Performance for the DrawManyMaterialsPerformance unit test is about 4000fps (release, in debug still 2500fps) for 10000 vertices,
  105. // which is 40 million dynamic vertices with 60 million indices for 20 million dynamic polys
  106. // per second (not really dynamic anymore because of caching, but you can change them anytime)!
  107. // Other unit tests are also doing well, almost all simple tests easily reach 10000fps in debug mode!
  108. //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()!
  109. //OK: continue optimizing, first get this as fast as possible (even in debug mode, some checks are just too slow)
  110. //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).
  111. //OK: we should get a few thousand fps :)
  112. //OK: indices are always the same, why not calculate when creating indices?
  113. //OK: do most checks at initialization time of the render geometry (does not change after that)
  114. //OK: put in method, always the same, isn't it? same as creation!
  115. //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!
  116. //OK: all GeometryData initialization code is always the same and can be in a method too!
  117. /*performance result in iPad debug mode:
  118. 12.673 totalCpuTimeMs=5121 (95.36313%)
  119. 12.674 totalGpuTimeMs=249 (4.636871%)
  120. 12.675 Last frame OnRenderFrame time: 34ms (CPU: 34ms, GPU: 0ms)
  121. 12.675 Last frame Time.Delta: 0.03338146
  122. 12.678 Total Application Time: 12.17727s
  123. We got 5 sections that took in total: 6.317755s
  124. Section Time took: 0.007066727s (0.111855%), Calls: 294, first time called: 1.151879s
  125. Section Application delegate run code took: 5.47965s (86.73415%), Calls: 294, first time called: 1.154123s
  126. Section ContentManager took: 0.004089594s (0.06473176%), Calls: 294, first time called: 1.581513s
  127. Section OpenGLES20Graphic took: 0.008611083s (0.1362997%), Calls: 293, first time called: 1.623532s
  128. Section MaterialManager took: 0.818337s (12.95297%), Calls: 293, first time called: 2.007285s
  129. */
  130. // 5 layers * 50 * 10 = 2500 draws per frame (which is 5000 polygons or
  131. // 10000 vertices with 15000 indices being rendered, these are also the
  132. // limits for MaterialManager right now)!
  133. int numberOfLayerTypes = EnumHelper.GetCount<RenderLayer>();
  134. const int HorizontalDrawsPerLayer = 50;
  135. const int VerticalDrawsPerLayer = 10;
  136. const float DrawSize = 0.6f / HorizontalDrawsPerLayer;
  137. Material2D material = new Material2D(Content.EmptyName)
  138. {
  139. DrawLayer = RenderLayer.Text,
  140. };
  141. Application.Start(delegate
  142. {
  143. for (int layer = 0; layer < numberOfLayerTypes; layer++)
  144. {
  145. for (int y = 0; y < VerticalDrawsPerLayer; y++)
  146. {
  147. for (int x = 0; x < HorizontalDrawsPerLayer; x++)
  148. {
  149. material.Draw(new Rectangle(x / (float)HorizontalDrawsPerLayer,
  150. 0.1f + 0.15f * (layer + (y / (float)VerticalDrawsPerLayer)),
  151. DrawSize, DrawSize));
  152. }
  153. } // for
  154. } // for
  155. });
  156. }
  157. #endregion
  158. #region DrawTwoSameMaterials
  159. /// <summary>
  160. /// Helper unit test to check if Niko's Bug report about rendering the same
  161. /// image in a different order does not affect rendering. This test proves
  162. /// that it all works fine (he had different content caching bugs btw,
  163. /// non-loaded images were compared to loaded ones, this is already fixed
  164. /// in v0.8.3.2).
  165. /// </summary>
  166. [Test]
  167. public static void DrawTwoSameMaterials()
  168. {
  169. Material2D material1 = new Material2D("DeltaEngineLogo");
  170. Material2D material2 = new Material2D("DeltaEngineLogo");
  171. bool material2First = false;
  172. Application.Start(delegate
  173. {
  174. // All the same, just render material2 first if space was pressed,
  175. // otherwise render material1 first, then material2.
  176. if (material2First)
  177. {
  178. material2.Draw(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f));
  179. material1.Draw(ScreenSpace.DrawArea);
  180. }
  181. else
  182. {
  183. material1.Draw(ScreenSpace.DrawArea);
  184. material2.Draw(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f));
  185. }
  186. if (Input.Keyboard.SpaceReleased)
  187. {
  188. material2First = !material2First;
  189. }
  190. });
  191. }
  192. #endregion
  193. #region DrawAnimatedAndNormalImagesAlternating
  194. /// <summary>
  195. /// Draw Animated and Normal Images Alternating
  196. /// </summary>
  197. [Test]
  198. public static void DrawAnimatedAndNormalImagesAlternating()
  199. {
  200. Material2D material = new Material2D("ImageAnimation");
  201. Material2D staticImage = new Material2D("DeltaEngineLogo");
  202. bool showAnimated = true;
  203. Application.Start(delegate
  204. {
  205. staticImage.Draw(new Rectangle(0f, 0f, 0.2f, 0.2f));
  206. if (showAnimated)
  207. {
  208. material.Draw(new Rectangle(0.2f, 0.35f, 0.6f, 0.3f));
  209. }
  210. else
  211. {
  212. staticImage.Draw(new Rectangle(0.2f, 0.35f, 0.6f, 0.3f));
  213. }
  214. if (Time.CheckEvery(2))
  215. {
  216. showAnimated = !showAnimated;
  217. }
  218. });
  219. }
  220. #endregion
  221. #region DrawAnimatedImageWithGameTimePause
  222. /// <summary>
  223. /// Draw Animated image with game time pause.
  224. /// </summary>
  225. [Test]
  226. public static void DrawAnimatedImageWithGameTimePause()
  227. {
  228. Material2D material = new Material2D("ImageAnimation");
  229. Application.Start(delegate
  230. {
  231. material.Draw(Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.2f));
  232. if (Input.Keyboard.SpaceReleased)
  233. {
  234. if (GameTime.IsRunning)
  235. {
  236. GameTime.Pause();
  237. }
  238. else
  239. {
  240. GameTime.Start();
  241. }
  242. }
  243. });
  244. }
  245. #endregion
  246. #region DrawMaterialFlipped
  247. /// <summary>
  248. /// This test explains how to draw materials vertically or horizontally
  249. /// flipped. Internally the UVs are flipped, nothing else changes. You can
  250. /// combine this with other operations like rotation or coloring as well.
  251. /// </summary>
  252. [Test]
  253. public static void DrawMaterialFlipped()
  254. {
  255. Material2DColored logo = new Material2DColored("DeltaEngineLogo");
  256. Application.Start(delegate
  257. {
  258. logo.Draw(Rectangle.FromCenter(
  259. Point.Half + new Size(-0.5f, -0.5f) * logo.Size, logo.Size), 0,
  260. FlipMode.None);
  261. logo.Draw(Rectangle.FromCenter(
  262. Point.Half + new Size(0.5f, -0.5f) * logo.Size, logo.Size), 0,
  263. FlipMode.Horizontal);
  264. logo.Draw(Rectangle.FromCenter(
  265. Point.Half + new Size(-0.5f, 0.5f) * logo.Size, logo.Size), 0,
  266. FlipMode.Vertical);
  267. logo.Draw(Rectangle.FromCenter(
  268. Point.Half + new Size(0.5f, 0.5f) * logo.Size, logo.Size), 0,
  269. FlipMode.VerticalAndHorizontal);
  270. });
  271. }
  272. #endregion
  273. #region DrawMaterialRotatedAroundPoint
  274. /// <summary>
  275. /// Draw default material rotated around a custom point. As requested in
  276. /// http://forum.deltaengine.net/yaf_postsm1094_Rotate-a-Material2D.aspx
  277. /// </summary>
  278. [Test]
  279. public static void DrawMaterialRotatedAroundPoint()
  280. {
  281. float rotation = 0.0f;
  282. Application.Start(delegate
  283. {
  284. rotation += Time.Delta * 100.0f;
  285. Material2D.Default.Draw(new Rectangle(0.3f, 0.3f, 0.4f, 0.4f),
  286. rotation, new Point(0.3f, 0.3f));
  287. });
  288. }
  289. #endregion
  290. #region BlendModes
  291. /// <summary>
  292. /// Blend modes. We test the different blend modes with textures.
  293. /// </summary>
  294. [Test]
  295. public static void BlendModes()
  296. {
  297. // Test Additive BlendMode
  298. Material2D additive = new Material2D("DeltaAdditive");
  299. // Test LightEffect BlendMode
  300. Material2D lightEffect = new Material2D("BlendModesLightEffect");
  301. // Test AlphaTest BlendMode
  302. Material2D transparent = new Material2D(
  303. "DeltaIcon");
  304. // Test Substractive BlendMode
  305. Material2D substractive = new Material2D("DeltaSubtractive");
  306. // And draw them all
  307. Application.Start(delegate
  308. {
  309. Material2D.Default.Draw(Rectangle.FromCenter(Point.Half, 0.25f));
  310. additive.Draw(new Rectangle(new Point(0.5f - additive.Size.Width,
  311. 0.5f - additive.Size.Height), additive.Size));
  312. lightEffect.Draw(new Rectangle(new Point(0.5f,
  313. 0.5f - lightEffect.Size.Height), lightEffect.Size));
  314. transparent.Draw(new Rectangle(new Point(0.5f - transparent.Size.Width,
  315. 0.5f), transparent.Size));
  316. substractive.Draw(new Rectangle(new Point(0.5f,
  317. 0.5f), substractive.Size));
  318. });
  319. }
  320. #endregion
  321. #region DrawTransparentMaterial
  322. /// <summary>
  323. /// Draw transparent material
  324. /// </summary>
  325. [Test]
  326. public static void DrawTransparentMaterial()
  327. {
  328. Material2D material = new Material2D("DeltaIcon");
  329. Application.Start(delegate
  330. {
  331. material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
  332. });
  333. }
  334. #endregion
  335. #region DrawOpaqueMaterial
  336. /// <summary>
  337. /// Draw opaque material
  338. /// </summary>
  339. [Test]
  340. public static void DrawOpaqueMaterial()
  341. {
  342. Material2D opaqueMat = new Material2D("ImageAnimation");
  343. Application.BackgroundColor = Color.Red;
  344. Application.Start(delegate
  345. {
  346. opaqueMat.Draw(ScreenSpace.DrawArea);
  347. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  348. });
  349. }
  350. #endregion
  351. #region DrawTwoMaterialsFromSameAtlas
  352. /// <summary>
  353. /// Draw two materials from same atlas. Helped fixing bug #3197, now we
  354. /// got 9000fps with 2 materials and only 1 draw call :)
  355. /// </summary>
  356. [Test]
  357. public static void DrawTwoMaterialsFromSameAtlas()
  358. {
  359. Material2D testMat1 = new Material2D("DeltaEngineLogo");
  360. Material2D testMat2 = new Material2D("DefaultButtonBackground");
  361. Application.Start(delegate
  362. {
  363. testMat1.Draw(new Rectangle(0.2f, 0.2f, 0.3f, 0.2f));
  364. testMat2.Draw(new Rectangle(0.3f, 0.3f, 0.3f, 0.2f));
  365. // Note: Fonts eat up lots of performance, disable for more fps
  366. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  367. });
  368. }
  369. #endregion
  370. #region DrawAdditiveMaterial
  371. /// <summary>
  372. /// DrawAdditiveMaterial for the bug #3173
  373. /// </summary>
  374. [Test]
  375. public static void DrawAdditiveMaterial()
  376. {
  377. Material2D background = new Material2D("DeltaEngineLogo");
  378. background.DrawLayer = RenderLayer.Background;
  379. Material2D material = new Material2D("DeltaAdditive");
  380. Material2D material2 = new Material2D("SmokeSmallAdditive");
  381. Application.Start(delegate
  382. {
  383. background.Draw(Rectangle.FromCenter(Point.Half, background.Size));
  384. material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
  385. material2.Draw(Rectangle.One);
  386. });
  387. }
  388. #endregion
  389. #region DrawSubtractiveMaterial
  390. /// <summary>
  391. /// DrawSubtractiveMaterial
  392. /// </summary>
  393. [Test]
  394. public static void DrawSubtractiveMaterial()
  395. {
  396. Material2D background = new Material2D("DeltaEngineLogo")
  397. {
  398. DrawLayer = RenderLayer.Background,
  399. };
  400. Material2D material = new Material2D("DeltaSubtractive");
  401. Application.Start(delegate
  402. {
  403. background.Draw(Rectangle.FromCenter(Point.Half, background.Size));
  404. material.Draw(Rectangle.FromCenter(Point.Half, material.Size));
  405. });
  406. }
  407. #endregion
  408. #region DrawAdditiveMaterialComplex
  409. /// <summary>
  410. /// Draw additive materials onto each other.
  411. /// Move two of the materials back and forth to see the additive effect
  412. /// at work.
  413. /// </summary>
  414. [Test]
  415. public static void DrawAdditiveMaterialComplex()
  416. {
  417. RenderLayer drawLayer = RenderLayer.Normal;
  418. Material2DColored additive = new Material2DColored("DeltaAdditive")
  419. {
  420. DrawLayer = drawLayer,
  421. };
  422. Material2DColored halfTransparent = new Material2DColored(
  423. "DeltaAdditive")
  424. {
  425. DrawLayer = drawLayer,
  426. BlendColor = Color.WhiteHalfTransparent,
  427. };
  428. Material2DColored transparentGrey = new Material2DColored(
  429. "DeltaAdditive")
  430. {
  431. DrawLayer = drawLayer,
  432. BlendColor = Color.Grey,
  433. };
  434. float start = 0.2f;
  435. float end = 0.6f;
  436. float offset = start;
  437. float speed = 0.05f;
  438. bool direction = true;
  439. Application.Start(delegate
  440. {
  441. offset +=
  442. Time.Delta *
  443. (direction
  444. ? speed
  445. : -speed);
  446. if (offset > end ||
  447. offset < start)
  448. {
  449. direction = !direction;
  450. }
  451. additive.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f), 0.0f);
  452. // WhiteHalfAlpha and Grey should both be the same (50% of original)
  453. halfTransparent.Draw(new Rectangle(offset, 0.41f, 0.2f, 0.2f));
  454. transparentGrey.Draw(new Rectangle(0.41f, offset, 0.2f, 0.2f), 0.0f);
  455. });
  456. }
  457. #endregion
  458. #region DrawLightEffectMaterial
  459. /// <summary>
  460. /// Draw LightEffect Material
  461. /// </summary>
  462. [Test]
  463. public static void DrawLightEffectMaterial()
  464. {
  465. Material2D lightEffect = new Material2D("BlendModesLightEffect");
  466. Application.Start(delegate
  467. {
  468. lightEffect.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  469. });
  470. }
  471. #endregion
  472. #region DrawAnimatedMaterial
  473. /// <summary>
  474. /// Test animated material from Content.xml, must exist there and be setup
  475. /// correctly (either with AnimatedSpeed or with AnimationIndexAndMs).
  476. /// </summary>
  477. [Test]
  478. public static void DrawAnimatedMaterial()
  479. {
  480. Material2D animatedMaterial = new Material2D("BigExplosion");
  481. Application.Start(delegate
  482. {
  483. // By default all animations play right away
  484. if (Input.Keyboard.SpaceReleased)
  485. {
  486. if (animatedMaterial.IsPlaying)
  487. {
  488. animatedMaterial.StopAnimation();
  489. }
  490. else
  491. {
  492. animatedMaterial.StartAnimation(true);
  493. }
  494. }
  495. animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
  496. });
  497. }
  498. #endregion
  499. #region DrawAnimatedMaterialAndOverwritingAnimationSpeed
  500. /// <summary>
  501. /// Test animated material from Content and overwriting the AnimationSpeed.
  502. /// </summary>
  503. [Test]
  504. public static void DrawAnimatedMaterialAndOverwritingAnimationSpeed()
  505. {
  506. Material2D animatedMaterial = new Material2D("BigExplosion");
  507. animatedMaterial.AnimationSpeed = 5;
  508. Application.Start(delegate
  509. {
  510. // With space we switch 2 speed values to see the difference.
  511. if (Input.Keyboard.SpaceReleased)
  512. {
  513. animatedMaterial.AnimationSpeed =
  514. animatedMaterial.AnimationSpeed == 5
  515. ? 10
  516. : 5;
  517. }
  518. animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
  519. });
  520. }
  521. #endregion
  522. #region DrawAnimatedMaterialCustomIndicesAndTimes
  523. /// <summary>
  524. /// Test animated material from Content.xml and using custom
  525. /// AnimationIndexAndMs values.
  526. /// </summary>
  527. [Test]
  528. public static void DrawAnimatedMaterialCustomIndicesAndTimes()
  529. {
  530. Material2D animatedMaterial = new Material2D("ImageAnimation");
  531. // This is image meta-data and set automatically be the content!
  532. // We will overwrite it with our own indices and times in ms.
  533. animatedMaterial.AnimationFrameIndicesAndMs = new[]
  534. {
  535. // ImageAnimation has 10 frames (first number = frame, second = ms)
  536. // Count to 2 in 2 seconds
  537. 0, 1000,
  538. 1, 1000,
  539. // Count to 3 in 2 seconds
  540. 0, 666,
  541. 1, 666,
  542. 2, 666,
  543. // Count to 4 in 2 seconds
  544. 0, 500,
  545. 1, 500,
  546. 2, 500,
  547. 3, 500,
  548. // Count to 5 in 2 seconds
  549. 0, 400,
  550. 1, 400,
  551. 2, 400,
  552. 3, 400,
  553. 4, 400,
  554. // Count to 6 in 2 seconds
  555. 0, 333,
  556. 1, 333,
  557. 2, 333,
  558. 3, 333,
  559. 4, 333,
  560. 5, 333,
  561. // Count to 7 in 2 seconds
  562. 0, 285,
  563. 1, 285,
  564. 2, 285,
  565. 3, 285,
  566. 4, 285,
  567. 5, 285,
  568. 6, 285,
  569. // Count to 8 in 2 seconds
  570. 0, 250,
  571. 1, 250,
  572. 2, 250,
  573. 3, 250,
  574. 4, 250,
  575. 5, 250,
  576. 6, 250,
  577. 7, 250,
  578. // Count to 9 in 2 seconds
  579. 0, 222,
  580. 1, 222,
  581. 2, 222,
  582. 3, 222,
  583. 4, 222,
  584. 5, 222,
  585. 6, 222,
  586. 7, 222,
  587. 8, 222,
  588. // Count to 10
  589. 0, 200,
  590. 1, 200,
  591. 2, 200,
  592. 3, 200,
  593. 4, 200,
  594. 5, 200,
  595. 6, 200,
  596. 7, 200,
  597. 8, 200,
  598. 9, 200,
  599. };
  600. Application.Start(delegate
  601. {
  602. if (Input.Keyboard.SpaceReleased)
  603. {
  604. if (animatedMaterial.IsPlaying)
  605. {
  606. animatedMaterial.StopAnimation();
  607. }
  608. else
  609. {
  610. animatedMaterial.StartAnimation(true);
  611. }
  612. } // if
  613. animatedMaterial.Draw(new Rectangle(0.4f, 0.4f, 0.2f, 0.2f));
  614. });
  615. }
  616. #endregion
  617. #region DrawLayeredMaterials
  618. /// <summary>
  619. /// Helper to figure out some old bug with texture atlas switching
  620. /// </summary>
  621. [Test]
  622. public static void DrawLayeredMaterials()
  623. {
  624. /*iPad debug result:
  625. 12.427 OnUnload Fps: 31.87856
  626. 12.709 totalCpuTimeMs=1758 (90.10764%)
  627. 12.710 totalGpuTimeMs=193 (9.892363%)
  628. 12.710 Last frame OnRenderFrame time: 29ms (CPU: 29ms, GPU: 0ms)
  629. 12.710 Last frame Time.Delta: 0.03332901
  630. 12.713 Total Application Time: 12.07296s
  631. We got 6 sections that took in total: 3.000518s
  632. Section Time took: 0.01031613s (0.3438118%), Calls: 549, first time called: 1.075648s
  633. Section Application delegate run code took: 1.729822s (57.65079%), Calls: 549, first time called: 1.077253s
  634. Section ContentManager took: 0.00714159s (0.2380119%), Calls: 549, first time called: 2.362464s
  635. Section OpenGLES20Graphic took: 0.01350093s (0.4499533%), Calls: 548, first time called: 2.397646s
  636. Section MaterialManager took: 1.128s (37.59352%), Calls: 548, first time called: 2.401425s
  637. Section Draw took: 0.111737s (3.723924%), Calls: 548, first time called: 2.67375s
  638. */
  639. // Works fine with this or all default materials!
  640. Material2DColored[] testMaterials = new[]
  641. {
  642. new Material2DColored("SmokeSmallAdditive")
  643. {
  644. DrawLayer = RenderLayer.UI,
  645. BlendColor = Color.Yellow,
  646. },
  647. new Material2DColored("DeltaEngineLogo")
  648. {
  649. DrawLayer = RenderLayer.Front,
  650. BlendColor = Color.Red,
  651. },
  652. new Material2DColored("LensflareDTransparency")
  653. {
  654. DrawLayer = RenderLayer.Normal,
  655. BlendColor = Color.Blue,
  656. },
  657. new Material2DColored("SteamAdditive")
  658. {
  659. DrawLayer = RenderLayer.Background,
  660. },
  661. new Material2DColored("WaterSplatterAdditive")
  662. {
  663. DrawLayer = RenderLayer.Front,
  664. BlendColor = Color.Green,
  665. },
  666. };
  667. Application.Start(delegate
  668. {
  669. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  670. testMaterials[0].Draw(new Rectangle(0.15f, 0.15f, 0.15f, 0.15f));
  671. testMaterials[1].Draw(new Rectangle(0.2f, 0.2f, 0.34f, 0.34f));
  672. testMaterials[2].Draw(new Rectangle(0.2f, 0.2f, 0.45f, 0.45f));
  673. testMaterials[3].Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  674. testMaterials[4].Draw(new Rectangle(0.2f, 0.2f, 0.15f, 0.15f));
  675. });
  676. }
  677. #endregion
  678. #region DrawManyMaterialsInSameLayer
  679. /// <summary>
  680. /// Draw many materials in same layer
  681. /// </summary>
  682. [Test]
  683. public static void DrawManyMaterialsInSameLayer()
  684. {
  685. const int DrawCalls = 10;
  686. const float DrawOffset = 0.025f;
  687. const string textureName = "DeltaAdditive";
  688. Material2D material = new Material2D(textureName);
  689. Application.Start(delegate
  690. {
  691. float leftStart = ScreenSpace.DrawArea.X + 0.025f;
  692. float topStart = ScreenSpace.DrawArea.Y + 0.025f;
  693. // First test overlapped drawing when using the same material
  694. Rectangle drawArea = new Rectangle(leftStart, topStart, 0.225f, 0.15f);
  695. for (int index = 0; index < DrawCalls; index++)
  696. {
  697. material.Draw(drawArea);
  698. drawArea.X += DrawOffset;
  699. drawArea.Y = ((index + 1) == (DrawCalls / 2))
  700. ? topStart
  701. : drawArea.Y + DrawOffset;
  702. }
  703. // Second test overlapped drawing when using always a new material
  704. drawArea.X = leftStart + 0.5f;
  705. drawArea.Y = topStart;
  706. for (int index = 0; index < DrawCalls; index++)
  707. {
  708. new Material2DColored(textureName).Draw(drawArea);
  709. drawArea.X += DrawOffset;
  710. drawArea.Y = ((index + 1) == (DrawCalls / 2))
  711. ? topStart
  712. : drawArea.Y + DrawOffset;
  713. }
  714. });
  715. }
  716. #endregion
  717. #region DrawMaterialClipped
  718. /// <summary>
  719. /// Draw material clipped
  720. /// </summary>
  721. [Test]
  722. public static void DrawMaterialClipped()
  723. {
  724. Material2DColored testMaterial =
  725. new Material2DColored("DeltaEngineLogo");
  726. Rectangle materialArea = Rectangle.FromCenter(0.5f, 0.3f, 0.2f, 0.2f);
  727. Rectangle strechedArea = Rectangle.FromCenter(0.3f, 0.6f, 0.1f, 0.2f);
  728. Rectangle clippedArea = Rectangle.FromCenter(0.7f, 0.6f, 0.1f, 0.2f);
  729. float horizontalClipBorder = 0.5f;
  730. float verticalClipBorder = 0.5f;
  731. Application.Start(delegate
  732. {
  733. BaseMouse mouse = Input.Mouse;
  734. if (Input.Gestures.IsHorizontalDrag ||
  735. Input.Gestures.IsVerticalDrag)
  736. {
  737. horizontalClipBorder =
  738. MathHelper.Clamp(horizontalClipBorder - mouse.Movement.X);
  739. verticalClipBorder =
  740. MathHelper.Clamp(verticalClipBorder - mouse.Movement.Y);
  741. }
  742. testMaterial.Draw(materialArea);
  743. testMaterial.Draw(strechedArea);
  744. testMaterial.Draw(clippedArea,
  745. new Rectangle(horizontalClipBorder, verticalClipBorder,
  746. 1f - horizontalClipBorder, 1f - verticalClipBorder));
  747. });
  748. }
  749. #endregion
  750. #region DrawTwoTexturesSameSizeColorAndUV
  751. /// <summary>
  752. /// This is testing two (atlas) materials which has the same size, uv
  753. /// coordinates and the same blending color.
  754. /// </summary>
  755. [Test]
  756. public static void DrawTwoTexturesSameSizeColorAndUV()
  757. {
  758. Material2DColored material1 = new Material2DColored("ImageAnimation01",
  759. Color.White);
  760. Material2DColored material2 = new Material2DColored("ImageAnimation02",
  761. Color.White);
  762. Rectangle drawArea = new Rectangle(0.2f, 0.3f, 0.2f, 0.2f);
  763. Application.Start(delegate
  764. {
  765. // Use the space key to toggle the materials
  766. // -> we should see the image with "2" if the space is pressed
  767. if (Input.Keyboard.SpaceIsPressed)
  768. {
  769. material2.Draw(drawArea);
  770. }
  771. // or the image with the "1" if we do nothing
  772. else
  773. {
  774. material1.Draw(drawArea);
  775. }
  776. });
  777. }
  778. #endregion
  779. #region DrawScreenRectangle
  780. /// <summary>
  781. /// Draw the default material in the screen area rectangle.
  782. /// </summary>
  783. [Test]
  784. public static void DrawScreenRectangle()
  785. {
  786. Application.Start(delegate
  787. {
  788. // There should be a border of 1 pixel around this image!
  789. Material2D.Default.Draw(ScreenSpace.ToQuadraticSpace(
  790. new Rectangle(1, 1, Application.Window.ViewportPixelWidth - 2,
  791. Application.Window.ViewportPixelHeight - 2)));
  792. });
  793. }
  794. #endregion
  795. #region DrawDDS888Image
  796. /// <summary>
  797. /// Draw image special unit test for testing DDS textures with the 888
  798. /// format like for the Shader lut helper textures.
  799. /// </summary>
  800. [Test]
  801. public static void DrawDDS888Image()
  802. {
  803. Material2D testMat = new Material2D("Specular10Fresnel4Lut");
  804. Application.Start(delegate
  805. {
  806. testMat.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
  807. });
  808. }
  809. #endregion
  810. #region DrawPureColoredMaterial
  811. /// <summary>
  812. /// This test shows 3 pure colored materials and a colored one with a
  813. /// texture.
  814. /// </summary>
  815. [Test]
  816. public static void DrawPureColoredMaterial()
  817. {
  818. Material2DColored material1 = new Material2DColored(Color.Red);
  819. Material2DColored material2 = new Material2DColored(Color.Green);
  820. Material2DColored material3 = new Material2DColored(Color.Blue);
  821. Material2DColored materialTexture = new Material2DColored("",
  822. Color.Yellow);
  823. Application.Start(delegate
  824. {
  825. material1.Draw(new Rectangle(0.2f, 0.2f, 0.2f, 0.2f));
  826. material2.Draw(new Rectangle(0.4f, 0.2f, 0.2f, 0.2f));
  827. material3.Draw(new Rectangle(0.6f, 0.2f, 0.2f, 0.2f));
  828. materialTexture.Draw(new Rectangle(0.2f, 0.4f, 0.2f, 0.2f));
  829. });
  830. }
  831. #endregion
  832. #region DrawSimpleBrownMaterial
  833. /// <summary>
  834. /// Draws a simple brown material fullscreen. Testing bug report #7482.
  835. /// </summary>
  836. [Test]
  837. public static void DrawSimpleBrownMaterial()
  838. {
  839. Material2DColored background = new Material2DColored(Color.Brown);
  840. Application.Start(delegate
  841. {
  842. background.Draw(Rectangle.One);
  843. });
  844. }
  845. #endregion
  846. #region ResizeWindowAndDrawBoxAndTexture
  847. /// <summary>
  848. /// Helper unit test to fix bug #4472: Different resolution doesn't work
  849. /// with materials, can even happenin some cases that the resolution in the
  850. /// settings file is "reset". Materials are drawn at the wrong positition,
  851. /// DrawManager based drawings works correctly. Tested with a resolution of
  852. /// 640 * 960 pixels, but happens at any resolution.
  853. /// </summary>
  854. [Test]
  855. public static void ResizeWindowAndDrawBoxAndTexture()
  856. {
  857. Material2DColored mat = new Material2DColored("DeltaEngineLogo");
  858. // Note: This is the only way to resize now :)
  859. Application.Window.Resize(640, 960);
  860. // Restore the old size again when closing!
  861. Application.ApplicationClosing += delegate
  862. {
  863. Application.Window.Resize(1024, 768);
  864. };
  865. Rectangle drawArea = Rectangle.FromCenter(Point.Half, mat.Size);
  866. // This will resize the already created window to fix the first part of
  867. // the problem. Then we also have to fix the graphics device, which might
  868. // also be created already, which actually caused all the render bugs!
  869. Application.Start(delegate
  870. {
  871. Font.DrawTopLeftInformation(
  872. "Resolution=" + Settings.Resolution + "\n" +
  873. "Window=" + Application.Window.ViewportPixelWidth + ", " +
  874. Application.Window.ViewportPixelHeight);
  875. Line.Draw(Point.Zero, drawArea.Position, Color.Red);
  876. mat.Draw(drawArea);
  877. });
  878. }
  879. #endregion
  880. #region DrawMultipleColoredMaterials
  881. /// <summary>
  882. /// Helper unit test to fix bug #7481: Most of the materials will be
  883. /// rendered wrong if many different are used - maybe corrupt check for
  884. /// caching?
  885. /// </summary>
  886. [Test]
  887. public static void DrawMultipleColoredMaterials()
  888. {
  889. Material2DColored mat1 = new Material2DColored("DeltaEngineLogo");
  890. Material2DColored mat2 = new Material2DColored("DeltaIcon");
  891. Material2DColored mat3 = new Material2DColored("ImageAnimation01");
  892. Material2DColored mat4 = new Material2DColored("ImageAnimation02");
  893. Material2DColored mat5 = new Material2DColored("ImageAnimation03");
  894. Material2DColored mat6 = new Material2DColored("ImageAnimation02");
  895. Material2DColored mat7 = new Material2DColored("ImageAnimation03");
  896. Material2DColored mat8 = new Material2DColored("ImageAnimation01");
  897. Material2DColored mat9 = new Material2DColored("ImageAnimation03");
  898. Application.Start(delegate
  899. {
  900. Font.DrawTopLeftInformation(
  901. "Resolution=" + Settings.Resolution);
  902. Line.Draw(Point.Zero, Point.One, Color.Red);
  903. mat1.Draw(new Rectangle(0.3f, 0.3f, 0.1f, 0.1f));
  904. mat2.Draw(new Rectangle(0.4f, 0.3f, 0.1f, 0.1f));
  905. mat3.Draw(new Rectangle(0.5f, 0.3f, 0.1f, 0.1f));
  906. mat4.Draw(new Rectangle(0.3f, 0.4f, 0.1f, 0.1f));
  907. mat5.Draw(new Rectangle(0.4f, 0.4f, 0.1f, 0.1f));
  908. mat6.Draw(new Rectangle(0.5f, 0.4f, 0.1f, 0.1f));
  909. mat7.Draw(new Rectangle(0.3f, 0.5f, 0.1f, 0.1f));
  910. mat8.Draw(new Rectangle(0.4f, 0.5f, 0.1f, 0.1f));
  911. mat9.Draw(new Rectangle(0.5f, 0.5f, 0.1f, 0.1f));
  912. });
  913. /*this part of the test only works with ZooMumbaMobile test content!
  914. Material2DColored mat1 = new Material2DColored("AnimalTileBlue");
  915. Material2DColored mat2 = new Material2DColored("AnimalTileCyan");
  916. Material2DColored mat3 = new Material2DColored("AnimalTileGreen");
  917. Material2DColored mat4 = new Material2DColored("AnimalTileRed");
  918. Material2DColored mat5 = new Material2DColored("AnimalTileYellow");
  919. Material2DColored mat6 = new Material2DColored("IconChimpanze");
  920. Material2DColored mat7 = new Material2DColored("IconDove");
  921. Material2DColored mat8 = new Material2DColored("IconElephant");
  922. Material2DColored mat9 = new Material2DColored("IconFlamingo");
  923. Material2DColored mat10 = mat1;// new Material2DColored("AnimalTileBlue");
  924. Material2DColored mat11 = mat2;// new Material2DColored("AnimalTileCyan");
  925. Material2DColored mat12 = mat2;// new Material2DColored("AnimalTileCyan");
  926. Material2DColored mat13 = mat2;// new Material2DColored("AnimalTileBlue");
  927. Material2DColored mat14 = mat1;// new Material2DColored("AnimalTileBlue");
  928. Material2DColored mat15 = mat2;// new Material2DColored("AnimalTileYellow");
  929. Application.Start(delegate
  930. {
  931. //Font.DrawTopLeftInformation(
  932. // "Resolution=" + Settings.Resolution);
  933. //Line.Draw(Point.Zero, Point.One, Color.Red);
  934. mat1.Draw(new Rectangle(0.3f, 0.3f, 0.1f, 0.1f));
  935. if (Time.Milliseconds > 500)
  936. mat2.Draw(new Rectangle(0.4f, 0.3f, 0.1f, 0.1f));
  937. if (Time.Milliseconds > 1000)
  938. mat3.Draw(new Rectangle(0.5f, 0.3f, 0.1f, 0.1f));
  939. if (Time.Milliseconds > 1500)
  940. mat4.Draw(new Rectangle(0.3f, 0.4f, 0.1f, 0.1f));
  941. if (Time.Milliseconds > 2000)
  942. mat5.Draw(new Rectangle(0.4f, 0.4f, 0.1f, 0.1f));
  943. if (Time.Milliseconds > 2500)
  944. mat6.Draw(new Rectangle(0.5f, 0.4f, 0.1f, 0.1f));
  945. if (Time.Milliseconds > 3000)
  946. mat7.Draw(new Rectangle(0.3f, 0.5f, 0.1f, 0.1f));
  947. if (Time.Milliseconds > 3500)
  948. mat8.Draw(new Rectangle(0.4f, 0.5f, 0.1f, 0.1f));
  949. if (Time.Milliseconds > 4000)
  950. mat9.Draw(new Rectangle(0.5f, 0.5f, 0.1f, 0.1f));
  951. if (Time.Milliseconds > 4000)
  952. mat10.Draw(new Rectangle(0.6f, 0.5f, 0.1f, 0.1f));
  953. if (Time.Milliseconds > 4000)
  954. mat11.Draw(new Rectangle(0.3f, 0.6f, 0.1f, 0.1f));
  955. if (Time.Milliseconds > 4000)
  956. mat12.Draw(new Rectangle(0.4f, 0.6f, 0.1f, 0.1f));
  957. if (Time.Milliseconds > 4000)
  958. mat13.Draw(new Rectangle(0.5f, 0.6f, 0.1f, 0.1f));
  959. if (Time.Milliseconds > 4000)
  960. mat14.Draw(new Rectangle(0.6f, 0.6f, 0.1f, 0.1f));
  961. if (Time.Milliseconds > 4000)
  962. mat15.Draw(new Rectangle(0.3f, 0.7f, 0.1f, 0.1f));
  963. });
  964. */
  965. }
  966. #endregion
  967. #region DrawMaterial3D
  968. /// <summary>
  969. /// Draw a material in 3d by using the geometry class.
  970. /// </summary>
  971. [Test]
  972. public static void DrawMaterial3D()
  973. {
  974. LookAtCamera cam = new LookAtCamera(new Vector(1f, -5f, 3f));
  975. Material material = new Material("DeltaEngineLogo",
  976. Shader.Create("TexturedShader3D"));
  977. GeometryData data = GeometryData.CreatePlane(
  978. VertexFormat.Position3DTextured, 20f, 5f);
  979. Geometry geometry = Geometry.Create(data);
  980. Application.Start(delegate
  981. {
  982. Grid.Draw();
  983. material.Draw(geometry);
  984. });
  985. }
  986. #endregion
  987. #region DrawBillboard3D
  988. /// <summary>
  989. /// Draw billboard 3D
  990. /// </summary>
  991. [Test]
  992. public static void DrawBillboard3D()
  993. {
  994. LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
  995. // Note: Texture may not look like a particle, but it is good for
  996. // checking billboard orientation.
  997. MaterialColored billboard = new MaterialColored("DeltaEngineLogo");
  998. Size size = new Size(2f, 2f);
  999. float r = 0f;
  1000. Application.Start(delegate
  1001. {
  1002. // Draw a Grid
  1003. Grid.Draw();
  1004. // Draw Billboards unrotated in different modes and rotated
  1005. // in different modes
  1006. for (int index = 0; index < 2; index++)
  1007. {
  1008. r += Time.CheckEvery(0.03f)
  1009. ? 0.5f
  1010. : 0f;
  1011. r = (r > 360f)
  1012. ? (r - 360f)
  1013. : r;
  1014. float z = index == 0
  1015. ? -1f
  1016. : 1f;
  1017. float rotation = index == 0
  1018. ? 0f
  1019. : r;
  1020. billboard.billboardMode = BillboardMode.CameraFacingPrecise;
  1021. BillboardManager.Instance.Draw(billboard, new Vector(-4f, 0f, z),
  1022. size, rotation);
  1023. billboard.billboardMode = BillboardMode.UpAxis;
  1024. BillboardManager.Instance.Draw(billboard, new Vector(-2f, 0f, z),
  1025. size, rotation);
  1026. billboard.billboardMode = BillboardMode.FrontAxis;
  1027. BillboardManager.Instance.Draw(billboard, new Vector(0f, 0f, z),
  1028. size, rotation);
  1029. billboard.billboardMode = BillboardMode.Ground;
  1030. BillboardManager.Instance.Draw(billboard, new Vector(4f, 0f, z),
  1031. size, rotation);
  1032. }
  1033. });
  1034. }
  1035. #endregion
  1036. #region CreateTextureDynamically
  1037. /// <summary>
  1038. /// Unit test that shows how to create a texture dynamically from rgb data.
  1039. /// </summary>
  1040. [Test]
  1041. public static void CreateTextureDynamically()
  1042. {
  1043. // Define a 8x8 texture with some colors.
  1044. Color[] textureColors = new[]
  1045. {
  1046. Color.Red, Color.Green, Color.Yellow, Color.Blue,
  1047. Color.Black, Color.White, Color.Brown, Color.Orange,
  1048. Color.Green, Color.Yellow, Color.Blue, Color.Black,
  1049. Color.White, Color.Brown, Color.Orange, Color.Red,
  1050. Color.Yellow, Color.Blue, Color.Black, Color.White,
  1051. Color.Brown, Color.Orange, Color.Red, Color.Green,
  1052. Color.Blue, Color.Black, Color.White, Color.Brown,
  1053. Color.Orange, Color.Red, Color.Green, Color.Yellow,
  1054. Color.Black, Color.White, Color.Brown, Color.Orange,
  1055. Color.Red, Color.Green, Color.Yellow, Color.Blue,
  1056. Color.White, Color.Brown, Color.Orange, Color.Red,
  1057. Color.Green, Color.Yellow, Color.Blue, Color.Black,
  1058. Color.Brown, Color.Orange, Color.Red, Color.Green,
  1059. Color.Yellow, Color.Blue, Color.Black, Color.White,
  1060. Color.Orange, Color.Red, Color.Green, Color.Yellow,
  1061. Color.Blue, Color.Black, Color.White, Color.Brown,
  1062. };
  1063. // Convert the colors to rgb data and create texture out of it plus
  1064. // pass it into a material for rendering.
  1065. Material2D dynamicMaterial = new Material2D(Texture.Create(
  1066. Texture.ConvertColorToRgb(textureColors), new Size(8, 8),
  1067. BlendMode.Opaque, false));
  1068. Application.Start(delegate
  1069. {
  1070. dynamicMaterial.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  1071. });
  1072. }
  1073. #endregion
  1074. #region CreateTextureDynamicallyWithAlpha
  1075. /// <summary>
  1076. /// Unit test that shows how to create a texture dynamically from RGBA
  1077. /// data.
  1078. /// </summary>
  1079. [Test]
  1080. public static void CreateTextureDynamicallyWithAlpha()
  1081. {
  1082. // We will use partially transparent Black and Green.
  1083. Color transparentBlack = new Color(Color.Black, 0.5f);
  1084. Color transparentGreen = new Color(Color.Green, 0.2f);
  1085. // Define a 8x8 texture with some colors.
  1086. Color[] textureColors = new[]
  1087. {
  1088. Color.Unused, transparentGreen, Color.Yellow, Color.Blue,
  1089. transparentBlack, Color.White, Color.Brown, Color.Orange,
  1090. transparentGreen, Color.Yellow, Color.Blue, transparentBlack,
  1091. Color.White, Color.Brown, Color.Orange, Color.Red,
  1092. Color.Yellow, Color.Blue, transparentBlack, Color.White,
  1093. Color.Brown, Color.Orange, Color.Red, transparentGreen,
  1094. Color.Blue, transparentBlack, Color.White, Color.Brown,
  1095. Color.Orange, Color.Red, transparentGreen, Color.Yellow,
  1096. transparentBlack, Color.White, Color.Brown, Color.Orange,
  1097. Color.Red, transparentGreen, Color.Yellow, Color.Blue,
  1098. Color.White, Color.Brown, Color.Orange, Color.Red,
  1099. transparentGreen, Color.Yellow, Color.Blue, transparentBlack,
  1100. Color.Brown, Color.Orange, Color.Red, transparentGreen,
  1101. Color.Yellow, Color.Blue, transparentBlack, Color.White,
  1102. Color.Orange, Color.Red, transparentGreen, Color.Yellow,
  1103. Color.Blue, transparentBlack, Color.White, Color.Brown,
  1104. };
  1105. // Convert the colors to RGBA data and create texture out of it plus
  1106. // pass it into a material for rendering.
  1107. Material2D dynamicMaterial = new Material2D(Texture.Create(
  1108. Texture.ConvertColorToRgba(textureColors), new Size(8, 8),
  1109. BlendMode.Translucency, false, true));
  1110. Application.Start(delegate
  1111. {
  1112. dynamicMaterial.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  1113. Font.DrawTopLeftInformation(
  1114. "Click to change background color to see alpha a bit better");
  1115. // Clicking changes the background color
  1116. if (Input.Mouse.LeftButtonReleased)
  1117. {
  1118. Application.BackgroundColor = Color.Random;
  1119. }
  1120. });
  1121. }
  1122. #endregion
  1123. #region DrawLineOnTexture
  1124. /// <summary>
  1125. /// Draw line onto render to texture
  1126. /// </summary>
  1127. [Test]
  1128. public static void DrawLineOnTexture()
  1129. {
  1130. RenderToTexture renderToTexture = RenderToTexture.Create(
  1131. new Size(256, 256), false, false);
  1132. Material2D mat = new Material2D(renderToTexture);
  1133. Application.Start(delegate
  1134. {
  1135. // Render line into texture.
  1136. renderToTexture.Render(delegate
  1137. {
  1138. // Line from top left up to bottom right in quadratic space
  1139. Line.Draw(new Point(0, 0), new Point(1, 1), Color.Red);
  1140. Line.Draw(new Point(0, 0.3f), new Point(1, 0.3f), Color.Red);
  1141. // Also draw a default material.
  1142. //Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  1143. DrawManager.Instance.Run();
  1144. //MaterialManager.Instance.Run();
  1145. });
  1146. // Also draw line and material in normal mode.
  1147. //Line.Draw(new Point(0, 0), new Point(1, 1), Color.Green);
  1148. //Material2D.Default.Draw(new Rectangle(0.7f, 0.3f, 0.1f, 0.1f));
  1149. // Show rendered texture with our material.
  1150. mat.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
  1151. });
  1152. }
  1153. #endregion
  1154. #region MaskTexture
  1155. /// <summary>
  1156. /// Draws a mask into a stencilbuffer hiding parts of a texture
  1157. /// </summary>
  1158. [Test]
  1159. public static void MaskTexture()
  1160. {
  1161. Application.BackgroundColor = Color.Red;
  1162. RenderToTexture renderToTextureWithStencil = RenderToTexture.Create(
  1163. new Size(256, 256), false, false, true);
  1164. Material2D stencilMask = new Material2D(renderToTextureWithStencil);
  1165. Application.Start(delegate
  1166. {
  1167. // Render line into texture.
  1168. renderToTextureWithStencil.Render(delegate
  1169. {
  1170. // Draw a circle mask in the center
  1171. Circle.DrawFilled(new Point(0.5f, 0.5f), 0.3f, Color.Black);
  1172. DrawManager.Instance.Run();
  1173. });
  1174. // Show masked texture with our stencilMask material.
  1175. stencilMask.Draw(new Rectangle(0.2f, 0.3f, 0.5f, 0.5f));
  1176. });
  1177. }
  1178. #endregion
  1179. #region TakeScreenshot
  1180. /// <summary>
  1181. /// Test the screenshot capturer.
  1182. /// </summary>
  1183. [Test]
  1184. public static void TakeScreenshot()
  1185. {
  1186. Application.Start(delegate
  1187. {
  1188. // Draw dummy image
  1189. Material2D.Default.Draw(new Rectangle(0, 0, 0.5f, 0.5f));
  1190. // And some text
  1191. Font.Default.Draw(
  1192. "Click Left to take a screenshot!\n" +
  1193. "The app will then close and open the image automatically.",
  1194. Rectangle.FromCenter(Point.Half, Size.Half));
  1195. // Capture when clicking.
  1196. if (Input.Mouse.LeftButtonReleased)
  1197. {
  1198. string screenshotPath = DrawManager.MakeScreenshot();
  1199. // Also quit the application and show the screenshot we just made.
  1200. Application.Quit();
  1201. Process.Start(screenshotPath);
  1202. }
  1203. });
  1204. }
  1205. #endregion
  1206. }
  1207. }