Text3D.cpp 22 KB

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