UIBatch.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Graphics.h"
  24. #include "../Graphics/Texture.h"
  25. #include "../UI/UIElement.h"
  26. #include "../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. Vector3 UIBatch::posAdjust(0.0f, 0.0f, 0.0f);
  30. UIBatch::UIBatch()
  31. {
  32. SetDefaultColor();
  33. }
  34. UIBatch::UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, PODVector<float>* vertexData) : // NOLINT(modernize-pass-by-value)
  35. element_(element),
  36. blendMode_(blendMode),
  37. scissor_(scissor),
  38. texture_(texture),
  39. invTextureSize_(texture ? Vector2(1.0f / (float)texture->GetWidth(), 1.0f / (float)texture->GetHeight()) : Vector2::ONE),
  40. vertexData_(vertexData),
  41. vertexStart_(vertexData->Size()),
  42. vertexEnd_(vertexData->Size())
  43. {
  44. SetDefaultColor();
  45. }
  46. void UIBatch::SetColor(const Color& color, bool overrideAlpha)
  47. {
  48. if (!element_)
  49. overrideAlpha = true;
  50. useGradient_ = false;
  51. color_ =
  52. overrideAlpha ? color.ToUInt() : Color(color.r_, color.g_, color.b_, color.a_ * element_->GetDerivedOpacity()).ToUInt();
  53. }
  54. void UIBatch::SetDefaultColor()
  55. {
  56. if (element_)
  57. {
  58. color_ = element_->GetDerivedColor().ToUInt();
  59. useGradient_ = element_->HasColorGradient();
  60. }
  61. else
  62. {
  63. color_ = 0xffffffff;
  64. useGradient_ = false;
  65. }
  66. }
  67. void UIBatch::AddQuad(float x, float y, float width, float height, int texOffsetX, int texOffsetY, int texWidth, int texHeight)
  68. {
  69. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  70. if (!useGradient_)
  71. {
  72. // If alpha is 0, nothing will be rendered, so do not add the quad
  73. if (!(color_ & 0xff000000))
  74. return;
  75. topLeftColor = color_;
  76. topRightColor = color_;
  77. bottomLeftColor = color_;
  78. bottomRightColor = color_;
  79. }
  80. else
  81. {
  82. topLeftColor = GetInterpolatedColor(x, y);
  83. topRightColor = GetInterpolatedColor(x + width, y);
  84. bottomLeftColor = GetInterpolatedColor(x, y + height);
  85. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  86. }
  87. const IntVector2& screenPos = element_->GetScreenPosition();
  88. float left = x + screenPos.x_ - posAdjust.x_;
  89. float right = left + width;
  90. float top = y + screenPos.y_ - posAdjust.x_;
  91. float bottom = top + height;
  92. float leftUV = texOffsetX * invTextureSize_.x_;
  93. float topUV = texOffsetY * invTextureSize_.y_;
  94. float rightUV = (texOffsetX + (texWidth ? texWidth : width)) * invTextureSize_.x_;
  95. float bottomUV = (texOffsetY + (texHeight ? texHeight : height)) * invTextureSize_.y_;
  96. unsigned begin = vertexData_->Size();
  97. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  98. float* dest = &(vertexData_->At(begin));
  99. vertexEnd_ = vertexData_->Size();
  100. dest[0] = left;
  101. dest[1] = top;
  102. dest[2] = 0.0f;
  103. ((unsigned&)dest[3]) = topLeftColor;
  104. dest[4] = leftUV;
  105. dest[5] = topUV;
  106. dest[6] = right;
  107. dest[7] = top;
  108. dest[8] = 0.0f;
  109. ((unsigned&)dest[9]) = topRightColor;
  110. dest[10] = rightUV;
  111. dest[11] = topUV;
  112. dest[12] = left;
  113. dest[13] = bottom;
  114. dest[14] = 0.0f;
  115. ((unsigned&)dest[15]) = bottomLeftColor;
  116. dest[16] = leftUV;
  117. dest[17] = bottomUV;
  118. dest[18] = right;
  119. dest[19] = top;
  120. dest[20] = 0.0f;
  121. ((unsigned&)dest[21]) = topRightColor;
  122. dest[22] = rightUV;
  123. dest[23] = topUV;
  124. dest[24] = right;
  125. dest[25] = bottom;
  126. dest[26] = 0.0f;
  127. ((unsigned&)dest[27]) = bottomRightColor;
  128. dest[28] = rightUV;
  129. dest[29] = bottomUV;
  130. dest[30] = left;
  131. dest[31] = bottom;
  132. dest[32] = 0.0f;
  133. ((unsigned&)dest[33]) = bottomLeftColor;
  134. dest[34] = leftUV;
  135. dest[35] = bottomUV;
  136. }
  137. void UIBatch::AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY,
  138. int texWidth, int texHeight)
  139. {
  140. unsigned topLeftColor, topRightColor, bottomLeftColor, bottomRightColor;
  141. if (!useGradient_)
  142. {
  143. // If alpha is 0, nothing will be rendered, so do not add the quad
  144. if (!(color_ & 0xff000000))
  145. return;
  146. topLeftColor = color_;
  147. topRightColor = color_;
  148. bottomLeftColor = color_;
  149. bottomRightColor = color_;
  150. }
  151. else
  152. {
  153. topLeftColor = GetInterpolatedColor(x, y);
  154. topRightColor = GetInterpolatedColor(x + width, y);
  155. bottomLeftColor = GetInterpolatedColor(x, y + height);
  156. bottomRightColor = GetInterpolatedColor(x + width, y + height);
  157. }
  158. Vector3 v1 = (transform * Vector3((float)x, (float)y, 0.0f)) - posAdjust;
  159. Vector3 v2 = (transform * Vector3((float)x + (float)width, (float)y, 0.0f)) - posAdjust;
  160. Vector3 v3 = (transform * Vector3((float)x, (float)y + (float)height, 0.0f)) - posAdjust;
  161. Vector3 v4 = (transform * Vector3((float)x + (float)width, (float)y + (float)height, 0.0f)) - posAdjust;
  162. float leftUV = ((float)texOffsetX) * invTextureSize_.x_;
  163. float topUV = ((float)texOffsetY) * invTextureSize_.y_;
  164. float rightUV = ((float)(texOffsetX + (texWidth ? texWidth : width))) * invTextureSize_.x_;
  165. float bottomUV = ((float)(texOffsetY + (texHeight ? texHeight : height))) * invTextureSize_.y_;
  166. unsigned begin = vertexData_->Size();
  167. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  168. float* dest = &(vertexData_->At(begin));
  169. vertexEnd_ = vertexData_->Size();
  170. dest[0] = v1.x_;
  171. dest[1] = v1.y_;
  172. dest[2] = 0.0f;
  173. ((unsigned&)dest[3]) = topLeftColor;
  174. dest[4] = leftUV;
  175. dest[5] = topUV;
  176. dest[6] = v2.x_;
  177. dest[7] = v2.y_;
  178. dest[8] = 0.0f;
  179. ((unsigned&)dest[9]) = topRightColor;
  180. dest[10] = rightUV;
  181. dest[11] = topUV;
  182. dest[12] = v3.x_;
  183. dest[13] = v3.y_;
  184. dest[14] = 0.0f;
  185. ((unsigned&)dest[15]) = bottomLeftColor;
  186. dest[16] = leftUV;
  187. dest[17] = bottomUV;
  188. dest[18] = v2.x_;
  189. dest[19] = v2.y_;
  190. dest[20] = 0.0f;
  191. ((unsigned&)dest[21]) = topRightColor;
  192. dest[22] = rightUV;
  193. dest[23] = topUV;
  194. dest[24] = v4.x_;
  195. dest[25] = v4.y_;
  196. dest[26] = 0.0f;
  197. ((unsigned&)dest[27]) = bottomRightColor;
  198. dest[28] = rightUV;
  199. dest[29] = bottomUV;
  200. dest[30] = v3.x_;
  201. dest[31] = v3.y_;
  202. dest[32] = 0.0f;
  203. ((unsigned&)dest[33]) = bottomLeftColor;
  204. dest[34] = leftUV;
  205. dest[35] = bottomUV;
  206. }
  207. void UIBatch::AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled)
  208. {
  209. if (!(element_->HasColorGradient() || element_->GetDerivedColor().ToUInt() & 0xff000000))
  210. return; // No gradient and alpha is 0, so do not add the quad
  211. if (!tiled)
  212. {
  213. AddQuad(x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
  214. return;
  215. }
  216. int tileX = 0;
  217. int tileY = 0;
  218. int tileW = 0;
  219. int tileH = 0;
  220. while (tileY < height)
  221. {
  222. tileX = 0;
  223. tileH = Min(height - tileY, texHeight);
  224. while (tileX < width)
  225. {
  226. tileW = Min(width - tileX, texWidth);
  227. AddQuad(x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
  228. tileX += tileW;
  229. }
  230. tileY += tileH;
  231. }
  232. }
  233. void UIBatch::AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  234. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD)
  235. {
  236. Vector3 v1 = (transform * Vector3((float)a.x_, (float)a.y_, 0.0f)) - posAdjust;
  237. Vector3 v2 = (transform * Vector3((float)b.x_, (float)b.y_, 0.0f)) - posAdjust;
  238. Vector3 v3 = (transform * Vector3((float)c.x_, (float)c.y_, 0.0f)) - posAdjust;
  239. Vector3 v4 = (transform * Vector3((float)d.x_, (float)d.y_, 0.0f)) - posAdjust;
  240. Vector2 uv1((float)texA.x_ * invTextureSize_.x_, (float)texA.y_ * invTextureSize_.y_);
  241. Vector2 uv2((float)texB.x_ * invTextureSize_.x_, (float)texB.y_ * invTextureSize_.y_);
  242. Vector2 uv3((float)texC.x_ * invTextureSize_.x_, (float)texC.y_ * invTextureSize_.y_);
  243. Vector2 uv4((float)texD.x_ * invTextureSize_.x_, (float)texD.y_ * invTextureSize_.y_);
  244. unsigned begin = vertexData_->Size();
  245. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  246. float* dest = &(vertexData_->At(begin));
  247. vertexEnd_ = vertexData_->Size();
  248. dest[0] = v1.x_;
  249. dest[1] = v1.y_;
  250. dest[2] = 0.0f;
  251. ((unsigned&)dest[3]) = color_;
  252. dest[4] = uv1.x_;
  253. dest[5] = uv1.y_;
  254. dest[6] = v2.x_;
  255. dest[7] = v2.y_;
  256. dest[8] = 0.0f;
  257. ((unsigned&)dest[9]) = color_;
  258. dest[10] = uv2.x_;
  259. dest[11] = uv2.y_;
  260. dest[12] = v3.x_;
  261. dest[13] = v3.y_;
  262. dest[14] = 0.0f;
  263. ((unsigned&)dest[15]) = color_;
  264. dest[16] = uv3.x_;
  265. dest[17] = uv3.y_;
  266. dest[18] = v1.x_;
  267. dest[19] = v1.y_;
  268. dest[20] = 0.0f;
  269. ((unsigned&)dest[21]) = color_;
  270. dest[22] = uv1.x_;
  271. dest[23] = uv1.y_;
  272. dest[24] = v3.x_;
  273. dest[25] = v3.y_;
  274. dest[26] = 0.0f;
  275. ((unsigned&)dest[27]) = color_;
  276. dest[28] = uv3.x_;
  277. dest[29] = uv3.y_;
  278. dest[30] = v4.x_;
  279. dest[31] = v4.y_;
  280. dest[32] = 0.0f;
  281. ((unsigned&)dest[33]) = color_;
  282. dest[34] = uv4.x_;
  283. dest[35] = uv4.y_;
  284. }
  285. void UIBatch::AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  286. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD, const Color& colA,
  287. const Color& colB, const Color& colC, const Color& colD)
  288. {
  289. Vector3 v1 = (transform * Vector3((float)a.x_, (float)a.y_, 0.0f)) - posAdjust;
  290. Vector3 v2 = (transform * Vector3((float)b.x_, (float)b.y_, 0.0f)) - posAdjust;
  291. Vector3 v3 = (transform * Vector3((float)c.x_, (float)c.y_, 0.0f)) - posAdjust;
  292. Vector3 v4 = (transform * Vector3((float)d.x_, (float)d.y_, 0.0f)) - posAdjust;
  293. Vector2 uv1((float)texA.x_ * invTextureSize_.x_, (float)texA.y_ * invTextureSize_.y_);
  294. Vector2 uv2((float)texB.x_ * invTextureSize_.x_, (float)texB.y_ * invTextureSize_.y_);
  295. Vector2 uv3((float)texC.x_ * invTextureSize_.x_, (float)texC.y_ * invTextureSize_.y_);
  296. Vector2 uv4((float)texD.x_ * invTextureSize_.x_, (float)texD.y_ * invTextureSize_.y_);
  297. unsigned c1 = colA.ToUInt();
  298. unsigned c2 = colB.ToUInt();
  299. unsigned c3 = colC.ToUInt();
  300. unsigned c4 = colD.ToUInt();
  301. unsigned begin = vertexData_->Size();
  302. vertexData_->Resize(begin + 6 * UI_VERTEX_SIZE);
  303. float* dest = &(vertexData_->At(begin));
  304. vertexEnd_ = vertexData_->Size();
  305. dest[0] = v1.x_;
  306. dest[1] = v1.y_;
  307. dest[2] = 0.0f;
  308. ((unsigned&)dest[3]) = c1;
  309. dest[4] = uv1.x_;
  310. dest[5] = uv1.y_;
  311. dest[6] = v2.x_;
  312. dest[7] = v2.y_;
  313. dest[8] = 0.0f;
  314. ((unsigned&)dest[9]) = c2;
  315. dest[10] = uv2.x_;
  316. dest[11] = uv2.y_;
  317. dest[12] = v3.x_;
  318. dest[13] = v3.y_;
  319. dest[14] = 0.0f;
  320. ((unsigned&)dest[15]) = c3;
  321. dest[16] = uv3.x_;
  322. dest[17] = uv3.y_;
  323. dest[18] = v1.x_;
  324. dest[19] = v1.y_;
  325. dest[20] = 0.0f;
  326. ((unsigned&)dest[21]) = c1;
  327. dest[22] = uv1.x_;
  328. dest[23] = uv1.y_;
  329. dest[24] = v3.x_;
  330. dest[25] = v3.y_;
  331. dest[26] = 0.0f;
  332. ((unsigned&)dest[27]) = c3;
  333. dest[28] = uv3.x_;
  334. dest[29] = uv3.y_;
  335. dest[30] = v4.x_;
  336. dest[31] = v4.y_;
  337. dest[32] = 0.0f;
  338. ((unsigned&)dest[33]) = c4;
  339. dest[34] = uv4.x_;
  340. dest[35] = uv4.y_;
  341. }
  342. bool UIBatch::Merge(const UIBatch& batch)
  343. {
  344. if (batch.blendMode_ != blendMode_ ||
  345. batch.scissor_ != scissor_ ||
  346. batch.texture_ != texture_ ||
  347. batch.vertexData_ != vertexData_ ||
  348. batch.vertexStart_ != vertexEnd_)
  349. return false;
  350. vertexEnd_ = batch.vertexEnd_;
  351. return true;
  352. }
  353. unsigned UIBatch::GetInterpolatedColor(float x, float y)
  354. {
  355. const IntVector2& size = element_->GetSize();
  356. if (size.x_ && size.y_)
  357. {
  358. float cLerpX = Clamp(x / (float)size.x_, 0.0f, 1.0f);
  359. float cLerpY = Clamp(y / (float)size.y_, 0.0f, 1.0f);
  360. Color topColor = element_->GetColor(C_TOPLEFT).Lerp(element_->GetColor(C_TOPRIGHT), cLerpX);
  361. Color bottomColor = element_->GetColor(C_BOTTOMLEFT).Lerp(element_->GetColor(C_BOTTOMRIGHT), cLerpX);
  362. Color color = topColor.Lerp(bottomColor, cLerpY);
  363. color.a_ *= element_->GetDerivedOpacity();
  364. return color.ToUInt();
  365. }
  366. else
  367. {
  368. Color color = element_->GetColor(C_TOPLEFT);
  369. color.a_ *= element_->GetDerivedOpacity();
  370. return color.ToUInt();
  371. }
  372. }
  373. void UIBatch::AddOrMerge(const UIBatch& batch, PODVector<UIBatch>& batches)
  374. {
  375. if (batch.vertexEnd_ == batch.vertexStart_)
  376. return;
  377. if (!batches.Empty() && batches.Back().Merge(batch))
  378. return;
  379. batches.Push(batch);
  380. }
  381. }