Material.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Scene;
  33. class Technique;
  34. class Texture;
  35. class Texture2D;
  36. class TextureCube;
  37. class ValueAnimationInfo;
  38. /// %Material's shader parameter definition.
  39. struct MaterialShaderParameter
  40. {
  41. /// Name.
  42. String name_;
  43. /// Value.
  44. Variant value_;
  45. };
  46. /// %Material's technique list entry.
  47. struct TechniqueEntry
  48. {
  49. /// Construct with defaults.
  50. TechniqueEntry();
  51. /// Construct with parameters.
  52. TechniqueEntry(Technique* tech, unsigned qualityLevel, float lodDistance);
  53. /// Destruct.
  54. ~TechniqueEntry();
  55. /// Technique.
  56. SharedPtr<Technique> technique_;
  57. /// Quality level.
  58. int qualityLevel_;
  59. /// LOD distance.
  60. float lodDistance_;
  61. };
  62. /// Material's shader parameter animation instance.
  63. class ShaderParameterAnimationInfo : public ValueAnimationInfo
  64. {
  65. public:
  66. /// Construct.
  67. ShaderParameterAnimationInfo(Material* material, const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed);
  68. /// Copy construct.
  69. ShaderParameterAnimationInfo(const ShaderParameterAnimationInfo& other);
  70. /// Destruct.
  71. ~ShaderParameterAnimationInfo();
  72. /// Return shader parameter name.
  73. const String& GetName() const { return name_; }
  74. protected:
  75. /// Apply new animation value to the target object. Called by Update().
  76. virtual void ApplyValue(const Variant& newValue);
  77. private:
  78. /// Shader parameter name.
  79. String name_;
  80. };
  81. /// Describes how to render 3D geometries.
  82. class URHO3D_API Material : public Resource
  83. {
  84. OBJECT(Material);
  85. public:
  86. /// Construct.
  87. Material(Context* context);
  88. /// Destruct.
  89. ~Material();
  90. /// Register object factory.
  91. static void RegisterObject(Context* context);
  92. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  93. virtual bool BeginLoad(Deserializer& source);
  94. /// Finish resource loading. Always called from the main thread. Return true if successful.
  95. virtual bool EndLoad();
  96. /// Save resource. Return true if successful.
  97. virtual bool Save(Serializer& dest) const;
  98. /// Load from an XML element. Return true if successful.
  99. bool Load(const XMLElement& source);
  100. /// Save to an XML element. Return true if successful.
  101. bool Save(XMLElement& dest) const;
  102. /// Set number of techniques.
  103. void SetNumTechniques(unsigned num);
  104. /// Set technique.
  105. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  106. /// Set shader parameter.
  107. void SetShaderParameter(const String& name, const Variant& value);
  108. /// Set shader parameter animation.
  109. void SetShaderParameterAnimation(const String& name, ValueAnimation* animation, WrapMode wrapMode = WM_LOOP, float speed = 1.0f);
  110. /// Set shader parameter animation wrap mode.
  111. void SetShaderParameterAnimationWrapMode(const String& name, WrapMode wrapMode);
  112. /// Set shader parameter animation speed.
  113. void SetShaderParameterAnimationSpeed(const String& name, float speed);
  114. /// Set texture.
  115. void SetTexture(TextureUnit unit, Texture* texture);
  116. /// Set texture coordinate transform.
  117. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  118. /// Set texture coordinate transform.
  119. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  120. /// Set culling mode.
  121. void SetCullMode(CullMode mode);
  122. /// Set culling mode for shadows.
  123. void SetShadowCullMode(CullMode mode);
  124. /// Set depth bias.
  125. void SetDepthBias(const BiasParameters& parameters);
  126. /// Associate the material with a scene to ensure that shader parameter animation happens in sync with scene update, respecting the scene time scale. If no scene is set, the global update events will be used.
  127. void SetScene(Scene* scene);
  128. /// Remove shader parameter.
  129. void RemoveShaderParameter(const String& name);
  130. /// Reset all shader pointers.
  131. void ReleaseShaders();
  132. /// Clone the material.
  133. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  134. /// Ensure that material techniques are listed in correct order.
  135. void SortTechniques();
  136. /// Mark material for auxiliary view rendering.
  137. void MarkForAuxView(unsigned frameNumber);
  138. /// Return number of techniques.
  139. unsigned GetNumTechniques() const { return techniques_.Size(); }
  140. /// Return all techniques.
  141. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  142. /// Return technique entry by index.
  143. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  144. /// Return technique by index.
  145. Technique* GetTechnique(unsigned index) const;
  146. /// Return pass by technique index and pass type.
  147. Pass* GetPass(unsigned index, StringHash passType) const;
  148. /// Return texture by unit.
  149. Texture* GetTexture(TextureUnit unit) const;
  150. /// Return all textures.
  151. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  152. /// Return shader parameter.
  153. const Variant& GetShaderParameter(const String& name) const;
  154. /// Return shader parameter animation.
  155. ValueAnimation* GetShaderParameterAnimation(const String& name) const;
  156. /// Return shader parameter animation wrap mode.
  157. WrapMode GetShaderParameterAnimationWrapMode(const String& name) const;
  158. /// Return shader parameter animation speed.
  159. float GetShaderParameterAnimationSpeed(const String& name) const;
  160. /// Return all shader parameters.
  161. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  162. /// Return normal culling mode.
  163. CullMode GetCullMode() const { return cullMode_; }
  164. /// Return culling mode for shadows.
  165. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  166. /// Return depth bias.
  167. const BiasParameters& GetDepthBias() const { return depthBias_; }
  168. /// Return last auxiliary view rendered frame number.
  169. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  170. /// Return whether should render occlusion.
  171. bool GetOcclusion() const { return occlusion_; }
  172. /// Return whether should render specular.
  173. bool GetSpecular() const { return specular_; }
  174. /// Return the scene associated with the material for shader parameter animation updates.
  175. Scene* GetScene() const;
  176. /// Return name for texture unit.
  177. static String GetTextureUnitName(TextureUnit unit);
  178. /// Parse a shader parameter value from a string. Retunrs either a bool, a float, or a 2 to 4-component vector.
  179. static Variant ParseShaderParameterValue(const String& value);
  180. private:
  181. /// Re-evaluate occlusion rendering.
  182. void CheckOcclusion();
  183. /// Reset to defaults.
  184. void ResetToDefaults();
  185. /// Recalculate the memory used by the material.
  186. void RefreshMemoryUse();
  187. /// Return shader parameter animation info.
  188. ShaderParameterAnimationInfo* GetShaderParameterAnimationInfo(const String& name) const;
  189. /// Update whether should be subscribed to scene or global update events for shader parameter animation.
  190. void UpdateEventSubscription();
  191. /// Update shader parameter animations.
  192. void HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData);
  193. /// Techniques.
  194. Vector<TechniqueEntry> techniques_;
  195. /// Textures.
  196. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  197. /// %Shader parameters.
  198. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  199. /// %Shader parameters animation infos.
  200. HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> > shaderParameterAnimationInfos_;
  201. /// Normal culling mode.
  202. CullMode cullMode_;
  203. /// Culling mode for shadow rendering.
  204. CullMode shadowCullMode_;
  205. /// Depth bias parameters.
  206. BiasParameters depthBias_;
  207. /// Last auxiliary view rendered frame number.
  208. unsigned auxViewFrameNumber_;
  209. /// Render occlusion flag.
  210. bool occlusion_;
  211. /// Specular lighting flag.
  212. bool specular_;
  213. /// Flag for whether is subscribed to animation updates.
  214. bool subscribed_;
  215. /// XML file used while loading.
  216. SharedPtr<XMLFile> loadXMLFile_;
  217. /// Associated scene for shader parameter animation updates.
  218. WeakPtr<Scene> scene_;
  219. };
  220. }