nitrogen.dixia.threejs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as THREE from 'three';
  2. import { getTextCanvas } from '/@/utils/threejs/util';
  3. import { CSS3DSprite } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  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 NitrogenUnderground {
  8. model;
  9. modelName = 'nitrogenUnderground';
  10. group: THREE.Object3D = new THREE.Object3D();
  11. animationTimer;
  12. isLRAnimation = true;
  13. direction = 1;
  14. nitrogenNum = 0;
  15. constructor(model) {
  16. this.model = model;
  17. this.group.name = this.modelName;
  18. }
  19. addLight() {
  20. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  21. directionalLight.position.set(64, 215, 500);
  22. directionalLight.target = this.group;
  23. this.group.add(directionalLight);
  24. const pointLight2 = new THREE.PointLight(0xffffff, 2, 300);
  25. pointLight2.position.set(-113, 29, 10);
  26. // light2.castShadow = true
  27. pointLight2.shadow.bias = -0.05;
  28. this.group.add(pointLight2);
  29. // gui.add(pointLight2.position, 'x', -1000, 1000);
  30. // gui.add(pointLight2.position, 'y', -1000, 1000);
  31. // gui.add(pointLight2.position, 'z', -1000, 1000);
  32. // gui.add(spotLight.position, 'x', -500, 500);
  33. // gui.add(spotLight.position, 'y', -500, 500);
  34. // gui.add(spotLight.position, 'z', -500, 500);
  35. // gui.add(spotLight, 'distance', 0, 1000);
  36. }
  37. // 设置模型位置
  38. setModalPosition() {
  39. this.group?.scale.set(22, 22, 22);
  40. this.group?.position.set(-10, 25, 20);
  41. }
  42. resetModel() {
  43. clearTimeout(this.animationTimer);
  44. this.isLRAnimation = false;
  45. }
  46. addCssText = () => {
  47. if (this.nitrogenNum > 0) {
  48. for (let i = 0; i < 2; i++) {
  49. if (this.group && !this.group.getObjectByName('monitorNitrogenText' + i)) {
  50. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  51. if (element) {
  52. const nitrogenMonitorCSS3D = new CSS3DSprite(element);
  53. nitrogenMonitorCSS3D.name = 'monitorNitrogenText' + i;
  54. nitrogenMonitorCSS3D.scale.set(0.0045, 0.0045, 0.0045);
  55. if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
  56. if (i == 1) nitrogenMonitorCSS3D.position.set(1.44, 0.31, 0.04);
  57. // if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  58. // if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  59. this.group.add(nitrogenMonitorCSS3D);
  60. } else {
  61. }
  62. }
  63. }
  64. }
  65. if (this.group && !this.group.getObjectByName('downWindMonitor')) {
  66. const element = document.getElementById('downWindMonitor') as HTMLElement;
  67. if (element) {
  68. const downWindMonitorCSS3D = new CSS3DSprite(element);
  69. downWindMonitorCSS3D.name = 'downWindMonitor';
  70. downWindMonitorCSS3D.scale.set(0.0045, 0.0045, 0.0045);
  71. downWindMonitorCSS3D.position.set(-2.95, 0.57, 0);
  72. this.group.add(downWindMonitorCSS3D);
  73. }
  74. }
  75. };
  76. clearCssText = () => {
  77. if (this.nitrogenNum > 0) {
  78. for (let i = 0; i < 2; i++) {
  79. const mesh = this.group.getObjectByName('monitorNitrogenText' + i);
  80. if (this.group && mesh) {
  81. this.group.remove(mesh);
  82. }
  83. }
  84. }
  85. };
  86. mountedThree() {
  87. return new Promise((resolve) => {
  88. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  89. this.group = gltf[0];
  90. this.setModalPosition();
  91. this.addLight();
  92. resolve(null);
  93. });
  94. });
  95. }
  96. destroy() {
  97. if (this.group) {
  98. this.model.clearGroup(this.group);
  99. }
  100. this.model = null;
  101. this.group = null;
  102. }
  103. }
  104. export default NitrogenUnderground;