window.threejs.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import singleWindow from './dandaoFcBetZh.threejs';
  4. import { animateCamera } from '/@/utils/threejs/util';
  5. import useEvent from '../../../../utils/threejs/useEvent';
  6. // import * as dat from 'dat.gui';
  7. // const gui = new dat.GUI();
  8. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  9. // 模型对象、 文字对象
  10. let model: UseThree,
  11. singleWindowObj,
  12. group: THREE.Object3D,
  13. windowType = 'singleWindow';
  14. const rotationParam = {
  15. frontDeg0: 0, // 前门初始
  16. frontDeg1: 0, // 前门目标
  17. backDeg0: 0, // 后门初始
  18. backDeg1: 0, // 后门目标
  19. };
  20. const { mouseDownFn } = useEvent();
  21. // 打灯光
  22. const addLight = () => {
  23. if (!model || !model.scene) return;
  24. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  25. directionalLight.position.set(-110, 150, 647);
  26. model.scene?.add(directionalLight);
  27. // directionalLight.target = group;
  28. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  29. pointLight2.position.set(-101, 34, 16);
  30. pointLight2.shadow.bias = 0.05;
  31. model.scene.add(pointLight2);
  32. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  33. pointLight3.position.set(19, 25, -7);
  34. pointLight3.shadow.bias = 0.05;
  35. model.scene.add(pointLight3);
  36. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  37. pointLight6.position.set(51, 51, 9);
  38. pointLight6.shadow.bias = 0.05;
  39. model.scene.add(pointLight6);
  40. };
  41. // 初始化左右摇摆动画
  42. const startAnimation = () => {
  43. // 定义鼠标点击事件
  44. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  45. model.canvasContainer?.addEventListener('pointerup', (event) => {
  46. event.stopPropagation();
  47. // 单道、 双道
  48. if (windowType === 'singleWindow' && singleWindowObj) {
  49. singleWindowObj.mouseUpModel.call(singleWindowObj);
  50. }
  51. });
  52. };
  53. // 鼠标点击、松开事件
  54. const mouseEvent = (event) => {
  55. if (event.button == 0) {
  56. mouseDownFn(model, group, event, (intersects) => {
  57. if (windowType === 'singleWindow' && singleWindowObj) {
  58. singleWindowObj.mousedownModel.call(singleWindowObj, intersects);
  59. }
  60. });
  61. }
  62. };
  63. export const addMonitorText = (selectData) => {
  64. if (windowType === 'singleWindow' && singleWindowObj) {
  65. return singleWindowObj.addMonitorText.call(singleWindowObj, selectData);
  66. }
  67. };
  68. export function computePlay(data, maxarea, isFirst = false) {
  69. if (windowType === 'doubleWindow' || windowType === 'singleWindow') {
  70. if (!maxarea) maxarea = 90;
  71. rotationParam.frontDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.forntArea);
  72. rotationParam.backDeg0 = (90 / maxarea) * Number(isFirst ? 0 : data.rearArea);
  73. rotationParam.frontDeg1 = (90 / maxarea) * Number(data.forntArea) || 0;
  74. rotationParam.backDeg1 = (90 / maxarea) * Number(data.rearArea) || 0;
  75. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  76. // 当返回值有误时默认关闭
  77. play(rotationParam, 0);
  78. } else {
  79. if (data.nwindownum == 1 || data.nwindownum == 2) {
  80. setTimeout(() => {
  81. play(rotationParam, 1);
  82. }, 0);
  83. }
  84. if (data.nwindownum == 2) {
  85. setTimeout(() => {
  86. play(rotationParam, 2);
  87. }, 0);
  88. }
  89. }
  90. } else if (windowType === 'singleXkWindow') {
  91. const acosToAngle = (cosValue) => {
  92. cosValue = Math.max(Math.min(cosValue, 1), -1);
  93. // 计算角度
  94. return Math.asin(cosValue) * (180 / Math.PI);
  95. };
  96. const sina = Math.sqrt((Math.sin((78 * Math.PI) / 180) ** 2 * parseFloat(data.forntArea)) / parseFloat(maxarea));
  97. const angleInRadians = acosToAngle(sina);
  98. rotationParam.frontDeg1 = angleInRadians;
  99. if (!rotationParam.frontDeg1 && !rotationParam.backDeg1) {
  100. // 当返回值有误时默认关闭
  101. play(rotationParam, 0);
  102. } else {
  103. setTimeout(() => {
  104. play(rotationParam, 1);
  105. }, 0);
  106. }
  107. }
  108. }
  109. export const play = (rotationParam, flag) => {
  110. if (windowType === 'singleWindow' && singleWindowObj) {
  111. return singleWindowObj.play.call(singleWindowObj, rotationParam, flag);
  112. }
  113. };
  114. // 切换风窗类型
  115. export const setModelType = (type) => {
  116. // if (!model || !model.scene) return;
  117. windowType = type;
  118. return new Promise((resolve) => {
  119. // 显示双道风窗
  120. if (windowType === 'singleWindow') {
  121. // 显示单道风窗
  122. model.startAnimation = singleWindowObj.render.bind(singleWindowObj);
  123. model.scene?.remove(group);
  124. group = singleWindowObj.group;
  125. const oldCameraPosition = { x: 100, y: 0, z: 10 };
  126. model.scene?.add(singleWindowObj.group);
  127. setTimeout(async () => {
  128. resolve(null);
  129. await animateCamera(oldCameraPosition, { x: 0, y: 0, z: 0 }, { x: 66.257, y: 57.539, z: 94.313 }, { x: 0, y: 0, z: 0 }, model);
  130. }, 1000);
  131. }
  132. });
  133. };
  134. export const mountedThree = (playerDom) => {
  135. return new Promise(async (resolve) => {
  136. model = new UseThree('#window3D');
  137. if (!model || !model.renderer || !model.camera) return;
  138. model.setEnvMap('test1.hdr');
  139. model.camera.position.set(100, 0, 1000);
  140. singleWindowObj = new singleWindow(model);
  141. singleWindowObj.mountedThree(playerDom);
  142. model.animate();
  143. addLight();
  144. startAnimation();
  145. resolve(null);
  146. });
  147. };
  148. export const destroy = () => {
  149. if (model) {
  150. model.isRender = false;
  151. console.log('场景销毁前信息----------->', model.renderer?.info);
  152. model.isRender = false;
  153. singleWindowObj.destroy();
  154. model.destroy();
  155. model = null;
  156. group = null;
  157. singleWindowObj = null;
  158. }
  159. };