Zone.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Copyright (c) 2008-2013 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. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "Node.h"
  26. #include "Octree.h"
  27. #include "Scene.h"
  28. #include "Zone.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const Vector3 DEFAULT_BOUNDING_BOX_MIN(-10.0f, -10.0f, -10.0f);
  33. static const Vector3 DEFAULT_BOUNDING_BOX_MAX(10.0f, 10.0f, 10.0f);
  34. static const Color DEFAULT_AMBIENT_COLOR(0.1f, 0.1f, 0.1f);
  35. static const Color DEFAULT_FOG_COLOR(0.0f, 0.0f, 0.0f);
  36. static const float DEFAULT_FOG_START = 250.0f;
  37. static const float DEFAULT_FOG_END = 1000.0f;
  38. extern const char* SCENE_CATEGORY;
  39. Zone::Zone(Context* context) :
  40. Drawable(context, DRAWABLE_ZONE),
  41. inverseWorldDirty_(true),
  42. override_(false),
  43. ambientGradient_(false),
  44. ambientColor_(DEFAULT_AMBIENT_COLOR),
  45. fogColor_(DEFAULT_FOG_COLOR),
  46. fogStart_(DEFAULT_FOG_START),
  47. fogEnd_(DEFAULT_FOG_END),
  48. priority_(0)
  49. {
  50. boundingBox_ = BoundingBox(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX);
  51. }
  52. Zone::~Zone()
  53. {
  54. /// \todo When zone is destroyed, there is now possibility of dangling zone pointers
  55. }
  56. void Zone::RegisterObject(Context* context)
  57. {
  58. context->RegisterFactory<Zone>(SCENE_CATEGORY);
  59. ACCESSOR_ATTRIBUTE(Zone, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  60. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Min", boundingBox_.min_, DEFAULT_BOUNDING_BOX_MIN, AM_DEFAULT);
  61. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Max", boundingBox_.max_, DEFAULT_BOUNDING_BOX_MAX, AM_DEFAULT);
  62. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color", ambientColor_, DEFAULT_AMBIENT_COLOR, AM_DEFAULT);
  63. ATTRIBUTE(Zone, VAR_COLOR, "Fog Color", fogColor_, DEFAULT_FOG_COLOR, AM_DEFAULT);
  64. ATTRIBUTE(Zone, VAR_FLOAT, "Fog Start", fogStart_, DEFAULT_FOG_START, AM_DEFAULT);
  65. ATTRIBUTE(Zone, VAR_FLOAT, "Fog End", fogEnd_, DEFAULT_FOG_END, AM_DEFAULT);
  66. ATTRIBUTE(Zone, VAR_BOOL, "Override Mode", override_, false, AM_DEFAULT);
  67. ATTRIBUTE(Zone, VAR_BOOL, "Ambient Gradient", ambientGradient_, false, AM_DEFAULT);
  68. ATTRIBUTE(Zone, VAR_INT, "Priority", priority_, 0, AM_DEFAULT);
  69. ATTRIBUTE(Zone, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  70. ATTRIBUTE(Zone, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  71. ACCESSOR_ATTRIBUTE(Zone, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  72. }
  73. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  74. {
  75. Component::OnSetAttribute(attr, src);
  76. // If bounding box or priority changes, dirty the drawable as applicable
  77. if ((attr.offset_ >= offsetof(Zone, boundingBox_) && attr.offset_ < (offsetof(Zone, boundingBox_) + sizeof(BoundingBox))) ||
  78. attr.offset_ == offsetof(Zone, priority_))
  79. OnMarkedDirty(node_);
  80. }
  81. void Zone::OnSetEnabled()
  82. {
  83. // When a Zone is disabled, clear the cached zone from all drawables inside bounding box before removing from octree
  84. if (!IsEnabledEffective())
  85. OnMarkedDirty(node_);
  86. Drawable::OnSetEnabled();
  87. }
  88. void Zone::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  89. {
  90. if (debug && IsEnabledEffective())
  91. debug->AddBoundingBox(boundingBox_, node_->GetWorldTransform(), Color::GREEN, depthTest);
  92. }
  93. void Zone::SetBoundingBox(const BoundingBox& box)
  94. {
  95. boundingBox_ = box;
  96. OnMarkedDirty(node_);
  97. MarkNetworkUpdate();
  98. }
  99. void Zone::SetAmbientColor(const Color& color)
  100. {
  101. ambientColor_ = Color(color, 1.0f);
  102. MarkNetworkUpdate();
  103. }
  104. void Zone::SetFogColor(const Color& color)
  105. {
  106. fogColor_ = Color(color, 1.0f);
  107. MarkNetworkUpdate();
  108. }
  109. void Zone::SetFogStart(float start)
  110. {
  111. if (start < 0.0f)
  112. start = 0.0f;
  113. fogStart_ = start;
  114. MarkNetworkUpdate();
  115. }
  116. void Zone::SetFogEnd(float end)
  117. {
  118. if (end < 0.0f)
  119. end = 0.0f;
  120. fogEnd_ = end;
  121. MarkNetworkUpdate();
  122. }
  123. void Zone::SetPriority(int priority)
  124. {
  125. priority_ = priority;
  126. MarkNetworkUpdate();
  127. }
  128. void Zone::SetOverride(bool enable)
  129. {
  130. override_ = enable;
  131. MarkNetworkUpdate();
  132. }
  133. void Zone::SetAmbientGradient(bool enable)
  134. {
  135. ambientGradient_ = enable;
  136. MarkNetworkUpdate();
  137. }
  138. const Matrix3x4& Zone::GetInverseWorldTransform() const
  139. {
  140. if (inverseWorldDirty_)
  141. {
  142. inverseWorld_ = node_ ? node_->GetWorldTransform().Inverse() : Matrix3x4::IDENTITY;
  143. inverseWorldDirty_ = false;
  144. }
  145. return inverseWorld_;
  146. }
  147. const Color& Zone::GetAmbientStartColor()
  148. {
  149. if (!ambientGradient_)
  150. return ambientColor_;
  151. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  152. UpdateAmbientGradient();
  153. return ambientStartColor_;
  154. }
  155. const Color& Zone::GetAmbientEndColor()
  156. {
  157. if (!ambientGradient_)
  158. return ambientColor_;
  159. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  160. UpdateAmbientGradient();
  161. return ambientEndColor_;
  162. }
  163. bool Zone::IsInside(const Vector3& point) const
  164. {
  165. // Use an oriented bounding box test
  166. Vector3 localPoint(GetInverseWorldTransform() * point);
  167. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  168. }
  169. void Zone::OnMarkedDirty(Node* node)
  170. {
  171. // Due to the octree query and weak pointer manipulation, is not safe from worker threads
  172. Scene* scene = GetScene();
  173. if (scene && scene->IsThreadedUpdate())
  174. {
  175. scene->DelayedMarkedDirty(this);
  176. return;
  177. }
  178. Drawable::OnMarkedDirty(node);
  179. // When marked dirty, clear the cached zone from all drawables inside the zone bounding box,
  180. // and mark gradient dirty in all neighbor zones
  181. if (octant_ && lastWorldBoundingBox_.defined_)
  182. {
  183. PODVector<Drawable*> result;
  184. BoxOctreeQuery query(result, lastWorldBoundingBox_, DRAWABLE_GEOMETRY | DRAWABLE_ZONE);
  185. octant_->GetRoot()->GetDrawables(query);
  186. for (PODVector<Drawable*>::Iterator i = result.Begin(); i != result.End(); ++i)
  187. {
  188. Drawable* drawable = *i;
  189. unsigned drawableFlags = drawable->GetDrawableFlags();
  190. if (drawableFlags & DRAWABLE_GEOMETRY)
  191. {
  192. if (drawable->GetZone() == this)
  193. drawable->SetZone(0);
  194. }
  195. else if (drawableFlags & DRAWABLE_ZONE)
  196. {
  197. Zone* zone = static_cast<Zone*>(drawable);
  198. zone->lastAmbientStartZone_.Reset();
  199. zone->lastAmbientEndZone_.Reset();
  200. }
  201. }
  202. }
  203. lastWorldBoundingBox_ = GetWorldBoundingBox();
  204. lastAmbientStartZone_.Reset();
  205. lastAmbientEndZone_.Reset();
  206. inverseWorldDirty_ = true;
  207. }
  208. void Zone::OnWorldBoundingBoxUpdate()
  209. {
  210. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  211. }
  212. void Zone::UpdateAmbientGradient()
  213. {
  214. // In case no neighbor zones are found, reset ambient start/end with own ambient color
  215. ambientStartColor_ = ambientColor_;
  216. ambientEndColor_ = ambientColor_;
  217. lastAmbientStartZone_ = this;
  218. lastAmbientEndZone_ = this;
  219. if (octant_)
  220. {
  221. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  222. Vector3 center = boundingBox_.Center();
  223. Vector3 minZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.min_.z_);
  224. Vector3 maxZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.max_.z_);
  225. PODVector<Zone*> result;
  226. {
  227. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), minZPosition, DRAWABLE_ZONE);
  228. octant_->GetRoot()->GetDrawables(query);
  229. }
  230. // Gradient start position: get the highest priority zone that is not this zone
  231. int bestPriority = M_MIN_INT;
  232. Zone* bestZone = 0;
  233. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  234. {
  235. Zone* zone = *i;
  236. int priority = zone->GetPriority();
  237. if (priority > bestPriority && zone != this && zone->IsInside(minZPosition))
  238. {
  239. bestZone = zone;
  240. bestPriority = priority;
  241. }
  242. }
  243. if (bestZone)
  244. {
  245. ambientStartColor_ = bestZone->GetAmbientColor();
  246. lastAmbientStartZone_ = bestZone;
  247. }
  248. // Do the same for gradient end position
  249. {
  250. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), maxZPosition, DRAWABLE_ZONE);
  251. octant_->GetRoot()->GetDrawables(query);
  252. }
  253. bestPriority = M_MIN_INT;
  254. bestZone = 0;
  255. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  256. {
  257. Zone* zone = *i;
  258. int priority = zone->GetPriority();
  259. if (priority > bestPriority && zone != this && zone->IsInside(maxZPosition))
  260. {
  261. bestZone = zone;
  262. bestPriority = priority;
  263. }
  264. }
  265. if (bestZone)
  266. {
  267. ambientEndColor_ = bestZone->GetAmbientColor();
  268. lastAmbientEndZone_ = bestZone;
  269. }
  270. }
  271. }
  272. }