nitrogen.dixia.threejs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. addMonitorText(selectData) {
  43. if (!this.group) {
  44. return;
  45. }
  46. const textArr = [
  47. {
  48. text: `单点式测风装置`,
  49. font: 'normal 32px Arial',
  50. color: '#009900',
  51. strokeStyle: '#002200',
  52. x: 140,
  53. y: 95,
  54. },
  55. {
  56. text: `风量(m3/min):`,
  57. font: 'normal 29px Arial',
  58. color: '#009900',
  59. strokeStyle: '#002200',
  60. x: 30,
  61. y: 150,
  62. },
  63. {
  64. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  65. font: 'normal 29px Arial',
  66. color: '#009900',
  67. strokeStyle: '#002200',
  68. x: 311,
  69. y: 150,
  70. },
  71. {
  72. text: `风速(m/s): `,
  73. font: 'normal 29px Arial',
  74. color: '#009900',
  75. strokeStyle: '#002200',
  76. x: 30,
  77. y: 202,
  78. },
  79. {
  80. text: `${selectData.va ? selectData.va : '-'}`,
  81. font: 'normal 29px Arial',
  82. color: '#009900',
  83. strokeStyle: '#002200',
  84. x: 310,
  85. y: 202,
  86. },
  87. {
  88. text: `断面积(㎡):`,
  89. font: 'normal 29px Arial',
  90. color: '#009900',
  91. strokeStyle: '#002200',
  92. x: 30,
  93. y: 252,
  94. },
  95. {
  96. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  97. font: 'normal 29px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 310,
  101. y: 252,
  102. },
  103. {
  104. text: `煤炭科学技术研究院有限公司研制`,
  105. font: 'normal 28px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 50,
  109. y: 302,
  110. },
  111. ];
  112. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  113. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  114. const textMaterial = new THREE.MeshBasicMaterial({
  115. map: textMap, // 设置纹理贴图
  116. transparent: true,
  117. side: THREE.DoubleSide, // 这里是双面渲染的意思
  118. });
  119. textMaterial.blending = THREE.CustomBlending;
  120. const monitorPlane = this.group?.getObjectByName('monitorText');
  121. if (monitorPlane) {
  122. monitorPlane.material = textMaterial;
  123. } else {
  124. const planeGeometry = new THREE.PlaneGeometry(5.6, 3.46); // 平面3维几何体PlaneGeometry
  125. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  126. planeMesh.name = 'monitorText';
  127. planeMesh.scale.set(0.22, 0.22, 0.22);
  128. planeMesh.position.set(-1.81, 0.068, -0.4);
  129. this.group?.add(planeMesh);
  130. }
  131. });
  132. }
  133. /* 风门动画 */
  134. render() {
  135. if (!this.model) {
  136. return;
  137. }
  138. if (this.isLRAnimation && this.group) {
  139. // 左右摇摆动画
  140. if (Math.abs(this.group.rotation.y) >= 0.2) {
  141. this.direction = -this.direction;
  142. this.group.rotation.y += 0.00002 * 30 * this.direction;
  143. } else {
  144. this.group.rotation.y += 0.00002 * 30 * this.direction;
  145. }
  146. }
  147. }
  148. /* 点击风窗,风窗全屏 */
  149. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  150. this.isLRAnimation = false;
  151. if (this.animationTimer) {
  152. clearTimeout(this.animationTimer);
  153. this.animationTimer = null;
  154. }
  155. // 判断是否点击到视频
  156. intersects.find((intersect) => {
  157. intersect;
  158. return false;
  159. });
  160. }
  161. mouseUpModel() {
  162. // 10s后开始摆动
  163. if (!this.animationTimer && !this.isLRAnimation) {
  164. this.animationTimer = setTimeout(() => {
  165. this.isLRAnimation = true;
  166. }, 10000);
  167. }
  168. }
  169. resetModel() {
  170. clearTimeout(this.animationTimer);
  171. this.isLRAnimation = false;
  172. }
  173. addCssText = () => {
  174. if (this.nitrogenNum > 0) {
  175. for (let i = 0; i < 2; i++) {
  176. if (this.group && !this.group.getObjectByName('monitorNitrogenText' + i)) {
  177. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  178. if (element) {
  179. const elementCopy = element.cloneNode(true) as HTMLElement;
  180. elementCopy.style.top = '0px';
  181. elementCopy.style.left = '0px';
  182. const nitrogenMonitorCSS3D = new CSS3DSprite(elementCopy);
  183. nitrogenMonitorCSS3D.name = 'monitorNitrogenText' + i;
  184. nitrogenMonitorCSS3D.scale.set(0.0045, 0.0045, 0.0045);
  185. if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
  186. if (i == 1) nitrogenMonitorCSS3D.position.set(1.44, 0.31, 0.04);
  187. // if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  188. // if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  189. this.group.add(nitrogenMonitorCSS3D);
  190. }
  191. }
  192. }
  193. }
  194. };
  195. clearCssText = () => {
  196. if (this.nitrogenNum > 0) {
  197. for (let i = 0; i < 2; i++) {
  198. const mesh = this.group.getObjectByName('monitorNitrogenText' + i);
  199. if (this.group && mesh) {
  200. this.group.remove(mesh);
  201. }
  202. }
  203. }
  204. };
  205. mountedThree() {
  206. return new Promise((resolve) => {
  207. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  208. this.group = gltf[0];
  209. this.setModalPosition();
  210. this.addLight();
  211. resolve(null);
  212. });
  213. });
  214. }
  215. destroy() {
  216. if (this.group) {
  217. this.model.clearGroup(this.group);
  218. }
  219. this.model = null;
  220. this.group = null;
  221. }
  222. }
  223. export default NitrogenUnderground;