Material.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2013 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. Vector4 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 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);
  73. /// Set number of techniques.
  74. void SetNumTechniques(unsigned num);
  75. /// Set technique.
  76. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  77. /// Set shader parameter.
  78. void SetShaderParameter(const String& name, const Vector4& value);
  79. /// Set texture.
  80. void SetTexture(TextureUnit unit, Texture* texture);
  81. /// Set texture coordinate transform.
  82. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  83. /// Set texture coordinate transform.
  84. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  85. /// Set culling mode.
  86. void SetCullMode(CullMode mode);
  87. /// Set culling mode for shadows.
  88. void SetShadowCullMode(CullMode mode);
  89. /// Set depth bias.
  90. void SetDepthBias(const BiasParameters& parameters);
  91. /// Remove shader parameter.
  92. void RemoveShaderParameter(const String& name);
  93. /// Reset all shader pointers.
  94. void ReleaseShaders();
  95. /// Clone material.
  96. SharedPtr<Material> Clone(const String& cloneName = String::EMPTY) const;
  97. /// Mark material for auxiliary view rendering.
  98. void MarkForAuxView(unsigned frameNumber);
  99. /// Return number of techniques.
  100. unsigned GetNumTechniques() const { return techniques_.Size(); }
  101. /// Return all techniques.
  102. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  103. /// Return technique entry by index.
  104. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  105. /// Return technique by index.
  106. Technique* GetTechnique(unsigned index) const;
  107. /// Return pass by technique index and pass type.
  108. Pass* GetPass(unsigned index, StringHash passType) const;
  109. /// Return texture by unit.
  110. Texture* GetTexture(TextureUnit unit) const;
  111. /// Return all textures.
  112. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  113. /// Return shader parameter.
  114. const Vector4& GetShaderParameter(const String& name) const;
  115. /// Return all shader parameters.
  116. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  117. /// Return normal culling mode.
  118. CullMode GetCullMode() const { return cullMode_; }
  119. /// Return culling mode for shadows.
  120. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  121. /// Return depth bias.
  122. const BiasParameters& GetDepthBias() const { return depthBias_; }
  123. /// Return last auxiliary view rendered frame number.
  124. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  125. /// Return whether should render occlusion.
  126. bool GetOcclusion() const { return occlusion_; }
  127. /// Return whether should render specular.
  128. bool GetSpecular() const { return specular_; }
  129. /// Return name for texture unit.
  130. static const String& GetTextureUnitName(TextureUnit unit);
  131. private:
  132. /// Re-evaluate occlusion rendering.
  133. void CheckOcclusion();
  134. /// Techniques.
  135. Vector<TechniqueEntry> techniques_;
  136. /// Textures.
  137. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  138. /// %Shader parameters.
  139. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  140. /// Normal culling mode.
  141. CullMode cullMode_;
  142. /// Culling mode for shadow rendering.
  143. CullMode shadowCullMode_;
  144. /// Depth bias parameters.
  145. BiasParameters depthBias_;
  146. /// Last auxiliary view rendered frame number.
  147. unsigned auxViewFrameNumber_;
  148. /// Render occlusion flag.
  149. bool occlusion_;
  150. /// Specular lighting flag.
  151. bool specular_;
  152. };
  153. }