Text3D.cpp 16 KB

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