spray.threejs.base.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as THREE from 'three';
  2. import { CSS3DSprite } from 'three/examples/jsm/renderers/CSS3DRenderer';
  3. import { setModalCenter } from '/@/utils/threejs/util';
  4. // import { setModalCenter } from '/@/utils/threejs/util';
  5. // import * as dat from 'dat.gui';
  6. // const gui = new dat.GUI();
  7. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  8. class ModelContext {
  9. model;
  10. modelName = 'tunFace';
  11. // modelName = 'dedust';
  12. group: THREE.Object3D | null = null;
  13. /** 本模型内支持的锚点位置数组 */
  14. anchors: [number, number, number][] = [
  15. [-46.1, -12.8, 8],
  16. // [0, 0, 0],
  17. // 右侧传感器
  18. [11.7, -14, 8],
  19. // [-25.847, 8.783, 3.267],
  20. // [31.142, 8.783, 3.267],
  21. ];
  22. /** 本模型显示的css详情元素数组 */
  23. cssSprites: CSS3DSprite[] = [];
  24. constructor(model) {
  25. this.model = model;
  26. }
  27. addLight() {
  28. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  29. directionalLight.position.set(6.3, 28, 20);
  30. this.group?.add(directionalLight);
  31. directionalLight.target = this.group as THREE.Object3D;
  32. const pointLight = new THREE.PointLight(0xffffff, 1, 1000);
  33. pointLight.position.set(45, 51, -4.1);
  34. pointLight.shadow.bias = 0.05;
  35. this.model.scene.add(pointLight);
  36. }
  37. /** 初始化css元素,将css元素选择器传入,该方法会将这些元素按顺序放入本模型支持的锚点中 */
  38. initCssElement(selectors: string[]) {
  39. selectors.forEach((selector, index) => {
  40. const element = document.querySelector(selector) as HTMLElement;
  41. if (element) {
  42. const css3D = new CSS3DSprite(element);
  43. this.cssSprites.push(css3D);
  44. css3D.name = selector;
  45. css3D.scale.set(0.05, 0.05, 0.05);
  46. // const ff = gui.addFolder(`css元素${index}`);
  47. // ff.add(css3D.position, 'x', -100, 100);
  48. // ff.add(css3D.position, 'y', -100, 100);
  49. // ff.add(css3D.position, 'z', -100, 100);
  50. if (index < this.anchors.length) {
  51. const [x, y, z] = this.anchors[index];
  52. css3D.position.set(x, y, z);
  53. this.group?.add(css3D);
  54. } else {
  55. console.warn(`指定的元素${selector}没有合适的位置放置`);
  56. }
  57. }
  58. });
  59. }
  60. /** 清除css元素 */
  61. clearCssElement() {
  62. this.cssSprites.forEach((sprite) => {
  63. this.group?.remove(sprite);
  64. });
  65. this.cssSprites = [];
  66. }
  67. mountedThree() {
  68. return new Promise((resolve) => {
  69. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  70. this.group = gltf[0];
  71. if (this.group) {
  72. setModalCenter(this.group);
  73. resolve(null);
  74. this.addLight();
  75. }
  76. });
  77. });
  78. }
  79. destroy() {
  80. if (this.model) {
  81. this.model.clearGroup(this.group);
  82. this.model = null;
  83. this.group = null;
  84. }
  85. }
  86. }
  87. export default ModelContext;