Material.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "HashMap.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* technique, 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* technique, 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. /// Remove a shader parameter
  89. void RemoveShaderParameter(const String& name);
  90. /// Reset all shader pointers
  91. void ReleaseShaders();
  92. /// Clone material
  93. SharedPtr<Material> Clone(const String& cloneName = String()) const;
  94. /// Mark material for auxiliary view rendering
  95. void MarkForAuxView(unsigned frameNumber);
  96. /// Return number of techniques
  97. unsigned GetNumTechniques() const { return techniques_.Size(); }
  98. /// Return all techniques
  99. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  100. /// Return technique entry by index
  101. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  102. /// Return technique by index
  103. Technique* GetTechnique(unsigned index) const;
  104. /// Return pass by technique index and pass type
  105. Pass* GetPass(unsigned index, PassType pass) const;
  106. /// Return all textures
  107. const Vector<SharedPtr<Texture> >& GetTextures() const { return textures_; }
  108. /// Return texture by unit
  109. Texture* GetTexture(TextureUnit unit) const;
  110. /// Return all shader parameters
  111. const HashMap<StringHash, MaterialShaderParameter>& GetShaderParameters() const { return shaderParameters_; }
  112. /// Return normal culling mode
  113. CullMode GetCullMode() const { return cullMode_; }
  114. /// Return culling mode for shadows
  115. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  116. /// Return last auxiliary view rendered frame number
  117. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  118. /// Return whether should render occlusion
  119. bool GetOcclusion() const { return occlusion_; }
  120. /// Return name for texture unit
  121. static const String& GetTextureUnitName(TextureUnit unit);
  122. private:
  123. /// Re-evaluate occlusion rendering
  124. void Update();
  125. /// Techniques
  126. Vector<TechniqueEntry> techniques_;
  127. /// Textures
  128. Vector<SharedPtr<Texture> > textures_;
  129. /// Shader parameters
  130. HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
  131. /// Normal culling mode
  132. CullMode cullMode_;
  133. /// Culling mode for shadow rendering
  134. CullMode shadowCullMode_;
  135. /// Last auxiliary view rendered frame number
  136. unsigned auxViewFrameNumber_;
  137. /// Render occlusion flag
  138. bool occlusion_;
  139. };