Zone.cpp 11 KB

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