Text3D.cpp 20 KB

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