TextGeometry.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. ExtrudeGeometry
  3. } from 'three';
  4. /**
  5. * A class for generating text as a single geometry. It is constructed by providing a string of text, and a set of
  6. * parameters consisting of a loaded font and extrude settings.
  7. *
  8. * See the {@link FontLoader} page for additional details.
  9. *
  10. * `TextGeometry` uses [typeface.json]{@link http://gero3.github.io/facetype.js/} generated fonts.
  11. * Some existing fonts can be found located in `/examples/fonts`.
  12. *
  13. * ```js
  14. * const loader = new FontLoader();
  15. * const font = await loader.loadAsync( 'fonts/helvetiker_regular.typeface.json' );
  16. * const geometry = new TextGeometry( 'Hello three.js!', {
  17. * font: font,
  18. * size: 80,
  19. * depth: 5,
  20. * curveSegments: 12
  21. * } );
  22. * ```
  23. *
  24. * @augments ExtrudeGeometry
  25. * @three_import import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
  26. */
  27. class TextGeometry extends ExtrudeGeometry {
  28. /**
  29. * Constructs a new text geometry.
  30. *
  31. * @param {string} text - The text that should be transformed into a geometry.
  32. * @param {TextGeometry~Options} [parameters] - The text settings.
  33. */
  34. constructor( text, parameters = {} ) {
  35. const font = parameters.font;
  36. if ( font === undefined ) {
  37. super(); // generate default extrude geometry
  38. } else {
  39. const shapes = font.generateShapes( text, parameters.size );
  40. // defaults
  41. if ( parameters.depth === undefined ) parameters.depth = 50;
  42. if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10;
  43. if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8;
  44. if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false;
  45. super( shapes, parameters );
  46. }
  47. this.type = 'TextGeometry';
  48. }
  49. }
  50. /**
  51. * Represents the `options` type of the geometry's constructor.
  52. *
  53. * @typedef {Object} TextGeometry~Options
  54. * @property {Font} [font] - The font.
  55. * @property {number} [size=100] - The text size.
  56. * @property {number} [depth=50] - Depth to extrude the shape.
  57. * @property {number} [curveSegments=12] - Number of points on the curves.
  58. * @property {number} [steps=1] - Number of points used for subdividing segments along the depth of the extruded spline.
  59. * @property {boolean} [bevelEnabled=false] - Whether to beveling to the shape or not.
  60. * @property {number} [bevelThickness=10] - How deep into the original shape the bevel goes.
  61. * @property {number} [bevelSize=8] - Distance from the shape outline that the bevel extends.
  62. * @property {number} [bevelOffset=0] - Distance from the shape outline that the bevel starts.
  63. * @property {number} [bevelSegments=3] - Number of bevel layers.
  64. * @property {?Curve} [extrudePath=null] - A 3D spline path along which the shape should be extruded. Bevels not supported for path extrusion.
  65. * @property {Object} [UVGenerator] - An object that provides UV generator functions for custom UV generation.
  66. **/
  67. export { TextGeometry };