nitrogen.threejs.base.ts 3.3 KB

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