2
0

CustomEffectMaterialProcessor.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CustomEffectMaterialProcessor.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.IO;
  12. using System.ComponentModel;
  13. using System.Collections.Generic;
  14. using Microsoft.Xna.Framework.Content.Pipeline;
  15. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  16. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  17. #endregion
  18. namespace CustomEffectPipeline
  19. {
  20. /// <summary>
  21. /// Custom material processor loads specified effect file to use for the model.
  22. /// </summary>
  23. [ContentProcessor]
  24. public class CustomEffectMaterialProcessor : MaterialProcessor
  25. {
  26. [DisplayName("Custom Effect")]
  27. [Description("The custom effect applied to the model.")]
  28. public string CustomEffect
  29. {
  30. get { return customEffect; }
  31. set { customEffect = value; }
  32. }
  33. private string customEffect;
  34. /// <summary>
  35. /// Creates new material with the new effect file.
  36. /// </summary>
  37. public override MaterialContent Process(MaterialContent input,
  38. ContentProcessorContext context)
  39. {
  40. if (string.IsNullOrEmpty(customEffect))
  41. throw new ArgumentException("Custom Effect not set to an effect file");
  42. // Create a new effect material.
  43. EffectMaterialContent customMaterial = new EffectMaterialContent();
  44. // Point the new material at the custom effect file.
  45. string effectFile = Path.GetFullPath(customEffect);
  46. customMaterial.Effect = new ExternalReference<EffectContent>(effectFile);
  47. // Loop over the textures in the current material adding them to
  48. // the new material.
  49. foreach (KeyValuePair<string,
  50. ExternalReference<TextureContent>> textureContent in input.Textures)
  51. {
  52. customMaterial.Textures.Add(textureContent.Key, textureContent.Value);
  53. }
  54. // Loop over the opaque data in the current material adding them to
  55. // the new material.
  56. foreach (KeyValuePair<string, Object> opaqueData in input.OpaqueData)
  57. {
  58. customMaterial.OpaqueData.Add(opaqueData.Key, opaqueData.Value);
  59. }
  60. // Call the base material processor to continue the rest of the processing.
  61. return base.Process(customMaterial, context);
  62. }
  63. }
  64. }