Material.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "GraphicsDefs.h"
  25. #include "Light.h"
  26. #include "Resource.h"
  27. #include "Vector4.h"
  28. class Pass;
  29. class Technique;
  30. class Texture;
  31. class Texture2D;
  32. class TextureCube;
  33. /// %Material's shader parameter definition.
  34. struct MaterialShaderParameter
  35. {
  36. /// Name.
  37. String name_;
  38. /// Value.
  39. Vector4 value_;
  40. };
  41. /// %Material's technique list entry.
  42. struct TechniqueEntry
  43. {
  44. /// Construct with defaults.
  45. TechniqueEntry();
  46. /// Construct with parameters.
  47. TechniqueEntry(Technique* tech, unsigned qualityLevel, float lodDistance);
  48. /// Destruct.
  49. ~TechniqueEntry();
  50. /// Technique.
  51. SharedPtr<Technique> technique_;
  52. /// Quality level.
  53. int qualityLevel_;
  54. /// LOD distance.
  55. float lodDistance_;
  56. };
  57. /// Describes how to render 3D geometries.
  58. class Material : public Resource
  59. {
  60. OBJECT(Material);
  61. public:
  62. /// Construct.
  63. Material(Context* context);
  64. /// Destruct.
  65. ~Material();
  66. /// Register object factory.
  67. static void RegisterObject(Context* context);
  68. /// Load resource. Return true if successful.
  69. virtual bool Load(Deserializer& source);
  70. /// Save resource. Return true if successful.
  71. virtual bool Save(Serializer& dest);
  72. /// Set number of techniques.
  73. void SetNumTechniques(unsigned num);
  74. /// Set technique.
  75. void SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  76. /// Set shader parameter.
  77. void SetShaderParameter(const String& name, const Vector4& value);
  78. /// Set texture.
  79. void SetTexture(TextureUnit unit, Texture* texture);
  80. /// Set texture coordinate transform.
  81. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  82. /// Set texture coordinate transform.
  83. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  84. /// Set culling mode.
  85. void SetCullMode(CullMode mode);
  86. /// Set culling mode for shadows.
  87. void SetShadowCullMode(CullMode mode);
  88. /// Set depth bias.
  89. void SetDepthBias(const BiasParameters& parameters);
  90. /// Remove shader parameter.
  91. void RemoveShaderParameter(const String& name);
  92. /// Reset all shader pointers.
  93. void ReleaseShaders();
  94. /// Clone material.
  95. SharedPtr<Material> Clone(const String& cloneName = String()) const;
  96. /// Mark material for auxiliary view rendering.
  97. void MarkForAuxView(unsigned frameNumber);
  98. /// Return number of techniques.
  99. unsigned GetNumTechniques() const { return techniques_.Size(); }
  100. /// Return all techniques.
  101. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  102. /// Return technique entry by index.
  103. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  104. /// Return technique by index.
  105. Technique* GetTechnique(unsigned index) const;
  106. /// Return pass by technique index and pass type.
  107. Pass* GetPass(unsigned index, PassType pass) const;
  108. /// Return texture by unit.
  109. Texture* GetTexture(TextureUnit unit) const;
  110. /// Return all textures.
  111. const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
  112. /// Return shader parameter.
  113. const Vector4& GetShaderParameter(const String& name) const;
  114. /// Return all shader parameters.
  115. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  116. /// Return normal culling mode.
  117. CullMode GetCullMode() const { return cullMode_; }
  118. /// Return culling mode for shadows.
  119. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  120. /// Return depth bias.
  121. const BiasParameters& GetDepthBias() const { return depthBias_; }
  122. /// Return last auxiliary view rendered frame number.
  123. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  124. /// Return whether should render occlusion.
  125. bool GetOcclusion() const { return occlusion_; }
  126. /// Return whether should render specular.
  127. bool GetSpecular() const { return specular_; }
  128. /// Return name for texture unit.
  129. static const String& GetTextureUnitName(TextureUnit unit);
  130. private:
  131. /// Re-evaluate occlusion rendering.
  132. void CheckOcclusion();
  133. /// Re-evaluate specular lighting.
  134. void CheckSpecular();
  135. /// Techniques.
  136. Vector<TechniqueEntry> techniques_;
  137. /// Textures.
  138. SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
  139. /// %Shader parameters.
  140. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  141. /// Normal culling mode.
  142. CullMode cullMode_;
  143. /// Culling mode for shadow rendering.
  144. CullMode shadowCullMode_;
  145. /// Depth bias parameters.
  146. BiasParameters depthBias_;
  147. /// Last auxiliary view rendered frame number.
  148. unsigned auxViewFrameNumber_;
  149. /// Render occlusion flag.
  150. bool occlusion_;
  151. /// Specular lighting flag.
  152. bool specular_;
  153. };