nitrogen.dixia.threejs.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. } else {
  60. }
  61. }
  62. }
  63. }
  64. if (this.group && !this.group.getObjectByName('downWindMonitor')) {
  65. const element = document.getElementById('downWindMonitor') as HTMLElement;
  66. if (element) {
  67. const downWindMonitorCSS3D = new CSS3DSprite(element);
  68. downWindMonitorCSS3D.name = 'downWindMonitor';
  69. downWindMonitorCSS3D.scale.set(0.005, 0.005, 0.005);
  70. downWindMonitorCSS3D.position.set(-2.95, 0.57, 0);
  71. this.group.add(downWindMonitorCSS3D);
  72. }
  73. }
  74. };
  75. clearCssText = () => {
  76. if (this.nitrogenNum > 0) {
  77. for (let i = 0; i < 2; i++) {
  78. const mesh = this.group.getObjectByName('monitorNitrogenText' + i);
  79. if (this.group && mesh) {
  80. this.group.remove(mesh);
  81. }
  82. }
  83. }
  84. };
  85. mountedThree() {
  86. return new Promise((resolve) => {
  87. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  88. this.group = gltf[0];
  89. this.setModalPosition();
  90. this.addLight();
  91. resolve(null);
  92. });
  93. });
  94. }
  95. destroy() {
  96. if (this.group) {
  97. this.model.clearGroup(this.group);
  98. }
  99. this.model = null;
  100. this.group = null;
  101. }
  102. }
  103. export default NitrogenUnderground;