spray.threejs.base.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = 'spray-ts';
  11. // modelName = 'dedust';
  12. group: THREE.Object3D | null = null;
  13. /** 本模型内支持的锚点位置数组 */
  14. anchors: [number, number, number][] = [
  15. [-52.838, 18.5, 9.219],
  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. /** 初始化时用到的摄像头位置参数,可以供外部访问或初始化时使用 */
  25. cameraPostion = {
  26. x: 0.03683131196052705,
  27. y: -0.010096816429235923,
  28. z: 1.217419706517893,
  29. };
  30. /** 初始化时用到的滑轨目标参数,可以供外部访问或初始化时使用,配合摄像头可以实现更精确的控制 */
  31. // orbitControlPostion = {
  32. // x: 0.11268219812743818,
  33. // y: 1.33090491940089,
  34. // z: 2.379502155322738,
  35. // };
  36. orbitControlPostion = {
  37. x: 0,
  38. y: 0,
  39. z: 0,
  40. };
  41. constructor(model) {
  42. this.model = model;
  43. }
  44. addLight() {
  45. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  46. directionalLight.position.set(6.3, 28, 20);
  47. this.group?.add(directionalLight);
  48. directionalLight.target = this.group as THREE.Object3D;
  49. const pointLight = new THREE.PointLight(0xffffff, 1, 1000);
  50. pointLight.position.set(45, 51, -4.1);
  51. pointLight.shadow.bias = 0.05;
  52. this.model.scene.add(pointLight);
  53. }
  54. /** 初始化css元素,将css元素选择器传入,该方法会将这些元素按顺序放入本模型支持的锚点中 */
  55. initCssElement(selectors: string[]) {
  56. selectors.forEach((selector, index) => {
  57. const element = document.querySelector(selector) as HTMLElement;
  58. if (element) {
  59. const css3D = new CSS3DSprite(element);
  60. this.cssSprites.push(css3D);
  61. css3D.name = selector;
  62. css3D.scale.set(0.05, 0.05, 0.05);
  63. // const ff = gui.addFolder(`css元素${index}`);
  64. // ff.add(css3D.position, 'x', -100, 100);
  65. // ff.add(css3D.position, 'y', -100, 100);
  66. // ff.add(css3D.position, 'z', -100, 100);
  67. if (index < this.anchors.length) {
  68. const [x, y, z] = this.anchors[index];
  69. css3D.position.set(x, y, z);
  70. this.group?.add(css3D);
  71. } else {
  72. console.warn(`指定的元素${selector}没有合适的位置放置`);
  73. }
  74. }
  75. });
  76. }
  77. /** 清除css元素 */
  78. clearCssElement() {
  79. this.cssSprites.forEach((sprite) => {
  80. this.group?.remove(sprite);
  81. });
  82. this.cssSprites = [];
  83. }
  84. mountedThree() {
  85. return new Promise((resolve) => {
  86. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  87. this.group = gltf[0];
  88. if (this.group) {
  89. setModalCenter(this.group);
  90. resolve(null);
  91. // this.addLight();
  92. }
  93. });
  94. });
  95. }
  96. destroy() {
  97. if (this.model) {
  98. this.model.clearGroup(this.group);
  99. this.model = null;
  100. this.group = null;
  101. }
  102. }
  103. }
  104. export default ModelContext;