Text3D.cpp 16 KB

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