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

/Chapter8/SkyboxPipeline/SkyboxPipeline/SykboxProcessor.cs

http://xnags30unleashed.codeplex.com
C# | 222 lines | 140 code | 42 blank | 40 comment | 0 complexity | 54ed4770fdace62910cf50ca7c9e7f28 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Content.Pipeline;
  6. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  7. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  8. using TInput = Microsoft.Xna.Framework.Content.Pipeline.Graphics.Texture2DContent;
  9. using TOutput = SkyboxPipeline.SkyboxContent;
  10. using System.ComponentModel;
  11. namespace SkyboxPipeline
  12. {
  13. /// <summary>
  14. /// This class will be instantiated by the XNA Framework Content Pipeline
  15. /// to apply custom processing to content data, converting an object of
  16. /// type TInput to TOutput. The input and output types may be the same if
  17. /// the processor wishes to alter data without changing its type.
  18. ///
  19. /// This should be part of a Content Pipeline Extension Library project.
  20. /// </summary>
  21. [ContentProcessor(DisplayName = "SkyboxProcessor")]
  22. public class SykboxProcessor : ContentProcessor<TInput, TOutput>
  23. {
  24. private int width = 1024;
  25. private int height = 512;
  26. private int cellSize = 256;
  27. public override TOutput Process(TInput input, ContentProcessorContext context)
  28. {
  29. MeshBuilder builder = MeshBuilder.StartMesh("XESkybox");
  30. CreatePositions(ref builder);
  31. AddVerticesInformation(ref builder);
  32. // Finish making the mesh
  33. MeshContent skyboxMesh = builder.FinishMesh();
  34. // Create the output object.
  35. SkyboxContent skybox = new SkyboxContent();
  36. //Compile the mesh we just built through the default ModelProcessor
  37. skybox.Model = context.Convert<MeshContent, ModelContent>(
  38. skyboxMesh, "ModelProcessor");
  39. skybox.Texture = input;
  40. return skybox;
  41. }
  42. private void CreatePositions(ref MeshBuilder builder)
  43. {
  44. Vector3 position;
  45. //&#x2014;&#x2014;&#x2014;&#x2014;near / back plane (behind the camera)
  46. //top left
  47. position = new Vector3(-1, 1, 1);
  48. builder.CreatePosition(position); //0
  49. //bottom right
  50. position = new Vector3(1, -1, 1);
  51. builder.CreatePosition(position); //1
  52. //bottom left
  53. position = new Vector3(-1, -1, 1);
  54. builder.CreatePosition(position); //2
  55. //top right
  56. position = new Vector3(1, 1, 1);
  57. builder.CreatePosition(position); //3
  58. //&#x2014;&#x2014;&#x2014;&#x2014;far / front plane (in front of camera)
  59. //top left
  60. position = new Vector3(-1, 1, -1); //4
  61. builder.CreatePosition(position);
  62. //bottom right
  63. position = new Vector3(1, -1, -1); //5
  64. builder.CreatePosition(position);
  65. //bottom left
  66. position = new Vector3(-1, -1, -1); //6
  67. builder.CreatePosition(position);
  68. //top right
  69. position = new Vector3(1, 1, -1); //7
  70. builder.CreatePosition(position);
  71. }
  72. private Vector2 UV(int u, int v, Vector2 cellIndex)
  73. {
  74. return (new Vector2((cellSize * (cellIndex.X + u) / width),
  75. (cellSize * (cellIndex.Y + v) / height)));
  76. }
  77. private void AddVerticesInformation(ref MeshBuilder builder)
  78. {
  79. //texture locations:
  80. //F,R,B,L
  81. //U,D
  82. //Front
  83. Vector2 fi = new Vector2(0, 0); //cell 0, row 0
  84. //Right
  85. Vector2 ri = new Vector2(1, 0); //cell 1, row 0
  86. //Back
  87. Vector2 bi = new Vector2(2, 0); //cell 2, row 0
  88. //Left
  89. Vector2 li = new Vector2(3, 0); //cell 3, row 0
  90. //Upward (Top)
  91. Vector2 ui = new Vector2(0, 1); //cell 0, row 1
  92. //Downward (Bottom)
  93. Vector2 di = new Vector2(1, 1); //cell 1, row 1
  94. int texCoordChannel = builder.CreateVertexChannel<Vector2>
  95. (VertexChannelNames.TextureCoordinate(0));
  96. //&#x2014;&#x2014;&#x2014;&#x2014;front plane first column, first row
  97. //bottom triangle of front plane
  98. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, fi));
  99. builder.AddTriangleVertex(4); //-1,1,-1
  100. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, fi));
  101. builder.AddTriangleVertex(5); //1,-1,-1
  102. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, fi));
  103. builder.AddTriangleVertex(6); //-1,-1,-1
  104. //top triangle of front plane
  105. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, fi));
  106. builder.AddTriangleVertex(4); //-1,1,-1
  107. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, fi));
  108. builder.AddTriangleVertex(7); //1,1,-1
  109. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, fi));
  110. builder.AddTriangleVertex(5); //1,-1,-1
  111. //&#x2014;&#x2014;&#x2014;&#x2014;right plane
  112. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, ri));
  113. builder.AddTriangleVertex(3);
  114. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, ri));
  115. builder.AddTriangleVertex(1);
  116. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, ri));
  117. builder.AddTriangleVertex(5);
  118. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, ri));
  119. builder.AddTriangleVertex(3);
  120. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, ri));
  121. builder.AddTriangleVertex(5);
  122. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, ri));
  123. builder.AddTriangleVertex(7);
  124. //&#x2014;&#x2014;&#x2014;&#x2014;back pane //3rd column, first row
  125. //bottom triangle of back plane
  126. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, bi)); //1,1
  127. builder.AddTriangleVertex(2); //-1,-1,1
  128. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, bi)); //0,1
  129. builder.AddTriangleVertex(1); //1,-1,1
  130. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, bi)); //1,0
  131. builder.AddTriangleVertex(0); //-1,1,1
  132. //top triangle of back plane
  133. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, bi)); //0,1
  134. builder.AddTriangleVertex(1); //1,-1,1
  135. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, bi)); //0,0
  136. builder.AddTriangleVertex(3); //1,1,1
  137. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, bi)); //1,0
  138. builder.AddTriangleVertex(0); //-1,1,1
  139. //&#x2014;&#x2014;&#x2014;&#x2014;left plane
  140. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, li));
  141. builder.AddTriangleVertex(6);
  142. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, li));
  143. builder.AddTriangleVertex(2);
  144. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, li));
  145. builder.AddTriangleVertex(0);
  146. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, li));
  147. builder.AddTriangleVertex(4);
  148. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, li));
  149. builder.AddTriangleVertex(6);
  150. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, li));
  151. builder.AddTriangleVertex(0);
  152. //&#x2014;&#x2014;&#x2014;&#x2014;upward (top) plane
  153. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, ui));
  154. builder.AddTriangleVertex(3);
  155. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, ui));
  156. builder.AddTriangleVertex(4);
  157. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, ui));
  158. builder.AddTriangleVertex(0);
  159. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, ui));
  160. builder.AddTriangleVertex(3);
  161. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, ui));
  162. builder.AddTriangleVertex(7);
  163. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, ui));
  164. builder.AddTriangleVertex(4);
  165. //&#x2014;&#x2014;&#x2014;&#x2014;downward (bottom) plane
  166. builder.SetVertexChannelData(texCoordChannel, UV(1, 0, di));
  167. builder.AddTriangleVertex(2);
  168. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, di));
  169. builder.AddTriangleVertex(6);
  170. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, di));
  171. builder.AddTriangleVertex(1);
  172. builder.SetVertexChannelData(texCoordChannel, UV(1, 1, di));
  173. builder.AddTriangleVertex(6);
  174. builder.SetVertexChannelData(texCoordChannel, UV(0, 1, di));
  175. builder.AddTriangleVertex(5);
  176. builder.SetVertexChannelData(texCoordChannel, UV(0, 0, di));
  177. builder.AddTriangleVertex(1);
  178. }
  179. }
  180. }