Text3D.cpp 17 KB

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