Material.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "Vector4.h"
  27. namespace Urho3D
  28. {
  29. class Pass;
  30. class Technique;
  31. class Texture;
  32. class Texture2D;
  33. class TextureCube;
  34. /// %Material's shader parameter definition.
  35. struct MaterialShaderParameter
  36. {
  37. /// Name.
  38. String name_;
  39. /// Value.
  40. Variant value_;
  41. };
  42. /// %Material's technique list entry.
  43. struct TechniqueEntry
  44. {
  45. /// Construct with defaults.
  46. TechniqueEntry();
  47. /// Construct with parameters.
  48. TechniqueEntry(Technique* tech, unsigned qualityLevel, float lodDistance);
  49. /// Destruct.
  50. ~TechniqueEntry();
  51. /// Technique.
  52. SharedPtr<Technique> technique_;
  53. /// Quality level.
  54. int qualityLevel_;
  55. /// LOD distance.
  56. float lodDistance_;
  57. };
  58. /// Describes how to render 3D geometries.
  59. class URHO3D_API Material : public Resource
  60. {
  61. OBJECT(Material);
  62. public:
  63. /// Construct.
  64. Material(Context* context);
  65. /// Destruct.
  66. ~Material();
  67. /// Register object factory.
  68. static void RegisterObject(Context* context);
  69. /// Load resource. Return true if successful.
  70. virtual bool Load(Deserializer& source);
  71. /// Save resource. Return true if successful.
  72. virtual bool Save(Serializer& dest) const;
  73. /// Load from an XML element. Return true if successful.
  74. bool Load(const XMLElement& source);
  75. /// Save to an XML element. Return true if successful.
  76. bool Save(XMLElement& dest) const;
  77. /// Set number of techniques.
  78. void SetNumTechniques(unsigned num);
  79. /// Set technique.
  80. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  81. /// Set shader parameter.
  82. void SetShaderParameter(const String& name, const Variant& value);
  83. /// Set texture.
  84. void SetTexture(TextureUnit unit, Texture* texture);
  85. /// Set texture coordinate transform.
  86. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  87. /// Set texture coordinate transform.
  88. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  89. /// Set culling mode.
  90. void SetCullMode(CullMode mode);
  91. /// Set culling mode for shadows.
  92. void SetShadowCullMode(CullMode mode);
  93. /// Set depth bias.
  94. void SetDepthBias(const BiasParameters& parameters);
  95. /// Remove shader parameter.
  96. void RemoveShaderParameter(const String& name);
  97. /// Reset all shader pointers.
  98. void ReleaseShaders();
  99. /// Clone material.
  100. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  101. /// Ensure that material techniques are listed in correct order.
  102. void SortTechniques();
  103. /// Mark material for auxiliary view rendering.
  104. void MarkForAuxView(unsigned frameNumber);
  105. /// Return number of techniques.
  106. unsigned GetNumTechniques() const { return techniques_.Size(); }
  107. /// Return all techniques.
  108. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  109. /// Return technique entry by index.
  110. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  111. /// Return technique by index.
  112. Technique* GetTechnique(unsigned index) const;
  113. /// Return pass by technique index and pass type.
  114. Pass* GetPass(unsigned index, StringHash passType) const;
  115. /// Return texture by unit.
  116. Texture* GetTexture(TextureUnit unit) const;
  117. /// Return all textures.
  118. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  119. /// Return shader parameter.
  120. const Variant& GetShaderParameter(const String& name) const;
  121. /// Return all shader parameters.
  122. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  123. /// Return normal culling mode.
  124. CullMode GetCullMode() const { return cullMode_; }
  125. /// Return culling mode for shadows.
  126. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  127. /// Return depth bias.
  128. const BiasParameters& GetDepthBias() const { return depthBias_; }
  129. /// Return last auxiliary view rendered frame number.
  130. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  131. /// Return whether should render occlusion.
  132. bool GetOcclusion() const { return occlusion_; }
  133. /// Return whether should render specular.
  134. bool GetSpecular() const { return specular_; }
  135. /// Return name for texture unit.
  136. static String GetTextureUnitName(TextureUnit unit);
  137. /// Parse a shader parameter value from a string. Retunrs either a bool, a float, or a 2 to 4-component vector.
  138. static Variant ParseShaderParameterValue(const String& value);
  139. private:
  140. /// Re-evaluate occlusion rendering.
  141. void CheckOcclusion();
  142. /// Reset to defaults.
  143. void ResetToDefaults();
  144. /// Recalculate the memory used by the material.
  145. void RefreshMemoryUse();
  146. /// Techniques.
  147. Vector<TechniqueEntry> techniques_;
  148. /// Textures.
  149. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  150. /// %Shader parameters.
  151. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  152. /// Normal culling mode.
  153. CullMode cullMode_;
  154. /// Culling mode for shadow rendering.
  155. CullMode shadowCullMode_;
  156. /// Depth bias parameters.
  157. BiasParameters depthBias_;
  158. /// Last auxiliary view rendered frame number.
  159. unsigned auxViewFrameNumber_;
  160. /// Render occlusion flag.
  161. bool occlusion_;
  162. /// Specular lighting flag.
  163. bool specular_;
  164. };
  165. }