Text3D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 "../Camera.h"
  25. #include "../Geometry.h"
  26. #include "../Graphics.h"
  27. #include "../Material.h"
  28. #include "../Technique.h"
  29. #include "../VertexBuffer.h"
  30. #include "../../IO/Log.h"
  31. #include "../../Resource/ResourceCache.h"
  32. #include "../../Scene/Node.h"
  33. #include "Text3DFont.h"
  34. #include "Text3DText.h"
  35. #include "Text3D.h"
  36. namespace Atomic
  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. horizontalAlignment_(HA_CENTER),
  58. verticalAlignment_(VA_CENTER)
  59. {
  60. text_.SetEffectDepthBias(DEFAULT_EFFECT_DEPTH_BIAS);
  61. }
  62. Text3D::~Text3D()
  63. {
  64. }
  65. void Text3D::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<Text3D>(GEOMETRY_CATEGORY);
  68. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  69. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Font", GetFontAttr, SetFontAttr, ResourceRef, ResourceRef(Text3DFont::GetTypeStatic()), AM_DEFAULT);
  70. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()),
  71. AM_DEFAULT);
  72. ATOMIC_ATTRIBUTE("Font Size", float, text_.fontSize_, TEXT3D_DEFAULT_FONT_SIZE, AM_DEFAULT);
  73. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Text", GetTextAttr, SetTextAttr, String, String::EMPTY, AM_DEFAULT);
  74. ATOMIC_ENUM_ATTRIBUTE("Text Alignment", text_.textAlignment_, horizontalAlignments, HA_LEFT, AM_DEFAULT);
  75. ATOMIC_ATTRIBUTE("Row Spacing", float, text_.rowSpacing_, 1.0f, AM_DEFAULT);
  76. ATOMIC_ATTRIBUTE("Word Wrap", bool, text_.wordWrap_, false, AM_DEFAULT);
  77. ATOMIC_ACCESSOR_ATTRIBUTE("Can Be Occluded", IsOccludee, SetOccludee, bool, true, AM_DEFAULT);
  78. ATOMIC_ACCESSOR_ATTRIBUTE("Fixed Screen Size", IsFixedScreenSize, SetFixedScreenSize, bool, false, AM_DEFAULT);
  79. ATOMIC_ENUM_ATTRIBUTE("Face Camera Mode", faceCameraMode_, faceCameraModeNames, FC_NONE, AM_DEFAULT);
  80. ATOMIC_ATTRIBUTE("Min Angle", float, minAngle_, 0.0f, AM_DEFAULT);
  81. ATOMIC_ACCESSOR_ATTRIBUTE("Draw Distance", GetDrawDistance, SetDrawDistance, float, 0.0f, AM_DEFAULT);
  82. ATOMIC_ACCESSOR_ATTRIBUTE("Width", GetWidth, SetWidth, int, 0, AM_DEFAULT);
  83. ATOMIC_ACCESSOR_ATTRIBUTE("Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_DEFAULT);
  84. ATOMIC_ACCESSOR_ATTRIBUTE("Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_DEFAULT);
  85. ATOMIC_ATTRIBUTE("Top Left Color", Color, text_.color_[0], Color::WHITE, AM_DEFAULT);
  86. ATOMIC_ATTRIBUTE("Top Right Color", Color, text_.color_[1], Color::WHITE, AM_DEFAULT);
  87. ATOMIC_ATTRIBUTE("Bottom Left Color", Color, text_.color_[2], Color::WHITE, AM_DEFAULT);
  88. ATOMIC_ATTRIBUTE("Bottom Right Color", Color, text_.color_[3], Color::WHITE, AM_DEFAULT);
  89. ATOMIC_ENUM_ATTRIBUTE("Text Effect", text_.textEffect_, textEffects, TE_NONE, AM_DEFAULT);
  90. ATOMIC_ATTRIBUTE("Shadow Offset", IntVector2, text_.shadowOffset_, IntVector2(1, 1), AM_DEFAULT);
  91. ATOMIC_ATTRIBUTE("Stroke Thickness", int, text_.strokeThickness_, 1, AM_DEFAULT);
  92. ATOMIC_ATTRIBUTE("Round Stroke", bool, text_.roundStroke_, false, AM_DEFAULT);
  93. ATOMIC_ACCESSOR_ATTRIBUTE("Effect Color", GetEffectColor, SetEffectColor, Color, Color::BLACK, AM_DEFAULT);
  94. ATOMIC_ATTRIBUTE("Effect Depth Bias", float, text_.effectDepthBias_, DEFAULT_EFFECT_DEPTH_BIAS, AM_DEFAULT);
  95. ATOMIC_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_ / TEXT3D_VERTEX_SIZE,
  141. (uiBatches_[i].vertexEnd_ - uiBatches_[i].vertexStart_) / TEXT3D_VERTEX_SIZE);
  142. }
  143. }
  144. if ((geometryDirty_ || vertexBuffer_->IsDataLost()) && uiVertexData_.Size())
  145. {
  146. unsigned vertexCount = uiVertexData_.Size() / TEXT3D_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(Text3DFont* 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::SetTextAlignment(Text3DHorizontalAlignment align)
  200. {
  201. text_.SetTextAlignment(align);
  202. MarkTextDirty();
  203. }
  204. void Text3D::SetRowSpacing(float spacing)
  205. {
  206. text_.SetRowSpacing(spacing);
  207. MarkTextDirty();
  208. }
  209. void Text3D::SetWordwrap(bool enable)
  210. {
  211. text_.SetWordwrap(enable);
  212. MarkTextDirty();
  213. }
  214. void Text3D::SetTextEffect(Text3DTextEffect textEffect)
  215. {
  216. text_.SetTextEffect(textEffect);
  217. MarkTextDirty();
  218. UpdateTextMaterials(true);
  219. }
  220. void Text3D::SetEffectShadowOffset(const IntVector2& offset)
  221. {
  222. text_.SetEffectShadowOffset(offset);
  223. }
  224. void Text3D::SetEffectStrokeThickness(int thickness)
  225. {
  226. text_.SetEffectStrokeThickness(thickness);
  227. }
  228. void Text3D::SetEffectRoundStroke(bool roundStroke)
  229. {
  230. text_.SetEffectRoundStroke(roundStroke);
  231. }
  232. void Text3D::SetEffectColor(const Color& effectColor)
  233. {
  234. text_.SetEffectColor(effectColor);
  235. MarkTextDirty();
  236. UpdateTextMaterials();
  237. }
  238. void Text3D::SetEffectDepthBias(float bias)
  239. {
  240. text_.SetEffectDepthBias(bias);
  241. MarkTextDirty();
  242. }
  243. void Text3D::SetWidth(int width)
  244. {
  245. text_.SetMinWidth(width);
  246. text_.SetWidth(width);
  247. MarkTextDirty();
  248. }
  249. void Text3D::SetColor(const Color& color)
  250. {
  251. float oldAlpha = text_.GetColor(C_TOPLEFT).a_;
  252. text_.SetColor(color);
  253. MarkTextDirty();
  254. // If alpha changes from zero to nonzero or vice versa, amount of text batches changes (optimization), so do full update
  255. if ((oldAlpha == 0.0f && color.a_ != 0.0f) || (oldAlpha != 0.0f && color.a_ == 0.0f))
  256. {
  257. UpdateTextBatches();
  258. UpdateTextMaterials();
  259. }
  260. }
  261. void Text3D::SetColor(Text3DCorner corner, const Color& color)
  262. {
  263. text_.SetColor(corner, color);
  264. MarkTextDirty();
  265. }
  266. void Text3D::SetOpacity(float opacity)
  267. {
  268. float oldOpacity = text_.GetOpacity();
  269. text_.SetOpacity(opacity);
  270. float newOpacity = text_.GetOpacity();
  271. MarkTextDirty();
  272. // If opacity changes from zero to nonzero or vice versa, amount of text batches changes (optimization), so do full update
  273. if ((oldOpacity == 0.0f && newOpacity != 0.0f) || (oldOpacity != 0.0f && newOpacity == 0.0f))
  274. {
  275. UpdateTextBatches();
  276. UpdateTextMaterials();
  277. }
  278. }
  279. void Text3D::SetFixedScreenSize(bool enable)
  280. {
  281. if (enable != fixedScreenSize_)
  282. {
  283. fixedScreenSize_ = enable;
  284. // Bounding box must be recalculated
  285. OnMarkedDirty(node_);
  286. MarkNetworkUpdate();
  287. }
  288. }
  289. void Text3D::SetFaceCameraMode(FaceCameraMode mode)
  290. {
  291. if (mode != faceCameraMode_)
  292. {
  293. faceCameraMode_ = mode;
  294. // Bounding box must be recalculated
  295. OnMarkedDirty(node_);
  296. MarkNetworkUpdate();
  297. }
  298. }
  299. Material* Text3D::GetMaterial() const
  300. {
  301. return material_;
  302. }
  303. Text3DFont* Text3D::GetFont() const
  304. {
  305. return text_.GetFont();
  306. }
  307. float Text3D::GetFontSize() const
  308. {
  309. return text_.GetFontSize();
  310. }
  311. const String& Text3D::GetText() const
  312. {
  313. return text_.GetText();
  314. }
  315. Text3DHorizontalAlignment Text3D::GetTextAlignment() const
  316. {
  317. return text_.GetTextAlignment();
  318. }
  319. float Text3D::GetRowSpacing() const
  320. {
  321. return text_.GetRowSpacing();
  322. }
  323. bool Text3D::GetWordwrap() const
  324. {
  325. return text_.GetWordwrap();
  326. }
  327. Text3DTextEffect Text3D::GetTextEffect() const
  328. {
  329. return text_.GetTextEffect();
  330. }
  331. const IntVector2& Text3D::GetEffectShadowOffset() const
  332. {
  333. return text_.GetEffectShadowOffset();
  334. }
  335. int Text3D::GetEffectStrokeThickness() const
  336. {
  337. return text_.GetEffectStrokeThickness();
  338. }
  339. bool Text3D::GetEffectRoundStroke() const
  340. {
  341. return text_.GetEffectRoundStroke();
  342. }
  343. const Color& Text3D::GetEffectColor() const
  344. {
  345. return text_.GetEffectColor();
  346. }
  347. float Text3D::GetEffectDepthBias() const
  348. {
  349. return text_.GetEffectDepthBias();
  350. }
  351. int Text3D::GetWidth() const
  352. {
  353. return text_.GetWidth();
  354. }
  355. int Text3D::GetHeight() const
  356. {
  357. return text_.GetHeight();
  358. }
  359. int Text3D::GetRowHeight() const
  360. {
  361. return text_.GetRowHeight();
  362. }
  363. unsigned Text3D::GetNumRows() const
  364. {
  365. return text_.GetNumRows();
  366. }
  367. unsigned Text3D::GetNumChars() const
  368. {
  369. return text_.GetNumChars();
  370. }
  371. int Text3D::GetRowWidth(unsigned index) const
  372. {
  373. return text_.GetRowWidth(index);
  374. }
  375. Vector2 Text3D::GetCharPosition(unsigned index)
  376. {
  377. return text_.GetCharPosition(index);
  378. }
  379. Vector2 Text3D::GetCharSize(unsigned index)
  380. {
  381. return text_.GetCharSize(index);
  382. }
  383. const Color& Text3D::GetColor(Text3DCorner corner) const
  384. {
  385. return text_.GetColor(corner);
  386. }
  387. float Text3D::GetOpacity() const
  388. {
  389. return text_.GetOpacity();
  390. }
  391. void Text3D::OnNodeSet(Node* node)
  392. {
  393. Drawable::OnNodeSet(node);
  394. if (node)
  395. customWorldTransform_ = node->GetWorldTransform();
  396. }
  397. void Text3D::OnWorldBoundingBoxUpdate()
  398. {
  399. if (textDirty_)
  400. UpdateTextBatches();
  401. // In face camera mode, use the last camera rotation to build the world bounding box
  402. if (faceCameraMode_ != FC_NONE || fixedScreenSize_)
  403. {
  404. worldBoundingBox_ = boundingBox_.Transformed(Matrix3x4(node_->GetWorldPosition(),
  405. customWorldTransform_.Rotation(), customWorldTransform_.Scale()));
  406. }
  407. else
  408. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  409. }
  410. void Text3D::MarkTextDirty()
  411. {
  412. textDirty_ = true;
  413. OnMarkedDirty(node_);
  414. MarkNetworkUpdate();
  415. }
  416. void Text3D::SetMaterialAttr(const ResourceRef& value)
  417. {
  418. ResourceCache* cache = GetSubsystem<ResourceCache>();
  419. SetMaterial(cache->GetResource<Material>(value.name_));
  420. }
  421. void Text3D::SetFontAttr(const ResourceRef& value)
  422. {
  423. ResourceCache* cache = GetSubsystem<ResourceCache>();
  424. text_.font_ = cache->GetResource<Text3DFont>(value.name_);
  425. }
  426. void Text3D::SetTextAttr(const String& value)
  427. {
  428. text_.SetTextAttr(value);
  429. }
  430. String Text3D::GetTextAttr() const
  431. {
  432. return text_.GetTextAttr();
  433. }
  434. ResourceRef Text3D::GetMaterialAttr() const
  435. {
  436. return GetResourceRef(material_, Material::GetTypeStatic());
  437. }
  438. ResourceRef Text3D::GetFontAttr() const
  439. {
  440. return GetResourceRef(text_.font_, Text3DFont::GetTypeStatic());
  441. }
  442. void Text3D::UpdateTextBatches()
  443. {
  444. uiBatches_.Clear();
  445. uiVertexData_.Clear();
  446. text_.GetBatches(uiBatches_, uiVertexData_, IntRect::ZERO);
  447. Vector3 offset(Vector3::ZERO);
  448. switch (GetHorizontalAlignment())
  449. {
  450. case HA_LEFT:
  451. break;
  452. case HA_CENTER:
  453. offset.x_ -= (float)text_.GetWidth() * 0.5f;
  454. break;
  455. case HA_RIGHT:
  456. offset.x_ -= (float)text_.GetWidth();
  457. break;
  458. }
  459. switch (GetVerticalAlignment())
  460. {
  461. case VA_TOP:
  462. break;
  463. case VA_CENTER:
  464. offset.y_ -= (float)text_.GetHeight() * 0.5f;
  465. break;
  466. case VA_BOTTOM:
  467. offset.y_ -= (float)text_.GetHeight();
  468. break;
  469. }
  470. if (uiVertexData_.Size())
  471. {
  472. boundingBox_.Clear();
  473. for (unsigned i = 0; i < uiVertexData_.Size(); i += TEXT3D_VERTEX_SIZE)
  474. {
  475. Vector3& position = *(reinterpret_cast<Vector3*>(&uiVertexData_[i]));
  476. position += offset;
  477. position *= TEXT_SCALING;
  478. position.y_ = -position.y_;
  479. boundingBox_.Merge(position);
  480. }
  481. }
  482. else
  483. boundingBox_.Define(Vector3::ZERO, Vector3::ZERO);
  484. textDirty_ = false;
  485. geometryDirty_ = true;
  486. }
  487. void Text3D::UpdateTextMaterials(bool forceUpdate)
  488. {
  489. Text3DFont* font = GetFont();
  490. bool isSDFFont = font ? font->IsSDFFont() : false;
  491. batches_.Resize(uiBatches_.Size());
  492. geometries_.Resize(uiBatches_.Size());
  493. for (unsigned i = 0; i < batches_.Size(); ++i)
  494. {
  495. if (!geometries_[i])
  496. {
  497. Geometry* geometry = new Geometry(context_);
  498. geometry->SetVertexBuffer(0, vertexBuffer_);
  499. batches_[i].geometry_ = geometries_[i] = geometry;
  500. }
  501. if (!batches_[i].material_ || forceUpdate || isSDFFont != usingSDFShader_)
  502. {
  503. // If material not defined, create a reasonable default from scratch
  504. if (!material_)
  505. {
  506. Material* material = new Material(context_);
  507. Technique* tech = new Technique(context_);
  508. Pass* pass = tech->CreatePass("alpha");
  509. pass->SetVertexShader("Text");
  510. pass->SetPixelShader("Text");
  511. pass->SetBlendMode(BLEND_ALPHA);
  512. pass->SetDepthWrite(false);
  513. material->SetTechnique(0, tech);
  514. material->SetCullMode(CULL_NONE);
  515. batches_[i].material_ = material;
  516. }
  517. else
  518. batches_[i].material_ = material_->Clone();
  519. usingSDFShader_ = isSDFFont;
  520. }
  521. Material* material = batches_[i].material_;
  522. Texture* texture = uiBatches_[i].texture_;
  523. material->SetTexture(TU_DIFFUSE, texture);
  524. if (isSDFFont)
  525. {
  526. // Note: custom defined material is assumed to have right shader defines; they aren't modified here
  527. if (!material_)
  528. {
  529. Technique* tech = material->GetTechnique(0);
  530. Pass* pass = tech ? tech->GetPass("alpha") : (Pass*)0;
  531. if (pass)
  532. {
  533. switch (GetTextEffect())
  534. {
  535. case TE_NONE:
  536. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD");
  537. break;
  538. case TE_SHADOW:
  539. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_SHADOW");
  540. break;
  541. case TE_STROKE:
  542. pass->SetPixelShaderDefines("SIGNED_DISTANCE_FIELD TEXT_EFFECT_STROKE");
  543. break;
  544. }
  545. }
  546. }
  547. switch (GetTextEffect())
  548. {
  549. case TE_SHADOW:
  550. if (texture)
  551. {
  552. Vector2 shadowOffset(0.5f / texture->GetWidth(), 0.5f / texture->GetHeight());
  553. material->SetShaderParameter("ShadowOffset", shadowOffset);
  554. }
  555. material->SetShaderParameter("ShadowColor", GetEffectColor());
  556. break;
  557. case TE_STROKE:
  558. material->SetShaderParameter("StrokeColor", GetEffectColor());
  559. break;
  560. default:
  561. break;
  562. }
  563. }
  564. else
  565. {
  566. // If not SDF, set shader defines based on whether font texture is full RGB or just alpha
  567. if (!material_)
  568. {
  569. Technique* tech = material->GetTechnique(0);
  570. Pass* pass = tech ? tech->GetPass("alpha") : (Pass*)0;
  571. if (pass)
  572. {
  573. if (texture && texture->GetFormat() == Graphics::GetAlphaFormat())
  574. pass->SetPixelShaderDefines("ALPHAMAP");
  575. else
  576. pass->SetPixelShaderDefines("");
  577. }
  578. }
  579. }
  580. }
  581. }
  582. void Text3D::CalculateFixedScreenSize(const FrameInfo& frame)
  583. {
  584. Vector3 worldPosition = node_->GetWorldPosition();
  585. Vector3 worldScale = node_->GetWorldScale();
  586. if (fixedScreenSize_)
  587. {
  588. float textScaling = 2.0f / TEXT_SCALING / frame.viewSize_.y_;
  589. float halfViewWorldSize = frame.camera_->GetHalfViewSize();
  590. if (!frame.camera_->IsOrthographic())
  591. {
  592. Matrix4 viewProj(frame.camera_->GetProjection() * frame.camera_->GetView());
  593. Vector4 projPos(viewProj * Vector4(worldPosition, 1.0f));
  594. worldScale *= textScaling * halfViewWorldSize * projPos.w_;
  595. }
  596. else
  597. worldScale *= textScaling * halfViewWorldSize;
  598. }
  599. customWorldTransform_ = Matrix3x4(worldPosition, frame.camera_->GetFaceCameraRotation(
  600. worldPosition, node_->GetWorldRotation(), faceCameraMode_, minAngle_), worldScale);
  601. worldBoundingBoxDirty_ = true;
  602. }
  603. Text3DHorizontalAlignment Text3D::GetHorizontalAlignment() const
  604. {
  605. return horizontalAlignment_;
  606. }
  607. Text3DVerticalAlignment Text3D::GetVerticalAlignment() const
  608. {
  609. return verticalAlignment_;
  610. }
  611. void Text3D::SetAlignment(Text3DHorizontalAlignment hAlign, Text3DVerticalAlignment vAlign)
  612. {
  613. horizontalAlignment_ = hAlign;
  614. verticalAlignment_ = vAlign;
  615. MarkTextDirty();
  616. }
  617. void Text3D::SetHorizontalAlignment(Text3DHorizontalAlignment align)
  618. {
  619. horizontalAlignment_ = align;
  620. MarkTextDirty();
  621. }
  622. void Text3D::SetVerticalAlignment(Text3DVerticalAlignment align)
  623. {
  624. verticalAlignment_ = align;
  625. MarkTextDirty();
  626. }
  627. }