shuangdaoFc.threejs.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import * as THREE from 'three';
  2. import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
  3. import gsap from 'gsap';
  4. class doubleWindow {
  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. if (this.isLRAnimation && this.group) {
  165. // 左右摇摆动画
  166. if (Math.abs(this.group.rotation.y) >= 0.2) {
  167. this.direction = -this.direction;
  168. this.group.rotation.y += 0.00002 * 30 * this.direction;
  169. } else {
  170. this.group.rotation.y += 0.00002 * 30 * this.direction;
  171. }
  172. }
  173. }
  174. /* 提取风门序列帧,初始化前后门动画 */
  175. initAnimation() {
  176. const meshArr01: THREE.Object3D[] = [];
  177. const meshArr02: THREE.Object3D[] = [];
  178. const windowGroup = new THREE.Group();
  179. windowGroup.name = 'hiddenGroup';
  180. this.group.getObjectByName('sdFc')?.children.forEach((obj) => {
  181. if (obj.type === 'Mesh' && obj.name && (obj.name.startsWith('shanye') || obj.name.startsWith('FCshanye'))) {
  182. if (obj.name.startsWith('FCshanye')) {
  183. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  184. meshArr01.push(obj);
  185. } else if (obj.name.startsWith('shanye')) {
  186. obj.rotateOnAxis(new THREE.Vector3(0, 1, 0), 0);
  187. meshArr02.push(obj);
  188. }
  189. }
  190. });
  191. this.windowsActionArr.frontWindow = meshArr01;
  192. this.windowsActionArr.backWindow = meshArr02;
  193. this.group?.add(windowGroup);
  194. }
  195. /* 点击风窗,风窗全屏 */
  196. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  197. this.isLRAnimation = false;
  198. if (this.animationTimer) {
  199. clearTimeout(this.animationTimer);
  200. this.animationTimer = null;
  201. }
  202. // 判断是否点击到视频
  203. intersects.find((intersect) => {
  204. const mesh = intersect.object;
  205. return false;
  206. });
  207. }
  208. mouseUpModel() {
  209. // 10s后开始摆动
  210. if (!this.animationTimer && !this.isLRAnimation) {
  211. this.animationTimer = setTimeout(() => {
  212. this.isLRAnimation = true;
  213. }, 10000);
  214. }
  215. }
  216. play(rotationParam, flag) {
  217. if (this.windowsActionArr.frontWindow.length <= 0 || this.windowsActionArr.backWindow.length <= 0) {
  218. return;
  219. }
  220. if (flag === 1) {
  221. // 前风窗动画
  222. this.windowsActionArr.frontWindow.forEach((mesh) => {
  223. gsap.to(mesh.rotation, {
  224. y: THREE.MathUtils.degToRad(rotationParam.frontDeg1),
  225. duration: (1 / 9) * Math.abs(rotationParam.frontDeg1 - mesh.rotation.y),
  226. overwrite: true,
  227. });
  228. });
  229. } else if (flag === 2) {
  230. // 后风窗动画
  231. this.windowsActionArr.backWindow.forEach((mesh) => {
  232. gsap.to(mesh.rotation, {
  233. y: THREE.MathUtils.degToRad(rotationParam.backDeg1),
  234. duration: (1 / 9) * Math.abs(rotationParam.backDeg1 - mesh.rotation.y),
  235. overwrite: true,
  236. });
  237. });
  238. } else if (flag === 0) {
  239. ([...this.windowsActionArr.frontWindow, ...this.windowsActionArr.backWindow] as THREE.Mesh[]).forEach((mesh) => {
  240. gsap.to(mesh.rotation, {
  241. y: 0,
  242. overwrite: true,
  243. });
  244. });
  245. }
  246. }
  247. mountedThree(playerDom) {
  248. return new Promise((resolve) => {
  249. this.model.setGLTFModel(['sdFc'], this.group).then(() => {
  250. this.setModalPosition();
  251. this.initAnimation();
  252. resolve(null);
  253. this.addLight();
  254. // this.initCamera(playerDom);
  255. });
  256. });
  257. }
  258. destroy() {
  259. this.model.clearGroup(this.group);
  260. this.windowsActionArr.frontWindow = undefined;
  261. this.windowsActionArr.backWindow = undefined;
  262. this.model = null;
  263. this.group = null;
  264. }
  265. }
  266. export default doubleWindow;