Drawable2D.cpp 9.5 KB

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