grout.threejs.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as THREE from 'three';
  2. import UseThree from '../../../../utils/threejs/useThree';
  3. import ChamberBase from './grout.threejs.base';
  4. import BertaiBase from './bertai.threejs.base'; //lxh
  5. import { animateCamera } from '/@/utils/threejs/util';
  6. import useEvent from '../../../../utils/threejs/useEvent';
  7. // 模型对象、 文字对象
  8. let model: UseThree | undefined,
  9. groutBaseObj: ChamberBase | undefined,
  10. BertaiObj: BertaiBase | undefined, //lxh
  11. group: THREE.Object3D | undefined,
  12. // groutType = 'groutBase'; // workerFaceFiber
  13. groutType = 'bertaiBase'; // workerFaceFiber
  14. const { mouseDownFn } = useEvent();
  15. // 鼠标点击事件
  16. const mouseEvent = (event) => {
  17. if (event.button == 0) {
  18. mouseDownFn(<UseThree>model, <THREE.Object3D>group, event, (intersects) => {
  19. if (groutType === 'groutBase') {
  20. // groutBaseObj.mousedownModel.call(groutBaseObj, model.rayCaster);
  21. } else if (groutType === 'bertaiBase') {
  22. //lxh
  23. // BertaiObj.mousedownModel.call(BertaiObj, model.rayCaster);
  24. }
  25. });
  26. console.log('摄像头控制信息', model?.orbitControls, model?.camera);
  27. }
  28. };
  29. const addMouseEvent = () => {
  30. // 定义鼠标点击事件
  31. model?.canvasContainer?.addEventListener('mousedown', mouseEvent);
  32. };
  33. // const render = () => {
  34. // if (model && model.isRender) {
  35. // model.animationId = requestAnimationFrame(render);
  36. // model.css3dRender?.render(model.scene as THREE.Scene, model.camera as THREE.PerspectiveCamera);
  37. // model.stats?.update();
  38. // }
  39. // };
  40. export const addChamberText = (selectData) => {
  41. if (groutType === 'groutBase') {
  42. return groutBaseObj?.addChamberText.call(groutBaseObj, selectData);
  43. } else if (groutType == 'bertaiBase') {
  44. //lxh
  45. return BertaiObj?.addChamberText.call(BertaiObj, selectData);
  46. }
  47. };
  48. // 切换模型类型
  49. export const setModelType = (type) => {
  50. groutType = type;
  51. return new Promise((resolve) => {
  52. if (groutType === 'groutBase' && groutBaseObj && groutBaseObj.group) {
  53. group = groutBaseObj.group;
  54. const oldCameraPosition = { x: -114.7969, y: 104.0741, z: 303.516 };
  55. model?.scene?.add(groutBaseObj.group);
  56. setTimeout(async () => {
  57. await animateCamera(
  58. oldCameraPosition,
  59. { x: 0, y: 0, z: 0 },
  60. { x: -36.224455845316, y: 21.749986776845592, z: -0.10614789855468158 },
  61. { x: 0, y: 0, z: 0 },
  62. model,
  63. 0.6
  64. );
  65. }, 300);
  66. resolve(null);
  67. } else if (groutType === 'bertaiBase' && BertaiObj && BertaiObj.group) {
  68. //lxh
  69. group = BertaiObj.group;
  70. const oldCameraPosition = { x: -114.7969, y: 104.0741, z: 303.516 };
  71. model?.scene?.add(BertaiObj.group);
  72. setTimeout(async () => {
  73. await animateCamera(
  74. oldCameraPosition,
  75. { x: 0, y: 0, z: 0 },
  76. { x: 0.06216, y: 17.7679, z: 20.28977 },
  77. { x: 0.12544, y: 2.18736, z: -1.30608 },
  78. model,
  79. 0.6
  80. );
  81. }, 300);
  82. resolve(null);
  83. }
  84. });
  85. };
  86. export const mountedThree = () => {
  87. return new Promise(async (resolve) => {
  88. model = new UseThree('#grout3D');
  89. model.setEnvMap('test1.hdr');
  90. model.renderer.toneMappingExposure = 1.0;
  91. model?.camera?.position.set(100, 0, 600);
  92. groutBaseObj = new ChamberBase(model);
  93. await groutBaseObj.mountedThree();
  94. BertaiObj = new BertaiBase(model); //lxh
  95. await BertaiObj.mountedThree(); //lxh
  96. addMouseEvent();
  97. model.animate();
  98. resolve(null);
  99. });
  100. };
  101. export const destroy = () => {
  102. if (model) {
  103. model.isRender = false;
  104. console.log('场景销毁前信息----------->', model.renderer?.info);
  105. groutBaseObj?.destroy();
  106. groutBaseObj = undefined;
  107. BertaiObj?.destroy();
  108. BertaiObj = undefined;
  109. group = undefined;
  110. model.destroy();
  111. model = undefined;
  112. }
  113. };