window.threejs.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import singleWindow from './dandaoFc.threejs';
  4. import doubleWindow from './shuangdaoFc.threejs';
  5. import singleWindowXk from './dandaoFcXk.threejs';
  6. import { animateCamera } from '/@/utils/threejs/util';
  7. import useEvent from '../../../../utils/threejs/useEvent';
  8. import { useGlobSetting } from '/@/hooks/setting';
  9. import { modal } from 'vxe-table';
  10. // import * as dat from 'dat.gui';
  11. // const gui = new dat.GUI();
  12. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  13. // 模型对象、 文字对象
  14. let model: UseThree,
  15. singleWindowObj,
  16. doubleWindowObj: doubleWindow,
  17. singleWindowXkObj: singleWindowXk,
  18. group: THREE.Object3D,
  19. windowType = 'singleWindow';
  20. const rotationParam = {
  21. frontDeg0: 0, // 前门初始
  22. frontDeg1: 0, // 前门目标
  23. backDeg0: 0, // 后门初始
  24. backDeg1: 0, // 后门目标
  25. };
  26. const { mouseDownFn } = useEvent();
  27. // 打灯光
  28. const addLight = () => {
  29. if (!model || !model.scene) return;
  30. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  31. directionalLight.position.set(-110, 150, 647);
  32. model.scene?.add(directionalLight);
  33. // directionalLight.target = group;
  34. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  35. pointLight2.position.set(-101, 34, 16);
  36. pointLight2.shadow.bias = 0.05;
  37. model.scene.add(pointLight2);
  38. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  39. pointLight3.position.set(19, 25, -7);
  40. pointLight3.shadow.bias = 0.05;
  41. model.scene.add(pointLight3);
  42. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  43. pointLight6.position.set(51, 51, 9);
  44. pointLight6.shadow.bias = 0.05;
  45. model.scene.add(pointLight6);
  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 (windowType === 'doubleWindow' && doubleWindowObj) {
  55. doubleWindowObj.mouseUpModel.call(doubleWindowObj);
  56. } else if (windowType === 'singleWindow' && singleWindowObj) {
  57. singleWindowObj.mouseUpModel.call(singleWindowObj);
  58. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  59. singleWindowXkObj.mouseUpModel.call(singleWindowXkObj);
  60. }
  61. });
  62. };
  63. // 鼠标点击、松开事件
  64. const mouseEvent = (event) => {
  65. if (event.button == 0) {
  66. mouseDownFn(model, group, event, (intersects) => {
  67. if (windowType === 'doubleWindow' && doubleWindowObj) {
  68. doubleWindowObj.mousedownModel.call(doubleWindowObj, intersects);
  69. } else if (windowType === 'singleWindow' && singleWindowObj) {
  70. singleWindowObj.mousedownModel.call(singleWindowObj, intersects);
  71. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  72. singleWindowXkObj.mousedownModel.call(singleWindowXkObj, intersects);
  73. }
  74. });
  75. console.log('摄像头控制信息', model.orbitControls, model.camera);
  76. }
  77. };
  78. export const addMonitorText = (selectData) => {
  79. if (windowType === 'doubleWindow' && doubleWindowObj) {
  80. return doubleWindowObj.addMonitorText.call(doubleWindowObj, selectData);
  81. } else if (windowType === 'singleWindow' && singleWindowObj) {
  82. return singleWindowObj.addMonitorText.call(singleWindowObj, selectData);
  83. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  84. return singleWindowXkObj.addMonitorText.call(singleWindowXkObj, selectData);
  85. }
  86. };
  87. export function computePlay(data, maxarea, isFirst = false) {
  88. if (windowType === 'doubleWindow' || windowType === 'singleWindow') {
  89. if (!maxarea) maxarea = 90;
  90. rotationParam.frontDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.forntArea);
  91. rotationParam.backDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.rearArea);
  92. rotationParam.frontDeg1 = (90 / maxarea) * Number(data.forntArea) || 0;
  93. rotationParam.backDeg1 = (90 / maxarea) * Number(data.rearArea) || 0;
  94. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  95. // 当返回值有误时默认关闭
  96. play(rotationParam, 0);
  97. } else {
  98. if (data.nwindownum == 1 || data.nwindownum == 2) {
  99. setTimeout(() => {
  100. play(rotationParam, 1);
  101. }, 0);
  102. }
  103. if (data.nwindownum == 2) {
  104. setTimeout(() => {
  105. play(rotationParam, 2);
  106. }, 0);
  107. }
  108. }
  109. } else if (windowType === 'singleXkWindow') {
  110. const acosToAngle = (cosValue) => {
  111. cosValue = Math.max(Math.min(cosValue, 1), -1);
  112. // 计算角度
  113. return Math.asin(cosValue) * (180 / Math.PI);
  114. };
  115. const sina = Math.sqrt((Math.sin((78 * Math.PI) / 180) ** 2 * parseFloat(data.forntArea)) / parseFloat(maxarea));
  116. const angleInRadians = acosToAngle(sina);
  117. rotationParam.frontDeg1 = angleInRadians;
  118. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  119. // 当返回值有误时默认关闭
  120. play(rotationParam, 0);
  121. } else {
  122. setTimeout(() => {
  123. play(rotationParam, 1);
  124. }, 0);
  125. }
  126. }
  127. }
  128. export const play = (rotationParam, flag) => {
  129. if (windowType === 'doubleWindow' && doubleWindowObj) {
  130. return doubleWindowObj.play.call(doubleWindowObj, rotationParam, flag);
  131. } else if (windowType === 'singleWindow' && singleWindowObj) {
  132. return singleWindowObj.play.call(singleWindowObj, rotationParam, flag);
  133. } else if (windowType === 'singleXkWindow' && singleWindowXkObj) {
  134. return singleWindowXkObj.play.call(singleWindowXkObj, rotationParam, flag);
  135. }
  136. };
  137. // 切换风窗类型
  138. export const setModelType = (type) => {
  139. // if (!model || !model.scene) return;
  140. const { sysOrgCode } = useGlobSetting();
  141. // sysOrgCode = 'sdmtjtyjlmk';
  142. windowType = type;
  143. return new Promise((resolve) => {
  144. // 显示双道风窗
  145. if (windowType === 'doubleWindow' && doubleWindowObj && doubleWindowObj.group) {
  146. model.startAnimation = doubleWindowObj.render.bind(doubleWindowObj);
  147. model.scene?.remove(group);
  148. group = doubleWindowObj.group;
  149. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  150. // let newP, newT;
  151. // if (sysOrgCode === 'sdmtjtyjlmk') {
  152. // newP = { x: 34.294433107431956, y: 19.284123769585108, z: 47.717286013509835 };
  153. // newT = { x: 12.311816240141978, y: -5.691930035759495, z: -5.57302688985693 };
  154. // } else {
  155. // newP = { x: 66.257, y: 57.539, z: 94.313 };
  156. // newT = { x: 0, y: 0, z: 0 };
  157. // }
  158. const newP = { x: 66.257, y: 57.539, z: 94.313 };
  159. const newT = { x: 0, y: 0, z: 0 };
  160. model.scene?.add(doubleWindowObj.group);
  161. setTimeout(async () => {
  162. resolve(null);
  163. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, newP, newT, model);
  164. }, 1000);
  165. } else if (windowType === 'singleWindow') {
  166. // 显示单道风窗
  167. model.startAnimation = singleWindowObj.render.bind(singleWindowObj);
  168. model.scene?.remove(group);
  169. group = singleWindowObj.group;
  170. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  171. let newP, newT;
  172. if (sysOrgCode === 'sdmtjtyjlmk') {
  173. newP = { x: 34.294433107431956, y: 19.284123769585108, z: 47.717286013509835 };
  174. newT = { x: 12.311816240141978, y: -5.691930035759495, z: -5.57302688985693 };
  175. } else {
  176. newP = { x: 66.257, y: 57.539, z: 94.313 };
  177. newT = { x: 0, y: 0, z: 0 };
  178. }
  179. model.scene?.add(singleWindowObj.group);
  180. setTimeout(async () => {
  181. resolve(null);
  182. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, newP, newT, model);
  183. }, 1000);
  184. } else if (windowType === 'singleXkWindow') {
  185. // 显示单道风窗
  186. model.startAnimation = singleWindowXkObj.render.bind(singleWindowXkObj);
  187. model.scene?.remove(group);
  188. group = singleWindowXkObj.group;
  189. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  190. model.scene?.add(singleWindowXkObj.group);
  191. setTimeout(async () => {
  192. resolve(null);
  193. await animateCamera(
  194. oldCameraPosition,
  195. { x: 0, y: 0, z: 0 },
  196. { x: 116.08531358656566, y: 81.45510733175816, z: 193.00752046594465 },
  197. { x: 23.446366480086372, y: -12.335134633777185, z: -5.63294282643405 },
  198. model
  199. );
  200. }, 1000);
  201. }
  202. });
  203. };
  204. export const mountedThree = (playerDom) => {
  205. const { sysOrgCode } = useGlobSetting();
  206. // sysOrgCode = 'sdmtjtyjlmk';
  207. return new Promise(async (resolve) => {
  208. model = new UseThree('#window3D');
  209. if (!model || !model.renderer || !model.camera) return;
  210. model.setEnvMap('test1');
  211. model.camera.position.set(100, 0, 1000);
  212. doubleWindowObj = new doubleWindow(model);
  213. singleWindowXkObj = new singleWindowXk(model);
  214. if (sysOrgCode === 'sdmtjtbetmk') {
  215. const singleWindowBet = await import('./dandaoFcBet.threejs');
  216. if (singleWindowBet) singleWindowObj = new singleWindowBet.default(model);
  217. } else if (sysOrgCode === 'sdmtjtyjlmk') {
  218. // 单
  219. const singleWindowYjl = await import('./dandaoFcYjl.threejs');
  220. if (singleWindowYjl) singleWindowObj = new singleWindowYjl.default(model);
  221. } else {
  222. singleWindowObj = new singleWindow(model);
  223. }
  224. doubleWindowObj.mountedThree(playerDom);
  225. singleWindowXkObj.mountedThree();
  226. singleWindowObj.mountedThree(playerDom);
  227. model.animate();
  228. // addLight();
  229. startAnimation();
  230. resolve(null);
  231. });
  232. };
  233. export const destroy = () => {
  234. if (model) {
  235. model.isRender = false;
  236. console.log('场景销毁前信息----------->', model.renderer?.info);
  237. model.isRender = false;
  238. doubleWindowObj.destroy();
  239. singleWindowObj.destroy();
  240. singleWindowXkObj.destroy();
  241. model.destroy();
  242. model = null;
  243. group = null;
  244. singleWindowObj = null;
  245. doubleWindowObj = null;
  246. singleWindowXkObj = null;
  247. }
  248. };