nitrogen.dixia.threejs.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  45. addCssText = () => {
  46. if (this.nitrogenNum > 0) {
  47. for (let i = 0; i < 2; i++) {
  48. if (this.group && !this.group.getObjectByName('monitorNitrogenText' + i)) {
  49. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  50. if (element) {
  51. const nitrogenMonitorCSS3D = new CSS3DSprite(element);
  52. nitrogenMonitorCSS3D.name = 'monitorNitrogenText' + i;
  53. nitrogenMonitorCSS3D.scale.set(0.005, 0.005, 0.005);
  54. if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
  55. if (i == 1) nitrogenMonitorCSS3D.position.set(1.44, 0.31, 0.04);
  56. // if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  57. // if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  58. this.group.add(nitrogenMonitorCSS3D);
  59. }
  60. }
  61. // 而如若此项超出了已绑定的注氮机数量,则隐藏或销毁
  62. if (this.group && i >= this.nitrogenNum && this.group.getObjectByName('monitorNitrogenText' + i)) {
  63. this.group.remove(this.group.getObjectByName('monitorNitrogenText' + i) as THREE.Object3D);
  64. }
  65. }
  66. }
  67. if (this.group && !this.group.getObjectByName('downWindMonitor')) {
  68. const element = document.getElementById('downWindMonitor') as HTMLElement;
  69. if (element) {
  70. const downWindMonitorCSS3D = new CSS3DSprite(element);
  71. downWindMonitorCSS3D.name = 'downWindMonitor';
  72. downWindMonitorCSS3D.scale.set(0.005, 0.005, 0.005);
  73. downWindMonitorCSS3D.position.set(-2.95, 0.57, 0);
  74. this.group.add(downWindMonitorCSS3D);
  75. }
  76. }
  77. };
  78. clearCssText = () => {
  79. if (this.nitrogenNum > 0) {
  80. for (let i = 0; i < 2; i++) {
  81. const mesh = this.group.getObjectByName('monitorNitrogenText' + i);
  82. if (this.group && mesh) {
  83. this.group.remove(mesh);
  84. }
  85. }
  86. }
  87. };
  88. mountedThree() {
  89. return new Promise((resolve) => {
  90. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  91. this.group = gltf[0];
  92. this.setModalPosition();
  93. this.addLight();
  94. resolve(null);
  95. });
  96. });
  97. }
  98. destroy() {
  99. if (this.group) {
  100. this.model.clearGroup(this.group);
  101. }
  102. this.model = null;
  103. this.group = null;
  104. }
  105. }
  106. export default NitrogenUnderground;