Zone.cpp 11 KB

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