Zone.cpp 11 KB

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