gasPump.threejs.under.ts 2.4 KB

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