Zone.cpp 11 KB

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