gate.threejs.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import Fm1 from './gate.threejs.two';
  4. import { animateCamera } from '/@/utils/threejs/util';
  5. import useEvent from '../../../../utils/threejs/useEvent';
  6. // 模型对象、 文字对象
  7. let model,
  8. fm1,
  9. fm2,
  10. group,
  11. fmType = '';
  12. const { mouseDownFn } = useEvent();
  13. // 打灯光
  14. const addLight = () => {
  15. const pointLight2 = new THREE.PointLight(0xffeeee, 1, 83);
  16. pointLight2.position.set(-101, 34, 16);
  17. pointLight2.shadow.bias = 0.05;
  18. model.scene.add(pointLight2);
  19. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  20. pointLight3.position.set(-61, 37, 13.9);
  21. pointLight3.shadow.bias = 0.05;
  22. model.scene.add(pointLight3);
  23. const pointLight4 = new THREE.PointLight(0xffeeee, 0.6, 300);
  24. pointLight4.position.set(-2, 26, 20);
  25. pointLight4.shadow.bias = 0.05;
  26. model.scene.add(pointLight4);
  27. const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 120);
  28. pointLight5.position.set(-54, 30, 23.8);
  29. pointLight5.shadow.bias = 0.05;
  30. model.scene.add(pointLight5);
  31. const pointLight7 = new THREE.PointLight(0xffffff, 1, 1000);
  32. pointLight7.position.set(45, 51, -4.1);
  33. pointLight7.shadow.bias = 0.05;
  34. model.scene.add(pointLight7);
  35. const spotLight = new THREE.SpotLight();
  36. spotLight.angle = Math.PI / 16;
  37. spotLight.penumbra = 0;
  38. spotLight.castShadow = true;
  39. spotLight.intensity = 1;
  40. spotLight.position.set(-231, 463, 687);
  41. model.scene.add(spotLight);
  42. spotLight.shadow.camera.near = 0.5; // default
  43. spotLight.shadow.camera.far = 1000; // default
  44. spotLight.shadow.focus = 1.2;
  45. spotLight.shadow.bias = -0.000002;
  46. };
  47. // 初始化左右摇摆动画
  48. const startAnimation = () => {
  49. // 定义鼠标点击事件
  50. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  51. model.canvasContainer?.addEventListener('pointerup', (event) => {
  52. event.stopPropagation();
  53. // 单道、 双道
  54. if (fmType === 'fm1') {
  55. fm1.mouseUpModel.call(fm1);
  56. } else if (fmType === 'fm2') {
  57. // fm2.mouseUpModel.call(fm2);
  58. }
  59. });
  60. };
  61. // 鼠标点击、松开事件
  62. const mouseEvent = (event) => {
  63. mouseDownFn(model, group, event, (intersects) => {
  64. if (fmType === 'fm1') {
  65. fm1.mousedownModel.call(fm1, intersects);
  66. } else if (fmType === 'fm2') {
  67. // fm2.mousedownModel.call(fm2, intersects);
  68. }
  69. });
  70. console.log('摄像头控制信息', model.orbitControls, model.camera);
  71. };
  72. export const addMonitorText = (selectData) => {
  73. if (fmType === 'fm1') {
  74. return fm1.addMonitorText.call(fm1, selectData);
  75. } else {
  76. // return fm2.addMonitorText.call(fm2, selectData);
  77. }
  78. };
  79. export const deviceDetailCard = () => {
  80. if (fmType === 'fm1') {
  81. return fm1.deviceDetailCard.call(fm1);
  82. } else {
  83. // return fm2.addMonitorText.call(fm2);
  84. }
  85. };
  86. export const play = (rotationParam, flag) => {
  87. if (fmType === 'fm1') {
  88. return fm1.play.call(fm1, rotationParam, flag);
  89. } else {
  90. // return fm2.play.call(fm2, rotationParam, flag);
  91. }
  92. };
  93. // 切换风门类型
  94. export const setModelType = (type) => {
  95. fmType = type;
  96. return new Promise((resolve) => {
  97. // 暂停风门1动画
  98. fm1.clipActionArr.frontDoor.reset();
  99. fm1.clipActionArr.frontDoor.time = 0.5;
  100. fm1.clipActionArr.backDoor.reset();
  101. fm1.clipActionArr.backDoor.time = 0.5;
  102. fm1.clipActionArr.frontDoor.stop();
  103. fm1.clipActionArr.backDoor.stop();
  104. if (fmType === 'fm1') {
  105. model.startAnimation = fm1.render.bind(fm1);
  106. group = fm1.group;
  107. group.rotation.y = 0;
  108. if (model.scene.getObjectByName('fm')) {
  109. model.scene.remove(fm2.group);
  110. }
  111. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  112. model.scene.add(fm1.group);
  113. setTimeout(async () => {
  114. resolve(null);
  115. await animateCamera(
  116. oldCameraPosition,
  117. { x: 0, y: 0, z: 0 },
  118. { x: 50.99, y: 69.32, z: 93.61 },
  119. { x: -10.04, y: -14.38, z: -31.4 },
  120. model,
  121. 0.8
  122. );
  123. }, 300);
  124. } else if (fmType === 'fm2') {
  125. // 显示单道风窗
  126. // model.startAnimation = fm2.render.bind(fm2);
  127. // group = fm2.group;
  128. // if (model.scene.getObjectByName('fm2')) {
  129. // model.scene.remove(fm1.group);
  130. // }
  131. // const oldCameraPosition = { x: 100, y: 0, z: 500 };
  132. // model.scene.add(fm2.group);
  133. // setTimeout(async () => {
  134. // resolve(null);
  135. // const position = { x: 0, y: 0, z: 0 };
  136. // await animateCamera(
  137. // oldCameraPosition,
  138. // { x: 0, y: 0, z: 0 },
  139. // { x: 66.257, y: 57.539, z: 94.313 },
  140. // { x: position.x, y: position.y, z: position.z },
  141. // model
  142. // );
  143. // }, 300);
  144. }
  145. });
  146. };
  147. export const mountedThree = (playerVal1, playerVal2) => {
  148. return new Promise(async (resolve) => {
  149. model = new UseThree('#damper3D', '', '#deviceDetail');
  150. model.setEnvMap('test1');
  151. model.renderer.toneMappingExposure = 0.9;
  152. model.camera.position.set(100, 0, 1000);
  153. // 单道、 双道
  154. fm1 = new Fm1(model, playerVal1, playerVal2);
  155. // fm2 = new singleWindow(model, playerVal1);
  156. await fm1.mountedThree();
  157. // await singleWindowObj.mountedThree();
  158. model.animate();
  159. resolve(null);
  160. addLight();
  161. startAnimation();
  162. });
  163. };
  164. export const destroy = () => {
  165. if (model) {
  166. fm1.destroy();
  167. // fm2.destroy();
  168. model.deleteModal();
  169. model = null;
  170. group = null;
  171. fm1 = null;
  172. fm2 = null;
  173. }
  174. };