fanLocal.threejs.ts 6.8 KB

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