EnvironmentMappedMaterialProcessor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EnvironmentMappedMaterialProcessor.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.IO;
  11. using System.ComponentModel;
  12. using Microsoft.Xna.Framework.Content.Pipeline;
  13. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  14. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  15. #endregion
  16. namespace CustomModelEffectPipeline
  17. {
  18. /// <summary>
  19. /// Custom content pipeline processor derives from the built-in
  20. /// MaterialProcessor. This changes the material to use our custom
  21. /// environment mapping effect, and also builds the environment map
  22. /// texture in a special way.
  23. /// </summary>
  24. [ContentProcessor]
  25. public class EnvironmentMappedMaterialProcessor : MaterialProcessor
  26. {
  27. private string environmentMap = "seattle.bmp";
  28. [DisplayName("Environment Map")]
  29. [DefaultValue("seattle.bmp")]
  30. [Description("The environment map applied to the model.")]
  31. public string EnvironmentMap
  32. {
  33. get { return environmentMap; }
  34. set { environmentMap = value; }
  35. }
  36. /// <summary>
  37. /// Converts a material.
  38. /// </summary>
  39. public override MaterialContent Process(MaterialContent input,
  40. ContentProcessorContext context)
  41. {
  42. // Create a new effect material.
  43. EffectMaterialContent customMaterial = new EffectMaterialContent();
  44. // Point the new material at our custom effect file.
  45. string effectFile = Path.GetFullPath("EnvironmentMap.fx");
  46. customMaterial.Effect = new ExternalReference<EffectContent>(effectFile);
  47. // Copy texture data across from the original material.
  48. BasicMaterialContent basicMaterial = (BasicMaterialContent)input;
  49. if (basicMaterial.Texture != null)
  50. {
  51. customMaterial.Textures.Add("Texture", basicMaterial.Texture);
  52. customMaterial.OpaqueData.Add("TextureEnabled", true);
  53. }
  54. // Add the reflection texture.
  55. string envmap = Path.GetFullPath(EnvironmentMap);
  56. customMaterial.Textures.Add("EnvironmentMap",
  57. new ExternalReference<TextureContent>(envmap));
  58. // Chain to the base material processor.
  59. return base.Process(customMaterial, context);
  60. }
  61. /// <summary>
  62. /// Builds a texture for use by this material.
  63. /// </summary>
  64. protected override ExternalReference<TextureContent> BuildTexture(
  65. string textureName,
  66. ExternalReference<TextureContent> texture,
  67. ContentProcessorContext context)
  68. {
  69. // Use our custom CubemapProcessor for the environment map texture.
  70. if (textureName == "EnvironmentMap")
  71. {
  72. return context.BuildAsset<TextureContent,
  73. TextureContent>(texture, "CubemapProcessor");
  74. }
  75. // Apply default processing to all other textures.
  76. return base.BuildTexture(textureName, texture, context);
  77. }
  78. }
  79. }