Material.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "GraphicsDefs.h"
  24. #include "Light.h"
  25. #include "Resource.h"
  26. #include "ValueAnimationInfo.h"
  27. #include "Vector4.h"
  28. namespace Urho3D
  29. {
  30. class Material;
  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. /// Material's shader parameter animation instance.
  62. class ShaderParameterAnimationInfo : public ValueAnimationInfo
  63. {
  64. public:
  65. /// Construct.
  66. ShaderParameterAnimationInfo(Material* material, const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed);
  67. /// Copy construct.
  68. ShaderParameterAnimationInfo(const ShaderParameterAnimationInfo& other);
  69. /// Destruct.
  70. ~ShaderParameterAnimationInfo();
  71. /// Return shader parameter name.
  72. const String& GetName() const { return name_; }
  73. protected:
  74. /// Apply new animation value to the target object. Called by Update().
  75. virtual void ApplyValue(const Variant& newValue);
  76. private:
  77. /// Shader parameter name.
  78. String name_;
  79. };
  80. /// Describes how to render 3D geometries.
  81. class URHO3D_API Material : public Resource
  82. {
  83. OBJECT(Material);
  84. public:
  85. /// Construct.
  86. Material(Context* context);
  87. /// Destruct.
  88. ~Material();
  89. /// Register object factory.
  90. static void RegisterObject(Context* context);
  91. /// Load resource. Return true if successful.
  92. virtual bool Load(Deserializer& source);
  93. /// Save resource. Return true if successful.
  94. virtual bool Save(Serializer& dest) const;
  95. /// Load from an XML element. Return true if successful.
  96. bool Load(const XMLElement& source);
  97. /// Save to an XML element. Return true if successful.
  98. bool Save(XMLElement& dest) const;
  99. /// Set number of techniques.
  100. void SetNumTechniques(unsigned num);
  101. /// Set technique.
  102. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  103. /// Set shader parameter.
  104. void SetShaderParameter(const String& name, const Variant& value);
  105. /// Set shader paramter animation.
  106. void SetShaderParameterAnimation(const String& name, ValueAnimation* animation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  107. /// Set shader paramter animation.
  108. void SetShaderParameterAnimationWrapMode(const String& name, WrapMode wrapMode);
  109. /// Set shader paramter animation.
  110. void SetShaderParameterAnimationSpeed(const String& name, float speed);
  111. /// Set texture.
  112. void SetTexture(TextureUnit unit, Texture* texture);
  113. /// Set texture coordinate transform.
  114. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  115. /// Set texture coordinate transform.
  116. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  117. /// Set culling mode.
  118. void SetCullMode(CullMode mode);
  119. /// Set culling mode for shadows.
  120. void SetShadowCullMode(CullMode mode);
  121. /// Set depth bias.
  122. void SetDepthBias(const BiasParameters& parameters);
  123. /// Remove shader parameter.
  124. void RemoveShaderParameter(const String& name);
  125. /// Reset all shader pointers.
  126. void ReleaseShaders();
  127. /// Clone material.
  128. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  129. /// Ensure that material techniques are listed in correct order.
  130. void SortTechniques();
  131. /// Mark material for auxiliary view rendering.
  132. void MarkForAuxView(unsigned frameNumber);
  133. /// Update shader parameter animations.
  134. void UpdateShaderParameterAnimations();
  135. /// Return number of techniques.
  136. unsigned GetNumTechniques() const { return techniques_.Size(); }
  137. /// Return all techniques.
  138. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  139. /// Return technique entry by index.
  140. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  141. /// Return technique by index.
  142. Technique* GetTechnique(unsigned index) const;
  143. /// Return pass by technique index and pass type.
  144. Pass* GetPass(unsigned index, StringHash passType) const;
  145. /// Return texture by unit.
  146. Texture* GetTexture(TextureUnit unit) const;
  147. /// Return all textures.
  148. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  149. /// Return shader parameter.
  150. const Variant& GetShaderParameter(const String& name) const;
  151. /// Return shader parameter animation.
  152. ValueAnimation* GetShaderParameterAnimation(const String& name) const;
  153. /// Return shader parameter animation wrap mode.
  154. WrapMode GetShaderParameterAnimationWrapMode(const String& name) const;
  155. /// Return shader parameter animation speed.
  156. float GetShaderParameterAnimationSpeed(const String& name) const;
  157. /// Return all shader parameters.
  158. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  159. /// Return normal culling mode.
  160. CullMode GetCullMode() const { return cullMode_; }
  161. /// Return culling mode for shadows.
  162. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  163. /// Return depth bias.
  164. const BiasParameters& GetDepthBias() const { return depthBias_; }
  165. /// Return last auxiliary view rendered frame number.
  166. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  167. /// Return whether should render occlusion.
  168. bool GetOcclusion() const { return occlusion_; }
  169. /// Return whether should render specular.
  170. bool GetSpecular() const { return specular_; }
  171. /// Return name for texture unit.
  172. static String GetTextureUnitName(TextureUnit unit);
  173. /// Parse a shader parameter value from a string. Retunrs either a bool, a float, or a 2 to 4-component vector.
  174. static Variant ParseShaderParameterValue(const String& value);
  175. private:
  176. /// Re-evaluate occlusion rendering.
  177. void CheckOcclusion();
  178. /// Reset to defaults.
  179. void ResetToDefaults();
  180. /// Recalculate the memory used by the material.
  181. void RefreshMemoryUse();
  182. /// Return shader parameter animation info.
  183. ShaderParameterAnimationInfo* GetShaderParameterAnimationInfo(const String& name) const;
  184. /// Techniques.
  185. Vector<TechniqueEntry> techniques_;
  186. /// Textures.
  187. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  188. /// %Shader parameters.
  189. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  190. /// %Shader parameters animation infos.
  191. HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> > shaderParameterAnimationInfos_;
  192. /// Normal culling mode.
  193. CullMode cullMode_;
  194. /// Culling mode for shadow rendering.
  195. CullMode shadowCullMode_;
  196. /// Depth bias parameters.
  197. BiasParameters depthBias_;
  198. /// Last auxiliary view rendered frame number.
  199. unsigned auxViewFrameNumber_;
  200. /// Render occlusion flag.
  201. bool occlusion_;
  202. /// Specular lighting flag.
  203. bool specular_;
  204. /// Last animation update frame number.
  205. unsigned animationFrameNumber_;
  206. };
  207. }