import * as THREE from 'three'; import { getTextCanvas, renderVideo } from '/@/utils/threejs/util'; // import * as dat from 'dat.gui'; // const gui = new dat.GUI(); // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999'; class zdWindRect { model; modelName = 'zdcf'; group: THREE.Object3D = new THREE.Object3D(); mixers: THREE.AnimationMixer[] = []; animations: THREE.AnimationClip[] = []; animationAction = null as unknown as THREE.AnimationAction; animationTimer; isLRAnimation = true; direction = 1; player1; playerStartClickTime1 = new Date().getTime(); constructor(model, playerVal1) { this.model = model; this.player1 = playerVal1; this.group.name = this.modelName; } addLight() { const directionalLight = new THREE.DirectionalLight(0xffffff, 1); directionalLight.position.set(42.4, -250, 53); this.group.add(directionalLight); directionalLight.target = this.group; // gui.add(directionalLight.position, 'x', -500, 500); // gui.add(directionalLight.position, 'y', -500, 500); // gui.add(directionalLight.position, 'z', -500, 500); const spotLight = new THREE.SpotLight(); spotLight.angle = Math.PI / 8; spotLight.penumbra = 0; spotLight.castShadow = true; spotLight.distance = 0; spotLight.position.set(-470, -500, 500); spotLight.target = this.group; this.group.add(spotLight); spotLight.shadow.camera.near = 0.5; // default spotLight.shadow.camera.far = 1000; // default spotLight.shadow.focus = 1; spotLight.shadow.bias = -0.000002; } // 设置模型位置 setModalPosition() { this.group?.scale.set(22, 22, 22); this.group?.position.set(-35, 25, 15); } addMonitorText(selectData) { if (!this.group) { return; } const textArr = [ { text: `折叠式测风装置`, font: 'normal 32px Arial', color: '#009900', strokeStyle: '#002200', x: 140, y: 90, }, { text: `风量(m3/min):`, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 2, y: 145, }, { text: `${selectData.m3 ? selectData.m3 : '-'}`, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 200, y: 145, }, { text: `气源压力(MPa): `, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 2, y: 202, }, { text: `${selectData.temperature ? selectData.temperature : '-'}`, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 215, y: 202, }, { text: `Va(m/s):`, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 2, y: 255, }, { text: `${selectData.va ? selectData.va : '-'}`, font: 'normal 29px Arial', color: '#009900', strokeStyle: '#002200', x: 130, y: 255, }, { text: `V1(m/s):`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 321, y: 145, }, { text: `${selectData.incipientWindSpeed1 ? selectData.incipientWindSpeed1 : '-'}`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 445, y: 145, }, { text: `V2(m/s):`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 320, y: 200, }, { text: `${selectData.incipientWindSpeed2 ? selectData.incipientWindSpeed2 : '-'}`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 442, y: 200, }, { text: `V3(m/s):`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 320, y: 252, }, { text: `${selectData.incipientWindSpeed3 ? selectData.incipientWindSpeed3 : '-'}`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 442, y: 252, }, { text: `煤炭科学技术研究院有限公司研制`, font: 'normal 28px Arial', color: '#009900', strokeStyle: '#002200', x: 60, y: 302, }, ]; getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => { const textMap = new THREE.CanvasTexture(canvas); // 关键一步 const textMaterial = new THREE.MeshBasicMaterial({ map: textMap, // 设置纹理贴图 transparent: true, side: THREE.DoubleSide, // 这里是双面渲染的意思 }); textMaterial.blending = THREE.CustomBlending; const monitorPlane = this.group?.getObjectByName('monitorText'); if (monitorPlane) { monitorPlane.material = textMaterial; } else { const planeGeometry = new THREE.PlaneGeometry(526, 346); // 平面3维几何体PlaneGeometry const planeMesh = new THREE.Mesh(planeGeometry, textMaterial); planeMesh.name = 'monitorText'; planeMesh.scale.set(0.002, 0.002, 0.002); planeMesh.position.set(-3.6, -0.123, -0.41); this.group?.add(planeMesh); } }); } /* 风门动画 */ render() { if (!this.model) { return; } if (this.isLRAnimation && this.group) { // 左右摇摆动画 if (Math.abs(this.group.rotation.y) >= 0.2) { this.direction = -this.direction; this.group.rotation.y += 0.00002 * 30 * this.direction; } else { this.group.rotation.y += 0.00002 * 30 * this.direction; } } if (this.mixers[0]) this.mixers[0]?.update(1 / 25); } /* 提取风门序列帧,初始化前后门动画 */ initAnimation() { if (this.group) { if (this.group.animations && this.group.animations.length > 0) { this.group.animations.forEach((animation) => { const mixer = new THREE.AnimationMixer(this.group); this.mixers.push(mixer); this.animations.push(animation); }); } this.animationAction = this.mixers[0].clipAction(this.animations[0]); this.animationAction.clampWhenFinished = true; this.animationAction.loop = THREE.LoopOnce; } } /* 点击风窗,风窗全屏 */ mousedownModel(intersects: THREE.Intersection>[]) { this.isLRAnimation = false; if (this.animationTimer) { clearTimeout(this.animationTimer); this.animationTimer = null; } // 判断是否点击到视频 intersects.find((intersect) => { const mesh = intersect.object; if (mesh.name === 'player1') { if (new Date().getTime() - this.playerStartClickTime1 < 400) { // 双击,视频放大 if (this.player1) { this.player1.requestFullscreen(); } } this.playerStartClickTime1 = new Date().getTime(); return true; } return false; }); } mouseUpModel() { // 10s后开始摆动 if (!this.animationTimer && !this.isLRAnimation) { this.animationTimer = setTimeout(() => { this.isLRAnimation = true; }, 10000); } } resetModel() { clearTimeout(this.animationTimer); this.isLRAnimation = false; } // 播放动画 play(flag, isDirect = false) { if (flag === 'up' && this.animationAction.time <= 0) { this.animationAction?.reset(); // @ts-ignore this.animationAction.time = 0; this.animations[0].duration = 200 / 25; this.mixers[0].timeScale = 0.15; this.animationAction?.play(); } else if (flag === 'center' && this.animationAction.time <= 8) { this.animationAction?.reset(); // @ts-ignore this.animationAction.time = 200 / 25; // 8 this.animations[0].duration = 350 / 25; this.mixers[0].timeScale = 0.15; this.animationAction?.play(); } else if (flag === 'down' && this.animationAction.time <= 14) { this.animationAction?.reset(); // @ts-ignore this.animationAction.time = 350 / 25; // 14 this.animations[0].duration = 530 / 25; this.mixers[0].timeScale = 0.12; this.animationAction?.play(); } // else if ((flag === 'reset' && this.animationAction.time != 0 && this.animationAction.time <= 18) || (flag === 'up' && this.animationAction.time == 18)) { // debugger; // this.animationAction?.reset(); // // @ts-ignore // this.animationAction.time = 450 / 25; // // this.animations[0].duration = 530 / 25; // this.mixers[0].timeScale = 0.15; // this.animationAction?.play(); // } } mountedThree() { return new Promise((resolve) => { this.model.setGLTFModel([this.modelName]).then((gltf) => { this.group = gltf[0]; this.setModalPosition(); this.initAnimation(); this.addLight(); setTimeout(async () => { const videoPlayer1 = document.getElementById('cf-player1')?.getElementsByClassName('vjs-tech')[0]; if (videoPlayer1) { const mesh = renderVideo(this.group, videoPlayer1, 'player1'); mesh?.scale.set(0.0385, 0.028, 0.022); mesh?.position.set(4.792, -0.16, -0.4); if (mesh) this.group?.add(mesh); } else { const textArr = [ { text: `无信号输入`, font: 'normal 40px Arial', color: '#009900', strokeStyle: '#002200', x: 170, y: 40, }, ]; getTextCanvas(560, 346, textArr, '').then((canvas: HTMLCanvasElement) => { const textMap = new THREE.CanvasTexture(canvas); // 关键一步 const textMaterial = new THREE.MeshBasicMaterial({ map: textMap, // 设置纹理贴图 transparent: true, side: THREE.DoubleSide, // 这里是双面渲染的意思 }); textMaterial.blending = THREE.CustomBlending; const monitorPlane = this.group?.getObjectByName('noPlayer'); if (monitorPlane) { monitorPlane.material = textMaterial; } else { const planeGeometry = new THREE.PlaneGeometry(100, 100); // 平面3维几何体PlaneGeometry const planeMesh = new THREE.Mesh(planeGeometry, textMaterial); planeMesh.name = 'noPlayer'; planeMesh.scale.set(0.012, 0.009, 0.012); planeMesh.position.set(4.83, -0.5, -0.39); this.group?.add(planeMesh); } }); } resolve(null); }, 0); }); }); } destroy() { if (this.mixers[0] && this.group) { this.mixers[0].uncacheClip(this.animationAction.getClip()); this.mixers[0].uncacheAction(this.animationAction.getClip(), this.group); this.mixers[0].uncacheRoot(this.group as THREE.Group); this.model.clearGroup(this.group); } this.model = null; this.group = null; } } export default zdWindRect;