window.threejs.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. debugger
  104. if (windowType === 'singleWindow' && singleWindowObj) {
  105. return await singleWindowObj.initCamera.call(singleWindowObj, playerVal1);
  106. } else if (windowType === 'doubleWindow' && doubleWindowObj) {
  107. return doubleWindowObj.initCamera.call(doubleWindowObj, playerVal1);
  108. }
  109. };
  110. // 切换风窗类型
  111. export const setModelType = (type) => {
  112. // if (!model || !model.scene) return;
  113. windowType = type;
  114. return new Promise((resolve) => {
  115. // 显示双道风窗
  116. if (windowType === 'doubleWindow' && doubleWindowObj && doubleWindowObj.group) {
  117. model.startAnimation = doubleWindowObj.render.bind(doubleWindowObj);
  118. group = doubleWindowObj.group;
  119. if (model.scene?.getObjectByName('ddFc')) {
  120. model.scene.remove(singleWindowObj.group);
  121. }
  122. const oldCameraPosition = { x: 100, y: 0, z: 500 };
  123. model.scene?.add(doubleWindowObj.group);
  124. setTimeout(async () => {
  125. resolve(null);
  126. 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);
  127. }, 300);
  128. } else if (windowType === 'singleWindow') {
  129. // 显示单道风窗
  130. model.startAnimation = singleWindowObj.render.bind(singleWindowObj);
  131. group = singleWindowObj.group;
  132. if (model.scene?.getObjectByName('sdFc')) {
  133. model.scene?.remove(doubleWindowObj.group);
  134. }
  135. const oldCameraPosition = { x: 100, y: 0, z: 500 };
  136. model.scene?.add(singleWindowObj.group);
  137. setTimeout(async () => {
  138. resolve(null);
  139. 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);
  140. }, 300);
  141. }
  142. });
  143. };
  144. export const mountedThree = () => {
  145. return new Promise(async (resolve) => {
  146. model = new UseThree('#window3D');
  147. if (!model || !model.renderer || !model.camera) return;
  148. model.setEnvMap('test1');
  149. model.camera.position.set(100, 0, 1000);
  150. // 单道、 双道
  151. doubleWindowObj = new doubleWindow(model);
  152. singleWindowObj = new singleWindow(model);
  153. await doubleWindowObj.mountedThree();
  154. await singleWindowObj.mountedThree();
  155. model.animate();
  156. resolve(null);
  157. addLight();
  158. startAnimation();
  159. });
  160. };
  161. export const destroy = () => {
  162. if (model) {
  163. model.isRender = false;
  164. console.log('场景销毁前信息----------->', model.renderer?.info);
  165. model.isRender = false;
  166. doubleWindowObj.destroy();
  167. singleWindowObj.destroy();
  168. model.destroy();
  169. model = null;
  170. group = null;
  171. singleWindowObj = null;
  172. doubleWindowObj = null;
  173. }
  174. };