window.threejs.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. model.scene?.add(doubleWindowObj.group);
  159. setTimeout(async () => {
  160. resolve(null);
  161. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, newP, newT, model);
  162. }, 1000);
  163. } else if (windowType === 'singleWindow') {
  164. // 显示单道风窗
  165. model.startAnimation = singleWindowObj.render.bind(singleWindowObj);
  166. model.scene?.remove(group);
  167. group = singleWindowObj.group;
  168. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  169. let newP, newT;
  170. if (sysOrgCode === 'sdmtjtyjlmk') {
  171. newP = { x: 66.257, y: 57.539, z: 94.313 };
  172. newT = { x: 0, y: 0, z: 0 };
  173. } else {
  174. newP = { x: 66.257, y: 57.539, z: 94.313 };
  175. newT = { x: 0, y: 0, z: 0 };
  176. }
  177. model.scene?.add(singleWindowObj.group);
  178. setTimeout(async () => {
  179. resolve(null);
  180. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, newP, newT, model);
  181. }, 1000);
  182. } else if (windowType === 'singleXkWindow') {
  183. // 显示单道风窗
  184. model.startAnimation = singleWindowXkObj.render.bind(singleWindowXkObj);
  185. model.scene?.remove(group);
  186. group = singleWindowXkObj.group;
  187. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  188. model.scene?.add(singleWindowXkObj.group);
  189. setTimeout(async () => {
  190. resolve(null);
  191. await animateCamera(
  192. oldCameraPosition,
  193. { x: 0, y: 0, z: 0 },
  194. { x: 116.08531358656566, y: 81.45510733175816, z: 193.00752046594465 },
  195. { x: 23.446366480086372, y: -12.335134633777185, z: -5.63294282643405 },
  196. model
  197. );
  198. }, 1000);
  199. }
  200. });
  201. };
  202. export const mountedThree = (playerDom) => {
  203. const { sysOrgCode } = useGlobSetting();
  204. // sysOrgCode = 'sdmtjtyjlmk';
  205. return new Promise(async (resolve) => {
  206. model = new UseThree('#window3D');
  207. if (!model || !model.renderer || !model.camera) return;
  208. model.setEnvMap('test1');
  209. model.camera.position.set(100, 0, 1000);
  210. singleWindowXkObj = new singleWindowXk(model);
  211. if (sysOrgCode === 'sdmtjtbetmk') {
  212. const singleWindowBet = await import('./dandaoFcBet.threejs');
  213. if (singleWindowBet) singleWindowObj = new singleWindowBet.default(model);
  214. doubleWindowObj = new doubleWindow(model);
  215. } else if (sysOrgCode === 'sdmtjtyjlmk') {
  216. // 单双
  217. const singleWindowYjl = await import('./dandaoFcYjl.threejs');
  218. if (singleWindowYjl) singleWindowObj = new singleWindowYjl.default(model);
  219. const doubleWindowYjl = await import('./shuangdaoFcYjl.threejs');
  220. if (doubleWindowYjl) doubleWindowObj = new doubleWindowYjl.default(model);
  221. } else {
  222. singleWindowObj = new singleWindow(model);
  223. doubleWindowObj = new doubleWindow(model);
  224. }
  225. doubleWindowObj.mountedThree(playerDom);
  226. singleWindowXkObj.mountedThree();
  227. singleWindowObj.mountedThree(playerDom);
  228. model.animate();
  229. addLight();
  230. startAnimation();
  231. resolve(null);
  232. });
  233. };
  234. export const destroy = () => {
  235. if (model) {
  236. model.isRender = false;
  237. console.log('场景销毁前信息----------->', model.renderer?.info);
  238. model.isRender = false;
  239. doubleWindowObj.destroy();
  240. singleWindowObj.destroy();
  241. singleWindowXkObj.destroy();
  242. model.destroy();
  243. model = null;
  244. group = null;
  245. singleWindowObj = null;
  246. doubleWindowObj = null;
  247. singleWindowXkObj = null;
  248. }
  249. };