Text3D.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 "../Core/Context.h"
  24. #include "../Graphics/Camera.h"
  25. #include "../Graphics/Geometry.h"
  26. #include "../Graphics/Graphics.h"
  27. #include "../Graphics/Material.h"
  28. #include "../Graphics/Technique.h"
  29. #include "../Graphics/VertexBuffer.h"
  30. #include "../IO/Log.h"
  31. #include "../Resource/ResourceCache.h"
  32. #include "../Scene/Node.h"
  33. #include "../UI/Font.h"
  34. #include "../UI/Text.h"
  35. #include "../UI/Text3D.h"
  36. namespace Urho3D
  37. {
  38. extern const char* horizontalAlignments[];
  39. extern const char* verticalAlignments[];
  40. extern const char* textEffects[];
  41. extern const char* faceCameraModeNames[];
  42. extern const char* GEOMETRY_CATEGORY;
  43. static const float TEXT_SCALING = 1.0f / 128.0f;
  44. static const float DEFAULT_EFFECT_DEPTH_BIAS = 0.1f;
  45. Text3D::Text3D(Context* context) :
  46. Drawable(context, DRAWABLE_GEOMETRY),
  47. text_(context),
  48. vertexBuffer_(new VertexBuffer(context_)),
  49. customWorldTransform_(Matrix3x4::IDENTITY),
  50. faceCameraMode_(FC_NONE),
  51. minAngle_(0.0f),
  52. fixedScreenSize_(false),
  53. textDirty_(true),
  54. geometryDirty_(true),
  55. usingSDFShader_(false),
  56. fontDataLost_(false)
  57. {
  58. text_.SetEffectDepthBias(DEFAULT_EFFECT_DEPTH_BIAS);
  59. }
  60. Text3D::~Text3D() = default;
  61. void Text3D::RegisterObject(Context* context)
  62. {
  63. context->RegisterFactory<Text3D>(GEOMETRY_CATEGORY);
  64. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  65. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Font::GetTypeStatic()), AM_DEFAULT);
  66. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()),
  67. AM_DEFAULT);
  68. URHO3D_ATTRIBUTE("Font Size", float, text_.fontSize_, DEFAULT_FONT_SIZE, AM_DEFAULT);
  69. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Text", GetTextAttr, SetTextAttr, String, String::EMPTY, AM_DEFAULT);
  70. URHO3D_ENUM_ATTRIBUTE("Text Alignment", text_.textAlignment_, horizontalAlignments, HA_LEFT, AM_DEFAULT);
  71. URHO3D_ATTRIBUTE("Row Spacing", float, text_.rowSpacing_, 1.0f, AM_DEFAULT);
  72. URHO3D_ATTRIBUTE("Word Wrap", bool, text_.wordWrap_, false, AM_DEFAULT);
  73. URHO3D_ACCESSOR_ATTRIBUTE("Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  74. URHO3D_ACCESSOR_ATTRIBUTE("Fixed Screen Size", IsFixedScreenSize, SetFixedScreenSize, bool, false, AM_DEFAULT);
  75. URHO3D_ENUM_ATTRIBUTE("Face Camera Mode", faceCameraMode_, faceCameraModeNames, FC_NONE, AM_DEFAULT);
  76. URHO3D_ATTRIBUTE("Min Angle", float, minAngle_, 0.0f, AM_DEFAULT);
  77. URHO3D_ACCESSOR_ATTRIBUTE("Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  78. URHO3D_ACCESSOR_ATTRIBUTE("Width", GetWidth, SetWidth, int, 0, AM_DEFAULT);
  79. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment,
  80. horizontalAlignments, HA_LEFT, AM_DEFAULT);
  81. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments,
  82. VA_TOP, AM_DEFAULT);
  83. URHO3D_ACCESSOR_ATTRIBUTE("Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_DEFAULT);
  84. URHO3D_ACCESSOR_ATTRIBUTE("Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_DEFAULT);
  85. URHO3D_ATTRIBUTE("Top Left Color", Color, text_.colors_[0], Color::WHITE, AM_DEFAULT);
  86. URHO3D_ATTRIBUTE("Top Right Color", Color, text_.colors_[1], Color::WHITE, AM_DEFAULT);
  87. URHO3D_ATTRIBUTE("Bottom Left Color", Color, text_.colors_[2], Color::WHITE, AM_DEFAULT);
  88. URHO3D_ATTRIBUTE("Bottom Right Color", Color, text_.colors_[3], Color::WHITE, AM_DEFAULT);
  89. URHO3D_ENUM_ATTRIBUTE("Text Effect", text_.textEffect_, textEffects, TE_NONE, AM_DEFAULT);
  90. URHO3D_ATTRIBUTE("Shadow Offset", IntVector2, text_.shadowOffset_, IntVector2(1, 1), AM_DEFAULT);
  91. URHO3D_ATTRIBUTE("Stroke Thickness", int, text_.strokeThickness_, 1, AM_DEFAULT);
  92. URHO3D_ATTRIBUTE("Round Stroke", bool, text_.roundStroke_, false, AM_DEFAULT);
  93. URHO3D_ACCESSOR_ATTRIBUTE("Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_DEFAULT);
  94. URHO3D_ATTRIBUTE("Effect Depth Bias", float, text_.effectDepthBias_, DEFAULT_EFFECT_DEPTH_BIAS, AM_DEFAULT);
  95. URHO3D_COPY_BASE_ATTRIBUTES(Drawable);
  96. }
  97. void Text3D::ApplyAttributes()
  98. {
  99. text_.ApplyAttributes();
  100. MarkTextDirty();
  101. UpdateTextBatches();
  102. UpdateTextMaterials();
  103. }
  104. void Text3D::UpdateBatches(const FrameInfo& frame)
  105. {
  106. distance_ = frame.camera_->GetDistance(GetWorldBoundingBox().Center());
  107. if (faceCameraMode_ != FC_NONE || fixedScreenSize_)
  108. CalculateFixedScreenSize(frame);
  109. for (unsigned i = 0; i < batches_.Size(); ++i)
  110. {
  111. batches_[i].distance_ = distance_;
  112. batches_[i].worldTransform_ = faceCameraMode_ != FC_NONE ? &customWorldTransform_ : &node_->GetWorldTransform();
  113. }
  114. for (unsigned i = 0; i < uiBatches_.Size(); ++i)
  115. {
  116. if (uiBatches_[i].texture_ && uiBatches_[i].texture_->IsDataLost())
  117. {
  118. fontDataLost_ = true;
  119. break;
  120. }
  121. }
  122. }
  123. void Text3D::UpdateGeometry(const FrameInfo& frame)
  124. {
  125. if (fontDataLost_)
  126. {
  127. // Re-evaluation of the text triggers the font face to reload itself
  128. UpdateTextBatches();
  129. UpdateTextMaterials();
  130. fontDataLost_ = false;
  131. }
  132. // In case is being rendered from multiple views, recalculate camera facing & fixed size
  133. if (faceCameraMode_ != FC_NONE || fixedScreenSize_)
  134. CalculateFixedScreenSize(frame);
  135. if (geometryDirty_)
  136. {
  137. for (unsigned i = 0; i < batches_.Size() && i < uiBatches_.Size(); ++i)
  138. {
  139. Geometry* geometry = geometries_[i];
  140. geometry->SetDrawRange(TRIANGLE_LIST, 0, 0, uiBatches_[i].vertexStart_ / UI_VERTEX_SIZE,
  141. (uiBatches_[i].vertexEnd_ - uiBatches_[i].vertexStart_) / UI_VERTEX_SIZE);
  142. }
  143. }
  144. if ((geometryDirty_ || vertexBuffer_->IsDataLost()) && uiVertexData_.Size())
  145. {
  146. unsigned vertexCount = uiVertexData_.Size() / UI_VERTEX_SIZE;
  147. if (vertexBuffer_->GetVertexCount() != vertexCount)
  148. vertexBuffer_->SetSize(vertexCount, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1);
  149. vertexBuffer_->SetData(&uiVertexData_[0]);
  150. }
  151. geometryDirty_ = false;
  152. }
  153. UpdateGeometryType Text3D::GetUpdateGeometryType()
  154. {
  155. if (geometryDirty_ || fontDataLost_ || vertexBuffer_->IsDataLost() || faceCameraMode_ != FC_NONE || fixedScreenSize_)
  156. return UPDATE_MAIN_THREAD;
  157. else
  158. return UPDATE_NONE;
  159. }
  160. void Text3D::SetMaterial(Material* material)
  161. {
  162. material_ = material;
  163. UpdateTextMaterials(true);
  164. }
  165. bool Text3D::SetFont(const String& fontName, float size)
  166. {
  167. bool success = text_.SetFont(fontName, size);
  168. // Changing font requires materials to be re-evaluated. Material evaluation can not be done in worker threads,
  169. // so UI batches must be brought up-to-date immediately
  170. MarkTextDirty();
  171. UpdateTextBatches();
  172. UpdateTextMaterials();
  173. return success;
  174. }
  175. bool Text3D::SetFont(Font* font, float size)
  176. {
  177. bool success = text_.SetFont(font, size);
  178. MarkTextDirty();
  179. UpdateTextBatches();
  180. UpdateTextMaterials();
  181. return success;
  182. }
  183. bool Text3D::SetFontSize(float size)
  184. {
  185. bool success = text_.SetFontSize(size);
  186. MarkTextDirty();
  187. UpdateTextBatches();
  188. UpdateTextMaterials();
  189. return success;
  190. }
  191. void Text3D::SetText(const String& text)
  192. {
  193. text_.SetText(text);
  194. // Changing text requires materials to be re-evaluated, in case the font is multi-page
  195. MarkTextDirty();
  196. UpdateTextBatches();
  197. UpdateTextMaterials();
  198. }
  199. void Text3D::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  200. {
  201. text_.SetAlignment(hAlign, vAlign);
  202. MarkTextDirty();
  203. }
  204. void Text3D::SetHorizontalAlignment(HorizontalAlignment align)
  205. {
  206. text_.SetHorizontalAlignment(align);
  207. MarkTextDirty();
  208. }
  209. void Text3D::SetVerticalAlignment(VerticalAlignment align)
  210. {
  211. text_.SetVerticalAlignment(align);
  212. MarkTextDirty();
  213. }
  214. void Text3D::SetTextAlignment(HorizontalAlignment align)
  215. {
  216. text_.SetTextAlignment(align);
  217. MarkTextDirty();
  218. }
  219. void Text3D::SetRowSpacing(float spacing)
  220. {
  221. text_.SetRowSpacing(spacing);
  222. MarkTextDirty();
  223. }
  224. void Text3D::SetWordwrap(bool enable)
  225. {
  226. text_.SetWordwrap(enable);
  227. MarkTextDirty();
  228. }
  229. void Text3D::SetTextEffect(TextEffect textEffect)
  230. {
  231. text_.SetTextEffect(textEffect);
  232. MarkTextDirty();
  233. UpdateTextMaterials(true);
  234. }
  235. void Text3D::SetEffectShadowOffset(const IntVector2& offset)
  236. {
  237. text_.SetEffectShadowOffset(offset);
  238. }
  239. void Text3D::SetEffectStrokeThickness(int thickness)
  240. {
  241. text_.SetEffectStrokeThickness(thickness);
  242. }
  243. void Text3D::SetEffectRoundStroke(bool roundStroke)
  244. {
  245. text_.SetEffectRoundStroke(roundStroke);
  246. }
  247. void Text3D::SetEffectColor(const Color& effectColor)
  248. {
  249. text_.SetEffectColor(effectColor);
  250. MarkTextDirty();
  251. UpdateTextMaterials();
  252. }
  253. void Text3D::SetEffectDepthBias(float bias)
  254. {
  255. text_.SetEffectDepthBias(bias);
  256. MarkTextDirty();
  257. }
  258. void Text3D::SetWidth(int width)
  259. {
  260. text_.SetMinWidth(width);
  261. text_.SetWidth(width);
  262. MarkTextDirty();
  263. }
  264. void Text3D::SetColor(const Color& color)
  265. {
  266. float oldAlpha = text_.GetColor(C_TOPLEFT).a_;
  267. text_.SetColor(color);
  268. MarkTextDirty();
  269. // If alpha changes from zero to nonzero or vice versa, amount of text batches changes (optimization), so do full update
  270. if ((oldAlpha == 0.0f && color.a_ != 0.0f) || (oldAlpha != 0.0f && color.a_ == 0.0f))
  271. {
  272. UpdateTextBatches();
  273. UpdateTextMaterials();
  274. }
  275. }
  276. void Text3D::SetColor(Corner corner, const Color& color)
  277. {
  278. text_.SetColor(corner, color);
  279. MarkTextDirty();
  280. }
  281. void Text3D::SetOpacity(float opacity)
  282. {
  283. float oldOpacity = text_.GetOpacity();
  284. text_.SetOpacity(opacity);
  285. float newOpacity = text_.GetOpacity();
  286. MarkTextDirty();
  287. // If opacity changes from zero to nonzero or vice versa, amount of text batches changes (optimization), so do full update
  288. if ((oldOpacity == 0.0f && newOpacity != 0.0f) || (oldOpacity != 0.0f && newOpacity == 0.0f))
  289. {
  290. UpdateTextBatches();
  291. UpdateTextMaterials();
  292. }
  293. }
  294. void Text3D::SetFixedScreenSize(bool enable)
  295. {
  296. if (enable != fixedScreenSize_)
  297. {
  298. fixedScreenSize_ = enable;
  299. // Bounding box must be recalculated
  300. OnMarkedDirty(node_);
  301. MarkNetworkUpdate();
  302. }
  303. }
  304. void Text3D::SetFaceCameraMode(FaceCameraMode mode)
  305. {
  306. if (mode != faceCameraMode_)
  307. {
  308. faceCameraMode_ = mode;
  309. // Bounding box must be recalculated
  310. OnMarkedDirty(node_);
  311. MarkNetworkUpdate();
  312. }
  313. }
  314. Material* Text3D::GetMaterial() const
  315. {
  316. return material_;
  317. }
  318. Font* Text3D::GetFont() const
  319. {
  320. return text_.GetFont();
  321. }
  322. float Text3D::GetFontSize() const
  323. {
  324. return text_.GetFontSize();
  325. }
  326. const String& Text3D::GetText() const
  327. {
  328. return text_.GetText();
  329. }
  330. HorizontalAlignment Text3D::GetHorizontalAlignment() const
  331. {
  332. return text_.GetHorizontalAlignment();
  333. }
  334. VerticalAlignment Text3D::GetVerticalAlignment() const
  335. {
  336. return text_.GetVerticalAlignment();
  337. }
  338. HorizontalAlignment Text3D::GetTextAlignment() const
  339. {
  340. return text_.GetTextAlignment();
  341. }
  342. float Text3D::GetRowSpacing() const
  343. {
  344. return text_.GetRowSpacing();
  345. }
  346. bool Text3D::GetWordwrap() const
  347. {
  348. return text_.GetWordwrap();
  349. }
  350. TextEffect Text3D::GetTextEffect() const
  351. {
  352. return text_.GetTextEffect();
  353. }
  354. const IntVector2& Text3D::GetEffectShadowOffset() const
  355. {
  356. return text_.GetEffectShadowOffset();
  357. }
  358. int Text3D::GetEffectStrokeThickness() const
  359. {
  360. return text_.GetEffectStrokeThickness();
  361. }
  362. bool Text3D::GetEffectRoundStroke() const
  363. {
  364. return text_.GetEffectRoundStroke();
  365. }
  366. const Color& Text3D::GetEffectColor() const
  367. {
  368. return text_.GetEffectColor();
  369. }
  370. float Text3D::GetEffectDepthBias() const
  371. {
  372. return text_.GetEffectDepthBias();
  373. }
  374. int Text3D::GetWidth() const
  375. {
  376. return text_.GetWidth();
  377. }
  378. int Text3D::GetHeight() const
  379. {
  380. return text_.GetHeight();
  381. }
  382. int Text3D::GetRowHeight() const
  383. {
  384. return text_.GetRowHeight();
  385. }
  386. unsigned Text3D::GetNumRows() const
  387. {
  388. return text_.GetNumRows();
  389. }
  390. unsigned Text3D::GetNumChars() const
  391. {
  392. return text_.GetNumChars();
  393. }
  394. int Text3D::GetRowWidth(unsigned index) const
  395. {
  396. return text_.GetRowWidth(index);
  397. }
  398. Vector2 Text3D::GetCharPosition(unsigned index)
  399. {
  400. return text_.GetCharPosition(index);
  401. }
  402. Vector2 Text3D::GetCharSize(unsigned index)
  403. {
  404. return text_.GetCharSize(index);
  405. }
  406. const Color& Text3D::GetColor(Corner corner) const
  407. {
  408. return text_.GetColor(corner);
  409. }
  410. float Text3D::GetOpacity() const
  411. {
  412. return text_.GetOpacity();
  413. }
  414. void Text3D::OnNodeSet(Node* node)
  415. {
  416. Drawable::OnNodeSet(node);
  417. if (node)
  418. customWorldTransform_ = node->GetWorldTransform();
  419. }
  420. void Text3D::OnWorldBoundingBoxUpdate()
  421. {
  422. if (textDirty_)
  423. UpdateTextBatches();
  424. // In face camera mode, use the last camera rotation to build the world bounding box
  425. if (faceCameraMode_ != FC_NONE || fixedScreenSize_)
  426. {
  427. worldBoundingBox_ = boundingBox_.Transformed(Matrix3x4(node_->GetWorldPosition(),
  428. customWorldTransform_.Rotation(), customWorldTransform_.Scale()));
  429. }
  430. else
  431. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  432. }
  433. void Text3D::MarkTextDirty()
  434. {
  435. textDirty_ = true;
  436. OnMarkedDirty(node_);
  437. MarkNetworkUpdate();
  438. }
  439. void Text3D::SetMaterialAttr(const ResourceRef& value)
  440. {
  441. auto* cache = GetSubsystem<ResourceCache>();
  442. SetMaterial(cache->GetResource<Material>(value.name_));
  443. }
  444. void Text3D::SetFontAttr(const ResourceRef& value)
  445. {
  446. auto* cache = GetSubsystem<ResourceCache>();
  447. text_.font_ = cache->GetResource<Font>(value.name_);
  448. }
  449. void Text3D::SetTextAttr(const String& value)
  450. {
  451. text_.SetTextAttr(value);
  452. }
  453. String Text3D::GetTextAttr() const
  454. {
  455. return text_.GetTextAttr();
  456. }
  457. ResourceRef Text3D::GetMaterialAttr() const
  458. {
  459. return GetResourceRef(material_, Material::GetTypeStatic());
  460. }
  461. ResourceRef Text3D::GetFontAttr() const
  462. {
  463. return GetResourceRef(text_.font_, Font::GetTypeStatic());
  464. }
  465. void Text3D::UpdateTextBatches()
  466. {
  467. uiBatches_.Clear();
  468. uiVertexData_.Clear();
  469. text_.GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
  470. Vector3 offset(Vector3::ZERO);
  471. switch (text_.GetHorizontalAlignment())
  472. {
  473. case HA_LEFT:
  474. break;
  475. case HA_CENTER:
  476. offset.x_ -= (float)text_.GetWidth() * 0.5f;
  477. break;
  478. case HA_RIGHT:
  479. offset.x_ -= (float)text_.GetWidth();
  480. break;
  481. case HA_CUSTOM:
  482. break;
  483. }
  484. switch (text_.GetVerticalAlignment())
  485. {
  486. case VA_TOP:
  487. break;
  488. case VA_CENTER:
  489. offset.y_ -= (float)text_.GetHeight() * 0.5f;
  490. break;
  491. case VA_BOTTOM:
  492. offset.y_ -= (float)text_.GetHeight();
  493. break;
  494. case VA_CUSTOM:
  495. break;
  496. }
  497. if (uiVertexData_.Size())
  498. {
  499. boundingBox_.Clear();
  500. for (unsigned i = 0; i < uiVertexData_.Size(); i += UI_VERTEX_SIZE)
  501. {
  502. Vector3& position = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
  503. position += offset;
  504. position *= TEXT_SCALING;
  505. position.y_ = -position.y_;
  506. boundingBox_.Merge(position);
  507. }
  508. }
  509. else
  510. boundingBox_.Define(Vector3::ZERO, Vector3::ZERO);
  511. textDirty_ = false;
  512. geometryDirty_ = true;
  513. }
  514. void Text3D::UpdateTextMaterials(bool forceUpdate)
  515. {
  516. Font* font = GetFont();
  517. bool isSDFFont = font ? font->IsSDFFont() : false;
  518. batches_.Resize(uiBatches_.Size());
  519. geometries_.Resize(uiBatches_.Size());
  520. for (unsigned i = 0; i < batches_.Size(); ++i)
  521. {
  522. if (!geometries_[i])
  523. {
  524. auto* geometry = new Geometry(context_);
  525. geometry->SetVertexBuffer(0, vertexBuffer_);
  526. batches_[i].geometry_ = geometries_[i] = geometry;
  527. }
  528. if (!batches_[i].material_ || forceUpdate || isSDFFont != usingSDFShader_)
  529. {
  530. // If material not defined, create a reasonable default from scratch
  531. if (!material_)
  532. {
  533. auto* material = new Material(context_);
  534. auto* tech = new Technique(context_);
  535. Pass* pass = tech->CreatePass("alpha");
  536. pass->SetVertexShader("Text");
  537. pass->SetPixelShader("Text");
  538. pass->SetBlendMode(BLEND_ALPHA);
  539. pass->SetDepthWrite(false);
  540. material->SetTechnique(0, tech);
  541. material->SetCullMode(CULL_NONE);
  542. batches_[i].material_ = material;
  543. }
  544. else
  545. batches_[i].material_ = material_->Clone();
  546. usingSDFShader_ = isSDFFont;
  547. }
  548. Material* material = batches_[i].material_;
  549. Texture* texture = uiBatches_[i].texture_;
  550. material->SetTexture(TU_DIFFUSE, texture);
  551. if (isSDFFont)
  552. {
  553. // Note: custom defined material is assumed to have right shader defines; they aren't modified here
  554. if (!material_)
  555. {
  556. Technique* tech = material->GetTechnique(0);
  557. Pass* pass = tech ? tech->GetPass("alpha") : nullptr;
  558. if (pass)
  559. {
  560. switch (GetTextEffect())
  561. {
  562. case TE_NONE:
  563. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD");
  564. break;
  565. case TE_SHADOW:
  566. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_SHADOW");
  567. break;
  568. case TE_STROKE:
  569. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_STROKE");
  570. break;
  571. }
  572. }
  573. }
  574. switch (GetTextEffect())
  575. {
  576. case TE_SHADOW:
  577. if (texture)
  578. {
  579. Vector2 shadowOffset(0.5f / texture->GetWidth(), 0.5f / texture->GetHeight());
  580. material->SetShaderParameter("ShadowOffset", shadowOffset);
  581. }
  582. material->SetShaderParameter("ShadowColor", GetEffectColor());
  583. break;
  584. case TE_STROKE:
  585. material->SetShaderParameter("StrokeColor", GetEffectColor());
  586. break;
  587. default:
  588. break;
  589. }
  590. }
  591. else
  592. {
  593. // If not SDF, set shader defines based on whether font texture is full RGB or just alpha
  594. if (!material_)
  595. {
  596. Technique* tech = material->GetTechnique(0);
  597. Pass* pass = tech ? tech->GetPass("alpha") : nullptr;
  598. if (pass)
  599. {
  600. if (texture && texture->GetFormat() == Graphics::GetAlphaFormat())
  601. pass->SetPixelShaderDefines("ALPHAMAP");
  602. else
  603. pass->SetPixelShaderDefines("");
  604. }
  605. }
  606. }
  607. }
  608. }
  609. void Text3D::CalculateFixedScreenSize(const FrameInfo& frame)
  610. {
  611. Vector3 worldPosition = node_->GetWorldPosition();
  612. Vector3 worldScale = node_->GetWorldScale();
  613. if (fixedScreenSize_)
  614. {
  615. float textScaling = 2.0f / TEXT_SCALING / frame.viewSize_.y_;
  616. float halfViewWorldSize = frame.camera_->GetHalfViewSize();
  617. if (!frame.camera_->IsOrthographic())
  618. {
  619. Matrix4 viewProj(frame.camera_->GetProjection() * frame.camera_->GetView());
  620. Vector4 projPos(viewProj * Vector4(worldPosition, 1.0f));
  621. worldScale *= textScaling * halfViewWorldSize * projPos.w_;
  622. }
  623. else
  624. worldScale *= textScaling * halfViewWorldSize;
  625. }
  626. customWorldTransform_ = Matrix3x4(worldPosition, frame.camera_->GetFaceCameraRotation(
  627. worldPosition, node_->GetWorldRotation(), faceCameraMode_, minAngle_), worldScale);
  628. worldBoundingBoxDirty_ = true;
  629. }
  630. }