three3D.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!-- eslint-disable vue/multi-word-component-names -->
  2. <template>
  3. <div id="three3D"></div>
  4. </template>
  5. <script lang="ts" setup>
  6. import { ref, watch, onMounted, onUnmounted } from 'vue';
  7. import UseThree from '/@/utils/threejs/useThree';
  8. import { animateCamera } from '/@/utils/threejs/util';
  9. import * as THREE from 'three';
  10. import useEvent from '/@/utils/threejs/useEvent';
  11. const props = defineProps<{
  12. modalName: string;
  13. }>();
  14. let modal, modalGroup;
  15. const mouseEvent = () => {
  16. console.log(modal.camera, modal.orbitControls);
  17. };
  18. const addMouseEvent = () => {
  19. // 定义鼠标点击事件
  20. modal.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  21. };
  22. const addLight = () => {
  23. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  24. directionalLight.position.set(-110, 150, 647);
  25. modalGroup?.add(directionalLight);
  26. };
  27. const setModelType = () => {
  28. return new Promise(async (resolve) => {
  29. const modalConfigurations = {
  30. workFace11: {
  31. render: null,
  32. group: modalGroup ? modalGroup : null,
  33. newP: { x: -2.211555197992825, y: 27.130029732875393, z: 2.3018919451652007 },
  34. newT: { x: -2.211555197992825, y: -8.604453425019353, z: 2.301856157557903 },
  35. },
  36. jdds: {
  37. render: null,
  38. group: modalGroup ? modalGroup : null,
  39. newP: { x: 0.22197787154285728, y: 29.010792085965782, z: 2.477736279196267 },
  40. newT: { x: 0.22197787154285728, y: -8.604453177192061, z: 2.477698375233975 },
  41. },
  42. };
  43. await initModal();
  44. if (modalConfigurations[props.modalName]) {
  45. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  46. const config = modalConfigurations[props.modalName];
  47. setTimeout(async () => {
  48. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, config.newP, config.newT, modal);
  49. modal.scene?.add(modalGroup);
  50. resolve(null);
  51. }, 1000);
  52. }
  53. });
  54. };
  55. const customModal = () => {
  56. if (props.modalName == 'workFace11') {
  57. const DiXing = modalGroup.getObjectByName('PouMian01')?.getObjectByName('DiXing')?.getObjectByName('DiXing_1');
  58. if (DiXing) {
  59. DiXing.visible = false;
  60. }
  61. }
  62. };
  63. const initModal = () => {
  64. return new Promise((resolve) => {
  65. modal.setGLTFModel(props.modalName).then(async (gltf) => {
  66. const gltfModal = gltf[0];
  67. modalGroup = gltfModal;
  68. console.log('模型文件', gltfModal);
  69. addLight();
  70. customModal();
  71. modal.animate();
  72. addMouseEvent();
  73. // this.group.name = this.modelName;
  74. // setModalCenter(this.group);
  75. // this.group.scale.set(2.5, 2.5, 2.5);
  76. // this.setThreePlane();
  77. // this.setControls();
  78. resolve(null);
  79. });
  80. });
  81. };
  82. watch(
  83. () => props.modalName,
  84. async (val) => {
  85. if (val) {
  86. await setModelType();
  87. }
  88. }
  89. );
  90. onMounted(async () => {
  91. modal = new UseThree('#three3D', '');
  92. if (modal) {
  93. modal.setEnvMap('test1.hdr');
  94. modal.renderer.toneMappingExposure = 1.0;
  95. modal.renderer.sortObjects = true;
  96. modal.orbitControls.update();
  97. await setModelType();
  98. }
  99. });
  100. onUnmounted(() => {
  101. modal.clearGroup(modalGroup);
  102. modal.destroy();
  103. modalGroup = null;
  104. modal = null;
  105. });
  106. </script>
  107. <style lang="less" scoped>
  108. #three3D {
  109. width: 100%;
  110. height: 100%;
  111. position: absolute;
  112. top: 0;
  113. left: 0;
  114. z-index: 0;
  115. }
  116. </style>