DecalSet.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Drawable.h"
  25. #include "Frustum.h"
  26. #include "List.h"
  27. #include "Skeleton.h"
  28. /// %Decal vertex.
  29. struct DecalVertex
  30. {
  31. /// Construct with defaults.
  32. DecalVertex()
  33. {
  34. }
  35. /// Construct with position and normal.
  36. DecalVertex(const Vector3& position, const Vector3& normal) :
  37. position_(position),
  38. normal_(normal)
  39. {
  40. }
  41. // Construct with position, normal and skinning information.
  42. DecalVertex(const Vector3& position, const Vector3& normal, const float* blendWeights, const unsigned char* blendIndices) :
  43. position_(position),
  44. normal_(normal)
  45. {
  46. for (unsigned i = 0; i < 4; ++i)
  47. {
  48. blendWeights_[i] = blendWeights[i];
  49. blendIndices_[i] = blendIndices[i];
  50. }
  51. }
  52. /// Position.
  53. Vector3 position_;
  54. /// Normal.
  55. Vector3 normal_;
  56. /// Texture coordinates coordinates.
  57. Vector2 texCoord_;
  58. /// Tangent.
  59. Vector4 tangent_;
  60. /// Blend weights.
  61. float blendWeights_[4];
  62. /// Blend indices.
  63. unsigned char blendIndices_[4];
  64. };
  65. /// One decal in a decal set.
  66. struct Decal
  67. {
  68. /// Construct with defaults.
  69. Decal() :
  70. timer_(0.0f),
  71. timeToLive_(0.0f)
  72. {
  73. }
  74. /// Add a vertex.
  75. void AddVertex(const DecalVertex& vertex);
  76. /// Calculate local-space bounding box.
  77. void CalculateBoundingBox();
  78. /// Decal age timer.
  79. float timer_;
  80. /// Maximum time to live in seconds (0 = infinite)
  81. float timeToLive_;
  82. /// Local-space bounding box.
  83. BoundingBox boundingBox_;
  84. /// Decal vertices.
  85. PODVector<DecalVertex> vertices_;
  86. /// Decal indices.
  87. PODVector<unsigned short> indices_;
  88. };
  89. /// %Decal renderer component.
  90. class DecalSet : public Drawable
  91. {
  92. OBJECT(DecalSet);
  93. /// Construct.
  94. DecalSet(Context* context);
  95. /// Destruct.
  96. virtual ~DecalSet();
  97. /// Register object factory.
  98. static void RegisterObject(Context* context);
  99. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  100. virtual void ApplyAttributes();
  101. /// Process octree raycast. May be called from a worker thread.
  102. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  103. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  104. virtual void UpdateBatches(const FrameInfo& frame);
  105. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  106. virtual void UpdateGeometry(const FrameInfo& frame);
  107. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  108. virtual UpdateGeometryType GetUpdateGeometryType();
  109. /// %Set material.
  110. void SetMaterial(Material* material);
  111. /// %Set maximum number of decal vertices.
  112. void SetMaxVertices(unsigned num);
  113. /// %Set maximum number of decal vertex indices.
  114. void SetMaxIndices(unsigned num);
  115. /// Add a decal at world coordinates, using an existing drawable's geometry for reference. Return true if successful.
  116. bool AddDecal(Drawable* target, const Vector3& worldPosition, const Quaternion& worldRotation, float size, float aspectRatio, float depth, const Vector2& topLeftUV, const Vector2& bottomRightUV, float timeToLive = 0.0f, float normalCutoff = 0.1f, unsigned subGeometry = M_MAX_UNSIGNED);
  117. /// Remove n oldest decals.
  118. void RemoveDecals(unsigned num);
  119. /// Remove all decals.
  120. void RemoveAllDecals();
  121. /// Return material.
  122. Material* GetMaterial() const;
  123. /// Return number of decals.
  124. unsigned GetNumDecals() const { return decals_.Size(); }
  125. /// Retur number of vertices in the decals.
  126. unsigned GetNumVertices() const { return numVertices_; }
  127. /// Retur number of vertex indices in the decals.
  128. unsigned GetNumIndices() const { return numIndices_; }
  129. /// Return maximum number of decal vertices.
  130. unsigned GetMaxVertices() const { return maxVertices_; }
  131. /// Return maximum number of decal vertex indices.
  132. unsigned GetMaxIndices() const { return maxIndices_; }
  133. /// %Set material attribute.
  134. void SetMaterialAttr(ResourceRef value);
  135. /// %Set decals attribute.
  136. void SetDecalsAttr(VariantVector value);
  137. /// Return material attribute.
  138. ResourceRef GetMaterialAttr() const;
  139. /// Return decals attribute.
  140. VariantVector GetDecalsAttr() const;
  141. protected:
  142. /// Recalculate the world-space bounding box.
  143. virtual void OnWorldBoundingBoxUpdate();
  144. /// Handle node transform being dirtied.
  145. virtual void OnMarkedDirty(Node* node);
  146. private:
  147. /// Get triangle faces from the target geometry.
  148. void GetFaces(Vector<PODVector<DecalVertex> >& faces, Drawable* target, unsigned batchIndex, const Frustum& frustum, const Vector3& decalNormal, float normalCutoff);
  149. /// Get triangle face from the target geometry.
  150. void GetFace(Vector<PODVector<DecalVertex> >& faces, Drawable* target, unsigned batchIndex, unsigned i0, unsigned i1, unsigned i2, const unsigned char* positionData, const unsigned char* normalData, const unsigned char* skinningData, unsigned positionStride, unsigned normalStride, unsigned skinningStride, const Frustum& frustum, const Vector3& decalNormal, float normalCutoff);
  151. /// Get bones referenced by skinning data and remap the skinning indices. Return true if successful.
  152. bool GetBones(Drawable* target, unsigned batchIndex, const float* blendWeights, const unsigned char* blendIndices, unsigned char* newBlendIndices);
  153. /// Calculate UV coordinates for the decal.
  154. void CalculateUVs(Decal& decal, const Matrix3x4& view, const Matrix4& projection, const Vector2& topLeftUV, const Vector2& bottomRightUV);
  155. /// Transform decal's vertices from the target geometry to the decal set local space.
  156. void TransformVertices(Decal& decal, const Matrix3x4& transform);
  157. /// Remove a decal by iterator and return iterator to the next decal.
  158. List<Decal>::Iterator RemoveDecal(List<Decal>::Iterator i);
  159. /// Mark decals and the bounding box dirty.
  160. void MarkDecalsDirty();
  161. /// Recalculate the local-space bounding box.
  162. void CalculateBoundingBox();
  163. /// Resize decal vertex and index buffers.
  164. void UpdateBufferSize();
  165. /// Rewrite decal vertex and index buffers.
  166. void UpdateBuffers();
  167. /// Recalculate skinning.
  168. void UpdateSkinning();
  169. /// Update the batch (geometry type, shader data.)
  170. void UpdateBatch();
  171. /// Find bones after loading.
  172. void AssignBoneNodes();
  173. /// Handle scene post-update event.
  174. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  175. /// Geometry.
  176. SharedPtr<Geometry> geometry_;
  177. /// Vertex buffer.
  178. SharedPtr<VertexBuffer> vertexBuffer_;
  179. /// Index buffer.
  180. SharedPtr<IndexBuffer> indexBuffer_;
  181. /// Decals.
  182. List<Decal> decals_;
  183. /// Bones used for skinned decals.
  184. Vector<Bone> bones_;
  185. /// Skinning matrices.
  186. PODVector<Matrix3x4> skinMatrices_;
  187. /// Local-space bounding box.
  188. BoundingBox boundingBox_;
  189. /// Vertices in the current decals.
  190. unsigned numVertices_;
  191. /// Indices in the current decals.
  192. unsigned numIndices_;
  193. /// Maximum vertices.
  194. unsigned maxVertices_;
  195. /// Maximum indices.
  196. unsigned maxIndices_;
  197. /// Skinned mode flag.
  198. bool skinned_;
  199. /// Vertex buffer needs resize flag.
  200. bool bufferSizeDirty_;
  201. /// Vertex buffer needs rewrite flag.
  202. bool bufferDirty_;
  203. /// Bounding box needs update flag.
  204. bool boundingBoxDirty_;
  205. /// Skinning dirty flag.
  206. bool skinningDirty_;
  207. /// Bone nodes assignment pending flag.
  208. bool assignBonesPending_;
  209. };