Octree.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Core/CoreEvents.h"
  25. #include "../Core/Profiler.h"
  26. #include "../Core/WorkQueue.h"
  27. #include "../Graphics/DebugRenderer.h"
  28. #include "../Graphics/Graphics.h"
  29. #include "../Graphics/Octree.h"
  30. #include "../IO/Log.h"
  31. #include "../Scene/Scene.h"
  32. #include "../Scene/SceneEvents.h"
  33. #include "../DebugNew.h"
  34. #ifdef _MSC_VER
  35. #pragma warning(disable:4355)
  36. #endif
  37. namespace Atomic
  38. {
  39. static const float DEFAULT_OCTREE_SIZE = 1000.0f;
  40. static const int DEFAULT_OCTREE_LEVELS = 8;
  41. static const int RAYCASTS_PER_WORK_ITEM = 4;
  42. extern const char* SUBSYSTEM_CATEGORY;
  43. void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex)
  44. {
  45. Octree* octree = reinterpret_cast<Octree*>(item->aux_);
  46. Drawable** start = reinterpret_cast<Drawable**>(item->start_);
  47. Drawable** end = reinterpret_cast<Drawable**>(item->end_);
  48. const RayOctreeQuery& query = *octree->rayQuery_;
  49. PODVector<RayQueryResult>& results = octree->rayQueryResults_[threadIndex];
  50. while (start != end)
  51. {
  52. Drawable* drawable = *start;
  53. drawable->ProcessRayQuery(query, results);
  54. ++start;
  55. }
  56. }
  57. void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex)
  58. {
  59. const FrameInfo& frame = *(reinterpret_cast<FrameInfo*>(item->aux_));
  60. Drawable** start = reinterpret_cast<Drawable**>(item->start_);
  61. Drawable** end = reinterpret_cast<Drawable**>(item->end_);
  62. while (start != end)
  63. {
  64. Drawable* drawable = *start;
  65. if (drawable)
  66. drawable->Update(frame);
  67. ++start;
  68. }
  69. }
  70. inline bool CompareRayQueryResults(const RayQueryResult& lhs, const RayQueryResult& rhs)
  71. {
  72. return lhs.distance_ < rhs.distance_;
  73. }
  74. Octant::Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root, unsigned index) :
  75. level_(level),
  76. numDrawables_(0),
  77. parent_(parent),
  78. root_(root),
  79. index_(index)
  80. {
  81. Initialize(box);
  82. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  83. children_[i] = 0;
  84. }
  85. Octant::~Octant()
  86. {
  87. if (root_)
  88. {
  89. // Remove the drawables (if any) from this octant to the root octant
  90. for (PODVector<Drawable*>::Iterator i = drawables_.Begin(); i != drawables_.End(); ++i)
  91. {
  92. (*i)->SetOctant(root_);
  93. root_->drawables_.Push(*i);
  94. root_->QueueUpdate(*i);
  95. }
  96. drawables_.Clear();
  97. numDrawables_ = 0;
  98. }
  99. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  100. DeleteChild(i);
  101. }
  102. Octant* Octant::GetOrCreateChild(unsigned index)
  103. {
  104. if (children_[index])
  105. return children_[index];
  106. Vector3 newMin = worldBoundingBox_.min_;
  107. Vector3 newMax = worldBoundingBox_.max_;
  108. Vector3 oldCenter = worldBoundingBox_.Center();
  109. if (index & 1)
  110. newMin.x_ = oldCenter.x_;
  111. else
  112. newMax.x_ = oldCenter.x_;
  113. if (index & 2)
  114. newMin.y_ = oldCenter.y_;
  115. else
  116. newMax.y_ = oldCenter.y_;
  117. if (index & 4)
  118. newMin.z_ = oldCenter.z_;
  119. else
  120. newMax.z_ = oldCenter.z_;
  121. children_[index] = new Octant(BoundingBox(newMin, newMax), level_ + 1, this, root_, index);
  122. return children_[index];
  123. }
  124. void Octant::DeleteChild(unsigned index)
  125. {
  126. assert(index < NUM_OCTANTS);
  127. delete children_[index];
  128. children_[index] = 0;
  129. }
  130. void Octant::InsertDrawable(Drawable* drawable)
  131. {
  132. const BoundingBox& box = drawable->GetWorldBoundingBox();
  133. // If root octant, insert all non-occludees here, so that octant occlusion does not hide the drawable.
  134. // Also if drawable is outside the root octant bounds, insert to root
  135. bool insertHere;
  136. if (this == root_)
  137. insertHere = !drawable->IsOccludee() || cullingBox_.IsInside(box) != INSIDE || CheckDrawableFit(box);
  138. else
  139. insertHere = CheckDrawableFit(box);
  140. if (insertHere)
  141. {
  142. Octant* oldOctant = drawable->octant_;
  143. if (oldOctant != this)
  144. {
  145. // Add first, then remove, because drawable count going to zero deletes the octree branch in question
  146. AddDrawable(drawable);
  147. if (oldOctant)
  148. oldOctant->RemoveDrawable(drawable, false);
  149. }
  150. }
  151. else
  152. {
  153. Vector3 boxCenter = box.Center();
  154. unsigned x = boxCenter.x_ < center_.x_ ? 0 : 1;
  155. unsigned y = boxCenter.y_ < center_.y_ ? 0 : 2;
  156. unsigned z = boxCenter.z_ < center_.z_ ? 0 : 4;
  157. GetOrCreateChild(x + y + z)->InsertDrawable(drawable);
  158. }
  159. }
  160. bool Octant::CheckDrawableFit(const BoundingBox& box) const
  161. {
  162. Vector3 boxSize = box.Size();
  163. // If max split level, size always OK, otherwise check that box is at least half size of octant
  164. if (level_ >= root_->GetNumLevels() || boxSize.x_ >= halfSize_.x_ || boxSize.y_ >= halfSize_.y_ ||
  165. boxSize.z_ >= halfSize_.z_)
  166. return true;
  167. // Also check if the box can not fit a child octant's culling box, in that case size OK (must insert here)
  168. else
  169. {
  170. if (box.min_.x_ <= worldBoundingBox_.min_.x_ - 0.5f * halfSize_.x_ ||
  171. box.max_.x_ >= worldBoundingBox_.max_.x_ + 0.5f * halfSize_.x_ ||
  172. box.min_.y_ <= worldBoundingBox_.min_.y_ - 0.5f * halfSize_.y_ ||
  173. box.max_.y_ >= worldBoundingBox_.max_.y_ + 0.5f * halfSize_.y_ ||
  174. box.min_.z_ <= worldBoundingBox_.min_.z_ - 0.5f * halfSize_.z_ ||
  175. box.max_.z_ >= worldBoundingBox_.max_.z_ + 0.5f * halfSize_.z_)
  176. return true;
  177. }
  178. // Bounding box too small, should create a child octant
  179. return false;
  180. }
  181. void Octant::ResetRoot()
  182. {
  183. root_ = 0;
  184. // The whole octree is being destroyed, just detach the drawables
  185. for (PODVector<Drawable*>::Iterator i = drawables_.Begin(); i != drawables_.End(); ++i)
  186. (*i)->SetOctant(0);
  187. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  188. {
  189. if (children_[i])
  190. children_[i]->ResetRoot();
  191. }
  192. }
  193. void Octant::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  194. {
  195. if (debug && debug->IsInside(worldBoundingBox_))
  196. {
  197. debug->AddBoundingBox(worldBoundingBox_, Color(0.25f, 0.25f, 0.25f), depthTest);
  198. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  199. {
  200. if (children_[i])
  201. children_[i]->DrawDebugGeometry(debug, depthTest);
  202. }
  203. }
  204. }
  205. void Octant::Initialize(const BoundingBox& box)
  206. {
  207. worldBoundingBox_ = box;
  208. center_ = box.Center();
  209. halfSize_ = 0.5f * box.Size();
  210. cullingBox_ = BoundingBox(worldBoundingBox_.min_ - halfSize_, worldBoundingBox_.max_ + halfSize_);
  211. }
  212. void Octant::GetDrawablesInternal(OctreeQuery& query, bool inside) const
  213. {
  214. if (this != root_)
  215. {
  216. Intersection res = query.TestOctant(cullingBox_, inside);
  217. if (res == INSIDE)
  218. inside = true;
  219. else if (res == OUTSIDE)
  220. {
  221. // Fully outside, so cull this octant, its children & drawables
  222. return;
  223. }
  224. }
  225. if (drawables_.Size())
  226. {
  227. Drawable** start = const_cast<Drawable**>(&drawables_[0]);
  228. Drawable** end = start + drawables_.Size();
  229. query.TestDrawables(start, end, inside);
  230. }
  231. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  232. {
  233. if (children_[i])
  234. children_[i]->GetDrawablesInternal(query, inside);
  235. }
  236. }
  237. void Octant::GetDrawablesInternal(RayOctreeQuery& query) const
  238. {
  239. float octantDist = query.ray_.HitDistance(cullingBox_);
  240. if (octantDist >= query.maxDistance_)
  241. return;
  242. if (drawables_.Size())
  243. {
  244. Drawable** start = const_cast<Drawable**>(&drawables_[0]);
  245. Drawable** end = start + drawables_.Size();
  246. while (start != end)
  247. {
  248. Drawable* drawable = *start++;
  249. if ((drawable->GetDrawableFlags() & query.drawableFlags_) && (drawable->GetViewMask() & query.viewMask_))
  250. drawable->ProcessRayQuery(query, query.result_);
  251. }
  252. }
  253. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  254. {
  255. if (children_[i])
  256. children_[i]->GetDrawablesInternal(query);
  257. }
  258. }
  259. void Octant::GetDrawablesOnlyInternal(RayOctreeQuery& query, PODVector<Drawable*>& drawables) const
  260. {
  261. float octantDist = query.ray_.HitDistance(cullingBox_);
  262. if (octantDist >= query.maxDistance_)
  263. return;
  264. if (drawables_.Size())
  265. {
  266. Drawable** start = const_cast<Drawable**>(&drawables_[0]);
  267. Drawable** end = start + drawables_.Size();
  268. while (start != end)
  269. {
  270. Drawable* drawable = *start++;
  271. if ((drawable->GetDrawableFlags() & query.drawableFlags_) && (drawable->GetViewMask() & query.viewMask_))
  272. drawables.Push(drawable);
  273. }
  274. }
  275. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  276. {
  277. if (children_[i])
  278. children_[i]->GetDrawablesOnlyInternal(query, drawables);
  279. }
  280. }
  281. Octree::Octree(Context* context) :
  282. Component(context),
  283. Octant(BoundingBox(-DEFAULT_OCTREE_SIZE, DEFAULT_OCTREE_SIZE), 0, 0, this),
  284. numLevels_(DEFAULT_OCTREE_LEVELS)
  285. {
  286. // Resize threaded ray query intermediate result vector according to number of worker threads
  287. WorkQueue* workQueue = GetSubsystem<WorkQueue>();
  288. rayQueryResults_.Resize(workQueue ? workQueue->GetNumThreads() + 1 : 1);
  289. // If the engine is running headless, subscribe to RenderUpdate events for manually updating the octree
  290. // to allow raycasts and animation update
  291. if (!GetSubsystem<Graphics>())
  292. SubscribeToEvent(E_RENDERUPDATE, HANDLER(Octree, HandleRenderUpdate));
  293. }
  294. Octree::~Octree()
  295. {
  296. // Reset root pointer from all child octants now so that they do not move their drawables to root
  297. drawableUpdates_.Clear();
  298. drawableReinsertions_.Clear();
  299. ResetRoot();
  300. }
  301. void Octree::RegisterObject(Context* context)
  302. {
  303. context->RegisterFactory<Octree>(SUBSYSTEM_CATEGORY);
  304. Vector3 defaultBoundsMin = -Vector3::ONE * DEFAULT_OCTREE_SIZE;
  305. Vector3 defaultBoundsMax = Vector3::ONE * DEFAULT_OCTREE_SIZE;
  306. ATTRIBUTE("Bounding Box Min", Vector3, worldBoundingBox_.min_, defaultBoundsMin, AM_DEFAULT);
  307. ATTRIBUTE("Bounding Box Max", Vector3, worldBoundingBox_.max_, defaultBoundsMax, AM_DEFAULT);
  308. ATTRIBUTE("Number of Levels", int, numLevels_, DEFAULT_OCTREE_LEVELS, AM_DEFAULT);
  309. }
  310. void Octree::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  311. {
  312. // If any of the (size) attributes change, resize the octree
  313. Serializable::OnSetAttribute(attr, src);
  314. SetSize(worldBoundingBox_, numLevels_);
  315. }
  316. void Octree::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  317. {
  318. if (debug)
  319. {
  320. PROFILE(OctreeDrawDebug);
  321. Octant::DrawDebugGeometry(debug, depthTest);
  322. }
  323. }
  324. void Octree::SetSize(const BoundingBox& box, unsigned numLevels)
  325. {
  326. PROFILE(ResizeOctree);
  327. // If drawables exist, they are temporarily moved to the root
  328. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  329. DeleteChild(i);
  330. Initialize(box);
  331. numDrawables_ = drawables_.Size();
  332. numLevels_ = (unsigned)Max((int)numLevels, 1);
  333. }
  334. void Octree::Update(const FrameInfo& frame)
  335. {
  336. // Let drawables update themselves before reinsertion. This can be used for animation
  337. if (!drawableUpdates_.Empty())
  338. {
  339. PROFILE(UpdateDrawables);
  340. // Perform updates in worker threads. Notify the scene that a threaded update is going on and components
  341. // (for example physics objects) should not perform non-threadsafe work when marked dirty
  342. Scene* scene = GetScene();
  343. WorkQueue* queue = GetSubsystem<WorkQueue>();
  344. scene->BeginThreadedUpdate();
  345. int numWorkItems = queue->GetNumThreads() + 1; // Worker threads + main thread
  346. int drawablesPerItem = Max((int)(drawableUpdates_.Size() / numWorkItems), 1);
  347. PODVector<Drawable*>::Iterator start = drawableUpdates_.Begin();
  348. // Create a work item for each thread
  349. for (int i = 0; i < numWorkItems; ++i)
  350. {
  351. SharedPtr<WorkItem> item = queue->GetFreeItem();
  352. item->priority_ = M_MAX_UNSIGNED;
  353. item->workFunction_ = UpdateDrawablesWork;
  354. item->aux_ = const_cast<FrameInfo*>(&frame);
  355. PODVector<Drawable*>::Iterator end = drawableUpdates_.End();
  356. if (i < numWorkItems - 1 && end - start > drawablesPerItem)
  357. end = start + drawablesPerItem;
  358. item->start_ = &(*start);
  359. item->end_ = &(*end);
  360. queue->AddWorkItem(item);
  361. start = end;
  362. }
  363. queue->Complete(M_MAX_UNSIGNED);
  364. scene->EndThreadedUpdate();
  365. }
  366. // Notify drawable update being finished. Custom animation (eg. IK) can be done at this point
  367. Scene* scene = GetScene();
  368. if (scene)
  369. {
  370. using namespace SceneDrawableUpdateFinished;
  371. VariantMap& eventData = GetEventDataMap();
  372. eventData[P_SCENE] = scene;
  373. eventData[P_TIMESTEP] = frame.timeStep_;
  374. scene->SendEvent(E_SCENEDRAWABLEUPDATEFINISHED, eventData);
  375. }
  376. // Reinsert drawables that have been moved or resized, or that have been newly added to the octree and do not sit inside
  377. // the proper octant yet
  378. if (!drawableUpdates_.Empty())
  379. {
  380. PROFILE(ReinsertToOctree);
  381. for (PODVector<Drawable*>::Iterator i = drawableUpdates_.Begin(); i != drawableUpdates_.End(); ++i)
  382. {
  383. Drawable* drawable = *i;
  384. drawable->updateQueued_ = false;
  385. Octant* octant = drawable->GetOctant();
  386. const BoundingBox& box = drawable->GetWorldBoundingBox();
  387. // Skip if no octant or does not belong to this octree anymore
  388. if (!octant || octant->GetRoot() != this)
  389. continue;
  390. // Skip if still fits the current octant
  391. if (drawable->IsOccludee() && octant->GetCullingBox().IsInside(box) == INSIDE && octant->CheckDrawableFit(box))
  392. continue;
  393. InsertDrawable(drawable);
  394. #ifdef _DEBUG
  395. // Verify that the drawable will be culled correctly
  396. octant = drawable->GetOctant();
  397. if (octant != this && octant->GetCullingBox().IsInside(box) != INSIDE)
  398. {
  399. LOGERROR("Drawable is not fully inside its octant's culling bounds: drawable box " + box.ToString() +
  400. " octant box " + octant->GetCullingBox().ToString());
  401. }
  402. #endif
  403. }
  404. }
  405. drawableUpdates_.Clear();
  406. }
  407. void Octree::AddManualDrawable(Drawable* drawable)
  408. {
  409. if (!drawable || drawable->GetOctant())
  410. return;
  411. AddDrawable(drawable);
  412. }
  413. void Octree::RemoveManualDrawable(Drawable* drawable)
  414. {
  415. if (!drawable)
  416. return;
  417. Octant* octant = drawable->GetOctant();
  418. if (octant && octant->GetRoot() == this)
  419. octant->RemoveDrawable(drawable);
  420. }
  421. void Octree::GetDrawables(OctreeQuery& query) const
  422. {
  423. query.result_.Clear();
  424. GetDrawablesInternal(query, false);
  425. }
  426. void Octree::Raycast(RayOctreeQuery& query) const
  427. {
  428. PROFILE(Raycast);
  429. query.result_.Clear();
  430. WorkQueue* queue = GetSubsystem<WorkQueue>();
  431. // If no worker threads or no triangle-level testing, do not create work items
  432. if (!queue->GetNumThreads() || query.level_ < RAY_TRIANGLE)
  433. GetDrawablesInternal(query);
  434. else
  435. {
  436. // Threaded ray query: first get the drawables
  437. rayQuery_ = &query;
  438. rayQueryDrawables_.Clear();
  439. GetDrawablesOnlyInternal(query, rayQueryDrawables_);
  440. // Check that amount of drawables is large enough to justify threading
  441. if (rayQueryDrawables_.Size() >= RAYCASTS_PER_WORK_ITEM * 2)
  442. {
  443. for (unsigned i = 0; i < rayQueryResults_.Size(); ++i)
  444. rayQueryResults_[i].Clear();
  445. PODVector<Drawable*>::Iterator start = rayQueryDrawables_.Begin();
  446. while (start != rayQueryDrawables_.End())
  447. {
  448. SharedPtr<WorkItem> item = queue->GetFreeItem();
  449. item->priority_ = M_MAX_UNSIGNED;
  450. item->workFunction_ = RaycastDrawablesWork;
  451. item->aux_ = const_cast<Octree*>(this);
  452. PODVector<Drawable*>::Iterator end = rayQueryDrawables_.End();
  453. if (end - start > RAYCASTS_PER_WORK_ITEM)
  454. end = start + RAYCASTS_PER_WORK_ITEM;
  455. item->start_ = &(*start);
  456. item->end_ = &(*end);
  457. queue->AddWorkItem(item);
  458. start = end;
  459. }
  460. // Merge per-thread results
  461. queue->Complete(M_MAX_UNSIGNED);
  462. for (unsigned i = 0; i < rayQueryResults_.Size(); ++i)
  463. query.result_.Insert(query.result_.End(), rayQueryResults_[i].Begin(), rayQueryResults_[i].End());
  464. }
  465. else
  466. {
  467. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  468. (*i)->ProcessRayQuery(query, query.result_);
  469. }
  470. }
  471. Sort(query.result_.Begin(), query.result_.End(), CompareRayQueryResults);
  472. }
  473. void Octree::RaycastSingle(RayOctreeQuery& query) const
  474. {
  475. PROFILE(Raycast);
  476. query.result_.Clear();
  477. rayQueryDrawables_.Clear();
  478. GetDrawablesOnlyInternal(query, rayQueryDrawables_);
  479. // Sort by increasing hit distance to AABB
  480. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  481. {
  482. Drawable* drawable = *i;
  483. drawable->SetSortValue(query.ray_.HitDistance(drawable->GetWorldBoundingBox()));
  484. }
  485. Sort(rayQueryDrawables_.Begin(), rayQueryDrawables_.End(), CompareDrawables);
  486. // Then do the actual test according to the query, and early-out as possible
  487. float closestHit = M_INFINITY;
  488. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  489. {
  490. Drawable* drawable = *i;
  491. if (drawable->GetSortValue() < Min(closestHit, query.maxDistance_))
  492. {
  493. unsigned oldSize = query.result_.Size();
  494. drawable->ProcessRayQuery(query, query.result_);
  495. if (query.result_.Size() > oldSize)
  496. closestHit = Min(closestHit, query.result_.Back().distance_);
  497. }
  498. else
  499. break;
  500. }
  501. if (query.result_.Size() > 1)
  502. {
  503. Sort(query.result_.Begin(), query.result_.End(), CompareRayQueryResults);
  504. query.result_.Resize(1);
  505. }
  506. }
  507. void Octree::QueueUpdate(Drawable* drawable)
  508. {
  509. Scene* scene = GetScene();
  510. if (scene && scene->IsThreadedUpdate())
  511. {
  512. MutexLock lock(octreeMutex_);
  513. drawableUpdates_.Push(drawable);
  514. }
  515. else
  516. drawableUpdates_.Push(drawable);
  517. drawable->updateQueued_ = true;
  518. }
  519. void Octree::CancelUpdate(Drawable* drawable)
  520. {
  521. drawableUpdates_.Remove(drawable);
  522. drawable->updateQueued_ = false;
  523. }
  524. void Octree::DrawDebugGeometry(bool depthTest)
  525. {
  526. DebugRenderer* debug = GetComponent<DebugRenderer>();
  527. DrawDebugGeometry(debug, depthTest);
  528. }
  529. void Octree::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  530. {
  531. // When running in headless mode, update the Octree manually during the RenderUpdate event
  532. Scene* scene = GetScene();
  533. if (!scene || !scene->IsUpdateEnabled())
  534. return;
  535. using namespace RenderUpdate;
  536. FrameInfo frame;
  537. frame.frameNumber_ = GetSubsystem<Time>()->GetFrameNumber();
  538. frame.timeStep_ = eventData[P_TIMESTEP].GetFloat();
  539. frame.camera_ = 0;
  540. Update(frame);
  541. }
  542. }