shuangdaoFc.threejs.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class sdFc_1 {
  5. model;
  6. modelName = 'sdFc';
  7. group: THREE.Object3D = new THREE.Object3D();
  8. animationTimer;
  9. isLRAnimation = true;
  10. direction = 1;
  11. windowsActionArr = {
  12. frontWindow: <THREE.Mesh[]>[],
  13. backWindow: <THREE.Mesh[]>[],
  14. };
  15. constructor(model) {
  16. this.model = model;
  17. // this.group.name = this.modelName;
  18. }
  19. addLight = () => {
  20. if (!this.model) return;
  21. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  22. directionalLight.position.set(-110, 150, 647);
  23. this.group?.add(directionalLight);
  24. // directionalLight.target = group;
  25. const pointLight2 = new THREE.PointLight(0xffffff, 1, 150);
  26. pointLight2.position.set(-101, 34, 16);
  27. pointLight2.shadow.bias = 0.05;
  28. this.group.add(pointLight2);
  29. const pointLight3 = new THREE.PointLight(0xffffff, 1, 150);
  30. pointLight3.position.set(19, 25, -7);
  31. pointLight3.shadow.bias = 0.05;
  32. this.group.add(pointLight3);
  33. const pointLight6 = new THREE.PointLight(0xffffff, 1, 300);
  34. pointLight6.position.set(51, 51, 9);
  35. pointLight6.shadow.bias = 0.05;
  36. this.group.add(pointLight6);
  37. };
  38. // 设置模型位置
  39. setModalPosition() {
  40. // this.group.getObjectByName(this.modelName)?.scale.set(9, 9, 9);
  41. this.group?.scale.set(22, 22, 22);
  42. this.group?.position.set(-15, 25, 15);
  43. }
  44. addMonitorText(selectData) {
  45. if (!this.group) {
  46. return;
  47. }
  48. const screenDownText = VENT_PARAM['modalText']
  49. ? VENT_PARAM['modalText']
  50. : History_Type['type'] == 'remote'
  51. ? `国能神东煤炭集团监制`
  52. : '煤炭科学技术研究院有限公司研制';
  53. const screenDownTextX = 120 - (screenDownText.length - 10) * 6;
  54. const textArr = [
  55. {
  56. text: `远程定量调节自动风窗`,
  57. font: 'normal 30px Arial',
  58. color: '#009900',
  59. strokeStyle: '#002200',
  60. x: 95,
  61. y: 97,
  62. },
  63. {
  64. text: `${selectData.OpenDegree1 ? '前窗开度值(°)' : selectData.forntArea ? '前窗过风面积(㎡)' : '前窗过风面积(㎡)'}:`,
  65. font: 'normal 28px Arial',
  66. color: '#009900',
  67. strokeStyle: '#002200',
  68. x: 5,
  69. y: 150,
  70. },
  71. {
  72. text: selectData.OpenDegree1
  73. ? Number(`${selectData.OpenDegree1}`).toFixed(2)
  74. : selectData.forntArea
  75. ? Number(`${selectData.forntArea}`).toFixed(2)
  76. : '-',
  77. font: 'normal 28px Arial',
  78. color: '#009900',
  79. strokeStyle: '#002200',
  80. x: 330,
  81. y: 150,
  82. },
  83. {
  84. text: `${selectData.OpenDegree2 ? '后窗开度值(°)' : selectData.forntArea ? '后窗过风面积(㎡)' : '后窗过风面积(㎡)'}:`,
  85. font: 'normal 28px Arial',
  86. color: '#009900',
  87. strokeStyle: '#002200',
  88. x: 5,
  89. y: 210,
  90. },
  91. {
  92. text: selectData.OpenDegree2
  93. ? Number(`${selectData.OpenDegree2}`).toFixed(2)
  94. : selectData.rearArea
  95. ? Number(`${selectData.rearArea}`).toFixed(2)
  96. : '-',
  97. font: 'normal 28px Arial',
  98. color: '#009900',
  99. strokeStyle: '#002200',
  100. x: 325,
  101. y: 210,
  102. },
  103. {
  104. text: `${selectData.frontRearDP ? '风窗压差(Pa)' : selectData.windSpeed ? '风速(m/s)' : '通信状态'}:`,
  105. font: 'normal 28px Arial',
  106. color: '#009900',
  107. strokeStyle: '#002200',
  108. x: 5,
  109. y: 266,
  110. },
  111. {
  112. text: `${
  113. selectData.frontRearDP
  114. ? selectData.frontRearDP
  115. : selectData.windSpeed
  116. ? selectData.windSpeed
  117. : selectData.netStatus == '0'
  118. ? '断开'
  119. : '连接'
  120. }`,
  121. font: 'normal 28px Arial',
  122. color: '#009900',
  123. strokeStyle: '#002200',
  124. x: 330,
  125. y: 266,
  126. },
  127. {
  128. text: screenDownText,
  129. font: 'normal 28px Arial',
  130. color: '#009900',
  131. strokeStyle: '#002200',
  132. x: screenDownTextX,
  133. y: 322,
  134. },
  135. ];
  136. getTextCanvas(570, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
  137. const textMap = new THREE.CanvasTexture(canvas); // 关键一步
  138. const textMaterial = new THREE.MeshBasicMaterial({
  139. // 关于材质并未讲解 实操即可熟悉 这里是漫反射类似纸张的材质,对应的就有高光类似金属的材质.
  140. map: textMap, // 设置纹理贴图
  141. transparent: true,
  142. side: THREE.DoubleSide, // 这里是双面渲染的意思
  143. });
  144. textMaterial.blending = THREE.CustomBlending;
  145. const monitorPlane = this.group?.getObjectByName('monitorText');
  146. if (monitorPlane) {
  147. monitorPlane.material = textMaterial;
  148. } else {
  149. const planeGeometry = new THREE.PlaneGeometry(570, 346); // 平面3维几何体PlaneGeometry
  150. const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
  151. planeMesh.name = 'monitorText';
  152. planeMesh.scale.set(0.0018, 0.0018, 0.0018);
  153. planeMesh.position.set(4.04, 0.178, -0.2);
  154. this.group?.add(planeMesh);
  155. }
  156. textMap.dispose();
  157. });
  158. }
  159. /* 风门动画 */
  160. render() {
  161. if (!this.model) {
  162. return;
  163. }
  164. }
  165. /* 提取风门序列帧,初始化前后门动画 */
  166. initAnimation() {
  167. const meshArr01: THREE.Object3D[] = [];
  168. const meshArr02: THREE.Object3D[] = [];
  169. const windowGroup = new THREE.Group();
  170. windowGroup.name = 'hiddenGroup';
  171. this.group.getObjectByName('sdFc')?.children.forEach((obj) => {
  172. if (obj.type === 'Mesh' && obj.name && (obj.name.startsWith('shanye') || obj.name.startsWith('FCshanye'))) {
  173. if (obj.name.startsWith('FCshanye')) {
  174. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  175. meshArr01.push(obj);
  176. } else if (obj.name.startsWith('shanye')) {
  177. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  178. meshArr02.push(obj);
  179. }
  180. }
  181. });
  182. this.windowsActionArr.frontWindow = meshArr01;
  183. this.windowsActionArr.backWindow = meshArr02;
  184. this.group?.add(windowGroup);
  185. }
  186. /* 点击风窗,风窗全屏 */
  187. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  188. if (this.animationTimer) {
  189. clearTimeout(this.animationTimer);
  190. this.animationTimer = null;
  191. }
  192. // 判断是否点击到视频
  193. intersects.find((intersect) => {
  194. const mesh = intersect.object;
  195. return false;
  196. });
  197. }
  198. mouseUpModel() {}
  199. play(rotationParam, flag) {
  200. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  201. return;
  202. }
  203. if (flag === 1) {
  204. // 前风窗动画
  205. this.windowsActionArr.frontWindow.forEach((mesh) => {
  206. gsap.to(mesh.rotation, {
  207. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  208. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  209. overwrite: true,
  210. });
  211. });
  212. } else if (flag === 2) {
  213. // 后风窗动画
  214. this.windowsActionArr.backWindow.forEach((mesh) => {
  215. gsap.to(mesh.rotation, {
  216. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  217. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  218. overwrite: true,
  219. });
  220. });
  221. } else if (flag === 0) {
  222. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  223. gsap.to(mesh.rotation, {
  224. y: 0,
  225. overwrite: true,
  226. });
  227. });
  228. }
  229. }
  230. mountedThree() {
  231. return new Promise((resolve) => {
  232. this.model.setGLTFModel(['sdFc'], this.group).then(() => {
  233. this.setModalPosition();
  234. this.initAnimation();
  235. resolve(null);
  236. this.addLight();
  237. // this.initCamera(playerDom);
  238. });
  239. });
  240. }
  241. destroy() {
  242. this.model.clearGroup(this.group);
  243. this.windowsActionArr.frontWindow = undefined;
  244. this.windowsActionArr.backWindow = undefined;
  245. this.model = null;
  246. this.group = null;
  247. }
  248. }
  249. export default sdFc_1;