window.threejs.ts 11 KB

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