Drawable2D.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "DrawableProxy2D.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Material.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. zValue_(0.0f),
  46. blendMode_(BLEND_ALPHA),
  47. vertexBuffer_(new VertexBuffer(context_)),
  48. verticesDirty_(true),
  49. geometryDirty_(true),
  50. materialUpdatePending_(false)
  51. {
  52. geometry_ = new Geometry(context);
  53. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  54. batches_.Resize(1);
  55. batches_[0].geometry_ = geometry_;
  56. }
  57. Drawable2D::~Drawable2D()
  58. {
  59. if (drawableProxy_)
  60. drawableProxy_->RemoveDrawable(this);
  61. }
  62. void Drawable2D::RegisterObject(Context* context)
  63. {
  64. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_FLOAT, "Z Value", GetZValue, SetZValue, float, 0.0f, AM_DEFAULT);
  65. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  66. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  67. ENUM_ACCESSOR_ATTRIBUTE(Drawable2D, "Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, BLEND_ALPHA, 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::SetSprite(Sprite2D* sprite)
  126. {
  127. if (sprite == sprite_)
  128. return;
  129. sprite_ = sprite;
  130. MarkDirty();
  131. UpdateMaterial();
  132. MarkNetworkUpdate();
  133. }
  134. void Drawable2D::SetMaterial(Material* material)
  135. {
  136. if (material == material_)
  137. return;
  138. material_ = material;
  139. UpdateMaterial();
  140. MarkNetworkUpdate();
  141. }
  142. void Drawable2D::SetBlendMode(BlendMode mode)
  143. {
  144. if (mode == blendMode_)
  145. return;
  146. blendMode_ = mode;
  147. UpdateMaterial();
  148. MarkNetworkUpdate();
  149. }
  150. void Drawable2D::SetZValue(float zValue)
  151. {
  152. if (zValue == zValue_)
  153. return;
  154. zValue_ = zValue;
  155. MarkDirty();
  156. MarkNetworkUpdate();
  157. }
  158. Material* Drawable2D::GetMaterial() const
  159. {
  160. return material_;
  161. }
  162. void Drawable2D::MarkDirty(bool markWorldBoundingBoxDirty)
  163. {
  164. verticesDirty_ = true;
  165. geometryDirty_ = true;
  166. if (markWorldBoundingBoxDirty)
  167. OnMarkedDirty(node_);
  168. }
  169. void Drawable2D::SetSpriteAttr(ResourceRef value)
  170. {
  171. // Delay applying material update
  172. materialUpdatePending_ = true;
  173. ResourceCache* cache = GetSubsystem<ResourceCache>();
  174. if (value.type_ == Sprite2D::GetTypeStatic())
  175. {
  176. SetSprite(cache->GetResource<Sprite2D>(value.name_));
  177. return;
  178. }
  179. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  180. {
  181. // value.name_ include sprite speet name and sprite name.
  182. Vector<String> names = value.name_.Split('@');
  183. if (names.Size() != 2)
  184. return;
  185. const String& spriteSheetName = names[0];
  186. const String& spriteName = names[1];
  187. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  188. if (!spriteSheet)
  189. return;
  190. SetSprite(spriteSheet->GetSprite(spriteName));
  191. }
  192. }
  193. ResourceRef Drawable2D::GetSpriteAttr() const
  194. {
  195. SpriteSheet2D* spriteSheet = 0;
  196. if (sprite_)
  197. spriteSheet = sprite_->GetSpriteSheet();
  198. if (!spriteSheet)
  199. return GetResourceRef(sprite_, Sprite2D::GetTypeStatic());
  200. // Combine sprite sheet name and sprite name as resource name.
  201. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite_->GetName());
  202. }
  203. void Drawable2D::SetMaterialAttr(ResourceRef value)
  204. {
  205. // Delay applying material update
  206. materialUpdatePending_ = true;
  207. ResourceCache* cache = GetSubsystem<ResourceCache>();
  208. SetMaterial(cache->GetResource<Material>(value.name_));
  209. }
  210. ResourceRef Drawable2D::GetMaterialAttr() const
  211. {
  212. return GetResourceRef(material_, Material::GetTypeStatic());
  213. }
  214. void Drawable2D::SetBlendModeAttr(BlendMode mode)
  215. {
  216. // Delay applying material update
  217. materialUpdatePending_ = true;
  218. SetBlendMode(mode);
  219. }
  220. void Drawable2D::OnNodeSet(Node* node)
  221. {
  222. Drawable::OnNodeSet(node);
  223. if (node)
  224. {
  225. drawableProxy_ = GetScene()->GetOrCreateComponent<DrawableProxy2D>();
  226. drawableProxy_->AddDrawable(this);
  227. }
  228. }
  229. void Drawable2D::OnWorldBoundingBoxUpdate()
  230. {
  231. if (verticesDirty_)
  232. {
  233. UpdateVertices();
  234. boundingBox_.Clear();
  235. for (unsigned i = 0; i < vertices_.Size(); ++i)
  236. boundingBox_.Merge(vertices_[i].position_);
  237. }
  238. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  239. }
  240. void Drawable2D::UpdateMaterial()
  241. {
  242. // Delay the material update
  243. if (materialUpdatePending_)
  244. return;
  245. if (material_)
  246. batches_[0].material_ = material_;
  247. else
  248. batches_[0].material_ = drawableProxy_->GetMaterial(this);
  249. }
  250. }