coordinateAxes.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as THREE from 'three'
  2. import {
  3. FontLoader
  4. } from "three/examples/jsm/loaders/FontLoader.js"
  5. import {
  6. TextGeometry
  7. } from "three/examples/jsm/geometries/TextGeometry.js"
  8. let publicPath = process.env.BASE_URL
  9. export default class CoordinateAxes extends THREE.Object3D {
  10. name = 'COORDINATE_AXES'
  11. AXIS_LENGTH = 150
  12. // follows right-hand coordinate system
  13. AXIS_COLOR_X = 0xff0000 // red
  14. AXIS_COLOR_Y = 0x00ff00 // green
  15. AXIS_COLOR_Z = 0x0000ff // blue
  16. constructor() {
  17. super()
  18. const origin = new THREE.Vector3(0, 0, 0)
  19. const axisX = new THREE.Vector3(1, 0, 0)
  20. const axisY = new THREE.Vector3(0, 1, 0)
  21. const axisZ = new THREE.Vector3(0, 0, 1)
  22. const arrowX = new THREE.ArrowHelper(axisX, origin, this.AXIS_LENGTH, this.AXIS_COLOR_X, this.AXIS_LENGTH / 5, this.AXIS_LENGTH / 10)
  23. const arrowY = new THREE.ArrowHelper(axisY, origin, this.AXIS_LENGTH, this.AXIS_COLOR_Y, this.AXIS_LENGTH / 5, this.AXIS_LENGTH / 10)
  24. const arrowZ = new THREE.ArrowHelper(axisZ, origin, this.AXIS_LENGTH, this.AXIS_COLOR_Z, this.AXIS_LENGTH / 5, this.AXIS_LENGTH / 10)
  25. this.add(arrowX, arrowY, arrowZ)
  26. // an additional box at the origin
  27. // const sphere = new THREE.SphereGeometry(this.AXIS_LENGTH / 20)
  28. // const object = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({
  29. // color: 0xffff00
  30. // }))
  31. // const box = new THREE.BoxHelper(object, 0xffff00)
  32. // this.add(box)
  33. let that = this
  34. const loader = new FontLoader();
  35. loader.load(
  36. // 资源URL
  37. `${publicPath}font.json`,
  38. // onLoad回调
  39. function (font) {
  40. // do something with the font
  41. // console.log(font);
  42. const geometryX = new TextGeometry('X', {
  43. font: font,
  44. size: 40, // 字体大小
  45. height: 2, // 字体深度
  46. curveSegments: 12, // 曲线控制点数
  47. bevelEnabled: true, // 斜角
  48. bevelThickness: 0.1, // 斜角的深度
  49. bevelSize: 1, // 斜角的大小
  50. bevelSegments: 1 // 斜角段数
  51. });
  52. var matX = new THREE.MeshBasicMaterial({
  53. color: 0x0000ff,
  54. // opacity: 0.6,
  55. // shininess: 1,
  56. // transparent: true,
  57. });
  58. var meshX = new THREE.Mesh(geometryX, matX);
  59. meshX.rotation.y = -Math.PI / 2
  60. // meshX.rotation.z = Math.PI / 10
  61. // meshX.position.set(0, 0, 160);
  62. meshX.position.set(0, 0, 36);
  63. that.add(meshX);
  64. const geometryY = new TextGeometry('Z', {
  65. font: font,
  66. size: 34, // 字体大小
  67. height: 2, // 字体深度
  68. curveSegments: 12, // 曲线控制点数
  69. bevelEnabled: true, // 斜角
  70. bevelThickness: 0.1, // 斜角的深度
  71. bevelSize: 1, // 斜角的大小
  72. bevelSegments: 1 // 斜角段数
  73. });
  74. var matY = new THREE.MeshBasicMaterial({
  75. color: 0x00ff00,
  76. });
  77. var meshY = new THREE.Mesh(geometryY, matY);
  78. meshY.rotation.y = Math.PI / 4.5
  79. // meshY.position.set(0, 160, 0);
  80. meshY.position.set(0, 36, 0);
  81. that.add(meshY);
  82. const geometryZ = new TextGeometry('Y', {
  83. font: font,
  84. size: 40, // 字体大小
  85. height: 2, // 字体深度
  86. curveSegments: 12, // 曲线控制点数
  87. bevelEnabled: true, // 斜角
  88. bevelThickness: 0.1, // 斜角的深度
  89. bevelSize: 1, // 斜角的大小
  90. bevelSegments: 1 // 斜角段数
  91. });
  92. var matZ = new THREE.MeshBasicMaterial({
  93. color: 0xff0000,
  94. });
  95. var meshZ = new THREE.Mesh(geometryZ, matZ);
  96. // meshZ.rotation.y = Math.PI / 5
  97. // meshZ.rotation.x = -Math.PI / 6
  98. // meshZ.rotation.z = Math.PI / 6
  99. // meshZ.position.set(160, 0, 0);
  100. meshZ.position.set(36, 0, 0);
  101. that.add(meshZ);
  102. },
  103. // onProgress回调
  104. function (xhr) {
  105. // console.log((xhr.loaded / xhr.total * 100) + '% loaded');
  106. },
  107. // onError回调
  108. function (err) {
  109. console.log('An error happened');
  110. }
  111. );
  112. }
  113. }