windrect.threejs.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import * as THREE from 'three';
  2. import { animateCamera, getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import UseThree from '../../../../utils/threejs/useThree';
  4. import lmWindRect from './longmen.threejs';
  5. import zdWindRect from './zhedie.threejs';
  6. import dsWindRect from './duishe.threejs';
  7. import { useAppStore } from '/@/store/modules/app';
  8. import gsap from 'gsap';
  9. import * as dat from 'dat.gui';
  10. // const gui = new dat.GUI();
  11. // 模型对象、 文字对象
  12. let model, //
  13. group,
  14. lmWindRectObj,
  15. zdWindRectObj,
  16. dsWindRectObj,
  17. windRectType = 'lmWindRect';
  18. const appStore = useAppStore();
  19. // 打灯光
  20. const addLight = () => {
  21. // if (windRectType === 'lmWindRect') {
  22. // lmWindRectObj.addLight.call(lmWindRectObj);
  23. // } else if (windRectType === 'zdWindRect') {
  24. // zdWindRectObj.addLight.call(zdWindRectObj);
  25. // } else if (windRectType === 'dsWindRect') {
  26. // dsWindRectObj.addLight.call(dsWindRectObj);
  27. // }
  28. const pointLight2 = new THREE.PointLight(0xffeeee, 1, 0);
  29. pointLight2.position.set(317, 41, -21);
  30. pointLight2.shadow.bias = 0.05;
  31. model.scene.add(pointLight2);
  32. //
  33. const pointLight3 = new THREE.PointLight(0xffffff, 1, 138);
  34. pointLight3.position.set(-202, 50, 27.8);
  35. pointLight3.shadow.bias = 0.05;
  36. model.scene.add(pointLight3);
  37. //
  38. const pointLight4 = new THREE.PointLight(0xffeeee, 1, 162);
  39. pointLight4.position.set(83, 62, -14);
  40. pointLight4.shadow.bias = 0.05;
  41. model.scene.add(pointLight4);
  42. //
  43. const pointLight5 = new THREE.PointLight(0xffffff, 0.8, 176);
  44. pointLight5.position.set(-123, 61.6, -18);
  45. pointLight5.shadow.bias = 0.05;
  46. model.scene.add(pointLight5);
  47. //
  48. const pointLight6 = new THREE.PointLight(0xffffff, 1.5, 64);
  49. pointLight6.position.set(-76, 48, 18);
  50. pointLight6.shadow.bias = 0.05;
  51. model.scene.add(pointLight6);
  52. const pointLight7 = new THREE.PointLight(0xffffff, 1, 302);
  53. pointLight7.position.set(-220, -177, -3.5);
  54. pointLight7.shadow.bias = -0.05;
  55. model.scene.add(pointLight7);
  56. const spotLight = new THREE.SpotLight();
  57. spotLight.angle = Math.PI / 16;
  58. spotLight.penumbra = 0;
  59. spotLight.castShadow = true;
  60. spotLight.distance = 0;
  61. spotLight.position.set(-470, -500, 500);
  62. model.scene.add(spotLight);
  63. spotLight.shadow.camera.near = 0.5; // default
  64. spotLight.shadow.camera.far = 1000; // default
  65. spotLight.shadow.focus = 1;
  66. spotLight.shadow.bias = -0.000002;
  67. };
  68. // 重置摄像头
  69. const resetCamera = () => {
  70. model.camera.position.set(0, 0, 500);
  71. model.camera.rotation.set(-27.88, 14.35, 7.47);
  72. model.orbitControls?.update();
  73. model.camera.updateProjectionMatrix();
  74. };
  75. // 初始化左右摇摆动画
  76. const startAnimation = () => {
  77. // 定义鼠标点击事件
  78. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  79. model.canvasContainer?.addEventListener('pointerup', (event) => {
  80. event.stopPropagation();
  81. // 单道、 双道
  82. if (windRectType === 'lmWindRect') {
  83. lmWindRectObj.mouseUpModel.call(lmWindRectObj);
  84. } else if (windRectType === 'zdWindRect') {
  85. zdWindRectObj.mouseUpModel.call(zdWindRectObj);
  86. } else if (windRectType === 'dsWindRect') {
  87. dsWindRectObj.mouseUpModel.call(dsWindRectObj);
  88. }
  89. });
  90. };
  91. // 鼠标点击、松开事件
  92. const mouseEvent = (event) => {
  93. event.stopPropagation();
  94. const widthScale = appStore.getWidthScale;
  95. const heightScale = appStore.getHeightScale;
  96. // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
  97. model.mouse.x =
  98. ((-model.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (model.canvasContainer.clientWidth * widthScale)) * 2 - 1;
  99. model.mouse.y =
  100. -((-model.canvasContainer.getBoundingClientRect().top + event.clientY) / (model.canvasContainer.clientHeight * heightScale)) * 2 + 1;
  101. (model.rayCaster as THREE.Raycaster).setFromCamera(model.mouse, model.camera as THREE.Camera);
  102. if (group) {
  103. const intersects = model.rayCaster?.intersectObjects(group.children, false) as THREE.Intersection[];
  104. if (intersects.length > 0) {
  105. // 单道、 双道
  106. if (windRectType === 'lmWindRect') {
  107. lmWindRectObj.mousedownModel.call(lmWindRectObj, intersects);
  108. } else if (windRectType === 'zdWindRect') {
  109. zdWindRectObj.mousedownModel.call(zdWindRectObj, intersects);
  110. } else if (windRectType === 'dsWindRect') {
  111. dsWindRectObj.mousedownModel.call(dsWindRectObj, intersects);
  112. }
  113. }
  114. }
  115. };
  116. /* 添加监控数据 */
  117. export const addFmText = (selectData) => {
  118. if (windRectType === 'lmWindRect') {
  119. return lmWindRectObj.addFmText.call(lmWindRectObj, selectData);
  120. } else if (windRectType === 'zdWindRect') {
  121. return zdWindRectObj.addFmText.call(zdWindRectObj, selectData);
  122. } else if (windRectType === 'dsWindRect') {
  123. return dsWindRectObj.addFmText.call(dsWindRectObj, selectData);
  124. }
  125. };
  126. export const play = (flag, isDirect = false) => {
  127. if (windRectType === 'lmWindRect') {
  128. return lmWindRectObj.play.call(lmWindRectObj, flag, isDirect);
  129. } else if (windRectType === 'zdWindRect') {
  130. return zdWindRectObj.play.call(zdWindRectObj, flag, isDirect);
  131. } else if (windRectType === 'dsWindRect') {
  132. return dsWindRectObj.play.call(dsWindRectObj, flag, isDirect);
  133. }
  134. };
  135. // 切换风窗类型
  136. export const setModelType = (type) => {
  137. windRectType = type;
  138. model.camera.position.set(-1000, 100, 500);
  139. return new Promise((resolve) => {
  140. // 显示双道风窗
  141. if (windRectType === 'lmWindRect') {
  142. model.startAnimation = lmWindRectObj.render.bind(lmWindRectObj);
  143. group = lmWindRectObj.group;
  144. // const cfTanTou = lmWindRectObj.group?.getObjectByName('lmTanTou') as THREE.Group;
  145. // cfTanTou.position.setY(0);
  146. lmWindRectObj.deviceRunState == '';
  147. if (model.scene.getObjectByName('zdcf')) {
  148. model.scene.remove(zdWindRectObj.group);
  149. }
  150. if (model.scene.getObjectByName('dscf')) {
  151. model.scene.remove(dsWindRectObj.group);
  152. }
  153. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  154. model.scene.add(lmWindRectObj.group);
  155. setTimeout(async () => {
  156. resolve(null);
  157. const position = lmWindRectObj.group.position;
  158. await animateCamera(
  159. oldCameraPosition,
  160. oldCameraPosition,
  161. { x: 46.257, y: 57.539, z: 94.313 },
  162. { x: position.x, y: position.y, z: position.z },
  163. model,
  164. 0.8
  165. );
  166. }, 300);
  167. } else if (windRectType === 'zdWindRect') {
  168. // 停用混合器上所有预定的动作
  169. zdWindRectObj.animationAction.time = 0;
  170. model.startAnimation = zdWindRectObj.render.bind(zdWindRectObj);
  171. group = zdWindRectObj.group;
  172. if (model.scene.getObjectByName('lmcf')) {
  173. model.scene.remove(lmWindRectObj.group);
  174. }
  175. if (model.scene.getObjectByName('dscf')) {
  176. model.scene.remove(dsWindRectObj.group);
  177. }
  178. model.scene.add(zdWindRectObj.group);
  179. const oldCameraPosition = { x: -1000, y: 100, z: 500 };
  180. setTimeout(async () => {
  181. resolve(null);
  182. const position = zdWindRectObj.group.position;
  183. await animateCamera(
  184. oldCameraPosition,
  185. oldCameraPosition,
  186. { x: 66.257, y: 57.539, z: 104.313 },
  187. { x: position.x, y: position.y, z: position.z },
  188. model,
  189. 0.8
  190. );
  191. }, 300);
  192. } else if (windRectType === 'dsWindRect') {
  193. dsWindRectObj.isRun = false
  194. model.startAnimation = dsWindRectObj.render.bind(dsWindRectObj);
  195. group = dsWindRectObj.group;
  196. const dsTanTou = dsWindRectObj.group?.getObjectByName('dsTanTou') as THREE.Group;
  197. dsTanTou.position.setY(0);
  198. if (model.scene.getObjectByName('lmcf')) {
  199. model.scene.remove(lmWindRectObj.group);
  200. }
  201. if (model.scene.getObjectByName('zdcf')) {
  202. model.scene.remove(zdWindRectObj.group);
  203. }
  204. model.scene.add(dsWindRectObj.group);
  205. setTimeout(async () => {
  206. resolve(null);
  207. const position = dsWindRectObj.group.position;
  208. const oldCameraPosition = { x: -1020, y: 100, z: 500 };
  209. await animateCamera(
  210. oldCameraPosition,
  211. oldCameraPosition,
  212. { x: 46.257, y: 57.539, z: 104.313 },
  213. { x: position.x, y: position.y, z: position.z },
  214. model,
  215. 0.8
  216. );
  217. }, 300);
  218. }
  219. });
  220. };
  221. export const mountedThree = (playerVal1, playerVal2) => {
  222. return new Promise(async (resolve) => {
  223. model = new UseThree('#window3D');
  224. model.setEnvMap('test1');
  225. model.renderer.toneMappingExposure = 0.8;
  226. addLight();
  227. lmWindRectObj = new lmWindRect(model, playerVal1, playerVal2);
  228. await lmWindRectObj.mountedThree();
  229. zdWindRectObj = new zdWindRect(model, playerVal1);
  230. await zdWindRectObj.mountedThree();
  231. dsWindRectObj = new dsWindRect(model, playerVal1);
  232. await dsWindRectObj.mountedThree();
  233. resolve(null);
  234. model.animate();
  235. startAnimation();
  236. });
  237. };
  238. export const destroy = () => {
  239. if (model) {
  240. lmWindRectObj?.destroy();
  241. zdWindRectObj?.destroy();
  242. zdWindRectObj?.destroy();
  243. model.deleteModal();
  244. model = null;
  245. group = null;
  246. }
  247. };