chamber.threejs.base.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import * as THREE from 'three';
  2. import Smoke from '/@/views/vent/comment/threejs/Smoke';
  3. import Smoke1 from '/@/views/vent/comment/threejs/Smoke1';
  4. // import * as dat from 'dat.gui';
  5. // const gui = new dat.GUI();
  6. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  7. class ChamberBase {
  8. model;
  9. modelName = 'chamber';
  10. group: THREE.Object3D | null = null;
  11. smoke: Smoke | undefined;
  12. smoke1: Smoke1 | undefined;
  13. constructor(model) {
  14. this.model = model;
  15. }
  16. addLight() {
  17. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.2);
  18. directionalLight.position.set(15, 69, -39);
  19. this.group?.add(directionalLight);
  20. directionalLight.target = this.group as THREE.Object3D;
  21. }
  22. async initSmoke() {
  23. // 喷雾
  24. const moveCurve = [
  25. {
  26. path0: new THREE.Vector3(0, 0, 0),
  27. path1: new THREE.Vector3(0, -8, 0),
  28. isSpread: true,
  29. spreadDirection: 1, // 1是由小变大,-1是由大变小
  30. spreadRang: 1,
  31. },
  32. ];
  33. this.smoke = new Smoke('/model/img/texture-smoke.png', '#ffffffaa', 10, 0.9, 0.4, 30);
  34. this.smoke.setPath(moveCurve);
  35. await this.smoke.setPoints();
  36. this.group?.add(this.smoke.points);
  37. this.smoke.points.scale.set(55, 100, 55);
  38. this.smoke.points.position.set(272.432, 547.837, 1174.679);
  39. // // 喷粉
  40. this.smoke1 = new Smoke1('/model/img/texture-smoke.png', '#ffffff', 0.1);
  41. await this.smoke1.setPoints();
  42. this.group?.add(this.smoke1.points);
  43. }
  44. changeSmoke() {
  45. // this.smoke?.startSmoke(0);
  46. this.smoke1?.startSmoke();
  47. }
  48. mountedThree() {
  49. return new Promise((resolve) => {
  50. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  51. this.group = gltf[0];
  52. if (this.group) {
  53. this.group?.scale.set(0.01, 0.01, 0.01);
  54. // this.group.position.y += 40;
  55. resolve(null);
  56. this.addLight();
  57. await this.initSmoke();
  58. this.changeSmoke();
  59. }
  60. });
  61. });
  62. }
  63. destroy() {
  64. if (this.smoke) this.smoke.clearSmoke();
  65. if (this.smoke1) this.smoke1.clearSmoke();
  66. this.model.clearGroup(this.group);
  67. this.model = null;
  68. this.group = null;
  69. }
  70. }
  71. export default ChamberBase;