Zone.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Node.h"
  27. #include "Octree.h"
  28. #include "Scene.h"
  29. #include "XMLElement.h"
  30. #include "Zone.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const Vector3 DEFAULT_BOUNDING_BOX_MIN(-10.0f, -10.0f, -10.0f);
  35. static const Vector3 DEFAULT_BOUNDING_BOX_MAX(10.0f, 10.0f, 10.0f);
  36. static const Color DEFAULT_AMBIENT_COLOR(0.1f, 0.1f, 0.1f);
  37. static const Color DEFAULT_FOG_COLOR(0.0f, 0.0f, 0.0f);
  38. static const float DEFAULT_FOG_START = 250.0f;
  39. static const float DEFAULT_FOG_END = 1000.0f;
  40. OBJECTTYPESTATIC(Zone);
  41. Zone::Zone(Context* context) :
  42. Drawable(context),
  43. inverseWorldDirty_(true),
  44. override_(false),
  45. ambientGradient_(false),
  46. boundingBox_(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX),
  47. ambientColor_(DEFAULT_AMBIENT_COLOR),
  48. fogColor_(DEFAULT_FOG_COLOR),
  49. fogStart_(DEFAULT_FOG_START),
  50. fogEnd_(DEFAULT_FOG_END),
  51. priority_(0)
  52. {
  53. drawableFlags_ = DRAWABLE_ZONE;
  54. }
  55. Zone::~Zone()
  56. {
  57. }
  58. void Zone::RegisterObject(Context* context)
  59. {
  60. context->RegisterFactory<Zone>();
  61. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Min", boundingBox_.min_, DEFAULT_BOUNDING_BOX_MIN, AM_DEFAULT);
  62. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Max", boundingBox_.max_, DEFAULT_BOUNDING_BOX_MAX, AM_DEFAULT);
  63. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color", ambientColor_, DEFAULT_AMBIENT_COLOR, AM_DEFAULT);
  64. ATTRIBUTE(Zone, VAR_COLOR, "Fog Color", fogColor_, DEFAULT_FOG_COLOR, AM_DEFAULT);
  65. ATTRIBUTE(Zone, VAR_FLOAT, "Fog Start", fogStart_, DEFAULT_FOG_START, AM_DEFAULT);
  66. ATTRIBUTE(Zone, VAR_FLOAT, "Fog End", fogEnd_, DEFAULT_FOG_END, AM_DEFAULT);
  67. ATTRIBUTE(Zone, VAR_BOOL, "Is Visible", visible_, true, AM_DEFAULT);
  68. ATTRIBUTE(Zone, VAR_BOOL, "Override Mode", override_, 0, AM_DEFAULT);
  69. ATTRIBUTE(Zone, VAR_BOOL, "Ambient Gradient", ambientGradient_, 0, AM_DEFAULT);
  70. ATTRIBUTE(Zone, VAR_INT, "Priority", priority_, 0, AM_DEFAULT);
  71. ATTRIBUTE(Zone, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  72. ATTRIBUTE(Zone, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  73. ACCESSOR_ATTRIBUTE(Zone, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  74. }
  75. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  76. {
  77. Component::OnSetAttribute(attr, src);
  78. // If bounding box, override mode, visibility or priority changes, dirty the drawable as applicable
  79. if ((attr.offset_ >= offsetof(Zone, boundingBox_) && attr.offset_ < (offsetof(Zone, boundingBox_) + sizeof(BoundingBox))) ||
  80. attr.offset_ == offsetof(Zone, visible_) || attr.offset_ == offsetof(Zone, priority_))
  81. OnMarkedDirty(node_);
  82. }
  83. void Zone::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  84. {
  85. if (debug)
  86. debug->AddBoundingBox(boundingBox_, node_->GetWorldTransform(), Color::GREEN, depthTest);
  87. }
  88. void Zone::SetBoundingBox(const BoundingBox& box)
  89. {
  90. boundingBox_ = box;
  91. OnMarkedDirty(node_);
  92. MarkNetworkUpdate();
  93. }
  94. void Zone::SetAmbientColor(const Color& color)
  95. {
  96. ambientColor_ = Color(color, 1.0f);
  97. MarkNetworkUpdate();
  98. }
  99. void Zone::SetFogColor(const Color& color)
  100. {
  101. fogColor_ = Color(color, 1.0f);
  102. MarkNetworkUpdate();
  103. }
  104. void Zone::SetFogStart(float start)
  105. {
  106. if (start < 0.0f)
  107. start = 0.0f;
  108. fogStart_ = start;
  109. MarkNetworkUpdate();
  110. }
  111. void Zone::SetFogEnd(float end)
  112. {
  113. if (end < 0.0f)
  114. end = 0.0f;
  115. fogEnd_ = end;
  116. MarkNetworkUpdate();
  117. }
  118. void Zone::SetPriority(int priority)
  119. {
  120. priority_ = priority;
  121. MarkNetworkUpdate();
  122. }
  123. void Zone::SetOverride(bool enable)
  124. {
  125. override_ = enable;
  126. MarkNetworkUpdate();
  127. }
  128. void Zone::SetAmbientGradient(bool enable)
  129. {
  130. ambientGradient_ = enable;
  131. MarkNetworkUpdate();
  132. }
  133. const Matrix3x4& Zone::GetInverseWorldTransform() const
  134. {
  135. if (inverseWorldDirty_ && node_)
  136. {
  137. const Matrix3x4& worldTransform = node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  138. inverseWorld_ = worldTransform.Inverse();
  139. inverseWorldDirty_ = false;
  140. }
  141. return inverseWorld_;
  142. }
  143. const Color& Zone::GetAmbientStartColor()
  144. {
  145. if (!ambientGradient_)
  146. return ambientColor_;
  147. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  148. UpdateAmbientGradient();
  149. return ambientStartColor_;
  150. }
  151. const Color& Zone::GetAmbientEndColor()
  152. {
  153. if (!ambientGradient_)
  154. return ambientColor_;
  155. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  156. UpdateAmbientGradient();
  157. return ambientEndColor_;
  158. }
  159. bool Zone::IsInside(const Vector3& point) const
  160. {
  161. // Use an oriented bounding box test
  162. Vector3 localPoint(GetInverseWorldTransform() * point);
  163. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  164. }
  165. void Zone::OnMarkedDirty(Node* node)
  166. {
  167. // Due to the octree query, is not safe from worker threads
  168. Scene* scene = GetScene();
  169. if (scene && scene->IsThreadedUpdate())
  170. {
  171. scene->DelayedMarkedDirty(this);
  172. return;
  173. }
  174. Drawable::OnMarkedDirty(node);
  175. // When marked dirty, clear the cached zone from all drawables inside the zone bounding box,
  176. // and mark gradient dirty in all neighbor zones
  177. if (octant_ && lastWorldBoundingBox_.defined_)
  178. {
  179. PODVector<Drawable*> result;
  180. BoxOctreeQuery query(result, lastWorldBoundingBox_, DRAWABLE_GEOMETRY | DRAWABLE_ZONE);
  181. octant_->GetRoot()->GetDrawables(query);
  182. for (PODVector<Drawable*>::Iterator i = result.Begin(); i != result.End(); ++i)
  183. {
  184. Drawable* drawable = *i;
  185. unsigned drawableFlags = drawable->GetDrawableFlags();
  186. if (drawableFlags & DRAWABLE_GEOMETRY)
  187. (*i)->SetZone(0);
  188. if (drawableFlags & DRAWABLE_ZONE)
  189. {
  190. Zone* zone = static_cast<Zone*>(drawable);
  191. zone->lastAmbientStartZone_.Reset();
  192. zone->lastAmbientEndZone_.Reset();
  193. }
  194. }
  195. }
  196. lastWorldBoundingBox_ = GetWorldBoundingBox();
  197. lastAmbientStartZone_.Reset();
  198. lastAmbientEndZone_.Reset();
  199. inverseWorldDirty_ = true;
  200. }
  201. void Zone::OnWorldBoundingBoxUpdate()
  202. {
  203. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  204. }
  205. void Zone::UpdateAmbientGradient()
  206. {
  207. // In case no neighbor zones are found, reset ambient start/end with own ambient color
  208. ambientStartColor_ = ambientColor_;
  209. ambientEndColor_ = ambientColor_;
  210. lastAmbientStartZone_ = this;
  211. lastAmbientEndZone_ = this;
  212. if (octant_)
  213. {
  214. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  215. Vector3 center = boundingBox_.Center();
  216. Vector3 minZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.min_.z_);
  217. Vector3 maxZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.max_.z_);
  218. PODVector<Zone*> result;
  219. {
  220. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), minZPosition, DRAWABLE_ZONE);
  221. octant_->GetRoot()->GetDrawables(query);
  222. }
  223. // Gradient start position: get the highest priority zone that is not this zone
  224. int bestPriority = M_MIN_INT;
  225. Zone* bestZone = 0;
  226. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  227. {
  228. Zone* zone = *i;
  229. int priority = zone->GetPriority();
  230. if (zone != this && priority > bestPriority && zone->IsInside(minZPosition))
  231. {
  232. bestZone = zone;
  233. bestPriority = priority;
  234. }
  235. }
  236. if (bestZone)
  237. {
  238. ambientStartColor_ = bestZone->GetAmbientColor();
  239. lastAmbientStartZone_ = bestZone;
  240. }
  241. // Do the same for gradient end position
  242. {
  243. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), maxZPosition, DRAWABLE_ZONE);
  244. octant_->GetRoot()->GetDrawables(query);
  245. }
  246. bestPriority = M_MIN_INT;
  247. bestZone = 0;
  248. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  249. {
  250. Zone* zone = *i;
  251. int priority = zone->GetPriority();
  252. if (zone != this && priority > bestPriority && zone->IsInside(maxZPosition))
  253. {
  254. bestZone = zone;
  255. bestPriority = priority;
  256. }
  257. }
  258. if (bestZone)
  259. {
  260. ambientEndColor_ = bestZone->GetAmbientColor();
  261. lastAmbientEndZone_ = bestZone;
  262. }
  263. }
  264. }
  265. }