Material.h 5.8 KB

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