UIBatch.cpp 12 KB

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