fanLocal.threejs.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import FanLocal from './fanLocal.threejs.base';
  4. import FanLocalDual from './fanLocalDual.threejs.base';
  5. import { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. /** 模型总控制器 */
  8. let model: UseThree;
  9. /** 当前展示的具体模型的 Object3D 对象 */
  10. let group: THREE.Object3D;
  11. /** 当前展示的具体模型及状态组成的判断唯一性的key */
  12. let cacheKey: string = '';
  13. /** 具体模型内容列表,包含此模型总控制器下的所有可用的具体模型内容 */
  14. const modelContextList: {
  15. /** 当前模型类型,在控制器下有多个具体模型时分辨它们 */
  16. type: string;
  17. /** 模型的具体内容,即负责模型导入、绘制的上下文对象,一个控制器可以新建多个 */
  18. context?: FanLocal | FanLocalDual;
  19. }[] = [];
  20. const { mouseDownFn } = useEvent();
  21. /** 分发鼠标事件到具体实现方法 */
  22. function dispatchMouseEvent(event) {
  23. if (event.button == 0 && model && group) {
  24. mouseDownFn(model, group, event, () => {});
  25. console.log(model.camera, model.orbitControls);
  26. }
  27. }
  28. /** 为模型控制器设置非默认的摄像头位置 */
  29. function setCamera() {
  30. // if (!model || !model.camera) return;
  31. // model.camera.position.set(0, -2000, 1000);
  32. // model.camera.far = 10000;
  33. // model.orbitControls?.update();
  34. // model.camera.updateProjectionMatrix();
  35. }
  36. /** 初始化模型CSS展示框的鼠标事件,应该在模型总控制器初始化后调用 */
  37. function initEventListender() {
  38. if (!model) return;
  39. model.canvasContainer?.addEventListener('mousedown', (e) => dispatchMouseEvent(e));
  40. // model.orbitControls?.addEventListener('change', () => render());
  41. }
  42. /** 渲染并更新总模型 */
  43. // function render() {
  44. // if (model && model.isRender && model.renderer) {
  45. // // model.animationId = requestAnimationFrame(render);
  46. // model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  47. // model.renderer.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  48. // model.stats?.update();
  49. // }
  50. // }
  51. /** 刷新(再渲染)总模型 */
  52. // export function refreshModal() {
  53. // render();
  54. // // modelContextList.forEach((item) => {
  55. // // if (item.context) {
  56. // // item.context.render();
  57. // // }
  58. // // });
  59. // }
  60. /** 设置模型类型并切换,不同的类型通常对应不同的具体模型,在模型总控制器下的具体模型会根据传入的参数彼此交互、切换 */
  61. export function setModelType(modelType: 'fanLocal' | 'fanLocalDual' | string, subModelType: string, data?: any) {
  62. return new Promise((resolve, reject) => {
  63. if (!model) return reject('模型控制器未初始化');
  64. // 判断是否是同一个/类模型
  65. if (cacheKey === `${modelType}-${subModelType}`) return resolve(null);
  66. cacheKey = `${modelType}-${subModelType}`;
  67. modelContextList.forEach(({ type, context }) => {
  68. if (!context || !context.group) return;
  69. // 先把模型相关的内容隐藏,另起的隐藏子元素的代码是为了隐藏 CSS 元素
  70. context.group.visible = false;
  71. context.group.children.forEach((e) => {
  72. e.visible = false;
  73. });
  74. // model.scene?.remove(context.group);
  75. if (modelType === type) {
  76. group = context?.group as THREE.Object3D;
  77. context.setModelType(subModelType, data);
  78. setTimeout(async () => {
  79. // 还没添加到控制器的添加进去
  80. if (!model.scene?.getObjectByName(group.name) && group) {
  81. model.scene?.add(group);
  82. }
  83. group.visible = true;
  84. group.children.forEach((e) => {
  85. e.visible = true;
  86. });
  87. // 模型不同需要不同的初始角度与位置
  88. if (type == 'fanLocal') {
  89. const oldCameraPosition = { x: 615, y: 275, z: 744 };
  90. await animateCamera(
  91. oldCameraPosition,
  92. { x: 0, y: 0, z: 0 },
  93. { x: -1.85, y: 13.58, z: 37.39 },
  94. { x: -1.83, y: 2.58, z: -0.75 },
  95. model,
  96. 0.8
  97. );
  98. } else {
  99. const oldCameraPosition = { x: -693, y: 474, z: 398 };
  100. const position = { x: 14.826074594663222, y: 16.901762713393836, z: 36.459944037951004 };
  101. await animateCamera(
  102. oldCameraPosition,
  103. { x: 0, y: 0, z: 0 },
  104. { x: position.x, y: position.y, z: position.z },
  105. { x: 0, y: 0, z: 0 },
  106. model,
  107. 0.8
  108. );
  109. }
  110. resolve(null);
  111. }, 400);
  112. }
  113. });
  114. });
  115. }
  116. /** 挂载模型控制器,sceneSelctor表示放置模型的元素选择器,cssSelectors表示放置类似详情框的元素选择器,其中第一项需要是根元素选择器 */
  117. export function mountedThree(sceneSelctor: string, cssSelectors: string[]) {
  118. return new Promise(async (resolve) => {
  119. const [rootSelector] = cssSelectors;
  120. model = new UseThree(sceneSelctor, rootSelector);
  121. model.setEnvMap('test1.hdr');
  122. /** @ts-ignore-next-line */
  123. model.renderer.toneMappingExposure = 1.0;
  124. if (model.renderer) {
  125. model.renderer.sortObjects = true;
  126. }
  127. const model1 = new FanLocal(model);
  128. await model1.mountedThree();
  129. modelContextList.push({
  130. type: 'fanLocal',
  131. context: model1,
  132. });
  133. const model2 = new FanLocalDual(model);
  134. await model2.mountedThree();
  135. // 暂时先不加双行
  136. modelContextList.push({
  137. type: 'fanLocalDual',
  138. context: model2,
  139. });
  140. initEventListender();
  141. setCamera();
  142. model.animate();
  143. resolve(null);
  144. });
  145. }
  146. export const destroy = () => {
  147. if (!model) return;
  148. model.isRender = false;
  149. modelContextList.forEach((item) => {
  150. if (item.context) item.context.destroy();
  151. });
  152. model.destroy();
  153. };
  154. // 为了兼容性而添加的方法导出
  155. export function addText(d) {
  156. if (modelContextList[0]) {
  157. // @ts-ignore
  158. modelContextList[0].context?.addText(d);
  159. }
  160. }
  161. export function addCssText() {
  162. if (modelContextList[0]) {
  163. // @ts-ignore
  164. modelContextList[0].context?.addCssText();
  165. }
  166. }
  167. export function playSmoke(d) {
  168. if (modelContextList[0]) {
  169. // @ts-ignore
  170. modelContextList[0].context?.playSmoke(d);
  171. }
  172. }