creating-text.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Creating Text</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 – Creating Text">
  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>Creating Text</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <div>
  29. <p>
  30. There are often times when you might need to use text in your three.js application - here are
  31. a couple of ways that you can do so.
  32. </p>
  33. </div>
  34. <h2>1. DOM + CSS</h2>
  35. <div>
  36. <p>
  37. Using HTML is generally the easiest and fastest manner to add text. This is the method
  38. used for descriptive overlays in most three.js examples.
  39. </p>
  40. <p>You can add content to a</p>
  41. <pre class="prettyprint notranslate lang-js" translate="no">
  42. &lt;div id="info"&gt;Description&lt;/div&gt;
  43. </pre>
  44. <p>
  45. and use CSS markup to position absolutely at a position above all others with a
  46. z-index especially if you are running three.js full screen.
  47. </p>
  48. <pre class="prettyprint notranslate lang-js" translate="no">
  49. #info {
  50. position: absolute;
  51. top: 10px;
  52. width: 100%;
  53. text-align: center;
  54. z-index: 100;
  55. display:block;
  56. }
  57. </pre>
  58. </div>
  59. <h2>2. Use `CSS2DRenderer` or `CSS3DRenderer`</h2>
  60. <div>
  61. <p>
  62. Use these renderers to draw high-quality text contained in DOM elements to your three.js scene.
  63. This is similar to 1. except that with these renderers elements can be integrated more tightly and dynamically into the scene.
  64. </p>
  65. </div>
  66. <h2>3. Draw text to canvas and use as a `Texture`</h2>
  67. <div>
  68. <p>Use this method if you wish to draw text easily on a plane in your three.js scene.</p>
  69. </div>
  70. <h2>4. Create a model in your favourite 3D application and export to three.js</h2>
  71. <div>
  72. <p>Use this method if you prefer working with your 3d applications and importing the models to three.js.</p>
  73. </div>
  74. <h2>5. Procedural Text Geometry</h2>
  75. <div>
  76. <p>
  77. If you prefer to work purely in THREE.js or to create procedural and dynamic 3D
  78. text geometries, you can create a mesh whose geometry is an instance of THREE.TextGeometry:
  79. </p>
  80. <p>
  81. <code>new THREE.TextGeometry( text, parameters );</code>
  82. </p>
  83. <p>
  84. In order for this to work, however, your TextGeometry will need an instance of THREE.Font
  85. to be set on its "font" parameter.
  86. See the `TextGeometry` page for more info on how this can be done, descriptions of each
  87. accepted parameter, and a list of the JSON fonts that come with the THREE.js distribution itself.
  88. </p>
  89. <h3>Examples</h3>
  90. <p>
  91. [example:webgl_geometry_text WebGL / geometry / text]<br />
  92. [example:webgl_shadowmap WebGL / shadowmap]
  93. </p>
  94. <p>
  95. If Typeface is down, or you want to use a font that is not there, there's a tutorial
  96. with a python script for blender that allows you to export text to Three.js's JSON format:
  97. [link:http://www.jaanga.com/2012/03/blender-to-threejs-create-3d-text-with.html]
  98. </p>
  99. </div>
  100. <h2>6. Bitmap Fonts</h2>
  101. <div>
  102. <p>
  103. BMFonts (bitmap fonts) allow batching glyphs into a single BufferGeometry. BMFont rendering
  104. supports word-wrapping, letter spacing, kerning, signed distance fields with standard
  105. derivatives, multi-channel signed distance fields, multi-texture fonts, and more.
  106. See [link:https://github.com/felixmariotto/three-mesh-ui three-mesh-ui] or [link:https://github.com/Jam3/three-bmfont-text three-bmfont-text].
  107. </p>
  108. <p>
  109. Stock fonts are available in projects like
  110. [link:https://github.com/etiennepinchon/aframe-fonts A-Frame Fonts], or you can create your own
  111. from any .TTF font, optimizing to include only characters required for a project.
  112. </p>
  113. <p>
  114. Some helpful tools:
  115. </p>
  116. <ul>
  117. <li>[link:http://msdf-bmfont.donmccurdy.com/ msdf-bmfont-web] <i>(web-based)</i></li>
  118. <li>[link:https://github.com/soimy/msdf-bmfont-xml msdf-bmfont-xml] <i>(commandline)</i></li>
  119. <li>[link:https://github.com/libgdx/libgdx/wiki/Hiero hiero] <i>(desktop app)</i></li>
  120. </ul>
  121. </div>
  122. <h2>7. Troika Text</h2>
  123. <div>
  124. <p>
  125. The [link:https://www.npmjs.com/package/troika-three-text troika-three-text] package renders
  126. quality antialiased text using a similar technique as BMFonts, but works directly with any .TTF
  127. or .WOFF font file so you don't have to pregenerate a glyph texture offline. It also adds
  128. capabilities including:
  129. </p>
  130. <ul>
  131. <li>Effects like strokes, drop shadows, and curvature</li>
  132. <li>The ability to apply any three.js Material, even a custom ShaderMaterial</li>
  133. <li>Support for font ligatures, scripts with joined letters, and right-to-left/bidirectional layout</li>
  134. <li>Optimization for large amounts of dynamic text, performing most work off the main thread in a web worker</li>
  135. </ul>
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. <script src="../resources/prettify.js"></script>
  141. <script src="../resources/lesson.js"></script>
  142. </body></html>
粤ICP备19079148号