BillboardSet.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. //
  2. // Copyright (c) 2008-2016 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/Profiler.h"
  25. #include "../Graphics/Batch.h"
  26. #include "../Graphics/BillboardSet.h"
  27. #include "../Graphics/Camera.h"
  28. #include "../Graphics/Geometry.h"
  29. #include "../Graphics/Graphics.h"
  30. #include "../Graphics/IndexBuffer.h"
  31. #include "../Graphics/Material.h"
  32. #include "../Graphics/OctreeQuery.h"
  33. #include "../Graphics/VertexBuffer.h"
  34. #include "../IO/MemoryBuffer.h"
  35. #include "../Resource/ResourceCache.h"
  36. #include "../Scene/Node.h"
  37. #include "../DebugNew.h"
  38. namespace Urho3D
  39. {
  40. extern const char* GEOMETRY_CATEGORY;
  41. static const float INV_SQRT_TWO = 1.0f / sqrtf(2.0f);
  42. const char* faceCameraModeNames[] =
  43. {
  44. "None",
  45. "Rotate XYZ",
  46. "Rotate Y",
  47. "LookAt XYZ",
  48. "LookAt Y",
  49. "Direction",
  50. 0
  51. };
  52. inline bool CompareBillboards(Billboard* lhs, Billboard* rhs)
  53. {
  54. return lhs->sortDistance_ > rhs->sortDistance_;
  55. }
  56. BillboardSet::BillboardSet(Context* context) :
  57. Drawable(context, DRAWABLE_GEOMETRY),
  58. animationLodBias_(1.0f),
  59. animationLodTimer_(0.0f),
  60. relative_(true),
  61. scaled_(true),
  62. sorted_(false),
  63. faceCameraMode_(FC_ROTATE_XYZ),
  64. geometry_(new Geometry(context)),
  65. vertexBuffer_(new VertexBuffer(context_)),
  66. indexBuffer_(new IndexBuffer(context_)),
  67. bufferSizeDirty_(true),
  68. bufferDirty_(true),
  69. forceUpdate_(false),
  70. geometryTypeUpdate_(false),
  71. sortThisFrame_(false),
  72. sortFrameNumber_(0),
  73. previousOffset_(Vector3::ZERO)
  74. {
  75. // Vertex buffer doesn't have its format defined yet, so manually define the elements into Geometry now
  76. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1 | MASK_TEXCOORD2);
  77. geometry_->SetIndexBuffer(indexBuffer_);
  78. batches_.Resize(1);
  79. batches_[0].geometry_ = geometry_;
  80. batches_[0].geometryType_ = GEOM_BILLBOARD;
  81. batches_[0].worldTransform_ = &transforms_[0];
  82. }
  83. BillboardSet::~BillboardSet()
  84. {
  85. }
  86. void BillboardSet::RegisterObject(Context* context)
  87. {
  88. context->RegisterFactory<BillboardSet>(GEOMETRY_CATEGORY);
  89. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  90. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()),
  91. AM_DEFAULT);
  92. URHO3D_ACCESSOR_ATTRIBUTE("Relative Position", IsRelative, SetRelative, bool, true, AM_DEFAULT);
  93. URHO3D_ACCESSOR_ATTRIBUTE("Relative Scale", IsScaled, SetScaled, bool, true, AM_DEFAULT);
  94. URHO3D_ACCESSOR_ATTRIBUTE("Sort By Distance", IsSorted, SetSorted, bool, false, AM_DEFAULT);
  95. URHO3D_ACCESSOR_ATTRIBUTE("Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  96. URHO3D_ATTRIBUTE("Cast Shadows", bool, castShadows_, false, AM_DEFAULT);
  97. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Face Camera Mode", GetFaceCameraMode, SetFaceCameraMode, FaceCameraMode, faceCameraModeNames, FC_ROTATE_XYZ, AM_DEFAULT);
  98. URHO3D_ACCESSOR_ATTRIBUTE("Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  99. URHO3D_ACCESSOR_ATTRIBUTE("Shadow Distance", GetShadowDistance, SetShadowDistance, float, 0.0f, AM_DEFAULT);
  100. URHO3D_ACCESSOR_ATTRIBUTE("Animation LOD Bias", GetAnimationLodBias, SetAnimationLodBias, float, 1.0f, AM_DEFAULT);
  101. URHO3D_COPY_BASE_ATTRIBUTES(Drawable);
  102. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Billboards", GetBillboardsAttr, SetBillboardsAttr, VariantVector, Variant::emptyVariantVector,
  103. AM_FILE);
  104. URHO3D_ACCESSOR_ATTRIBUTE("Network Billboards", GetNetBillboardsAttr, SetNetBillboardsAttr, PODVector<unsigned char>,
  105. Variant::emptyBuffer, AM_NET | AM_NOEDIT);
  106. }
  107. void BillboardSet::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  108. {
  109. // If no billboard-level testing, use the Drawable test
  110. if (query.level_ < RAY_TRIANGLE)
  111. {
  112. Drawable::ProcessRayQuery(query, results);
  113. return;
  114. }
  115. // Check ray hit distance to AABB before proceeding with billboard-level tests
  116. if (query.ray_.HitDistance(GetWorldBoundingBox()) >= query.maxDistance_)
  117. return;
  118. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  119. Matrix3x4 billboardTransform = relative_ ? worldTransform : Matrix3x4::IDENTITY;
  120. Vector3 billboardScale = scaled_ ? worldTransform.Scale() : Vector3::ONE;
  121. for (unsigned i = 0; i < billboards_.Size(); ++i)
  122. {
  123. if (!billboards_[i].enabled_)
  124. continue;
  125. // Approximate the billboards as spheres for raycasting
  126. float size = INV_SQRT_TWO * (billboards_[i].size_.x_ * billboardScale.x_ + billboards_[i].size_.y_ * billboardScale.y_);
  127. Vector3 center = billboardTransform * billboards_[i].position_;
  128. Sphere billboardSphere(center, size);
  129. float distance = query.ray_.HitDistance(billboardSphere);
  130. if (distance < query.maxDistance_)
  131. {
  132. // If the code reaches here then we have a hit
  133. RayQueryResult result;
  134. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  135. result.normal_ = -query.ray_.direction_;
  136. result.distance_ = distance;
  137. result.drawable_ = this;
  138. result.node_ = node_;
  139. result.subObject_ = i;
  140. results.Push(result);
  141. }
  142. }
  143. }
  144. void BillboardSet::UpdateBatches(const FrameInfo& frame)
  145. {
  146. // If beginning a new frame, assume no sorting first
  147. if (frame.frameNumber_ != sortFrameNumber_)
  148. {
  149. sortThisFrame_ = false;
  150. sortFrameNumber_ = frame.frameNumber_;
  151. }
  152. Vector3 worldPos = node_->GetWorldPosition();
  153. Vector3 offset = (worldPos - frame.camera_->GetNode()->GetWorldPosition());
  154. // Sort if position relative to camera has changed
  155. if (offset != previousOffset_)
  156. {
  157. if(sorted_)
  158. sortThisFrame_ = true;
  159. if(faceCameraMode_ == FC_DIRECTION)
  160. bufferDirty_ = true;
  161. }
  162. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  163. // Calculate scaled distance for animation LOD
  164. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  165. // If there are no billboards, the size becomes zero, and LOD'ed updates no longer happen. Disable LOD in that case
  166. if (scale > M_EPSILON)
  167. lodDistance_ = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  168. else
  169. lodDistance_ = 0.0f;
  170. batches_[0].distance_ = distance_;
  171. batches_[0].numWorldTransforms_ = 2;
  172. // Billboard positioning
  173. transforms_[0] = relative_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  174. // Billboard rotation
  175. transforms_[1] = Matrix3x4(Vector3::ZERO, faceCameraMode_ != FC_NONE ? frame.camera_->GetFaceCameraRotation(
  176. node_->GetWorldPosition(), node_->GetWorldRotation(), faceCameraMode_) : node_->GetWorldRotation(), Vector3::ONE);
  177. }
  178. void BillboardSet::UpdateGeometry(const FrameInfo& frame)
  179. {
  180. if (bufferSizeDirty_ || indexBuffer_->IsDataLost())
  181. UpdateBufferSize();
  182. if (bufferDirty_ || sortThisFrame_ || vertexBuffer_->IsDataLost())
  183. UpdateVertexBuffer(frame);
  184. // If using camera facing, re-update the rotation for the current view now
  185. if (faceCameraMode_ != FC_NONE)
  186. {
  187. transforms_[1] = Matrix3x4(Vector3::ZERO, frame.camera_->GetFaceCameraRotation(node_->GetWorldPosition(),
  188. node_->GetWorldRotation(), faceCameraMode_), Vector3::ONE);
  189. }
  190. }
  191. UpdateGeometryType BillboardSet::GetUpdateGeometryType()
  192. {
  193. // If using camera facing, always need some kind of geometry update, in case the billboard set is rendered from several views
  194. if (bufferDirty_ || bufferSizeDirty_ || vertexBuffer_->IsDataLost() || indexBuffer_->IsDataLost() || sortThisFrame_)
  195. return UPDATE_MAIN_THREAD;
  196. else if (faceCameraMode_ != FC_NONE)
  197. return UPDATE_WORKER_THREAD;
  198. else
  199. return UPDATE_NONE;
  200. }
  201. void BillboardSet::SetMaterial(Material* material)
  202. {
  203. batches_[0].material_ = material;
  204. MarkNetworkUpdate();
  205. }
  206. void BillboardSet::SetNumBillboards(unsigned num)
  207. {
  208. // Prevent negative value being assigned from the editor
  209. if (num > M_MAX_INT)
  210. num = 0;
  211. if (num > MAX_BILLBOARDS)
  212. num = MAX_BILLBOARDS;
  213. unsigned oldNum = billboards_.Size();
  214. billboards_.Resize(num);
  215. // Set default values to new billboards
  216. for (unsigned i = oldNum; i < num; ++i)
  217. {
  218. billboards_[i].position_ = Vector3::ZERO;
  219. billboards_[i].size_ = Vector2::ONE;
  220. billboards_[i].uv_ = Rect::POSITIVE;
  221. billboards_[i].color_ = Color(1.0f, 1.0f, 1.0f);
  222. billboards_[i].rotation_ = 0.0f;
  223. billboards_[i].direction_ = Vector3::UP;
  224. billboards_[i].enabled_ = false;
  225. }
  226. bufferSizeDirty_ = true;
  227. Commit();
  228. }
  229. void BillboardSet::SetRelative(bool enable)
  230. {
  231. relative_ = enable;
  232. Commit();
  233. }
  234. void BillboardSet::SetScaled(bool enable)
  235. {
  236. scaled_ = enable;
  237. Commit();
  238. }
  239. void BillboardSet::SetSorted(bool enable)
  240. {
  241. sorted_ = enable;
  242. Commit();
  243. }
  244. void BillboardSet::SetFaceCameraMode(FaceCameraMode mode)
  245. {
  246. if ((faceCameraMode_ != FC_DIRECTION && mode == FC_DIRECTION) || (faceCameraMode_ == FC_DIRECTION && mode != FC_DIRECTION))
  247. {
  248. faceCameraMode_ = mode;
  249. if (faceCameraMode_ == FC_DIRECTION)
  250. batches_[0].geometryType_ = GEOM_DIRBILLBOARD;
  251. else
  252. batches_[0].geometryType_ = GEOM_BILLBOARD;
  253. geometryTypeUpdate_ = true;
  254. bufferSizeDirty_ = true;
  255. Commit();
  256. }
  257. else
  258. {
  259. faceCameraMode_ = mode;
  260. MarkNetworkUpdate();
  261. }
  262. }
  263. void BillboardSet::SetAnimationLodBias(float bias)
  264. {
  265. animationLodBias_ = Max(bias, 0.0f);
  266. MarkNetworkUpdate();
  267. }
  268. void BillboardSet::Commit()
  269. {
  270. MarkPositionsDirty();
  271. MarkNetworkUpdate();
  272. }
  273. Material* BillboardSet::GetMaterial() const
  274. {
  275. return batches_[0].material_;
  276. }
  277. Billboard* BillboardSet::GetBillboard(unsigned index)
  278. {
  279. return index < billboards_.Size() ? &billboards_[index] : (Billboard*)0;
  280. }
  281. void BillboardSet::SetMaterialAttr(const ResourceRef& value)
  282. {
  283. ResourceCache* cache = GetSubsystem<ResourceCache>();
  284. SetMaterial(cache->GetResource<Material>(value.name_));
  285. }
  286. void BillboardSet::SetBillboardsAttr(const VariantVector& value)
  287. {
  288. unsigned index = 0;
  289. unsigned numBillboards = index < value.Size() ? value[index++].GetUInt() : 0;
  290. SetNumBillboards(numBillboards);
  291. // Dealing with old billboard format
  292. if (value.Size() == billboards_.Size() * 6 + 1)
  293. {
  294. for (PODVector<Billboard>::Iterator i = billboards_.Begin(); i != billboards_.End() && index < value.Size(); ++i)
  295. {
  296. i->position_ = value[index++].GetVector3();
  297. i->size_ = value[index++].GetVector2();
  298. Vector4 uv = value[index++].GetVector4();
  299. i->uv_ = Rect(uv.x_, uv.y_, uv.z_, uv.w_);
  300. i->color_ = value[index++].GetColor();
  301. i->rotation_ = value[index++].GetFloat();
  302. i->enabled_ = value[index++].GetBool();
  303. }
  304. }
  305. // New billboard format
  306. else
  307. {
  308. for (PODVector<Billboard>::Iterator i = billboards_.Begin(); i != billboards_.End() && index < value.Size(); ++i)
  309. {
  310. i->position_ = value[index++].GetVector3();
  311. i->size_ = value[index++].GetVector2();
  312. Vector4 uv = value[index++].GetVector4();
  313. i->uv_ = Rect(uv.x_, uv.y_, uv.z_, uv.w_);
  314. i->color_ = value[index++].GetColor();
  315. i->rotation_ = value[index++].GetFloat();
  316. i->direction_ = value[index++].GetVector3();
  317. i->enabled_ = value[index++].GetBool();
  318. }
  319. }
  320. Commit();
  321. }
  322. void BillboardSet::SetNetBillboardsAttr(const PODVector<unsigned char>& value)
  323. {
  324. MemoryBuffer buf(value);
  325. unsigned numBillboards = buf.ReadVLE();
  326. SetNumBillboards(numBillboards);
  327. for (PODVector<Billboard>::Iterator i = billboards_.Begin(); i != billboards_.End(); ++i)
  328. {
  329. i->position_ = buf.ReadVector3();
  330. i->size_ = buf.ReadVector2();
  331. i->uv_ = buf.ReadRect();
  332. i->color_ = buf.ReadColor();
  333. i->rotation_ = buf.ReadFloat();
  334. i->direction_ = buf.ReadVector3();
  335. i->enabled_ = buf.ReadBool();
  336. }
  337. Commit();
  338. }
  339. ResourceRef BillboardSet::GetMaterialAttr() const
  340. {
  341. return GetResourceRef(batches_[0].material_, Material::GetTypeStatic());
  342. }
  343. VariantVector BillboardSet::GetBillboardsAttr() const
  344. {
  345. VariantVector ret;
  346. ret.Reserve(billboards_.Size() * 7 + 1);
  347. ret.Push(billboards_.Size());
  348. for (PODVector<Billboard>::ConstIterator i = billboards_.Begin(); i != billboards_.End(); ++i)
  349. {
  350. ret.Push(i->position_);
  351. ret.Push(i->size_);
  352. ret.Push(Vector4(i->uv_.min_.x_, i->uv_.min_.y_, i->uv_.max_.x_, i->uv_.max_.y_));
  353. ret.Push(i->color_);
  354. ret.Push(i->rotation_);
  355. ret.Push(i->direction_);
  356. ret.Push(i->enabled_);
  357. }
  358. return ret;
  359. }
  360. const PODVector<unsigned char>& BillboardSet::GetNetBillboardsAttr() const
  361. {
  362. attrBuffer_.Clear();
  363. attrBuffer_.WriteVLE(billboards_.Size());
  364. for (PODVector<Billboard>::ConstIterator i = billboards_.Begin(); i != billboards_.End(); ++i)
  365. {
  366. attrBuffer_.WriteVector3(i->position_);
  367. attrBuffer_.WriteVector2(i->size_);
  368. attrBuffer_.WriteRect(i->uv_);
  369. attrBuffer_.WriteColor(i->color_);
  370. attrBuffer_.WriteFloat(i->rotation_);
  371. attrBuffer_.WriteVector3(i->direction_);
  372. attrBuffer_.WriteBool(i->enabled_);
  373. }
  374. return attrBuffer_.GetBuffer();
  375. }
  376. void BillboardSet::OnWorldBoundingBoxUpdate()
  377. {
  378. unsigned enabledBillboards = 0;
  379. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  380. Matrix3x4 billboardTransform = relative_ ? worldTransform : Matrix3x4::IDENTITY;
  381. Vector3 billboardScale = scaled_ ? worldTransform.Scale() : Vector3::ONE;
  382. BoundingBox worldBox;
  383. for (unsigned i = 0; i < billboards_.Size(); ++i)
  384. {
  385. if (!billboards_[i].enabled_)
  386. continue;
  387. float size = INV_SQRT_TWO * (billboards_[i].size_.x_ * billboardScale.x_ + billboards_[i].size_.y_ * billboardScale.y_);
  388. Vector3 center = billboardTransform * billboards_[i].position_;
  389. Vector3 edge = Vector3::ONE * size;
  390. worldBox.Merge(BoundingBox(center - edge, center + edge));
  391. ++enabledBillboards;
  392. }
  393. // Always merge the node's own position to ensure particle emitter updates continue when the relative mode is switched
  394. worldBox.Merge(node_->GetWorldPosition());
  395. worldBoundingBox_ = worldBox;
  396. }
  397. void BillboardSet::UpdateBufferSize()
  398. {
  399. unsigned numBillboards = billboards_.Size();
  400. if (vertexBuffer_->GetVertexCount() != numBillboards * 4 || geometryTypeUpdate_)
  401. {
  402. if (faceCameraMode_ == FC_DIRECTION)
  403. {
  404. vertexBuffer_->SetSize(numBillboards * 4, MASK_POSITION | MASK_NORMAL | MASK_COLOR | MASK_TEXCOORD1 | MASK_TEXCOORD2 | MASK_TANGENT, true);
  405. geometry_->SetVertexBuffer(0, vertexBuffer_);
  406. }
  407. else
  408. {
  409. vertexBuffer_->SetSize(numBillboards * 4, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1 | MASK_TEXCOORD2, true);
  410. geometry_->SetVertexBuffer(0, vertexBuffer_);
  411. }
  412. geometryTypeUpdate_ = false;
  413. }
  414. if (indexBuffer_->GetIndexCount() != numBillboards * 6)
  415. indexBuffer_->SetSize(numBillboards * 6, false);
  416. bufferSizeDirty_ = false;
  417. bufferDirty_ = true;
  418. forceUpdate_ = true;
  419. if (!numBillboards)
  420. return;
  421. // Indices do not change for a given billboard capacity
  422. unsigned short* dest = (unsigned short*)indexBuffer_->Lock(0, numBillboards * 6, true);
  423. if (!dest)
  424. return;
  425. unsigned vertexIndex = 0;
  426. while (numBillboards--)
  427. {
  428. dest[0] = (unsigned short)vertexIndex;
  429. dest[1] = (unsigned short)(vertexIndex + 1);
  430. dest[2] = (unsigned short)(vertexIndex + 2);
  431. dest[3] = (unsigned short)(vertexIndex + 2);
  432. dest[4] = (unsigned short)(vertexIndex + 3);
  433. dest[5] = (unsigned short)vertexIndex;
  434. dest += 6;
  435. vertexIndex += 4;
  436. }
  437. indexBuffer_->Unlock();
  438. indexBuffer_->ClearDataLost();
  439. }
  440. void BillboardSet::UpdateVertexBuffer(const FrameInfo& frame)
  441. {
  442. // If using animation LOD, accumulate time and see if it is time to update
  443. if (animationLodBias_ > 0.0f && lodDistance_ > 0.0f)
  444. {
  445. animationLodTimer_ += animationLodBias_ * frame.timeStep_ * ANIMATION_LOD_BASESCALE;
  446. if (animationLodTimer_ >= lodDistance_)
  447. animationLodTimer_ = fmodf(animationLodTimer_, lodDistance_);
  448. else
  449. {
  450. // No LOD if immediate update forced
  451. if (!forceUpdate_)
  452. return;
  453. }
  454. }
  455. unsigned numBillboards = billboards_.Size();
  456. unsigned enabledBillboards = 0;
  457. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  458. Matrix3x4 billboardTransform = relative_ ? worldTransform : Matrix3x4::IDENTITY;
  459. Vector3 billboardScale = scaled_ ? worldTransform.Scale() : Vector3::ONE;
  460. // First check number of enabled billboards
  461. for (unsigned i = 0; i < numBillboards; ++i)
  462. {
  463. if (billboards_[i].enabled_)
  464. ++enabledBillboards;
  465. }
  466. sortedBillboards_.Resize(enabledBillboards);
  467. unsigned index = 0;
  468. // Then set initial sort order and distances
  469. for (unsigned i = 0; i < numBillboards; ++i)
  470. {
  471. Billboard& billboard = billboards_[i];
  472. if (billboard.enabled_)
  473. {
  474. sortedBillboards_[index++] = &billboard;
  475. if (sorted_)
  476. billboard.sortDistance_ = frame.camera_->GetDistanceSquared(billboardTransform * billboards_[i].position_);
  477. }
  478. }
  479. batches_[0].geometry_->SetDrawRange(TRIANGLE_LIST, 0, enabledBillboards * 6, false);
  480. bufferDirty_ = false;
  481. forceUpdate_ = false;
  482. if (!enabledBillboards)
  483. return;
  484. if (sorted_)
  485. {
  486. Sort(sortedBillboards_.Begin(), sortedBillboards_.End(), CompareBillboards);
  487. Vector3 worldPos = node_->GetWorldPosition();
  488. // Store the "last sorted position" now
  489. previousOffset_ = (worldPos - frame.camera_->GetNode()->GetWorldPosition());
  490. }
  491. float* dest = (float*)vertexBuffer_->Lock(0, enabledBillboards * 4, true);
  492. if (!dest)
  493. return;
  494. if (faceCameraMode_ != FC_DIRECTION)
  495. {
  496. for (unsigned i = 0; i < enabledBillboards; ++i)
  497. {
  498. Billboard& billboard = *sortedBillboards_[i];
  499. Vector2 size(billboard.size_.x_ * billboardScale.x_, billboard.size_.y_ * billboardScale.y_);
  500. unsigned color = billboard.color_.ToUInt();
  501. float rotationMatrix[2][2];
  502. SinCos(billboard.rotation_, rotationMatrix[0][1], rotationMatrix[0][0]);
  503. rotationMatrix[1][0] = -rotationMatrix[0][1];
  504. rotationMatrix[1][1] = rotationMatrix[0][0];
  505. dest[0] = billboard.position_.x_;
  506. dest[1] = billboard.position_.y_;
  507. dest[2] = billboard.position_.z_;
  508. ((unsigned&)dest[3]) = color;
  509. dest[4] = billboard.uv_.min_.x_;
  510. dest[5] = billboard.uv_.min_.y_;
  511. dest[6] = -size.x_ * rotationMatrix[0][0] + size.y_ * rotationMatrix[0][1];
  512. dest[7] = -size.x_ * rotationMatrix[1][0] + size.y_ * rotationMatrix[1][1];
  513. dest[8] = billboard.position_.x_;
  514. dest[9] = billboard.position_.y_;
  515. dest[10] = billboard.position_.z_;
  516. ((unsigned&)dest[11]) = color;
  517. dest[12] = billboard.uv_.max_.x_;
  518. dest[13] = billboard.uv_.min_.y_;
  519. dest[14] = size.x_ * rotationMatrix[0][0] + size.y_ * rotationMatrix[0][1];
  520. dest[15] = size.x_ * rotationMatrix[1][0] + size.y_ * rotationMatrix[1][1];
  521. dest[16] = billboard.position_.x_;
  522. dest[17] = billboard.position_.y_;
  523. dest[18] = billboard.position_.z_;
  524. ((unsigned&)dest[19]) = color;
  525. dest[20] = billboard.uv_.max_.x_;
  526. dest[21] = billboard.uv_.max_.y_;
  527. dest[22] = size.x_ * rotationMatrix[0][0] - size.y_ * rotationMatrix[0][1];
  528. dest[23] = size.x_ * rotationMatrix[1][0] - size.y_ * rotationMatrix[1][1];
  529. dest[24] = billboard.position_.x_;
  530. dest[25] = billboard.position_.y_;
  531. dest[26] = billboard.position_.z_;
  532. ((unsigned&)dest[27]) = color;
  533. dest[28] = billboard.uv_.min_.x_;
  534. dest[29] = billboard.uv_.max_.y_;
  535. dest[30] = -size.x_ * rotationMatrix[0][0] - size.y_ * rotationMatrix[0][1];
  536. dest[31] = -size.x_ * rotationMatrix[1][0] - size.y_ * rotationMatrix[1][1];
  537. dest += 32;
  538. }
  539. }
  540. else
  541. {
  542. for (unsigned i = 0; i < enabledBillboards; ++i)
  543. {
  544. Billboard& billboard = *sortedBillboards_[i];
  545. Vector2 size(billboard.size_.x_ * billboardScale.x_, billboard.size_.y_ * billboardScale.y_);
  546. unsigned color = billboard.color_.ToUInt();
  547. float rot2D[2][2];
  548. SinCos(billboard.rotation_, rot2D[0][1], rot2D[0][0]);
  549. rot2D[1][0] = -rot2D[0][1];
  550. rot2D[1][1] = rot2D[0][0];
  551. dest[0] = billboard.position_.x_;
  552. dest[1] = billboard.position_.y_;
  553. dest[2] = billboard.position_.z_;
  554. dest[3] = billboard.direction_.x_;
  555. dest[4] = billboard.direction_.y_;
  556. dest[5] = billboard.direction_.z_;
  557. ((unsigned&)dest[6]) = color;
  558. dest[7] = billboard.uv_.min_.x_;
  559. dest[8] = billboard.uv_.min_.y_;
  560. dest[9] = -size.x_ * rot2D[0][0] + size.y_ * rot2D[0][1];
  561. dest[10] = -size.x_ * rot2D[1][0] + size.y_ * rot2D[1][1];
  562. dest[11] = frame.camera_->GetNode()->GetWorldPosition().x_;
  563. dest[12] = frame.camera_->GetNode()->GetWorldPosition().y_;
  564. dest[13] = frame.camera_->GetNode()->GetWorldPosition().z_;
  565. dest[14] = 1.0f;
  566. dest[15] = billboard.position_.x_;
  567. dest[16] = billboard.position_.y_;
  568. dest[17] = billboard.position_.z_;
  569. dest[18] = billboard.direction_.x_;
  570. dest[19] = billboard.direction_.y_;
  571. dest[20] = billboard.direction_.z_;
  572. ((unsigned&)dest[21]) = color;
  573. dest[22] = billboard.uv_.max_.x_;
  574. dest[23] = billboard.uv_.min_.y_;
  575. dest[24] = size.x_ * rot2D[0][0] + size.y_ * rot2D[0][1];
  576. dest[25] = size.x_ * rot2D[1][0] + size.y_ * rot2D[1][1];
  577. dest[26] = frame.camera_->GetNode()->GetWorldPosition().x_;
  578. dest[27] = frame.camera_->GetNode()->GetWorldPosition().y_;
  579. dest[28] = frame.camera_->GetNode()->GetWorldPosition().z_;
  580. dest[29] = 1.0f;
  581. dest[30] = billboard.position_.x_;
  582. dest[31] = billboard.position_.y_;
  583. dest[32] = billboard.position_.z_;
  584. dest[33] = billboard.direction_.x_;
  585. dest[34] = billboard.direction_.y_;
  586. dest[35] = billboard.direction_.z_;
  587. ((unsigned&)dest[36]) = color;
  588. dest[37] = billboard.uv_.max_.x_;
  589. dest[38] = billboard.uv_.max_.y_;
  590. dest[39] = size.x_ * rot2D[0][0] - size.y_ * rot2D[0][1];
  591. dest[40] = size.x_ * rot2D[1][0] - size.y_ * rot2D[1][1];
  592. dest[41] = frame.camera_->GetNode()->GetWorldPosition().x_;
  593. dest[42] = frame.camera_->GetNode()->GetWorldPosition().y_;
  594. dest[43] = frame.camera_->GetNode()->GetWorldPosition().z_;
  595. dest[44] = 1.0f;
  596. dest[45] = billboard.position_.x_;
  597. dest[46] = billboard.position_.y_;
  598. dest[47] = billboard.position_.z_;
  599. dest[48] = billboard.direction_.x_;
  600. dest[49] = billboard.direction_.y_;
  601. dest[50] = billboard.direction_.z_;
  602. ((unsigned&)dest[51]) = color;
  603. dest[52] = billboard.uv_.min_.x_;
  604. dest[53] = billboard.uv_.max_.y_;
  605. dest[54] = -size.x_ * rot2D[0][0] - size.y_ * rot2D[0][1];
  606. dest[55] = -size.x_ * rot2D[1][0] - size.y_ * rot2D[1][1];
  607. dest[56] = frame.camera_->GetNode()->GetWorldPosition().x_;
  608. dest[57] = frame.camera_->GetNode()->GetWorldPosition().y_;
  609. dest[58] = frame.camera_->GetNode()->GetWorldPosition().z_;
  610. dest[59] = 1.0f;
  611. dest += 60;
  612. }
  613. }
  614. vertexBuffer_->Unlock();
  615. vertexBuffer_->ClearDataLost();
  616. }
  617. void BillboardSet::MarkPositionsDirty()
  618. {
  619. Drawable::OnMarkedDirty(node_);
  620. bufferDirty_ = true;
  621. }
  622. }