windrect.threejs.ts 8.3 KB

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