window.threejs.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. // import * as dat from 'dat.gui';
  8. // const gui = new dat.GUI();
  9. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  10. // 模型对象、 文字对象
  11. let model: UseThree,
  12. singleWindowObj: singleWindow,
  13. doubleWindowObj: doubleWindow,
  14. group: THREE.Object3D,
  15. windowType = 'singleWindow';
  16. const { mouseDownFn } = useEvent();
  17. // 打灯光
  18. const addLight = () => {
  19. if (!model || !model.scene) return;
  20. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  21. directionalLight.position.set(-110, 150, 647);
  22. model.scene?.add(directionalLight);
  23. // directionalLight.target = group;
  24. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  25. pointLight2.position.set(-101, 34, 16);
  26. pointLight2.shadow.bias = 0.05;
  27. model.scene.add(pointLight2);
  28. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  29. pointLight3.position.set(19, 25, -7);
  30. pointLight3.shadow.bias = 0.05;
  31. model.scene.add(pointLight3);
  32. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  33. pointLight6.position.set(51, 51, 9);
  34. pointLight6.shadow.bias = 0.05;
  35. model.scene.add(pointLight6);
  36. // const pointLight7 = new THREE.PointLight(0xffffff, 1, 1000);
  37. // pointLight7.position.set(45, 51, -4.1);
  38. // pointLight7.shadow.bias = 0.05;
  39. // model.scene.add(pointLight7);
  40. // const spotLight = new THREE.SpotLight();
  41. // spotLight.angle = Math.PI / 16;
  42. // spotLight.penumbra = 0;
  43. // spotLight.castShadow = true;
  44. // spotLight.intensity = 1;
  45. // spotLight.position.set(-231, 463, 687);
  46. // model.scene.add(spotLight);
  47. // spotLight.shadow.camera.near = 0.5; // default
  48. // spotLight.shadow.camera.far = 1000; // default
  49. // spotLight.shadow.focus = 1.2;
  50. // spotLight.shadow.bias = -0.000002;
  51. // gui.add(directionalLight.position, 'x', -1000, 1000);
  52. // gui.add(directionalLight.position, 'y', -1000, 1000);
  53. // gui.add(directionalLight.position, 'z', -1000, 1000);
  54. };
  55. // // 重置摄像头
  56. // const resetCamera = () => {
  57. // model.camera.position.set(30.328, 58.993, 148.315);
  58. // model.camera.rotation.set(-31.85, 30.07, 17.29);
  59. // model.orbitControls?.update();
  60. // model.camera.updateProjectionMatrix();
  61. // };
  62. // 初始化左右摇摆动画
  63. const startAnimation = () => {
  64. // 定义鼠标点击事件
  65. model.canvasContainer?.addEventListener('mousedown', mouseEvent.bind(null));
  66. model.canvasContainer?.addEventListener('pointerup', (event) => {
  67. event.stopPropagation();
  68. // 单道、 双道
  69. if (windowType === 'doubleWindow' && doubleWindowObj) {
  70. doubleWindowObj.mouseUpModel.call(doubleWindowObj);
  71. } else if (windowType === 'singleWindow' && singleWindowObj) {
  72. singleWindowObj.mouseUpModel.call(singleWindowObj);
  73. }
  74. });
  75. };
  76. // 鼠标点击、松开事件
  77. const mouseEvent = (event) => {
  78. if (event.button == 0) {
  79. mouseDownFn(model, group, event, (intersects) => {
  80. if (windowType === 'doubleWindow' && doubleWindowObj) {
  81. doubleWindowObj.mousedownModel.call(doubleWindowObj, intersects);
  82. } else if (windowType === 'singleWindow' && singleWindowObj) {
  83. singleWindowObj.mousedownModel.call(singleWindowObj, intersects);
  84. }
  85. });
  86. }
  87. };
  88. export const addMonitorText = (selectData) => {
  89. if (windowType === 'doubleWindow' && doubleWindowObj) {
  90. return doubleWindowObj.addMonitorText.call(doubleWindowObj, selectData);
  91. } else if (windowType === 'singleWindow' && singleWindowObj) {
  92. return singleWindowObj.addMonitorText.call(singleWindowObj, selectData);
  93. }
  94. };
  95. export const play = (rotationParam, flag) => {
  96. if (windowType === 'doubleWindow' && doubleWindowObj) {
  97. return doubleWindowObj.play.call(doubleWindowObj, rotationParam, flag);
  98. } else if (windowType === 'singleWindow' && singleWindowObj) {
  99. return singleWindowObj.play.call(singleWindowObj, rotationParam, flag);
  100. }
  101. };
  102. export const initCameraCanvas = async (playerVal1?, playerVal2?) => {
  103. if (windowType === 'singleWindow' && singleWindowObj) {
  104. return await singleWindowObj.initCamera.call(singleWindowObj, playerVal1);
  105. } else if (windowType === 'doubleWindow' && doubleWindowObj) {
  106. return doubleWindowObj.initCamera.call(doubleWindowObj, playerVal1);
  107. }
  108. };
  109. // 切换风窗类型
  110. export const setModelType = (type) => {
  111. // if (!model || !model.scene) return;
  112. windowType = type;
  113. return new Promise((resolve) => {
  114. // 显示双道风窗
  115. if (windowType === 'doubleWindow' && doubleWindowObj && doubleWindowObj.group) {
  116. model.startAnimation = doubleWindowObj.render.bind(doubleWindowObj);
  117. group = doubleWindowObj.group;
  118. if (model.scene?.getObjectByName('ddFc')) {
  119. model.scene.remove(singleWindowObj.group);
  120. }
  121. const oldCameraPosition = { x: 100, y: 0, z: 500 };
  122. model.scene?.add(doubleWindowObj.group);
  123. setTimeout(async () => {
  124. resolve(null);
  125. 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);
  126. }, 300);
  127. } else if (windowType === 'singleWindow') {
  128. // 显示单道风窗
  129. model.startAnimation = singleWindowObj.render.bind(singleWindowObj);
  130. group = singleWindowObj.group;
  131. if (model.scene?.getObjectByName('sdFc')) {
  132. model.scene?.remove(doubleWindowObj.group);
  133. }
  134. const oldCameraPosition = { x: 100, y: 0, z: 500 };
  135. model.scene?.add(singleWindowObj.group);
  136. setTimeout(async () => {
  137. resolve(null);
  138. 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);
  139. }, 300);
  140. }
  141. });
  142. };
  143. export const mountedThree = (playerDom) => {
  144. return new Promise(async (resolve) => {
  145. model = new UseThree('#window3D');
  146. if (!model || !model.renderer || !model.camera) return;
  147. model.setEnvMap('test1');
  148. model.camera.position.set(100, 0, 1000);
  149. // 单道、 双道
  150. doubleWindowObj = new doubleWindow(model);
  151. singleWindowObj = new singleWindow(model);
  152. await doubleWindowObj.mountedThree(playerDom);
  153. await singleWindowObj.mountedThree(playerDom);
  154. model.animate();
  155. resolve(null);
  156. addLight();
  157. startAnimation();
  158. });
  159. };
  160. export const destroy = () => {
  161. if (model) {
  162. model.isRender = false;
  163. console.log('场景销毁前信息----------->', model.renderer?.info);
  164. model.isRender = false;
  165. doubleWindowObj.destroy();
  166. singleWindowObj.destroy();
  167. model.destroy();
  168. model = null;
  169. group = null;
  170. singleWindowObj = null;
  171. doubleWindowObj = null;
  172. }
  173. };