Material.h 5.6 KB

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