gasPump.threejs.under.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as THREE from 'three';
  2. import { CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  3. // import * as dat from 'dat.gui';
  4. // const gui = new dat.GUI();
  5. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  6. class gasPumpUnder {
  7. model;
  8. modelName = 'gasPumpUnder';
  9. group: THREE.Object3D | null = null;
  10. constructor(model) {
  11. this.model = model;
  12. }
  13. addLight() {
  14. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  15. directionalLight.position.set(-48, 107, 36);
  16. this.group?.add(directionalLight);
  17. directionalLight.target = this.group as THREE.Object3D;
  18. // gui.add(directionalLight.position, 'x', -500, 800);
  19. // gui.add(directionalLight.position, 'y', -500, 800);
  20. // gui.add(directionalLight.position, 'z', -500, 800);
  21. // const pointLight2 = new THREE.PointLight(0xffffff, 2, 500);
  22. // pointLight2.position.set(-113, 29, 10);
  23. // // light2.castShadow = true
  24. // pointLight2.shadow.bias = -0.05;
  25. // this.group?.add(pointLight2);
  26. // gui.add(pointLight2.position, 'x', -500, 500);
  27. // gui.add(pointLight2.position, 'y', -500, 500);
  28. // gui.add(pointLight2.position, 'z', -500, 500);
  29. }
  30. addCssText = () => {
  31. if (!this.group) return;
  32. if (!this.group.getObjectByName('text1')) {
  33. const element = document.getElementById('FlowSensor') as HTMLElement;
  34. if (element) {
  35. const parentElement = document.getElementById('gas3DCSS') as HTMLElement;
  36. parentElement.appendChild(element);
  37. const fanLocalCSS3D = new CSS3DObject(element);
  38. fanLocalCSS3D.name = 'text1';
  39. fanLocalCSS3D.scale.set(0.007, 0.007, 0.007);
  40. fanLocalCSS3D.position.set(0, 1.6, 0);
  41. this.group.add(fanLocalCSS3D);
  42. }
  43. }
  44. };
  45. clearCssText = () => {
  46. const fanLocalCSS3D = this.group?.getObjectByName('text1') as THREE.Object3D;
  47. this.group?.remove(fanLocalCSS3D);
  48. };
  49. mountedThree() {
  50. return new Promise((resolve) => {
  51. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  52. this.group = gltf[0];
  53. if (this.group) {
  54. // this.group?.scale.set(0.1, 0.1, 0.1);
  55. // this.group.position.y += 40;
  56. resolve(null);
  57. this.addLight();
  58. }
  59. });
  60. });
  61. }
  62. destroy() {
  63. this.model.clearGroup(this.group);
  64. this.model = null;
  65. this.group = null;
  66. }
  67. }
  68. export default gasPumpUnder;