Zone.cpp 11 KB

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