Material.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "AnimationDefs.h"
  24. #include "GraphicsDefs.h"
  25. #include "Light.h"
  26. #include "Resource.h"
  27. #include "Vector4.h"
  28. namespace Urho3D
  29. {
  30. class MaterialShaderParameterAnimationInstance;
  31. class Pass;
  32. class Technique;
  33. class Texture;
  34. class Texture2D;
  35. class TextureCube;
  36. class ValueAnimationInfo;
  37. /// %Material's shader parameter definition.
  38. struct MaterialShaderParameter
  39. {
  40. /// Name.
  41. String name_;
  42. /// Value.
  43. Variant value_;
  44. };
  45. /// %Material's technique list entry.
  46. struct TechniqueEntry
  47. {
  48. /// Construct with defaults.
  49. TechniqueEntry();
  50. /// Construct with parameters.
  51. TechniqueEntry(Technique* tech, unsigned qualityLevel, float lodDistance);
  52. /// Destruct.
  53. ~TechniqueEntry();
  54. /// Technique.
  55. SharedPtr<Technique> technique_;
  56. /// Quality level.
  57. int qualityLevel_;
  58. /// LOD distance.
  59. float lodDistance_;
  60. };
  61. /// Describes how to render 3D geometries.
  62. class URHO3D_API Material : public Resource
  63. {
  64. OBJECT(Material);
  65. public:
  66. /// Construct.
  67. Material(Context* context);
  68. /// Destruct.
  69. ~Material();
  70. /// Register object factory.
  71. static void RegisterObject(Context* context);
  72. /// Load resource. Return true if successful.
  73. virtual bool Load(Deserializer& source);
  74. /// Save resource. Return true if successful.
  75. virtual bool Save(Serializer& dest) const;
  76. /// Load from an XML element. Return true if successful.
  77. bool Load(const XMLElement& source);
  78. /// Save to an XML element. Return true if successful.
  79. bool Save(XMLElement& dest) const;
  80. /// Set number of techniques.
  81. void SetNumTechniques(unsigned num);
  82. /// Set technique.
  83. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  84. /// Set shader parameter.
  85. void SetShaderParameter(const String& name, const Variant& value);
  86. /// Set shader paramter animation.
  87. void SetShaderParameterAnimation(const String& name, ValueAnimation* animation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  88. /// Set shader paramter animation.
  89. void SetShaderParameterAnimationWrapMode(const String& name, WrapMode wrapMode);
  90. /// Set shader paramter animation.
  91. void SetShaderParameterAnimationSpeed(const String& name, float speed);
  92. /// Set texture.
  93. void SetTexture(TextureUnit unit, Texture* texture);
  94. /// Set texture coordinate transform.
  95. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  96. /// Set texture coordinate transform.
  97. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  98. /// Set culling mode.
  99. void SetCullMode(CullMode mode);
  100. /// Set culling mode for shadows.
  101. void SetShadowCullMode(CullMode mode);
  102. /// Set depth bias.
  103. void SetDepthBias(const BiasParameters& parameters);
  104. /// Remove shader parameter.
  105. void RemoveShaderParameter(const String& name);
  106. /// Reset all shader pointers.
  107. void ReleaseShaders();
  108. /// Clone material.
  109. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  110. /// Ensure that material techniques are listed in correct order.
  111. void SortTechniques();
  112. /// Mark material for auxiliary view rendering.
  113. void MarkForAuxView(unsigned frameNumber);
  114. /// Update shader parameter animations.
  115. void UpdateShaderParameterAnimations();
  116. /// Return number of techniques.
  117. unsigned GetNumTechniques() const { return techniques_.Size(); }
  118. /// Return all techniques.
  119. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  120. /// Return technique entry by index.
  121. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  122. /// Return technique by index.
  123. Technique* GetTechnique(unsigned index) const;
  124. /// Return pass by technique index and pass type.
  125. Pass* GetPass(unsigned index, StringHash passType) const;
  126. /// Return texture by unit.
  127. Texture* GetTexture(TextureUnit unit) const;
  128. /// Return all textures.
  129. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  130. /// Return shader parameter.
  131. const Variant& GetShaderParameter(const String& name) const;
  132. /// Return shader parameter animation.
  133. ValueAnimation* GetShaderParameterAnimation(const String& name) const;
  134. /// Return shader parameter animation wrap mode.
  135. WrapMode GetShaderParameterAnimationWrapMode(const String& name) const;
  136. /// Return shader parameter animation speed.
  137. float GetShaderParameterAnimationSpeed(const String& name) const;
  138. /// Return all shader parameters.
  139. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  140. /// Return normal culling mode.
  141. CullMode GetCullMode() const { return cullMode_; }
  142. /// Return culling mode for shadows.
  143. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  144. /// Return depth bias.
  145. const BiasParameters& GetDepthBias() const { return depthBias_; }
  146. /// Return last auxiliary view rendered frame number.
  147. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  148. /// Return whether should render occlusion.
  149. bool GetOcclusion() const { return occlusion_; }
  150. /// Return whether should render specular.
  151. bool GetSpecular() const { return specular_; }
  152. /// Return name for texture unit.
  153. static String GetTextureUnitName(TextureUnit unit);
  154. /// Parse a shader parameter value from a string. Retunrs either a bool, a float, or a 2 to 4-component vector.
  155. static Variant ParseShaderParameterValue(const String& value);
  156. private:
  157. /// Re-evaluate occlusion rendering.
  158. void CheckOcclusion();
  159. /// Reset to defaults.
  160. void ResetToDefaults();
  161. /// Recalculate the memory used by the material.
  162. void RefreshMemoryUse();
  163. /// Return shader parameter animation instance.
  164. MaterialShaderParameterAnimationInstance* GetMaterialShaderParameterAnimationInstance(const String& name) const;
  165. /// Techniques.
  166. Vector<TechniqueEntry> techniques_;
  167. /// Textures.
  168. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  169. /// %Shader parameters.
  170. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  171. /// %Shader parameters animation infos.
  172. HashMap<StringHash, SharedPtr<MaterialShaderParameterAnimationInstance> > materialShaderParameterAnimationInstances_;
  173. /// Normal culling mode.
  174. CullMode cullMode_;
  175. /// Culling mode for shadow rendering.
  176. CullMode shadowCullMode_;
  177. /// Depth bias parameters.
  178. BiasParameters depthBias_;
  179. /// Last auxiliary view rendered frame number.
  180. unsigned auxViewFrameNumber_;
  181. /// Render occlusion flag.
  182. bool occlusion_;
  183. /// Specular lighting flag.
  184. bool specular_;
  185. /// Material update frame number.
  186. unsigned frameNumber_;
  187. };
  188. }