NormalMappingModelProcessor.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // NormalMappingModelProcessor.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Audio;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  17. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  18. using Microsoft.Xna.Framework.Content.Pipeline;
  19. using System.IO;
  20. using System.ComponentModel;
  21. namespace NormalMappingEffectPipeline
  22. {
  23. /// <summary>
  24. /// The NormalMappingModelProcessor is used to change the material/effect applied
  25. /// to a model. After going through this processor, the output model will be set
  26. /// up to be rendered with NormalMapping.fx.
  27. /// </summary>
  28. [ContentProcessor(DisplayName="Normal Mapping Model Processor")]
  29. public class NormalMappingModelProcessor : ModelProcessor
  30. {
  31. // this constant determines where we will look for the normal map in the opaque
  32. // data dictionary.
  33. public const string NormalMapKey = "NormalMap";
  34. /// <summary>
  35. /// We override this property from the base processor and force it to always
  36. /// return true: tangent frames are required for normal mapping, so they should
  37. /// not be optional.
  38. /// </summary>
  39. [Browsable(false)]
  40. public override bool GenerateTangentFrames
  41. {
  42. get { return true; }
  43. set { }
  44. }
  45. /// <summary>
  46. /// The user can set this value in the property grid. If it is set, the model
  47. /// will use this value for its normal map texture, overriding anything in the
  48. /// opaque data. We use the display name and description attributes to control
  49. /// how the property appears in the UI.
  50. /// </summary>
  51. [DisplayName("Normal Map Texture")]
  52. [Description("If set, this file will be used as the normal map on the model, " +
  53. "overriding anything found in the opaque data.")]
  54. [DefaultValue("")]
  55. public string NormalMapTexture
  56. {
  57. get { return normalMapTexture; }
  58. set { normalMapTexture = value; }
  59. }
  60. private string normalMapTexture;
  61. String directory;
  62. public override ModelContent Process(NodeContent input,
  63. ContentProcessorContext context)
  64. {
  65. if (input == null)
  66. {
  67. throw new ArgumentNullException("input");
  68. }
  69. directory = Path.GetDirectoryName(input.Identity.SourceFilename);
  70. LookUpNormalMapAndAddToTextures(input);
  71. return base.Process(input, context);
  72. }
  73. /// <summary>
  74. /// Looks into the OpaqueData property on the "mesh" object, and looks for a
  75. /// a string containing the path to the normal map. The normal map is added
  76. /// to the Textures collection for each of the mesh's materials.
  77. /// </summary>
  78. private void LookUpNormalMapAndAddToTextures(NodeContent node)
  79. {
  80. MeshContent mesh = node as MeshContent;
  81. if (mesh != null)
  82. {
  83. string pathToNormalMap;
  84. // if NormalMapTexture hasn't been set in the UI, we'll try to look up
  85. // the normal map using the opaque data.
  86. if (String.IsNullOrEmpty(NormalMapTexture))
  87. {
  88. pathToNormalMap =
  89. mesh.OpaqueData.GetValue<string>(NormalMapKey, null);
  90. }
  91. // However, if the NormalMapTexture is set, we'll use that value for
  92. // ever mesh in the scene.
  93. else
  94. {
  95. pathToNormalMap = NormalMapTexture;
  96. }
  97. if (pathToNormalMap == null)
  98. {
  99. throw new InvalidContentException("the normal map is missing!");
  100. }
  101. pathToNormalMap = Path.Combine(directory, pathToNormalMap);
  102. foreach (GeometryContent geometry in mesh.Geometry)
  103. {
  104. geometry.Material.Textures.Add(NormalMapKey,
  105. new ExternalReference<TextureContent>(pathToNormalMap));
  106. }
  107. }
  108. foreach (NodeContent child in node.Children)
  109. {
  110. LookUpNormalMapAndAddToTextures(child);
  111. }
  112. }
  113. // acceptableVertexChannelNames are the inputs that the normal map effect
  114. // expects. The NormalMappingModelProcessor overrides ProcessVertexChannel
  115. // to remove all vertex channels which don't have one of these four
  116. // names.
  117. static IList<string> acceptableVertexChannelNames =
  118. new string[]
  119. {
  120. VertexChannelNames.TextureCoordinate(0),
  121. VertexChannelNames.Normal(0),
  122. VertexChannelNames.Binormal(0),
  123. VertexChannelNames.Tangent(0)
  124. };
  125. /// <summary>
  126. /// As an optimization, ProcessVertexChannel is overriden to remove data which
  127. /// is not used by the vertex shader.
  128. /// </summary>
  129. /// <param name="geometry">the geometry object which contains the
  130. /// vertex channel</param>
  131. /// <param name="vertexChannelIndex">the index of the vertex channel
  132. /// to operate on</param>
  133. /// <param name="context">the context that the processor is operating
  134. /// under. in most cases, this parameter isn't necessary; but could
  135. /// be used to log a warning that a channel had been removed.</param>
  136. protected override void ProcessVertexChannel(GeometryContent geometry,
  137. int vertexChannelIndex, ContentProcessorContext context)
  138. {
  139. String vertexChannelName =
  140. geometry.Vertices.Channels[vertexChannelIndex].Name;
  141. // if this vertex channel has an acceptable names, process it as normal.
  142. if (acceptableVertexChannelNames.Contains(vertexChannelName))
  143. {
  144. base.ProcessVertexChannel(geometry, vertexChannelIndex, context);
  145. }
  146. // otherwise, remove it from the vertex channels; it's just extra data
  147. // we don't need.
  148. else
  149. {
  150. geometry.Vertices.Channels.Remove(vertexChannelName);
  151. }
  152. }
  153. protected override MaterialContent ConvertMaterial(MaterialContent material,
  154. ContentProcessorContext context)
  155. {
  156. EffectMaterialContent normalMappingMaterial = new EffectMaterialContent();
  157. normalMappingMaterial.Effect = new ExternalReference<EffectContent>
  158. (Path.Combine(directory, "NormalMapping.fx"));
  159. OpaqueDataDictionary processorParameters = new OpaqueDataDictionary();
  160. processorParameters["ColorKeyColor"] = this.ColorKeyColor;
  161. processorParameters["ColorKeyEnabled"] = this.ColorKeyEnabled;
  162. processorParameters["TextureFormat"] = this.TextureFormat;
  163. processorParameters["GenerateMipmaps"] = this.GenerateMipmaps;
  164. processorParameters["ResizeTexturesToPowerOfTwo"] =
  165. this.ResizeTexturesToPowerOfTwo;
  166. // copy the textures in the original material to the new normal mapping
  167. // material. this way the diffuse texture is preserved. The
  168. // PreprocessSceneHierarchy function has already added the normal map
  169. // texture to the Textures collection, so that will be copied as well.
  170. foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture
  171. in material.Textures)
  172. {
  173. normalMappingMaterial.Textures.Add(texture.Key, texture.Value);
  174. }
  175. // and convert the material using the NormalMappingMaterialProcessor,
  176. // who has something special in store for the normal map.
  177. return context.Convert<MaterialContent, MaterialContent>
  178. (normalMappingMaterial, typeof(NormalMappingMaterialProcessor).Name,
  179. processorParameters);
  180. }
  181. }
  182. }