dandaoFcXk.threejs.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import * as THREE from 'three';
  2. import { getTextCanvas } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class singleWindowXk {
  5. model;
  6. modelName = 'xkFcGroup';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: [],
  13. };
  14. constructor(model) {
  15. this.model = model;
  16. this.group.name = 'xkFcGroup';
  17. }
  18. addLight = () => {
  19. if (!this.group || !this.group) return;
  20. const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5);
  21. directionalLight.position.set(800.0, 120.0, 80.0);
  22. this.group.add(directionalLight);
  23. };
  24. // 设置模型位置
  25. setModalPosition() {
  26. this.group?.scale.set(22, 22, 22);
  27. this.group?.position.set(-35, 25, 15);
  28. }
  29. addMonitorText(selectData) {
  30. if (!this.group) {
  31. return;
  32. }
  33. const textArr = [
  34. {
  35. text: `远程定量调节自动风窗`,
  36. font: 'normal 30px Arial',
  37. color: '#009900',
  38. strokeStyle: '#002200',
  39. x: 100,
  40. y: 95,
  41. },
  42. {
  43. text: `${selectData.OpenDegree ? '开度值(%)' : selectData.forntArea ? '过风面积(m2)' : '过风面积(m2)'}:`,
  44. font: 'normal 30px Arial',
  45. color: '#009900',
  46. strokeStyle: '#002200',
  47. x: 5,
  48. y: 145,
  49. },
  50. {
  51. text: selectData.OpenDegree
  52. ? Number(`${selectData.OpenDegree}`).toFixed(2)
  53. : selectData.forntArea
  54. ? Number(`${selectData.forntArea}`).toFixed(2)
  55. : '-',
  56. font: 'normal 30px Arial',
  57. color: '#009900',
  58. strokeStyle: '#002200',
  59. x: 330,
  60. y: 145,
  61. },
  62. {
  63. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态:'}:`,
  64. font: 'normal 30px Arial',
  65. color: '#009900',
  66. strokeStyle: '#002200',
  67. x: 5,
  68. y: 200,
  69. },
  70. {
  71. text: `${
  72. selectData.frontRearDP
  73. ? selectData.frontRearDP
  74. : selectData.windSpeed
  75. ? selectData.windSpeed
  76. : selectData.netStatus == '0'
  77. ? '断开'
  78. : '连接'
  79. }`,
  80. font: 'normal 30px Arial',
  81. color: '#009900',
  82. strokeStyle: '#002200',
  83. x: 330,
  84. y: 200,
  85. },
  86. {
  87. text: `${selectData.fWindowM3 ? '过风量(m³/min)' : '风窗道数'}: `,
  88. font: 'normal 30px Arial',
  89. color: '#009900',
  90. strokeStyle: '#002200',
  91. x: 5,
  92. y: 250,
  93. },
  94. {
  95. text: `${selectData.fWindowM3 ? selectData.fWindowM3 : selectData.nwindownum}`,
  96. font: 'normal 30px Arial',
  97. color: '#009900',
  98. strokeStyle: '#002200',
  99. x: 330,
  100. y: 250,
  101. },
  102. {
  103. text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
  104. font: 'normal 28px Arial',
  105. color: '#009900',
  106. strokeStyle: '#002200',
  107. x: History_Type['type'] == 'remote' ? 90 : 30,
  108. y: 300,
  109. },
  110. ];
  111. getTextCanvas(750, 546, textArr, '').then((canvas: HTMLCanvasElement) => {
  112. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  113. const textMaterial = new THREE.MeshBasicMaterial({
  114. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  115. map: textMap, // 设置纹理贴图
  116. transparent: true,
  117. side: THREE.DoubleSide, // 这里是双面渲染的意思
  118. });
  119. textMap.dispose();
  120. textMaterial.blending = THREE.CustomBlending;
  121. const monitorPlane = this.group?.getObjectByName('monitorText');
  122. if (monitorPlane) {
  123. monitorPlane.material = textMaterial;
  124. } else {
  125. const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry
  126. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  127. planeMesh.name = 'monitorText';
  128. planeMesh.scale.set(0.0095, 0.01, 0.01);
  129. planeMesh.position.set(6.97, -0.395, -0.63);
  130. this.group?.add(planeMesh);
  131. }
  132. });
  133. }
  134. /* 提取风门序列帧,初始化前后门动画 */
  135. initAnimation() {
  136. const meshArr01: THREE.Object3D[] = [];
  137. const fmGroup = this.group?.getObjectByName('Fc-Xk');
  138. if (fmGroup) {
  139. fmGroup.children.forEach((obj) => {
  140. if (obj.type === 'Mesh' && obj.name && obj.name.startsWith('Blades')) {
  141. obj.rotateOnAxis(new THREE.Vector3(0, 0, -1), 0);
  142. meshArr01.push(obj);
  143. }
  144. });
  145. }
  146. this.windowsActionArr.frontWindow = meshArr01;
  147. }
  148. play(rotationParam, flag) {
  149. if (!this.windowsActionArr.frontWindow) {
  150. return;
  151. }
  152. if (flag === 1) {
  153. // 前风窗动画
  154. this.windowsActionArr.frontWindow.forEach((mesh: THREE.Mesh) => {
  155. gsap.to(mesh.rotation, {
  156. x: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  157. duration: Math.abs(rotationParam.frontDeg1 - mesh.rotation.x) / 10,
  158. overwrite: true,
  159. });
  160. });
  161. } else if (flag === 0) {
  162. ([...this.windowsActionArr.frontWindow] as THREE.Mesh[]).forEach((mesh) => {
  163. gsap.to(mesh.rotation, {
  164. x: 0,
  165. overwrite: true,
  166. });
  167. });
  168. }
  169. }
  170. /* 点击风窗,风窗全屏 */
  171. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  172. this.isLRAnimation = false;
  173. if (this.animationTimer) {
  174. clearTimeout(this.animationTimer);
  175. this.animationTimer = null;
  176. }
  177. console.log('摄像头控制信息', this.model.orbitControls, this.model.camera);
  178. }
  179. mouseUpModel() {
  180. // 10s后开始摆动
  181. if (!this.animationTimer && !this.isLRAnimation) {
  182. this.animationTimer = setTimeout(() => {
  183. this.isLRAnimation = true;
  184. }, 10000);
  185. }
  186. }
  187. /* 风门动画 */
  188. render() {
  189. if (!this.model) {
  190. return;
  191. }
  192. if (this.isLRAnimation && this.group) {
  193. // 左右摇摆动画
  194. if (Math.abs(this.group.rotation.y) >= 0.2) {
  195. this.direction = -this.direction;
  196. this.group.rotation.y += 0.00002 * 30 * this.direction;
  197. } else {
  198. this.group.rotation.y += 0.00002 * 30 * this.direction;
  199. }
  200. }
  201. }
  202. resetMaterial() {
  203. const fcModal = this.group.getObjectByName('Fc-Xk');
  204. if (fcModal) {
  205. const stainlseeSteel_grey = (fcModal.getObjectByName('Box771') as THREE.Mesh)?.material; // 圈;
  206. const stainlessSteel_light = (fcModal.getObjectByName('Line12241') as THREE.Mesh)?.material; // 框;
  207. const fluorescent_green = (fcModal.getObjectByName('对象276') as THREE.Mesh)?.material; // fluorescent_green;
  208. const fluorescent_green1 = (fcModal.getObjectByName('Box770') as THREE.Mesh)?.material; // 23 - Default;
  209. const wire = (fcModal.getObjectByName('对象281') as THREE.Mesh)?.material; // wire_028149177;
  210. const net = (fcModal.getObjectByName('Plane9230') as THREE.Mesh)?.material; // wire_028149177;
  211. const stainlesssteel_blue = (fcModal.getObjectByName('Blades08') as THREE.Mesh)?.material; // wire_028149177;
  212. (stainlseeSteel_grey as THREE.MeshStandardMaterial).color.set(0xb3b3b3);
  213. (stainlseeSteel_grey as THREE.MeshStandardMaterial).roughness = 0.26;
  214. (stainlessSteel_light as THREE.MeshStandardMaterial).color.set(0x9e9e9e);
  215. (stainlessSteel_light as THREE.MeshStandardMaterial).roughness = 0.26;
  216. (stainlessSteel_light as THREE.MeshStandardMaterial).envMapIntensity = 0.92;
  217. (fluorescent_green as THREE.MeshStandardMaterial).color.set(0x00fff2);
  218. (fluorescent_green as THREE.MeshStandardMaterial).envMapIntensity = 1.5;
  219. //
  220. (fluorescent_green1 as THREE.MeshStandardMaterial).color.set(0x00fff2);
  221. (fluorescent_green1 as THREE.MeshStandardMaterial).envMapIntensity = 1.5;
  222. (wire as THREE.MeshStandardMaterial).color.set(0x38e4ff);
  223. (wire as THREE.MeshStandardMaterial).envMapIntensity = 2;
  224. (net as THREE.MeshStandardMaterial).color.set(0xcfcfcf);
  225. (stainlesssteel_blue as THREE.MeshStandardMaterial).color.set(0xababab);
  226. (stainlesssteel_blue as THREE.MeshStandardMaterial).roughness = 0.45;
  227. }
  228. }
  229. mountedThree() {
  230. return new Promise((resolve) => {
  231. this.model.setGLTFModel(['Fc-Xk'], this.group).then(async () => {
  232. resolve(null);
  233. this.resetMaterial();
  234. this.setModalPosition();
  235. this.initAnimation();
  236. this.addLight();
  237. });
  238. });
  239. }
  240. destroy() {
  241. this.model.clearGroup(this.group);
  242. this.model = null;
  243. this.group = null;
  244. }
  245. }
  246. export default singleWindowXk;