Zone.h 6.7 KB

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