UnlitEffect.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Microsoft.Xna.Framework.Graphics
  6. {
  7. public class UnlitEffect : AnimatedEffect , IEffectFog
  8. {
  9. #region lifecycle
  10. /// <summary>
  11. /// Creates a new AlphaTestEffect with default parameter settings.
  12. /// </summary>
  13. public UnlitEffect(GraphicsDevice device) : base(device, Resources.GetShaderByteCode("Unlit"))
  14. {
  15. _BaseColorMap = new EffectTexture2D.Scalar4(device, this.Parameters, "Primary", 1);
  16. _EmissiveMap = new EffectTexture2D.Scalar3(device, this.Parameters, "Emissive", 3);
  17. _OcclusionMap = new EffectTexture2D.Scalar1(device, this.Parameters, "Occlusion", 4);
  18. _Fog = new EffectBasicFog(device, this.Parameters);
  19. }
  20. #endregion
  21. #region data
  22. private readonly EffectTexture2D.Scalar4 _BaseColorMap;
  23. private readonly EffectTexture2D.Scalar3 _EmissiveMap;
  24. private readonly EffectTexture2D.Scalar1 _OcclusionMap;
  25. private readonly EffectBasicFog _Fog;
  26. #endregion
  27. #region properties - lights
  28. public float Exposure { get; set; } = 1;
  29. #endregion
  30. #region properties - material
  31. public EffectTexture2D.Scalar4 BaseColorMap => _BaseColorMap;
  32. public EffectTexture2D.Scalar3 EmissiveMap => _EmissiveMap;
  33. public EffectTexture2D.Scalar1 OcclusionMap => _OcclusionMap;
  34. #endregion
  35. #region properties - fog
  36. public Vector3 FogColor { get => _Fog.FogColor; set => _Fog.FogColor = value; }
  37. public bool FogEnabled { get => _Fog.FogEnabled; set => _Fog.FogEnabled = value; }
  38. public float FogEnd { get => _Fog.FogEnd; set => _Fog.FogEnd = value; }
  39. public float FogStart { get => _Fog.FogStart; set => _Fog.FogStart = value; }
  40. #endregion
  41. #region API
  42. protected override void OnApply()
  43. {
  44. base.OnApply();
  45. Parameters["Exposure"].SetValue(this.Exposure);
  46. _Fog.Apply();
  47. Resources.GenerateDotTextures(this.GraphicsDevice); // temporary hack
  48. _BaseColorMap.Apply();
  49. _EmissiveMap.Apply();
  50. _OcclusionMap.Apply();
  51. var techniqueIdx = new UnlitTechniqueOld(BoneCount, _BaseColorMap, EmissiveMap, OcclusionMap);
  52. CurrentTechnique = Techniques[techniqueIdx.Index];
  53. }
  54. #endregion
  55. }
  56. readonly struct UnlitTechnique
  57. {
  58. public UnlitTechnique(int BoneCount, EffectTexture2D diffuse, EffectTexture2D emissive, EffectTexture2D opacity)
  59. {
  60. bool hasSkin = BoneCount > 0;
  61. bool hasDiffuse = diffuse.Texture != null;
  62. bool hasEmissive = emissive.Texture != null;
  63. bool hasOpacity = opacity.Texture != null;
  64. var uvsets = EffectTexture2D.GetMinimumVertexUVSets(diffuse, emissive, opacity);
  65. int vrtMats = 0; // 0=Color, 1=UV0, 2=Color+UV0, 3=Color+UV0+UV1
  66. if (uvsets == 1) vrtMats = 2;
  67. if (uvsets == 2) vrtMats = 3;
  68. Index = (hasSkin ? 1 : 0)
  69. + vrtMats * 2
  70. + (hasDiffuse ? 8 : 0)
  71. + (hasEmissive ? 16 : 0)
  72. + (hasOpacity ? 32 : 0);
  73. }
  74. public readonly int Index;
  75. }
  76. readonly struct UnlitTechniqueOld
  77. {
  78. public UnlitTechniqueOld(int BoneCount, EffectTexture2D diffuse, EffectTexture2D emissive, EffectTexture2D opacity)
  79. {
  80. bool hasSkin = BoneCount > 0;
  81. bool hasDiffuse = diffuse.Texture != null;
  82. bool hasEmissive = emissive.Texture != null;
  83. bool hasOpacity = opacity.Texture != null;
  84. Index = (hasSkin ? 1 : 0)
  85. // 2 was reserved for morphing
  86. + (hasDiffuse ? 4 : 0)
  87. + (hasEmissive ? 8 : 0)
  88. + (hasOpacity ? 16 : 0);
  89. }
  90. public readonly int Index;
  91. }
  92. }