AlphaDemo.cs 9.1 KB

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