AlphaDemo.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //-----------------------------------------------------------------------------
  2. // AlphaDemo.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using SkinnedModel;
  14. using SimpleAnimation;
  15. using System.Diagnostics;
  16. namespace XnaGraphicsDemo
  17. {
  18. /// <summary>
  19. /// Demo shows how to use AlphaTestEffect.
  20. /// </summary>
  21. class AlphaDemo : MenuComponent
  22. {
  23. // Fields.
  24. Tank tank = new Tank();
  25. Model grid;
  26. RenderTarget2D renderTarget;
  27. AlphaTestEffect alphaTestEffect;
  28. float cameraRotation = 0;
  29. /// <summary>
  30. /// Constructor.
  31. /// </summary>
  32. public AlphaDemo(DemoGame game)
  33. : base(game)
  34. {
  35. Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
  36. }
  37. /// <summary>
  38. /// Resets the menu state.
  39. /// </summary>
  40. public override void Reset()
  41. {
  42. cameraRotation = 0.85f;
  43. base.Reset();
  44. }
  45. /// <summary>
  46. /// Loads content for this demo.
  47. /// </summary>
  48. protected override void LoadContent()
  49. {
  50. tank.Load(Game.Content);
  51. renderTarget = new RenderTarget2D(GraphicsDevice, 400, 400, false, SurfaceFormat.Color, DepthFormat.Depth24);
  52. alphaTestEffect = new AlphaTestEffect(GraphicsDevice);
  53. alphaTestEffect.AlphaFunction = CompareFunction.Greater;
  54. alphaTestEffect.ReferenceAlpha = 128;
  55. grid = Game.Content.Load<Model>("grid");
  56. }
  57. /// <summary>
  58. /// Animates the tank model.
  59. /// </summary>
  60. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  61. public override void Update(GameTime gameTime)
  62. {
  63. tank.Animate(gameTime);
  64. base.Update(gameTime);
  65. }
  66. /// <summary>
  67. /// Draws the AlphaTestEffect demo.
  68. /// </summary>
  69. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  70. public override void Draw(GameTime gameTime)
  71. {
  72. // Compute camera matrices.
  73. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  74. Matrix tankRotation = Matrix.CreateRotationY(time * 0.15f);
  75. Matrix sceneRotation = Matrix.CreateRotationY(cameraRotation);
  76. Vector3 cameraPosition = new Vector3(1250, 250, 0);
  77. Vector3 cameraTarget = new Vector3(0, -100, 0);
  78. Matrix view = Matrix.CreateLookAt(cameraPosition,
  79. cameraTarget,
  80. Vector3.Up);
  81. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  82. GraphicsDevice.Viewport.AspectRatio,
  83. 10,
  84. 10000);
  85. // Draw a single copy of the tank model into a rendertarget.
  86. DrawTankIntoRenderTarget(tankRotation, sceneRotation);
  87. // Draw the scene background.
  88. DrawTitle("alpha test effect", new Color(192, 192, 192), new Color(156, 156, 156));
  89. GraphicsDevice.BlendState = BlendState.Opaque;
  90. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  91. GraphicsDevice.DepthStencilState = DepthStencilState.None;
  92. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  93. grid.Draw(Matrix.CreateTranslation(0, -8, 0) * sceneRotation, view, projection);
  94. // Draw many copies of the imposter sprite, faking the illusion of a more complex 3D scene with many tanks.
  95. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  96. GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
  97. DrawImposterSprites(tankRotation, sceneRotation, cameraPosition, cameraTarget, view, projection);
  98. base.Draw(gameTime);
  99. }
  100. /// <summary>
  101. /// Draws the 3D tank model into a rendertarget.
  102. /// </summary>
  103. /// <param name="tankRotation">The tank rotation matrix.</param>
  104. /// <param name="sceneRotation">The scene rotation matrix.</param>
  105. void DrawTankIntoRenderTarget(Matrix tankRotation, Matrix sceneRotation)
  106. {
  107. Matrix view = Matrix.CreateLookAt(new Vector3(1250, 650, 0),
  108. new Vector3(0, 0, 0),
  109. Vector3.Up);
  110. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  111. 1,
  112. 10,
  113. 10000);
  114. RenderTargetBinding[] previousRenderTargets = GraphicsDevice.GetRenderTargets();
  115. GraphicsDevice.SetRenderTarget(renderTarget);
  116. GraphicsDevice.BlendState = BlendState.Opaque;
  117. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  118. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  119. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  120. GraphicsDevice.Clear(Color.Transparent);
  121. tank.Draw(tankRotation * sceneRotation * Matrix.CreateScale(0.9f), view, projection, LightingMode.OneVertexLight, true);
  122. GraphicsDevice.SetRenderTargets(previousRenderTargets);
  123. }
  124. /// <summary>
  125. /// Draws many copies of the rendertarget as 2D billboard sprites, positioned within the 3D scene.
  126. /// </summary>
  127. /// <param name="tankRotation">The tank rotation matrix.</param>
  128. /// <param name="sceneRotation">The scene rotation matrix.</param>
  129. /// <param name="cameraPosition">The camera position.</param>
  130. /// <param name="cameraTarget">The camera target.</param>
  131. /// <param name="view">The view matrix.</param>
  132. /// <param name="projection">The projection matrix.</param>
  133. void DrawImposterSprites(Matrix tankRotation, Matrix sceneRotation, Vector3 cameraPosition, Vector3 cameraTarget, Matrix view, Matrix projection)
  134. {
  135. const int start = -2;
  136. const int end = 3;
  137. const int count = (end - start) * (end - start);
  138. const float size = 0.2f;
  139. const float spacing = 240;
  140. const float width = 120;
  141. const float height1 = 135;
  142. const float height2 = -135;
  143. // Create billboard vertices.
  144. VertexPositionTexture[] vertices = new VertexPositionTexture[count * 4];
  145. int i = 0;
  146. for (int x = start; x < end; x++)
  147. {
  148. for (int z = start; z < end; z++)
  149. {
  150. Matrix scale = Matrix.CreateScale(size);
  151. Matrix translation = Matrix.CreateTranslation(x * spacing, 0, z * spacing);
  152. Matrix world = tankRotation * scale * translation * sceneRotation;
  153. Matrix billboard = Matrix.CreateConstrainedBillboard(world.Translation, cameraPosition, Vector3.Up, cameraTarget - cameraPosition, null);
  154. vertices[i].Position = Vector3.Transform(new Vector3(width, height1, 0), billboard);
  155. vertices[i++].TextureCoordinate = new Vector2(0, 0);
  156. vertices[i].Position = Vector3.Transform(new Vector3(-width, height1, 0), billboard);
  157. vertices[i++].TextureCoordinate = new Vector2(1, 0);
  158. vertices[i].Position = Vector3.Transform(new Vector3(-width, height2, 0), billboard);
  159. vertices[i++].TextureCoordinate = new Vector2(1, 1);
  160. vertices[i].Position = Vector3.Transform(new Vector3(width, height2, 0), billboard);
  161. vertices[i++].TextureCoordinate = new Vector2(0, 1);
  162. }
  163. }
  164. // Create billboard indices.
  165. short[] indices = new short[count * 6];
  166. short currentVertex = 0;
  167. i = 0;
  168. while (i < indices.Length)
  169. {
  170. indices[i++] = currentVertex;
  171. indices[i++] = (short)(currentVertex + 1);
  172. indices[i++] = (short)(currentVertex + 2);
  173. indices[i++] = currentVertex;
  174. indices[i++] = (short)(currentVertex + 2);
  175. indices[i++] = (short)(currentVertex + 3);
  176. currentVertex += 4;
  177. }
  178. // Draw the billboard sprites.
  179. alphaTestEffect.World = Matrix.Identity;
  180. alphaTestEffect.View = view;
  181. alphaTestEffect.Projection = projection;
  182. alphaTestEffect.Texture = renderTarget;
  183. alphaTestEffect.CurrentTechnique.Passes[0].Apply();
  184. GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, count * 4, indices, 0, count * 2);
  185. }
  186. /// <summary>
  187. /// Dragging on the menu background rotates the camera.
  188. /// </summary>
  189. /// <param name="delta">The amount the mouse was dragged.</param>
  190. protected override void OnDrag(Vector2 delta)
  191. {
  192. cameraRotation += delta.X / 400;
  193. }
  194. }
  195. }