1
0

creating-text.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <!DOCTYPE html><html lang="zh"><head>
  2. <meta charset="utf-8">
  3. <title>创建文本</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – 创建文本">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>创建文本</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <div>
  29. <p>
  30. 在 three.js 应用中,你经常需要用到文本——下面是几种实现方式。
  31. </p>
  32. </div>
  33. <h2>1. DOM + CSS</h2>
  34. <div>
  35. <p>
  36. 使用 HTML 通常是添加文本最简单、最快捷的方式。大多数 three.js 示例中的描述性叠加层都采用了这种方法。
  37. </p>
  38. <p>你可以向某个元素添加内容,例如:</p>
  39. <pre class="prettyprint notranslate lang-js" translate="no">
  40. &lt;div id="info"&gt;Description&lt;/div&gt;
  41. </pre>
  42. <p>
  43. 然后使用 CSS 将其绝对定位,并通过 z-index 使其显示在所有其他元素之上,尤其是在 three.js 全屏运行时。
  44. </p>
  45. <pre class="prettyprint notranslate lang-js" translate="no">
  46. #info {
  47. position: absolute;
  48. top: 10px;
  49. width: 100%;
  50. text-align: center;
  51. z-index: 100;
  52. display:block;
  53. }
  54. </pre>
  55. </div>
  56. <h2>2. 使用 `CSS2DRenderer` 或 `CSS3DRenderer`</h2>
  57. <div>
  58. <p>
  59. 使用这些渲染器可以将包含在 DOM 元素中的高质量文本绘制到 three.js 场景中。
  60. 这与方法 1 类似,但元素能更紧密、更动态地融入场景。
  61. </p>
  62. </div>
  63. <h2>3. 将文本绘制到 canvas 上并用作 `Texture`</h2>
  64. <div>
  65. <p>如果你希望在 three.js 场景中的平面上轻松绘制文本,可以使用此方法。</p>
  66. </div>
  67. <h2>4. 在你常用的 3D 应用中创建模型并导出到 three.js</h2>
  68. <div>
  69. <p>如果你更喜欢使用 3D 建模应用来制作模型,然后导入到 three.js 中,可以使用此方法。</p>
  70. </div>
  71. <h2>5. 程序化文本几何体</h2>
  72. <div>
  73. <p>
  74. 如果你更倾向于完全在 THREE.js 中工作,或者需要创建程序化、动态的 3D 文本几何体,
  75. 可以创建一个网格,其几何体是 THREE.TextGeometry 的实例:
  76. </p>
  77. <p>
  78. <code>new THREE.TextGeometry( text, parameters );</code>
  79. </p>
  80. <p>
  81. 不过要使其正常工作,TextGeometry 的 `font` 参数需要设置为一个 THREE.Font 实例。
  82. 请参阅 `TextGeometry` 页面,了解如何设置字体、各参数的说明,以及 THREE.js 发行版自带的 JSON 字体列表。
  83. </p>
  84. <h3>示例</h3>
  85. <p>
  86. [example:webgl_geometry_text WebGL / geometry / text]<br />
  87. [example:webgl_shadowmap WebGL / shadowmap]
  88. </p>
  89. <p>
  90. 如果 Typeface 不可用,或你想使用其中没有的字体,可参考一个教程,
  91. 其中包含用于 Blender 的 Python 脚本,可将文本导出为 Three.js 的 JSON 格式:
  92. [link:http://www.jaanga.com/2012/03/blender-to-threejs-create-3d-text-with.html]
  93. </p>
  94. </div>
  95. <h2>6. 位图字体</h2>
  96. <div>
  97. <p>
  98. BMFonts(位图字体)允许将字形批量合并到单个 BufferGeometry 中。BMFont 渲染支持自动换行、字母间距、字距调整、带标准导数的有符号距离场、多通道有符号距离场、多纹理字体等。
  99. 参阅 [link:https://github.com/felixmariotto/three-mesh-ui three-mesh-ui] 或 [link:https://github.com/Jam3/three-bmfont-text three-bmfont-text]。
  100. </p>
  101. <p>
  102. 现成的字体可以在 [link:https://github.com/etiennepinchon/aframe-fonts A-Frame Fonts] 等项目中找到,
  103. 你也可以从任何 .TTF 字体创建自己的位图字体,并优化为仅包含项目所需的字符。
  104. </p>
  105. <p>
  106. 一些有用的工具:
  107. </p>
  108. <ul>
  109. <li>[link:http://msdf-bmfont.donmccurdy.com/ msdf-bmfont-web] <i>(基于 Web)</i></li>
  110. <li>[link:https://github.com/soimy/msdf-bmfont-xml msdf-bmfont-xml] <i>(命令行)</i></li>
  111. <li>[link:https://github.com/libgdx/libgdx/wiki/Hiero hiero] <i>(桌面应用)</i></li>
  112. </ul>
  113. </div>
  114. <h2>7. Troika Text</h2>
  115. <div>
  116. <p>
  117. [link:https://www.npmjs.com/package/troika-three-text troika-three-text] 该包使用与 BMFonts 类似的技术渲染高质量抗锯齿文本,但可以直接使用任何 .TTF 或 .WOFF 字体文件,无需离线预生成字形纹理。它还提供了以下功能:
  118. </p>
  119. <ul>
  120. <li>描边、投影和弯曲等效果</li>
  121. <li>可以应用任何 three.js 材质,甚至是自定义 ShaderMaterial</li>
  122. <li>支持连字、连写字母书写体系(如阿拉伯文),以及从右到左/双向排版</li>
  123. <li>针对大量动态文本进行了优化,大部分工作在 Web Worker 中完成,避免占用主线程</li>
  124. </ul>
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. <script src="../resources/prettify.js"></script>
  130. <script src="../resources/lesson.js"></script>
  131. </body></html>
粤ICP备19079148号