Material.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 from stream. May be called from a worker thread. Return true if successful.
  92. virtual bool BeginLoad(Deserializer& source);
  93. /// Finish resource loading. Always called from the main thread. Return true if successful.
  94. virtual bool EndLoad();
  95. /// Save resource. Return true if successful.
  96. virtual bool Save(Serializer& dest) const;
  97. /// Load from an XML element. Return true if successful.
  98. bool Load(const XMLElement& source);
  99. /// Save to an XML element. Return true if successful.
  100. bool Save(XMLElement& dest) const;
  101. /// Set number of techniques.
  102. void SetNumTechniques(unsigned num);
  103. /// Set technique.
  104. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  105. /// Set shader parameter.
  106. void SetShaderParameter(const String& name, const Variant& value);
  107. /// Set shader parameter animation.
  108. void SetShaderParameterAnimation(const String& name, ValueAnimation* animation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  109. /// Set shader parameter animation wrap mode.
  110. void SetShaderParameterAnimationWrapMode(const String& name, WrapMode wrapMode);
  111. /// Set shader parameter animation speed.
  112. void SetShaderParameterAnimationSpeed(const String& name, float speed);
  113. /// Set texture.
  114. void SetTexture(TextureUnit unit, Texture* texture);
  115. /// Set texture coordinate transform.
  116. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  117. /// Set texture coordinate transform.
  118. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  119. /// Set culling mode.
  120. void SetCullMode(CullMode mode);
  121. /// Set culling mode for shadows.
  122. void SetShadowCullMode(CullMode mode);
  123. /// Set depth bias.
  124. void SetDepthBias(const BiasParameters& parameters);
  125. /// Remove shader parameter.
  126. void RemoveShaderParameter(const String& name);
  127. /// Reset all shader pointers.
  128. void ReleaseShaders();
  129. /// Clone the material.
  130. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  131. /// Ensure that material techniques are listed in correct order.
  132. void SortTechniques();
  133. /// Mark material for auxiliary view rendering.
  134. void MarkForAuxView(unsigned frameNumber);
  135. /// Update shader parameter animations.
  136. void UpdateShaderParameterAnimations();
  137. /// Return number of techniques.
  138. unsigned GetNumTechniques() const { return techniques_.Size(); }
  139. /// Return all techniques.
  140. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  141. /// Return technique entry by index.
  142. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  143. /// Return technique by index.
  144. Technique* GetTechnique(unsigned index) const;
  145. /// Return pass by technique index and pass type.
  146. Pass* GetPass(unsigned index, StringHash passType) const;
  147. /// Return texture by unit.
  148. Texture* GetTexture(TextureUnit unit) const;
  149. /// Return all textures.
  150. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  151. /// Return shader parameter.
  152. const Variant& GetShaderParameter(const String& name) const;
  153. /// Return shader parameter animation.
  154. ValueAnimation* GetShaderParameterAnimation(const String& name) const;
  155. /// Return shader parameter animation wrap mode.
  156. WrapMode GetShaderParameterAnimationWrapMode(const String& name) const;
  157. /// Return shader parameter animation speed.
  158. float GetShaderParameterAnimationSpeed(const String& name) const;
  159. /// Return all shader parameters.
  160. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  161. /// Return normal culling mode.
  162. CullMode GetCullMode() const { return cullMode_; }
  163. /// Return culling mode for shadows.
  164. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  165. /// Return depth bias.
  166. const BiasParameters& GetDepthBias() const { return depthBias_; }
  167. /// Return last auxiliary view rendered frame number.
  168. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  169. /// Return whether should render occlusion.
  170. bool GetOcclusion() const { return occlusion_; }
  171. /// Return whether should render specular.
  172. bool GetSpecular() const { return specular_; }
  173. /// Return name for texture unit.
  174. static String GetTextureUnitName(TextureUnit unit);
  175. /// Parse a shader parameter value from a string. Retunrs either a bool, a float, or a 2 to 4-component vector.
  176. static Variant ParseShaderParameterValue(const String& value);
  177. private:
  178. /// Re-evaluate occlusion rendering.
  179. void CheckOcclusion();
  180. /// Reset to defaults.
  181. void ResetToDefaults();
  182. /// Recalculate the memory used by the material.
  183. void RefreshMemoryUse();
  184. /// Return shader parameter animation info.
  185. ShaderParameterAnimationInfo* GetShaderParameterAnimationInfo(const String& name) const;
  186. /// Techniques.
  187. Vector<TechniqueEntry> techniques_;
  188. /// Textures.
  189. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  190. /// %Shader parameters.
  191. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  192. /// %Shader parameters animation infos.
  193. HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> > shaderParameterAnimationInfos_;
  194. /// Normal culling mode.
  195. CullMode cullMode_;
  196. /// Culling mode for shadow rendering.
  197. CullMode shadowCullMode_;
  198. /// Depth bias parameters.
  199. BiasParameters depthBias_;
  200. /// Last auxiliary view rendered frame number.
  201. unsigned auxViewFrameNumber_;
  202. /// Render occlusion flag.
  203. bool occlusion_;
  204. /// Specular lighting flag.
  205. bool specular_;
  206. /// Last animation update frame number.
  207. unsigned animationFrameNumber_;
  208. /// XML file used while loading.
  209. SharedPtr<XMLFile> loadXMLFile_;
  210. };
  211. }