Zone.cpp 9.9 KB

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