Zone.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. heightFog_(false),
  45. override_(false),
  46. ambientGradient_(false),
  47. ambientColor_(DEFAULT_AMBIENT_COLOR),
  48. fogColor_(DEFAULT_FOG_COLOR),
  49. fogStart_(DEFAULT_FOG_START),
  50. fogEnd_(DEFAULT_FOG_END),
  51. fogHeight_(DEFAULT_FOG_HEIGHT),
  52. fogHeightScale_(DEFAULT_FOG_HEIGHT_SCALE),
  53. priority_(0)
  54. {
  55. boundingBox_ = BoundingBox(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX);
  56. }
  57. Zone::~Zone()
  58. {
  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, "Height Fog Mode", heightFog_, false, AM_DEFAULT);
  73. ATTRIBUTE(Zone, VAR_BOOL, "Override Mode", override_, false, AM_DEFAULT);
  74. ATTRIBUTE(Zone, VAR_BOOL, "Ambient Gradient", ambientGradient_, false, AM_DEFAULT);
  75. ATTRIBUTE(Zone, VAR_INT, "Priority", priority_, 0, AM_DEFAULT);
  76. ATTRIBUTE(Zone, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  77. ATTRIBUTE(Zone, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  78. ACCESSOR_ATTRIBUTE(Zone, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  79. }
  80. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  81. {
  82. Component::OnSetAttribute(attr, src);
  83. // If bounding box or priority changes, dirty the drawable as applicable
  84. if ((attr.offset_ >= offsetof(Zone, boundingBox_) && attr.offset_ < (offsetof(Zone, boundingBox_) + sizeof(BoundingBox))) ||
  85. attr.offset_ == offsetof(Zone, priority_))
  86. OnMarkedDirty(node_);
  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::SetFogHeight(float height)
  124. {
  125. fogHeight_ = height;
  126. MarkNetworkUpdate();
  127. }
  128. void Zone::SetFogHeightScale(float scale)
  129. {
  130. fogHeightScale_ = scale;
  131. MarkNetworkUpdate();
  132. }
  133. void Zone::SetPriority(int priority)
  134. {
  135. priority_ = priority;
  136. MarkNetworkUpdate();
  137. }
  138. void Zone::SetHeightFog(bool enable)
  139. {
  140. heightFog_ = enable;
  141. MarkNetworkUpdate();
  142. }
  143. void Zone::SetOverride(bool enable)
  144. {
  145. override_ = enable;
  146. MarkNetworkUpdate();
  147. }
  148. void Zone::SetAmbientGradient(bool enable)
  149. {
  150. ambientGradient_ = enable;
  151. MarkNetworkUpdate();
  152. }
  153. const Matrix3x4& Zone::GetInverseWorldTransform() const
  154. {
  155. if (inverseWorldDirty_)
  156. {
  157. inverseWorld_ = node_ ? node_->GetWorldTransform().Inverse() : Matrix3x4::IDENTITY;
  158. inverseWorldDirty_ = false;
  159. }
  160. return inverseWorld_;
  161. }
  162. const Color& Zone::GetAmbientStartColor()
  163. {
  164. if (!ambientGradient_)
  165. return ambientColor_;
  166. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  167. UpdateAmbientGradient();
  168. return ambientStartColor_;
  169. }
  170. const Color& Zone::GetAmbientEndColor()
  171. {
  172. if (!ambientGradient_)
  173. return ambientColor_;
  174. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  175. UpdateAmbientGradient();
  176. return ambientEndColor_;
  177. }
  178. bool Zone::IsInside(const Vector3& point) const
  179. {
  180. // Use an oriented bounding box test
  181. Vector3 localPoint(GetInverseWorldTransform() * point);
  182. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  183. }
  184. void Zone::OnMarkedDirty(Node* node)
  185. {
  186. // Due to the octree query and weak pointer manipulation, is not safe from worker threads
  187. Scene* scene = GetScene();
  188. if (scene && scene->IsThreadedUpdate())
  189. {
  190. scene->DelayedMarkedDirty(this);
  191. return;
  192. }
  193. Drawable::OnMarkedDirty(node);
  194. // Clear zone reference from all drawables inside the bounding box, and mark gradient dirty in neighbor zones
  195. ClearDrawablesZone();
  196. inverseWorldDirty_ = true;
  197. }
  198. void Zone::OnWorldBoundingBoxUpdate()
  199. {
  200. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  201. }
  202. void Zone::UpdateAmbientGradient()
  203. {
  204. // In case no neighbor zones are found, reset ambient start/end with own ambient color
  205. ambientStartColor_ = ambientColor_;
  206. ambientEndColor_ = ambientColor_;
  207. lastAmbientStartZone_ = this;
  208. lastAmbientEndZone_ = this;
  209. if (octant_)
  210. {
  211. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  212. Vector3 center = boundingBox_.Center();
  213. Vector3 minZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.min_.z_);
  214. Vector3 maxZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.max_.z_);
  215. PODVector<Zone*> result;
  216. {
  217. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), minZPosition, DRAWABLE_ZONE);
  218. octant_->GetRoot()->GetDrawables(query);
  219. }
  220. // Gradient start position: get the highest priority zone that is not this zone
  221. int bestPriority = M_MIN_INT;
  222. Zone* bestZone = 0;
  223. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  224. {
  225. Zone* zone = *i;
  226. int priority = zone->GetPriority();
  227. if (priority > bestPriority && zone != this && zone->IsInside(minZPosition))
  228. {
  229. bestZone = zone;
  230. bestPriority = priority;
  231. }
  232. }
  233. if (bestZone)
  234. {
  235. ambientStartColor_ = bestZone->GetAmbientColor();
  236. lastAmbientStartZone_ = bestZone;
  237. }
  238. // Do the same for gradient end position
  239. {
  240. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), maxZPosition, DRAWABLE_ZONE);
  241. octant_->GetRoot()->GetDrawables(query);
  242. }
  243. bestPriority = M_MIN_INT;
  244. bestZone = 0;
  245. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  246. {
  247. Zone* zone = *i;
  248. int priority = zone->GetPriority();
  249. if (priority > bestPriority && zone != this && zone->IsInside(maxZPosition))
  250. {
  251. bestZone = zone;
  252. bestPriority = priority;
  253. }
  254. }
  255. if (bestZone)
  256. {
  257. ambientEndColor_ = bestZone->GetAmbientColor();
  258. lastAmbientEndZone_ = bestZone;
  259. }
  260. }
  261. }
  262. void Zone::OnRemoveFromOctree()
  263. {
  264. ClearDrawablesZone();
  265. }
  266. void Zone::ClearDrawablesZone()
  267. {
  268. if (octant_ && lastWorldBoundingBox_.defined_)
  269. {
  270. PODVector<Drawable*> result;
  271. BoxOctreeQuery query(result, lastWorldBoundingBox_, DRAWABLE_GEOMETRY | DRAWABLE_ZONE);
  272. octant_->GetRoot()->GetDrawables(query);
  273. for (PODVector<Drawable*>::Iterator i = result.Begin(); i != result.End(); ++i)
  274. {
  275. Drawable* drawable = *i;
  276. unsigned drawableFlags = drawable->GetDrawableFlags();
  277. if (drawableFlags & DRAWABLE_GEOMETRY)
  278. drawable->SetZone(0);
  279. else if (drawableFlags & DRAWABLE_ZONE)
  280. {
  281. Zone* zone = static_cast<Zone*>(drawable);
  282. zone->lastAmbientStartZone_.Reset();
  283. zone->lastAmbientEndZone_.Reset();
  284. }
  285. }
  286. }
  287. lastWorldBoundingBox_ = GetWorldBoundingBox();
  288. lastAmbientStartZone_.Reset();
  289. lastAmbientEndZone_.Reset();
  290. }
  291. }