SOverrideMaterial.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2017 Michael Zeilfelder
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include <vector>
  6. #include "SMaterial.h"
  7. namespace irr
  8. {
  9. namespace video
  10. {
  11. struct SOverrideMaterial
  12. {
  13. //! The Material values
  14. SMaterial Material;
  15. //! Which values are overridden
  16. /** OR'ed values from E_MATERIAL_PROPS. */
  17. u32 EnableProps;
  18. //! For those properties in EnableProps which affect layers, set which of the layers are affected
  19. bool EnableLayerProps[MATERIAL_MAX_TEXTURES];
  20. //! Which textures are overridden
  21. bool EnableTextures[MATERIAL_MAX_TEXTURES];
  22. //! Overwrite complete layers (settings of EnableLayerProps and EnableTextures don't matter then for layer data)
  23. bool EnableLayers[MATERIAL_MAX_TEXTURES];
  24. //! Set in which render passes the material override is active.
  25. /** OR'ed values from E_SCENE_NODE_RENDER_PASS. */
  26. u16 EnablePasses;
  27. //! Global enable flag, overwritten by the SceneManager in each pass
  28. /** NOTE: This is generally _not_ set by users of the engine, but the
  29. Scenemanager uses the EnablePass array and sets Enabled to true if the
  30. Override material is enabled in the current pass.
  31. As user you generally _only_ set EnablePasses.
  32. The exception is when rendering without SceneManager but using draw calls in the VideoDriver. */
  33. bool Enabled;
  34. struct SMaterialTypeReplacement
  35. {
  36. SMaterialTypeReplacement(s32 original, u32 replacement) :
  37. Original(original), Replacement(replacement) {}
  38. SMaterialTypeReplacement(u32 replacement) :
  39. Original(-1), Replacement(replacement) {}
  40. //! SMaterial.MaterialType to replace.
  41. //! -1 for all types or a specific value to only replace that one (which is either one of E_MATERIAL_TYPE or a shader material id)
  42. s32 Original;
  43. //! MaterialType to used to override Original (either one of E_MATERIAL_TYPE or a shader material id)
  44. u32 Replacement;
  45. };
  46. //! To overwrite SMaterial::MaterialType
  47. std::vector<SMaterialTypeReplacement> MaterialTypes;
  48. //! Default constructor
  49. SOverrideMaterial() :
  50. EnableProps(0), EnablePasses(0), Enabled(false)
  51. {
  52. }
  53. //! disable overrides and reset all properties
  54. void reset()
  55. {
  56. EnableProps = 0;
  57. EnablePasses = 0;
  58. Enabled = false;
  59. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  60. EnableLayerProps[i] = true; // doesn't do anything unless EnableProps is set, just saying by default all texture layers are affected by properties
  61. EnableTextures[i] = false;
  62. EnableLayers[i] = false;
  63. }
  64. MaterialTypes.clear();
  65. }
  66. //! Apply the enabled overrides
  67. void apply(SMaterial &material)
  68. {
  69. if (Enabled) {
  70. for (const auto &mtr : MaterialTypes) {
  71. if (mtr.Original < 0 || mtr.Original == (s32)material.MaterialType)
  72. material.MaterialType = (E_MATERIAL_TYPE)mtr.Replacement;
  73. }
  74. for (u32 f = 0; f < 32; ++f) {
  75. const u32 num = (1 << f);
  76. if (EnableProps & num) {
  77. switch (num) {
  78. case EMP_WIREFRAME:
  79. material.Wireframe = Material.Wireframe;
  80. break;
  81. case EMP_POINTCLOUD:
  82. material.PointCloud = Material.PointCloud;
  83. break;
  84. case EMP_ZBUFFER:
  85. material.ZBuffer = Material.ZBuffer;
  86. break;
  87. case EMP_ZWRITE_ENABLE:
  88. material.ZWriteEnable = Material.ZWriteEnable;
  89. break;
  90. case EMP_BACK_FACE_CULLING:
  91. material.BackfaceCulling = Material.BackfaceCulling;
  92. break;
  93. case EMP_FRONT_FACE_CULLING:
  94. material.FrontfaceCulling = Material.FrontfaceCulling;
  95. break;
  96. case EMP_MIN_FILTER:
  97. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  98. if (EnableLayerProps[i]) {
  99. material.TextureLayers[i].MinFilter = Material.TextureLayers[i].MinFilter;
  100. }
  101. }
  102. break;
  103. case EMP_MAG_FILTER:
  104. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  105. if (EnableLayerProps[i]) {
  106. material.TextureLayers[i].MagFilter = Material.TextureLayers[i].MagFilter;
  107. }
  108. }
  109. break;
  110. case EMP_ANISOTROPIC_FILTER:
  111. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  112. if (EnableLayerProps[i]) {
  113. material.TextureLayers[i].AnisotropicFilter = Material.TextureLayers[i].AnisotropicFilter;
  114. }
  115. }
  116. break;
  117. case EMP_FOG_ENABLE:
  118. material.FogEnable = Material.FogEnable;
  119. break;
  120. case EMP_TEXTURE_WRAP:
  121. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  122. if (EnableLayerProps[i]) {
  123. material.TextureLayers[i].TextureWrapU = Material.TextureLayers[i].TextureWrapU;
  124. material.TextureLayers[i].TextureWrapV = Material.TextureLayers[i].TextureWrapV;
  125. material.TextureLayers[i].TextureWrapW = Material.TextureLayers[i].TextureWrapW;
  126. }
  127. }
  128. break;
  129. case EMP_ANTI_ALIASING:
  130. material.AntiAliasing = Material.AntiAliasing;
  131. break;
  132. case EMP_COLOR_MASK:
  133. material.ColorMask = Material.ColorMask;
  134. break;
  135. case EMP_USE_MIP_MAPS:
  136. material.UseMipMaps = Material.UseMipMaps;
  137. break;
  138. case EMP_BLEND_OPERATION:
  139. material.BlendOperation = Material.BlendOperation;
  140. break;
  141. case EMP_BLEND_FACTOR:
  142. material.BlendFactor = Material.BlendFactor;
  143. break;
  144. case EMP_POLYGON_OFFSET:
  145. material.PolygonOffsetDepthBias = Material.PolygonOffsetDepthBias;
  146. material.PolygonOffsetSlopeScale = Material.PolygonOffsetSlopeScale;
  147. break;
  148. }
  149. }
  150. }
  151. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i) {
  152. if (EnableLayers[i]) {
  153. material.TextureLayers[i] = Material.TextureLayers[i];
  154. } else if (EnableTextures[i]) {
  155. material.TextureLayers[i].Texture = Material.TextureLayers[i].Texture;
  156. }
  157. }
  158. }
  159. }
  160. };
  161. } // end namespace video
  162. } // end namespace irr