123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- import * as THREE from 'three';
- import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
- import { getTextCanvas, renderVideo } from '/@/utils/threejs/util';
- import UseThree from '../../../../utils/threejs/useThree';
- import { drawHot } from '/@/utils/threejs/util';
- import { useAppStore } from '/@/store/modules/app';
- class FmXR {
- modelName = 'fmXr';
- model;
- group;
- isLRAnimation = true;
- direction = 1;
- animationTimer: NodeJS.Timeout | null = null;
- player1;
- player2;
- deviceDetailCSS3D;
- playerStartClickTime1 = new Date().getTime();
- playerStartClickTime2 = new Date().getTime();
- fmClock = new THREE.Clock();
- mixers: THREE.AnimationMixer | undefined;
- appStore = useAppStore();
- backDamperOpenMesh;
- backDamperClosedMesh;
- frontDamperOpenMesh;
- frontDamperClosedMesh;
- clipActionArr = {
- frontDoor: null as unknown as THREE.AnimationAction,
- backDoor: null as unknown as THREE.AnimationAction,
- };
- constructor(model) {
- this.model = model;
- }
- addLight() {
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- resetCamera() {
- this.model.camera.far = 274;
- this.model.orbitControls?.update();
- this.model.camera.updateProjectionMatrix();
- }
-
- setModalPosition() {
- this.group?.scale.set(22, 22, 22);
- this.group?.position.set(-20, 20, 9);
- }
-
- addMonitorText(selectData) {
- if (!this.group) {
- return;
- }
- const textArr = [
- {
- text: `远程控制自动风门`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 120,
- y: 95,
- },
- {
- text: `净通行高度(m):`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 0,
- y: 155,
- },
- {
- text: `${selectData.fclearheight ? selectData.fclearheight : '-'}`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 290,
- y: 155,
- },
- {
- text: `净通行宽度(m): `,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 0,
- y: 215,
- },
- {
- text: ` ${selectData.fclearwidth ? selectData.fclearwidth : '-'}`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 280,
- y: 215,
- },
- {
- text: `故障诊断:`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 0,
- y: 275,
- },
- {
- text: `${selectData.warnLevel_str ? selectData.warnLevel_str : '-'}`,
- font: 'normal 30px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: 280,
- y: 275,
- },
- {
- text: History_Type['type'] == 'remote' ? `国能神东煤炭集团监制` : '煤炭科学技术研究院有限公司研制',
- font: 'normal 28px Arial',
- color: '#00FF00',
- strokeStyle: '#007400',
- x: History_Type['type'] == 'remote' ? 80 : 20,
- y: 330,
- },
- ];
-
- getTextCanvas(526, 346, textArr, '').then((canvas: HTMLCanvasElement) => {
- const textMap = new THREE.CanvasTexture(canvas);
- textMap.colorSpace = THREE.SRGBColorSpace;
- const textMaterial = new THREE.MeshBasicMaterial({
-
- map: textMap,
- transparent: true,
- side: THREE.FrontSide,
- });
- textMaterial.blending = THREE.CustomBlending;
- const monitorPlane = this.group.getObjectByName('monitorText');
- if (monitorPlane) {
- monitorPlane.material = textMaterial;
- } else {
- const planeGeometry = new THREE.PlaneGeometry(526, 346);
- const planeMesh = new THREE.Mesh(planeGeometry, textMaterial);
- planeMesh.name = 'monitorText';
- planeMesh.scale.set(0.0018, 0.0015, 0.0015);
- planeMesh.position.set(2.975, 0.46, -0.33);
- this.group.add(planeMesh);
- }
- textMap.dispose();
- });
- }
-
- drawHots() {
- const hotPositions = [
- { x: -0.37, y: 0.26, z: -0.32 },
- { x: 0.28, y: -0.2, z: -0.43 },
- { x: 0.55, y: -0.22, z: -0.38 },
- ];
- for (let i = 0; i < 3; i++) {
- const hotPoint = drawHot(0.1);
- const position = hotPositions[i];
- hotPoint.scale.set(0.1, 0.1, 0.1);
- hotPoint.position.set(position.x, position.y, position.z);
- hotPoint.name = 'hotPoint' + i;
- this.group?.add(hotPoint);
- }
- }
-
- 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 && this.fmClock.running) {
- this.mixers.update(2);
- }
- }
-
- mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
- this.isLRAnimation = false;
- if (this.animationTimer) {
- clearTimeout(this.animationTimer);
- this.animationTimer = null;
- }
-
- intersects.find((intersect) => {
- const mesh = intersect.object;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return false;
- });
- }
- mouseUpModel() {
-
- if (!this.animationTimer && !this.isLRAnimation) {
- this.animationTimer = setTimeout(() => {
- this.isLRAnimation = true;
- }, 10000);
- }
- }
-
- initAnimation() {
- const fmGroup = this.group?.getObjectByName('fmXr-all');
- if (fmGroup) {
- const tracks = fmGroup.animations[0].tracks;
- const fontTracks: any[] = [],
- backTracks: any[] = [];
- for (let i = 0; i < tracks.length; i++) {
- const track = tracks[i];
- if (track.name.startsWith('Men_1')) {
- fontTracks.push(track);
- } else if (track.name.startsWith('Men_2')) {
- backTracks.push(track);
- }
- }
- this.mixers = new THREE.AnimationMixer(fmGroup);
- const frontDoor = new THREE.AnimationClip('frontDoor', 4, fontTracks);
- const frontClipAction = this.mixers.clipAction(frontDoor, fmGroup);
- frontClipAction.clampWhenFinished = true;
- frontClipAction.loop = THREE.LoopOnce;
- this.clipActionArr.frontDoor = frontClipAction;
- const backDoor = new THREE.AnimationClip('backDoor', 4, backTracks);
- const backClipAction = this.mixers.clipAction(backDoor, fmGroup);
- backClipAction.clampWhenFinished = true;
- backClipAction.loop = THREE.LoopOnce;
- this.clipActionArr.backDoor = backClipAction;
- }
- }
- deviceDetailCard(position = { x: 0, y: 0, z: 0 }) {
- const element = document.getElementById('deviceCard') as HTMLElement;
- if (element) {
- this.deviceDetailCSS3D = new CSS2DObject(element);
- this.deviceDetailCSS3D.name = 'deviceCard';
- this.deviceDetailCSS3D.position.set(position.x, position.y, position.z);
- this.deviceDetailCSS3D.visible = false;
-
- this.group.add(this.deviceDetailCSS3D);
- }
- }
-
- play(handlerState, timeScale = 0.01) {
- if (this.clipActionArr.frontDoor && this.clipActionArr.backDoor) {
- let handler = () => {};
- switch (handlerState) {
- case 1:
- handler = () => {
- this.clipActionArr.frontDoor.paused = true;
- this.clipActionArr.frontDoor.reset();
- this.clipActionArr.frontDoor.time = 0.5;
- this.clipActionArr.frontDoor.timeScale = timeScale;
-
- this.clipActionArr.frontDoor.play();
- this.fmClock.start();
-
- if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = true;
- if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = false;
- };
- break;
- case 2:
- handler = () => {
- this.clipActionArr.frontDoor.paused = true;
- this.clipActionArr.frontDoor.reset();
- this.clipActionArr.frontDoor.time = 2;
- this.clipActionArr.frontDoor.timeScale = -timeScale;
-
- this.clipActionArr.frontDoor.play();
- this.fmClock.start();
- if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
- if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
- };
- break;
- case 3:
- handler = () => {
- this.clipActionArr.backDoor.paused = true;
- this.clipActionArr.backDoor.reset();
- this.clipActionArr.backDoor.time = 0.5;
- this.clipActionArr.backDoor.timeScale = timeScale;
-
- this.clipActionArr.backDoor.play();
- this.fmClock.start();
- if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = true;
- if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = false;
- };
- break;
- case 4:
- handler = () => {
- this.clipActionArr.backDoor.paused = true;
- this.clipActionArr.backDoor.reset();
- this.clipActionArr.backDoor.time = 2;
- this.clipActionArr.backDoor.timeScale = -timeScale;
-
- this.clipActionArr.backDoor.play();
- this.fmClock.start();
- if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
- if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
- };
- break;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- default:
- }
- handler();
-
-
-
-
-
-
-
-
-
-
- }
- }
- async initCamera(dom1) {
- const videoPlayer1 = dom1;
- this.player1 = dom1;
- let monitorPlane: THREE.Mesh | null = null;
- if (!videoPlayer1) {
- const textArr = [
- {
- text: `无信号输入`,
- font: 'normal 40px Arial',
- color: '#009900',
- strokeStyle: '#002200',
- x: 170,
- y: 40,
- },
- ];
- const canvas = await getTextCanvas(320, 180, '', 'noSinge.png');
- let textMaterial: THREE.MeshBasicMaterial | null = null;
- if (canvas) {
- const textMap = new THREE.CanvasTexture(canvas);
- textMaterial = new THREE.MeshBasicMaterial({
- map: textMap,
- transparent: true,
- side: THREE.DoubleSide,
- });
- textMaterial.blending = THREE.CustomBlending;
- const planeGeometry = new THREE.PlaneGeometry(100, 100);
- monitorPlane = new THREE.Mesh(planeGeometry, textMaterial);
- textMaterial.dispose();
- planeGeometry.dispose();
- textMap.dispose();
- }
- }
- const player1 = this.group.getObjectByName('player1');
- if (player1) {
- this.model.clearMesh(player1);
- this.group.remove(player1);
- }
- const noPlayer1 = this.group.getObjectByName('noPlayer1');
- if (noPlayer1) {
- this.model.clearMesh(noPlayer1);
- this.group.remove(noPlayer1);
- }
- if (!videoPlayer1 && videoPlayer1 === null) {
- if (monitorPlane && !this.group.getObjectByName('noPlayer1')) {
- const planeMesh = monitorPlane.clone();
- planeMesh.name = 'noPlayer1';
- planeMesh.scale.set(0.0085, 0.0055, 0.012);
- planeMesh.position.set(-3.64, 0.01, -0.41);
- this.group?.add(planeMesh.clone());
- }
- } else if (videoPlayer1) {
- try {
- const mesh = renderVideo(this.group, videoPlayer1, 'player1');
- if (mesh) {
- mesh?.scale.set(-0.0275, 0.028, 1);
- mesh?.position.set(-3.643, 0.02, -0.4);
- mesh.rotation.y = -Math.PI;
- this.group.add(mesh);
- }
- } catch (error) {
- console.log('视频信号异常');
- }
- }
- }
- setMeshHide() {
- const doorWire = this.group.getObjectByName('JuXingHangDao');
- if (doorWire) {
- this.backDamperOpenMesh = doorWire.getObjectByName('Dampler_open_1');
- if (this.backDamperOpenMesh) this.backDamperOpenMesh.visible = false;
- this.backDamperClosedMesh = doorWire.getObjectByName('Damper_Closed_1');
- if (this.backDamperClosedMesh) this.backDamperClosedMesh.visible = true;
- this.frontDamperOpenMesh = doorWire.getObjectByName('Damper_Open_2');
- if (this.frontDamperOpenMesh) this.frontDamperOpenMesh.visible = false;
- this.frontDamperClosedMesh = doorWire.getObjectByName('Damper_Closed_2');
- if (this.frontDamperClosedMesh) this.frontDamperClosedMesh.visible = true;
- }
- }
- mountedThree(playerDom) {
- this.group = new THREE.Object3D();
- this.group.name = this.modelName;
- return new Promise((resolve) => {
-
-
-
- this.model.setGLTFModel(['fmXr-all'], this.group, true).then(() => {
- console.log(this.group);
- this.setModalPosition();
-
- this.initAnimation();
-
- this.addLight();
-
- this.model.animate();
- this.setMeshHide();
- resolve(this.model);
- });
- });
- }
- destroy() {
- if (this.model) {
- if (this.mixers) {
- this.mixers.uncacheClip(this.clipActionArr.frontDoor.getClip());
- this.mixers.uncacheClip(this.clipActionArr.backDoor.getClip());
- this.mixers.uncacheAction(this.clipActionArr.frontDoor.getClip(), this.group);
- this.mixers.uncacheAction(this.clipActionArr.backDoor.getClip(), this.group);
- this.mixers.uncacheRoot(this.group);
- if (this.model.animations[0]) this.model.animations[0].tracks = [];
- }
- this.model.clearGroup(this.group);
- this.clipActionArr.backDoor = undefined;
- this.clipActionArr.frontDoor = undefined;
- this.mixers = undefined;
-
- }
- }
- }
- export default FmXR;
|