Text3D.cpp 16 KB

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