Zone.cpp 9.5 KB

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