Zone.cpp 11 KB

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