StaticModelGroup.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Copyright (c) 2008-2013 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 "Batch.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "Geometry.h"
  27. #include "Material.h"
  28. #include "OcclusionBuffer.h"
  29. #include "OctreeQuery.h"
  30. #include "Scene.h"
  31. #include "StaticModelGroup.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. extern const char* GEOMETRY_CATEGORY;
  36. StaticModelGroup::StaticModelGroup(Context* context) :
  37. StaticModel(context),
  38. nodeIDsDirty_(false)
  39. {
  40. // Initialize the default node IDs attribute
  41. nodeIDsAttr_.Push(0);
  42. }
  43. StaticModelGroup::~StaticModelGroup()
  44. {
  45. }
  46. void StaticModelGroup::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<StaticModelGroup>(GEOMETRY_CATEGORY);
  49. COPY_BASE_ATTRIBUTES(StaticModelGroup, StaticModel);
  50. REF_ACCESSOR_ATTRIBUTE(StaticModelGroup, VAR_VARIANTVECTOR, "Instance Nodes", GetNodeIDsAttr, SetNodeIDsAttr, VariantVector, Variant::emptyVariantVector, AM_DEFAULT | AM_NODEIDVECTOR);
  51. }
  52. void StaticModelGroup::ApplyAttributes()
  53. {
  54. if (!nodeIDsDirty_)
  55. return;
  56. // Remove all old instance nodes before searching for new. Can not call RemoveAllInstances() as that would modify
  57. // the ID list on its own
  58. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  59. {
  60. Node* node = instanceNodes_[i];
  61. if (node)
  62. node->RemoveListener(this);
  63. }
  64. instanceNodes_.Clear();
  65. Scene* scene = GetScene();
  66. if (scene)
  67. {
  68. // The first index stores the number of IDs redundantly. This is for editing
  69. for (unsigned i = 1; i < nodeIDsAttr_.Size(); ++i)
  70. {
  71. Node* node = scene->GetNode(nodeIDsAttr_[i].GetUInt());
  72. if (node)
  73. {
  74. WeakPtr<Node> instanceWeak(node);
  75. node->AddListener(this);
  76. instanceNodes_.Push(instanceWeak);
  77. }
  78. }
  79. }
  80. OnMarkedDirty(GetNode());
  81. nodeIDsDirty_ = false;
  82. }
  83. void StaticModelGroup::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  84. {
  85. // If no bones or no bone-level testing, use the Drawable test
  86. RayQueryLevel level = query.level_;
  87. if (level < RAY_AABB)
  88. {
  89. Drawable::ProcessRayQuery(query, results);
  90. return;
  91. }
  92. // Check ray hit distance to AABB before proceeding with more accurate tests
  93. // GetWorldBoundingBox() updates the world transforms
  94. if (query.ray_.HitDistance(GetWorldBoundingBox()) >= query.maxDistance_)
  95. return;
  96. for (unsigned i = 0; i < worldTransforms_.Size(); ++i)
  97. {
  98. // Initial test using AABB
  99. float distance = query.ray_.HitDistance(boundingBox_.Transformed(worldTransforms_[i]));
  100. if (distance >= query.maxDistance_)
  101. continue;
  102. // Then proceed to OBB and triangle-level tests if necessary
  103. if (level >= RAY_OBB)
  104. {
  105. Matrix3x4 inverse = worldTransforms_[i].Inverse();
  106. Ray localRay = query.ray_.Transformed(inverse);
  107. distance = localRay.HitDistance(boundingBox_);
  108. if (distance >= query.maxDistance_)
  109. continue;
  110. if (level == RAY_TRIANGLE)
  111. {
  112. distance = M_INFINITY;
  113. for (unsigned i = 0; i < batches_.Size(); ++i)
  114. {
  115. Geometry* geometry = batches_[i].geometry_;
  116. if (geometry)
  117. {
  118. distance = geometry->GetHitDistance(localRay);
  119. if (distance < query.maxDistance_)
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. if (distance < query.maxDistance_)
  126. {
  127. RayQueryResult result;
  128. result.drawable_ = this;
  129. result.node_ = node_;
  130. result.distance_ = distance;
  131. result.subObject_ = i;
  132. results.Push(result);
  133. }
  134. }
  135. }
  136. void StaticModelGroup::UpdateBatches(const FrameInfo& frame)
  137. {
  138. // Getting the world bounding box ensures the transforms are updated
  139. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  140. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  141. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  142. if (batches_.Size() > 1)
  143. {
  144. for (unsigned i = 0; i < batches_.Size(); ++i)
  145. {
  146. batches_[i].distance_ = frame.camera_->GetDistance(worldTransform * geometryData_[i].center_);
  147. batches_[i].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  148. batches_[i].numWorldTransforms_ = worldTransforms_.Size();
  149. }
  150. }
  151. else if (batches_.Size() == 1)
  152. {
  153. batches_[0].distance_ = distance_;
  154. batches_[0].worldTransform_ = worldTransforms_.Size() ? &worldTransforms_[0] : &Matrix3x4::IDENTITY;
  155. batches_[0].numWorldTransforms_ = worldTransforms_.Size();
  156. }
  157. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  158. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  159. if (newLodDistance != lodDistance_)
  160. {
  161. lodDistance_ = newLodDistance;
  162. CalculateLodLevels();
  163. }
  164. }
  165. unsigned StaticModelGroup::GetNumOccluderTriangles()
  166. {
  167. // Make sure instance transforms are up-to-date
  168. GetWorldBoundingBox();
  169. unsigned triangles = 0;
  170. for (unsigned i = 0; i < batches_.Size(); ++i)
  171. {
  172. Geometry* geometry = GetLodGeometry(i, occlusionLodLevel_);
  173. if (!geometry)
  174. continue;
  175. // Check that the material is suitable for occlusion (default material always is)
  176. Material* mat = batches_[i].material_;
  177. if (mat && !mat->GetOcclusion())
  178. continue;
  179. triangles += worldTransforms_.Size() * geometry->GetIndexCount() / 3;
  180. }
  181. return triangles;
  182. }
  183. bool StaticModelGroup::DrawOcclusion(OcclusionBuffer* buffer)
  184. {
  185. // Make sure instance transforms are up-to-date
  186. GetWorldBoundingBox();
  187. for (unsigned i = 0; i < worldTransforms_.Size(); ++i)
  188. {
  189. for (unsigned j = 0; j < batches_.Size(); ++j)
  190. {
  191. Geometry* geometry = GetLodGeometry(j, occlusionLodLevel_);
  192. if (!geometry)
  193. continue;
  194. // Check that the material is suitable for occlusion (default material always is) and set culling mode
  195. Material* material = batches_[j].material_;
  196. if (material)
  197. {
  198. if (!material->GetOcclusion())
  199. continue;
  200. buffer->SetCullMode(material->GetCullMode());
  201. }
  202. else
  203. buffer->SetCullMode(CULL_CCW);
  204. const unsigned char* vertexData;
  205. unsigned vertexSize;
  206. const unsigned char* indexData;
  207. unsigned indexSize;
  208. unsigned elementMask;
  209. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  210. // Check for valid geometry data
  211. if (!vertexData || !indexData)
  212. continue;
  213. unsigned indexStart = geometry->GetIndexStart();
  214. unsigned indexCount = geometry->GetIndexCount();
  215. // Draw and check for running out of triangles
  216. if (!buffer->Draw(worldTransforms_[i], vertexData, vertexSize, indexData, indexSize, indexStart, indexCount))
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. void StaticModelGroup::AddInstanceNode(Node* node)
  223. {
  224. if (!node)
  225. return;
  226. WeakPtr<Node> instanceWeak(node);
  227. if (instanceNodes_.Contains(instanceWeak))
  228. return;
  229. // Add as a listener for the instance node, so that we know to dirty the transforms when the node moves or is enabled/disabled
  230. node->AddListener(this);
  231. instanceNodes_.Push(instanceWeak);
  232. OnMarkedDirty(GetNode());
  233. UpdateNodeIDs();
  234. MarkNetworkUpdate();
  235. }
  236. void StaticModelGroup::RemoveInstanceNode(Node* node)
  237. {
  238. if (!node)
  239. return;
  240. WeakPtr<Node> instanceWeak(node);
  241. node->RemoveListener(this);
  242. instanceNodes_.Remove(instanceWeak);
  243. OnMarkedDirty(GetNode());
  244. UpdateNodeIDs();
  245. MarkNetworkUpdate();
  246. }
  247. void StaticModelGroup::RemoveAllInstanceNodes()
  248. {
  249. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  250. {
  251. Node* node = instanceNodes_[i];
  252. if (node)
  253. node->RemoveListener(this);
  254. }
  255. instanceNodes_.Clear();
  256. OnMarkedDirty(GetNode());
  257. UpdateNodeIDs();
  258. MarkNetworkUpdate();
  259. }
  260. Node* StaticModelGroup::GetInstanceNode(unsigned index) const
  261. {
  262. return index < instanceNodes_.Size() ? instanceNodes_[index] : (Node*)0;
  263. }
  264. void StaticModelGroup::SetNodeIDsAttr(const VariantVector& value)
  265. {
  266. // Just remember the node IDs. They need to go through the SceneResolver, and we actually find the nodes during
  267. // ApplyAttributes()
  268. if (value.Size())
  269. {
  270. nodeIDsAttr_.Clear();
  271. unsigned index = 0;
  272. unsigned numInstances = value[index++].GetUInt();
  273. // Prevent crash on entering negative value in the editor
  274. if (numInstances > M_MAX_INT)
  275. numInstances = 0;
  276. nodeIDsAttr_.Push(numInstances);
  277. while (numInstances--)
  278. {
  279. // If vector contains less IDs than should, fill the rest with zeroes
  280. if (index < value.Size())
  281. nodeIDsAttr_.Push(value[index++].GetUInt());
  282. else
  283. nodeIDsAttr_.Push(0);
  284. }
  285. }
  286. else
  287. {
  288. nodeIDsAttr_.Clear();
  289. nodeIDsAttr_.Push(0);
  290. }
  291. nodeIDsDirty_ = true;
  292. }
  293. void StaticModelGroup::OnNodeSetEnabled(Node* node)
  294. {
  295. Drawable::OnMarkedDirty(node);
  296. }
  297. void StaticModelGroup::OnWorldBoundingBoxUpdate()
  298. {
  299. // Update transforms and bounding box at the same time to have to go through the objects only once
  300. worldBoundingBox_.defined_ = false;
  301. worldTransforms_.Resize(instanceNodes_.Size());
  302. unsigned index = 0;
  303. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  304. {
  305. Node* node = instanceNodes_[i];
  306. if (!node || !node->IsEnabled())
  307. continue;
  308. const Matrix3x4& worldTransform = node->GetWorldTransform();
  309. worldTransforms_[index++] = worldTransform;
  310. worldBoundingBox_.Merge(boundingBox_.Transformed(worldTransform));
  311. }
  312. worldTransforms_.Resize(index);
  313. }
  314. void StaticModelGroup::UpdateNodeIDs()
  315. {
  316. nodeIDsAttr_.Clear();
  317. nodeIDsAttr_.Push(instanceNodes_.Size());
  318. for (unsigned i = 0; i < instanceNodes_.Size(); ++i)
  319. {
  320. Node* node = instanceNodes_[i];
  321. nodeIDsAttr_.Push(node ? node->GetID() : 0);
  322. }
  323. }
  324. }