Zone.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Drawable.h"
  24. #include "../Graphics/Texture.h"
  25. #include "../Math/Color.h"
  26. namespace Urho3D
  27. {
  28. /// %Component that describes global rendering properties.
  29. class URHO3D_API Zone : public Drawable
  30. {
  31. URHO3D_OBJECT(Zone, Drawable);
  32. public:
  33. /// Construct.
  34. explicit Zone(Context* context);
  35. /// Destruct.
  36. ~Zone() override;
  37. /// Register object factory. Drawable must be registered first.
  38. static void RegisterObject(Context* context);
  39. /// Visualize the component as debug geometry.
  40. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  41. /// Set local-space bounding box. Will be used as an oriented bounding box to test whether objects or the camera are inside.
  42. /// @property
  43. void SetBoundingBox(const BoundingBox& box);
  44. /// Set ambient color.
  45. /// @property
  46. void SetAmbientColor(const Color& color);
  47. /// Set fog color.
  48. /// @property
  49. void SetFogColor(const Color& color);
  50. /// Set fog start distance.
  51. /// @property
  52. void SetFogStart(float start);
  53. /// Set fog end distance.
  54. /// @property
  55. void SetFogEnd(float end);
  56. /// Set fog height distance relative to the scene node's world position. Effective only in height fog mode.
  57. /// @property
  58. void SetFogHeight(float height);
  59. /// Set fog height scale. Effective only in height fog mode.
  60. /// @property
  61. void SetFogHeightScale(float scale);
  62. /// Set zone priority. If an object or camera is inside several zones, the one with highest priority is used.
  63. /// @property
  64. void SetPriority(int priority);
  65. /// Set height fog mode.
  66. /// @property
  67. void SetHeightFog(bool enable);
  68. /// Set override mode. If camera is inside an override zone, that zone will be used for all rendered objects instead of their own zone.
  69. /// @property
  70. void SetOverride(bool enable);
  71. /// Set ambient gradient mode. In gradient mode ambient color is interpolated from neighbor zones.
  72. /// @property
  73. void SetAmbientGradient(bool enable);
  74. /// Set zone texture. This will be bound to the zone texture unit when rendering objects inside the zone. Note that the default shaders do not use it.
  75. /// @property
  76. void SetZoneTexture(Texture* texture);
  77. /// Return inverse world transform.
  78. /// @property
  79. const Matrix3x4& GetInverseWorldTransform() const;
  80. /// Return zone's own ambient color, disregarding gradient mode.
  81. /// @property
  82. const Color& GetAmbientColor() const { return ambientColor_; }
  83. /// Return ambient start color. Not safe to call from worker threads due to possible octree query.
  84. /// @property
  85. const Color& GetAmbientStartColor();
  86. /// Return ambient end color. Not safe to call from worker threads due to possible octree query.
  87. /// @property
  88. const Color& GetAmbientEndColor();
  89. /// Return fog color.
  90. /// @property
  91. const Color& GetFogColor() const { return fogColor_; }
  92. /// Return fog start distance.
  93. /// @property
  94. float GetFogStart() const { return fogStart_; }
  95. /// Return fog end distance.
  96. /// @property
  97. float GetFogEnd() const { return fogEnd_; }
  98. /// Return fog height distance relative to the scene node's world position.
  99. /// @property
  100. float GetFogHeight() const { return fogHeight_; }
  101. /// Return fog height scale.
  102. /// @property
  103. float GetFogHeightScale() const { return fogHeightScale_; }
  104. /// Return zone priority.
  105. /// @property
  106. int GetPriority() const { return priority_; }
  107. /// Return whether height fog mode is enabled.
  108. /// @property
  109. bool GetHeightFog() const { return heightFog_; }
  110. /// Return whether override mode is enabled.
  111. /// @property
  112. bool GetOverride() const { return override_; }
  113. /// Return whether ambient gradient mode is enabled.
  114. /// @property
  115. bool GetAmbientGradient() const { return ambientGradient_; }
  116. /// Return zone texture.
  117. /// @property
  118. Texture* GetZoneTexture() const { return zoneTexture_; }
  119. /// Check whether a point is inside.
  120. bool IsInside(const Vector3& point) const;
  121. /// Set zone texture attribute.
  122. void SetZoneTextureAttr(const ResourceRef& value);
  123. /// Return zone texture attribute.
  124. ResourceRef GetZoneTextureAttr() const;
  125. protected:
  126. /// Handle node transform being dirtied.
  127. void OnMarkedDirty(Node* node) override;
  128. /// Recalculate the world-space bounding box.
  129. void OnWorldBoundingBoxUpdate() override;
  130. /// Handle removal from octree.
  131. void OnRemoveFromOctree() override;
  132. /// Recalculate the ambient gradient colors from neighbor zones. Not safe to call from worker threads due to octree query.
  133. void UpdateAmbientGradient();
  134. /// Clear zone reference from drawables inside the bounding box.
  135. void ClearDrawablesZone();
  136. /// Mark node transform dirty.
  137. void MarkNodeDirty() { OnMarkedDirty(node_); }
  138. /// Cached inverse world transform matrix.
  139. mutable Matrix3x4 inverseWorld_;
  140. /// Inverse transform dirty flag.
  141. mutable bool inverseWorldDirty_;
  142. /// Height fog mode flag.
  143. bool heightFog_;
  144. /// Override mode flag.
  145. bool override_;
  146. /// Ambient gradient mode flag.
  147. bool ambientGradient_;
  148. /// Last world-space bounding box.
  149. BoundingBox lastWorldBoundingBox_;
  150. /// Ambient color.
  151. Color ambientColor_;
  152. /// Cached ambient start color.
  153. Color ambientStartColor_;
  154. /// Cached ambient end color.
  155. Color ambientEndColor_;
  156. /// Fog color.
  157. Color fogColor_;
  158. /// Fog start distance.
  159. float fogStart_;
  160. /// Fog end distance.
  161. float fogEnd_;
  162. /// Fog height distance.
  163. float fogHeight_;
  164. /// Fog height cale.
  165. float fogHeightScale_;
  166. /// Zone priority.
  167. int priority_;
  168. /// Zone texture.
  169. SharedPtr<Texture> zoneTexture_;
  170. /// Last zone used for ambient gradient start color.
  171. WeakPtr<Zone> lastAmbientStartZone_;
  172. /// Last zone used for ambient gradient end color.
  173. WeakPtr<Zone> lastAmbientEndZone_;
  174. };
  175. }