EnvironmentMappedMaterialProcessor.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. EnvironmentMapMaterialContent customMaterial = new EnvironmentMapMaterialContent();
  44. // Copy texture data across from the original material.
  45. BasicMaterialContent basicMaterial = (BasicMaterialContent)input;
  46. if (basicMaterial.Texture != null)
  47. customMaterial.Texture = basicMaterial.Texture;
  48. // Add the reflection texture.
  49. string envmap = Path.GetFullPath(EnvironmentMap);
  50. customMaterial.EnvironmentMap = new ExternalReference<TextureContent>(envmap);
  51. // Chain to the base material processor.
  52. return base.Process(customMaterial, context);
  53. }
  54. /// <summary>
  55. /// Builds a texture for use by this material.
  56. /// </summary>
  57. protected override ExternalReference<TextureContent> BuildTexture(
  58. string textureName,
  59. ExternalReference<TextureContent> texture,
  60. ContentProcessorContext context)
  61. {
  62. // Use our custom CubemapProcessor for the environment map texture.
  63. if (textureName == "EnvironmentMap")
  64. {
  65. return context.BuildAsset<TextureContent,
  66. TextureContent>(texture, "CubemapProcessor");
  67. }
  68. // Apply default processing to all other textures.
  69. return base.BuildTexture(textureName, texture, context);
  70. }
  71. }
  72. }