Drawable2D.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "Camera.h"
  24. #include "Context.h"
  25. #include "Drawable2D.h"
  26. #include "Geometry.h"
  27. #include "Log.h"
  28. #include "Material.h"
  29. #include "MaterialCache2D.h"
  30. #include "Node.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "Sprite2D.h"
  34. #include "SpriteSheet2D.h"
  35. #include "Technique.h"
  36. #include "Texture2D.h"
  37. #include "VertexBuffer.h"
  38. #include "DebugNew.h"
  39. namespace Urho3D
  40. {
  41. const float PIXEL_SIZE = 0.01f;
  42. extern const char* blendModeNames[];
  43. Drawable2D::Drawable2D(Context* context) :
  44. Drawable(context, DRAWABLE_GEOMETRY),
  45. layer_(0),
  46. orderInLayer_(0),
  47. blendMode_(BLEND_ALPHA),
  48. vertexBuffer_(new VertexBuffer(context_)),
  49. verticesDirty_(true),
  50. geometryDirty_(true),
  51. materialUpdatePending_(false)
  52. {
  53. geometry_ = new Geometry(context);
  54. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  55. batches_.Resize(1);
  56. batches_[0].geometry_ = geometry_;
  57. }
  58. Drawable2D::~Drawable2D()
  59. {
  60. }
  61. void Drawable2D::RegisterObject(Context* context)
  62. {
  63. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_INT, "Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  64. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_INT, "Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  65. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  66. ENUM_ACCESSOR_ATTRIBUTE(Drawable2D, "Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  67. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  68. COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
  69. }
  70. void Drawable2D::ApplyAttributes()
  71. {
  72. if (materialUpdatePending_)
  73. {
  74. materialUpdatePending_ = false;
  75. UpdateMaterial();
  76. }
  77. }
  78. void Drawable2D::UpdateBatches(const FrameInfo& frame)
  79. {
  80. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  81. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  82. batches_[0].distance_ = distance_;
  83. batches_[0].worldTransform_ = &worldTransform;
  84. }
  85. void Drawable2D::UpdateGeometry(const FrameInfo& frame)
  86. {
  87. if (verticesDirty_)
  88. UpdateVertices();
  89. if (geometryDirty_ || vertexBuffer_->IsDataLost())
  90. {
  91. unsigned vertexCount = vertices_.Size() / 4 * 6;
  92. if (vertexCount)
  93. {
  94. vertexBuffer_->SetSize(vertexCount, MASK_VERTEX2D);
  95. Vertex2D* dest = reinterpret_cast<Vertex2D*>(vertexBuffer_->Lock(0, vertexCount, true));
  96. if (dest)
  97. {
  98. for (unsigned i = 0; i < vertices_.Size(); i += 4)
  99. {
  100. dest[0] = vertices_[i + 0];
  101. dest[1] = vertices_[i + 1];
  102. dest[2] = vertices_[i + 2];
  103. dest[3] = vertices_[i + 0];
  104. dest[4] = vertices_[i + 2];
  105. dest[5] = vertices_[i + 3];
  106. dest += 6;
  107. }
  108. vertexBuffer_->Unlock();
  109. }
  110. else
  111. LOGERROR("Failed to lock vertex buffer");
  112. }
  113. geometry_->SetDrawRange(TRIANGLE_LIST, 0, 0, 0, vertexCount);
  114. vertexBuffer_->ClearDataLost();
  115. geometryDirty_ = false;
  116. }
  117. }
  118. UpdateGeometryType Drawable2D::GetUpdateGeometryType()
  119. {
  120. if (geometryDirty_ || vertexBuffer_->IsDataLost())
  121. return UPDATE_MAIN_THREAD;
  122. else
  123. return UPDATE_NONE;
  124. }
  125. void Drawable2D::SetLayer(int layer)
  126. {
  127. if (layer == layer_)
  128. return;
  129. layer_ = layer;
  130. MarkNetworkUpdate();
  131. }
  132. void Drawable2D::SetOrderInLayer(int orderInLayer)
  133. {
  134. if (orderInLayer == orderInLayer_)
  135. return;
  136. orderInLayer_ = orderInLayer;
  137. MarkNetworkUpdate();
  138. }
  139. void Drawable2D::SetSprite(Sprite2D* sprite)
  140. {
  141. if (sprite == sprite_)
  142. return;
  143. sprite_ = sprite;
  144. MarkDirty();
  145. UpdateMaterial();
  146. MarkNetworkUpdate();
  147. }
  148. void Drawable2D::SetBlendMode(BlendMode mode)
  149. {
  150. if (mode == blendMode_)
  151. return;
  152. blendMode_ = mode;
  153. UpdateMaterial();
  154. MarkNetworkUpdate();
  155. }
  156. void Drawable2D::SetMaterial(Material* material)
  157. {
  158. if (material == material_)
  159. return;
  160. material_ = material;
  161. UpdateMaterial();
  162. MarkNetworkUpdate();
  163. }
  164. Material* Drawable2D::GetMaterial() const
  165. {
  166. return material_;
  167. }
  168. void Drawable2D::MarkDirty(bool markWorldBoundingBoxDirty)
  169. {
  170. verticesDirty_ = true;
  171. geometryDirty_ = true;
  172. if (markWorldBoundingBoxDirty)
  173. OnMarkedDirty(node_);
  174. }
  175. void Drawable2D::SetSpriteAttr(ResourceRef value)
  176. {
  177. // Delay applying material update
  178. materialUpdatePending_ = true;
  179. ResourceCache* cache = GetSubsystem<ResourceCache>();
  180. if (value.type_ == Sprite2D::GetTypeStatic())
  181. {
  182. SetSprite(cache->GetResource<Sprite2D>(value.name_));
  183. return;
  184. }
  185. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  186. {
  187. // value.name_ include sprite speet name and sprite name.
  188. Vector<String> names = value.name_.Split('@');
  189. if (names.Size() != 2)
  190. return;
  191. const String& spriteSheetName = names[0];
  192. const String& spriteName = names[1];
  193. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  194. if (!spriteSheet)
  195. return;
  196. SetSprite(spriteSheet->GetSprite(spriteName));
  197. }
  198. }
  199. ResourceRef Drawable2D::GetSpriteAttr() const
  200. {
  201. SpriteSheet2D* spriteSheet = 0;
  202. if (sprite_)
  203. spriteSheet = sprite_->GetSpriteSheet();
  204. if (!spriteSheet)
  205. return GetResourceRef(sprite_, Sprite2D::GetTypeStatic());
  206. // Combine sprite sheet name and sprite name as resource name.
  207. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite_->GetName());
  208. }
  209. void Drawable2D::SetBlendModeAttr(BlendMode mode)
  210. {
  211. // Delay applying material update
  212. materialUpdatePending_ = true;
  213. SetBlendMode(mode);
  214. }
  215. void Drawable2D::SetMaterialAttr(ResourceRef value)
  216. {
  217. // Delay applying material update
  218. materialUpdatePending_ = true;
  219. ResourceCache* cache = GetSubsystem<ResourceCache>();
  220. SetMaterial(cache->GetResource<Material>(value.name_));
  221. }
  222. ResourceRef Drawable2D::GetMaterialAttr() const
  223. {
  224. return GetResourceRef(material_, Material::GetTypeStatic());
  225. }
  226. void Drawable2D::OnNodeSet(Node* node)
  227. {
  228. Drawable::OnNodeSet(node);
  229. if (node)
  230. {
  231. node->AddListener(this);
  232. Scene* scene = GetScene();
  233. if (scene)
  234. {
  235. materialCache_ = scene->GetOrCreateComponent<MaterialCache2D>();
  236. }
  237. }
  238. }
  239. void Drawable2D::OnWorldBoundingBoxUpdate()
  240. {
  241. if (verticesDirty_)
  242. {
  243. UpdateVertices();
  244. boundingBox_.Clear();
  245. for (unsigned i = 0; i < vertices_.Size(); ++i)
  246. boundingBox_.Merge(vertices_[i].position_);
  247. }
  248. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  249. }
  250. void Drawable2D::UpdateMaterial()
  251. {
  252. // Delay the material update
  253. if (materialUpdatePending_)
  254. return;
  255. if (material_)
  256. batches_[0].material_ = material_;
  257. else
  258. {
  259. defaultMaterial_ = materialCache_->GetMaterial(GetTexture(), blendMode_);
  260. batches_[0].material_ = defaultMaterial_;
  261. }
  262. }
  263. }