dantou.threejs.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import * as THREE from 'three';
  2. import { getTextCanvas } from '/@/utils/threejs/util';
  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 ddWindRect {
  7. model;
  8. modelName = 'ddcf';
  9. group: THREE.Object3D = new THREE.Object3D();
  10. animationTimer;
  11. isLRAnimation = true;
  12. direction = 1;
  13. constructor(model) {
  14. this.model = model;
  15. this.group.name = this.modelName;
  16. }
  17. addLight() {
  18. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  19. directionalLight.position.set(64, 215, 500);
  20. directionalLight.target = this.group;
  21. this.group.add(directionalLight);
  22. const pointLight2 = new THREE.PointLight(0xffffff, 2, 300);
  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', -1000, 1000);
  28. gui.add(pointLight2.position, 'y', -1000, 1000);
  29. gui.add(pointLight2.position, 'z', -1000, 1000);
  30. // gui.add(spotLight.position, 'x', -500, 500);
  31. // gui.add(spotLight.position, 'y', -500, 500);
  32. // gui.add(spotLight.position, 'z', -500, 500);
  33. // gui.add(spotLight, 'distance', 0, 1000);
  34. }
  35. // 设置模型位置
  36. setModalPosition() {
  37. this.group?.scale.set(22, 22, 22);
  38. this.group?.position.set(-10, 25, 20);
  39. }
  40. addMonitorText(selectData) {
  41. if (!this.group) {
  42. return;
  43. }
  44. const textArr = [
  45. {
  46. text: `单点式测风装置`,
  47. font: 'normal 32px Arial',
  48. color: '#009900',
  49. strokeStyle: '#002200',
  50. x: 140,
  51. y: 95,
  52. },
  53. {
  54. text: `风量(m3/min):`,
  55. font: 'normal 29px Arial',
  56. color: '#009900',
  57. strokeStyle: '#002200',
  58. x: 30,
  59. y: 150,
  60. },
  61. {
  62. text: `${selectData.m3 ? selectData.m3 : '-'}`,
  63. font: 'normal 29px Arial',
  64. color: '#009900',
  65. strokeStyle: '#002200',
  66. x: 311,
  67. y: 150,
  68. },
  69. {
  70. text: `风速(m/s): `,
  71. font: 'normal 29px Arial',
  72. color: '#009900',
  73. strokeStyle: '#002200',
  74. x: 30,
  75. y: 202,
  76. },
  77. {
  78. text: `${selectData.va ? selectData.va : '-'}`,
  79. font: 'normal 29px Arial',
  80. color: '#009900',
  81. strokeStyle: '#002200',
  82. x: 310,
  83. y: 202,
  84. },
  85. {
  86. text: `断面积(㎡):`,
  87. font: 'normal 29px Arial',
  88. color: '#009900',
  89. strokeStyle: '#002200',
  90. x: 30,
  91. y: 252,
  92. },
  93. {
  94. text: `${selectData.fsectarea ? selectData.fsectarea : '-'}`,
  95. font: 'normal 29px Arial',
  96. color: '#009900',
  97. strokeStyle: '#002200',
  98. x: 310,
  99. y: 252,
  100. },
  101. {
  102. text: `煤炭科学技术研究院有限公司研制`,
  103. font: 'normal 28px Arial',
  104. color: '#009900',
  105. strokeStyle: '#002200',
  106. x: 50,
  107. y: 302,
  108. },
  109. ];
  110. getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  111. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  112. const textMaterial = new THREE.MeshBasicMaterial({
  113. map: textMap, // 设置纹理贴图
  114. transparent: true,
  115. side: THREE.DoubleSide, // 这里是双面渲染的意思
  116. });
  117. textMaterial.blending = THREE.CustomBlending;
  118. const monitorPlane = this.group?.getObjectByName('monitorText');
  119. if (monitorPlane) {
  120. monitorPlane.material = textMaterial;
  121. } else {
  122. const planeGeometry = new THREE.PlaneGeometry(5.6, 3.46); // 平面3维几何体PlaneGeometry
  123. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  124. planeMesh.name = 'monitorText';
  125. planeMesh.scale.set(0.22, 0.22, 0.22);
  126. planeMesh.position.set(-1.81, 0.068, -0.4);
  127. this.group?.add(planeMesh);
  128. }
  129. });
  130. }
  131. /* 风门动画 */
  132. render() {
  133. if (!this.model) {
  134. return;
  135. }
  136. if (this.isLRAnimation && this.group) {
  137. // 左右摇摆动画
  138. if (Math.abs(this.group.rotation.y) >= 0.2) {
  139. this.direction = -this.direction;
  140. this.group.rotation.y += 0.00002 * 30 * this.direction;
  141. } else {
  142. this.group.rotation.y += 0.00002 * 30 * this.direction;
  143. }
  144. }
  145. }
  146. /* 点击风窗,风窗全屏 */
  147. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  148. this.isLRAnimation = false;
  149. if (this.animationTimer) {
  150. clearTimeout(this.animationTimer);
  151. this.animationTimer = null;
  152. }
  153. // 判断是否点击到视频
  154. intersects.find((intersect) => {
  155. intersect;
  156. return false;
  157. });
  158. }
  159. mouseUpModel() {
  160. // 10s后开始摆动
  161. if (!this.animationTimer && !this.isLRAnimation) {
  162. this.animationTimer = setTimeout(() => {
  163. this.isLRAnimation = true;
  164. }, 10000);
  165. }
  166. }
  167. resetModel() {
  168. clearTimeout(this.animationTimer);
  169. this.isLRAnimation = false;
  170. }
  171. mountedThree() {
  172. return new Promise((resolve) => {
  173. this.model.setGLTFModel([this.modelName]).then((gltf) => {
  174. this.group = gltf[0];
  175. this.setModalPosition();
  176. this.addLight();
  177. resolve(null);
  178. });
  179. });
  180. }
  181. destroy() {
  182. if (this.group) {
  183. this.model.clearGroup(this.group);
  184. }
  185. this.model = null;
  186. this.group = null;
  187. }
  188. }
  189. export default ddWindRect;