dandaoFcXk.threejs.ts 9.0 KB

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