Drawable2D.cpp 9.2 KB

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