Drawable2D.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. unitPerPixel_(1.0f),
  43. blendMode_(BLEND_REPLACE),
  44. zValue_(0.0f),
  45. vertexBuffer_(new VertexBuffer(context_)),
  46. verticesDirty_(true),
  47. geometryDirty_(true),
  48. materialUpdatePending_(false)
  49. {
  50. geometry_ = new Geometry(context);
  51. batches_.Resize(1);
  52. batches_[0].geometry_ = geometry_;
  53. CreateDefaultMaterial();
  54. }
  55. Drawable2D::~Drawable2D()
  56. {
  57. }
  58. void Drawable2D::RegisterObject(Context* context)
  59. {
  60. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_FLOAT, "Unit Per Pixel", GetUnitPerPixel, SetUnitPerPixel, float, 1.0f, AM_DEFAULT);
  61. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  62. ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  63. ENUM_ACCESSOR_ATTRIBUTE(Drawable2D, "Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, 0, AM_DEFAULT);
  64. COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
  65. }
  66. void Drawable2D::ApplyAttributes()
  67. {
  68. if (materialUpdatePending_)
  69. {
  70. materialUpdatePending_ = false;
  71. UpdateMaterial();
  72. }
  73. }
  74. void Drawable2D::UpdateBatches(const FrameInfo& frame)
  75. {
  76. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  77. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  78. batches_[0].distance_ = distance_;
  79. batches_[0].worldTransform_ = &worldTransform;
  80. }
  81. void Drawable2D::UpdateGeometry(const FrameInfo& frame)
  82. {
  83. if (!geometryDirty_)
  84. return;
  85. if (verticesDirty_)
  86. UpdateVertices();
  87. if (vertices_.Size() > 0)
  88. {
  89. unsigned vertexCount = vertices_.Size() / 4 * 6;
  90. vertexBuffer_->SetSize(vertexCount, MASK_VERTEX2D);
  91. Vertex2D* dest = (Vertex2D*)vertexBuffer_->Lock(0, vertexCount, true);
  92. if (dest)
  93. {
  94. for (unsigned i = 0; i < vertices_.Size(); i += 4)
  95. {
  96. *(dest++) = vertices_[i + 0];
  97. *(dest++) = vertices_[i + 1];
  98. *(dest++) = vertices_[i + 2];
  99. *(dest++) = vertices_[i + 0];
  100. *(dest++) = vertices_[i + 2];
  101. *(dest++) = vertices_[i + 3];
  102. }
  103. vertexBuffer_->Unlock();
  104. }
  105. else
  106. LOGERROR("Failed to lock vertex buffer");
  107. if (geometry_->GetVertexBuffer(0) != vertexBuffer_)
  108. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  109. geometry_->SetDrawRange(TRIANGLE_LIST, 0, 0, 0, vertexCount);
  110. }
  111. else
  112. {
  113. if (geometry_->GetVertexBuffer(0) != vertexBuffer_)
  114. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  115. geometry_->SetDrawRange(TRIANGLE_LIST, 0, 0, 0, 0);
  116. }
  117. vertexBuffer_->ClearDataLost();
  118. geometryDirty_ = false;
  119. }
  120. UpdateGeometryType Drawable2D::GetUpdateGeometryType()
  121. {
  122. if (geometryDirty_ || vertexBuffer_->IsDataLost())
  123. return UPDATE_MAIN_THREAD;
  124. else
  125. return UPDATE_NONE;
  126. }
  127. void Drawable2D::SetUnitPerPixel(float unitPerPixel)
  128. {
  129. unitPerPixel_ = Max(1.0f, unitPerPixel);
  130. OnMarkedDirty(node_);
  131. MarkNetworkUpdate();
  132. }
  133. void Drawable2D::SetSprite(Sprite2D* sprite)
  134. {
  135. if (sprite == sprite_)
  136. return;
  137. sprite_ = sprite;
  138. OnMarkedDirty(node_);
  139. UpdateMaterial();
  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. OnMarkedDirty(node_);
  170. MarkNetworkUpdate();
  171. }
  172. Material* Drawable2D::GetMaterial() const
  173. {
  174. return material_;
  175. }
  176. void Drawable2D::SetSpriteAttr(ResourceRef value)
  177. {
  178. // Delay applying material update
  179. materialUpdatePending_ = true;
  180. ResourceCache* cache = GetSubsystem<ResourceCache>();
  181. if (value.type_ == Sprite2D::GetTypeStatic())
  182. {
  183. SetSprite(cache->GetResource<Sprite2D>(value.name_));
  184. return;
  185. }
  186. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  187. {
  188. // value.name_ include sprite speet name and sprite name.
  189. Vector<String> names = value.name_.Split('@');
  190. if (names.Size() != 2)
  191. return;
  192. const String& spriteSheetName = names[0];
  193. const String& spriteName = names[1];
  194. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  195. if (!spriteSheet)
  196. return;
  197. SetSprite(spriteSheet->GetSprite(spriteName));
  198. }
  199. }
  200. ResourceRef Drawable2D::GetSpriteAttr() const
  201. {
  202. SpriteSheet2D* spriteSheet = 0;
  203. if (sprite_)
  204. spriteSheet = sprite_->GetSpriteSheet();
  205. if (!spriteSheet)
  206. return GetResourceRef(sprite_, Sprite2D::GetTypeStatic());
  207. // Combine sprite sheet name and sprite name as resource name.
  208. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite_->GetName());
  209. }
  210. void Drawable2D::SetMaterialAttr(ResourceRef value)
  211. {
  212. // Delay applying material update
  213. materialUpdatePending_ = true;
  214. ResourceCache* cache = GetSubsystem<ResourceCache>();
  215. SetMaterial(cache->GetResource<Material>(value.name_));
  216. }
  217. ResourceRef Drawable2D::GetMaterialAttr() const
  218. {
  219. return GetResourceRef(material_, Material::GetTypeStatic());
  220. }
  221. void Drawable2D::SetBlendModeAttr(BlendMode mode)
  222. {
  223. // Delay applying material update
  224. materialUpdatePending_ = true;
  225. SetBlendMode(mode);
  226. }
  227. void Drawable2D::OnMarkedDirty(Node* node)
  228. {
  229. Drawable::OnMarkedDirty(node);
  230. verticesDirty_ = true;
  231. geometryDirty_ = true;
  232. }
  233. void Drawable2D::OnWorldBoundingBoxUpdate()
  234. {
  235. if (verticesDirty_)
  236. {
  237. UpdateVertices();
  238. boundingBox_.Clear();
  239. for (unsigned i = 0; i < vertices_.Size(); ++i)
  240. boundingBox_.Merge(vertices_[i].position_);
  241. }
  242. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  243. }
  244. void Drawable2D::CreateDefaultMaterial()
  245. {
  246. SharedPtr<Material> material(new Material(context_));
  247. Technique* tech = new Technique(context_);
  248. Pass* pass = tech->CreatePass(PASS_ALPHA);
  249. pass->SetVertexShader("Basic");
  250. pass->SetVertexShaderDefines("DIFFMAP VERTEXCOLOR");
  251. pass->SetPixelShader("Basic");
  252. pass->SetPixelShaderDefines("DIFFMAP VERTEXCOLOR");
  253. pass->SetDepthWrite(false);
  254. material->SetTechnique(0, tech);
  255. material->SetCullMode(CULL_NONE);
  256. batches_[0].material_ = material;
  257. }
  258. void Drawable2D::UpdateMaterial()
  259. {
  260. // Delay the material update
  261. if (materialUpdatePending_)
  262. return;
  263. Material* material = batches_[0].material_;
  264. assert(material);
  265. // If we are using the default created material, we can update blend mode. Otherwise must respect what is set in the material
  266. if (!material_)
  267. {
  268. Technique* technique = material->GetTechnique(0);
  269. Pass* pass = technique->GetPass(PASS_ALPHA);
  270. if (pass)
  271. pass->SetBlendMode(blendMode_);
  272. }
  273. // Update diffuse texture from sprite
  274. if (sprite_)
  275. {
  276. Texture2D* texture = sprite_->GetTexture();
  277. material->SetTexture(TU_DIFFUSE, texture);
  278. }
  279. }
  280. }