123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import * as THREE from 'three';
- import { getTextCanvas, addEnvMap, setModalCenter } from '/@/utils/threejs/util';
- import { CSS3DSprite } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
- class NitrogenOverground {
- model;
- modelName = 'nitrogen';
- group;
- animationTimer;
- isLRAnimation = true;
- direction = 1;
- playerStartClickTime1 = new Date().getTime();
- playerStartClickTime2 = new Date().getTime();
- deviceRunState = '';
- nitrogenNum = 0;
- nitrogenGroup = <THREE.Object3D[]>[];
- constructor(model) {
- this.model = model;
- this.group = new THREE.Object3D();
- this.group.name = this.modelName;
- }
- addLight() {
- const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
- directionalLight.position.set(32, -1, 60);
- this.group.add(directionalLight);
- directionalLight.target = this.group;
-
-
-
- const spotLight = new THREE.SpotLight();
- spotLight.angle = Math.PI / 4;
- spotLight.penumbra = 0;
- spotLight.castShadow = true;
- spotLight.distance = 0;
- spotLight.position.set(-88, 85, -88);
- spotLight.target = this.group;
- this.group.add(spotLight);
- spotLight.shadow.camera.near = 0.5;
- spotLight.shadow.camera.far = 1000;
- spotLight.shadow.focus = 1;
- spotLight.shadow.bias = -0.000002;
-
-
-
-
-
-
-
-
-
- }
-
- setModalPosition() {
- if (this.nitrogenNum == 4) {
- this.group.position.set(0, -17, 3);
- this.group?.scale.set(24.0, 24.0, 24.0);
- }
- if (this.nitrogenNum == 3) {
- this.group.position.set(0, -1, 3);
- this.group?.scale.set(24.0, 24.0, 24.0);
- }
- if (this.nitrogenNum == 2) {
- this.group.position.set(0, 0.42, 1.21);
- this.group?.scale.set(24.0, 24.0, 24.0);
- }
- if (this.nitrogenNum == 1) {
- this.group.position.set(0, 8, 3.0);
- this.group?.scale.set(24.0, 24.0, 24.0);
- }
- }
-
- initAnimation() {}
-
- 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() {}
-
- play() {}
-
- sortMeshChildren = (children: THREE.Mesh[]) => {
-
- children.sort((x, y) => {
- return x.geometry.attributes.position.count - y.geometry.attributes.position.count;
- });
- return children;
- };
-
- transparentModel = (model: THREE.Mesh) => {
- const transparentMaterial = new THREE.MeshBasicMaterial({
- transparent: true,
- opacity: 0,
- });
- model.material = transparentMaterial;
- };
- addCssText = () => {
- if (this.nitrogenNum > 0) {
- for (let i = 0; i < this.nitrogenNum; i++) {
- const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
- if (nitrogenModal && !nitrogenModal.getObjectByName('monitorNitrogenText')) {
- const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
- if (element) {
- const nitrogenMonitorCSS3D = new CSS3DSprite(element);
- nitrogenMonitorCSS3D.name = 'monitorNitrogenText' + i;
- nitrogenMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
- if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
- if (i == 1) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.04);
- if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
- if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
- nitrogenModal.add(nitrogenMonitorCSS3D);
- }
- }
- }
- }
- };
- clearCssText = () => {
- if (this.nitrogenNum > 0) {
- for (let i = 0; i < this.nitrogenNum; i++) {
- const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
- if (nitrogenModal && nitrogenModal.getObjectByName('monitorNitrogenText')) {
- nitrogenModal.remove(nitrogenModal.getObjectByName('monitorNitrogenText') as THREE.Object3D);
- }
- }
- }
- };
-
- handleGlassAndWrap = (
- objects: THREE.Object3D,
- withVolume: THREE.Object3D[],
- glassModel: THREE.Mesh,
- params: THREE.MeshPhysicalMaterialParameters,
- scale: number,
- position: THREE.Vector3,
- rotation?: THREE.Vector3
- ) => {
-
- const children = glassModel.children as THREE.Mesh[];
- this.sortMeshChildren(children);
- children.forEach((mesh) => {
- mesh.position.copy(position);
- mesh.scale.set(scale, scale, scale);
- rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
- });
- const [transparentWrap, glass] = children;
- this.transparentModel(transparentWrap);
- glass.material = new THREE.MeshPhysicalMaterial({
- side: THREE.DoubleSide,
-
-
- ...params,
- });
- objects.add(...children);
-
- withVolume.push(transparentWrap);
- };
- mountedThree(nitrogenNum) {
-
- return new Promise((resolve) => {
- if (nitrogenNum < 1) {
- resolve(null);
- return;
- }
- this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
- for (let i = 0; i < nitrogenNum; i++) {
- const nitrogenModal = gltf[0].clone();
- nitrogenModal.name = 'nitrogenModal' + i;
- this.nitrogenGroup.push(nitrogenModal);
-
-
-
-
-
-
- }
-
- this.addLight();
- resolve(null);
- });
- });
- }
- destroy() {
- this.nitrogenGroup.forEach((item) => {
- this.model.clearGroup(item);
- });
- this.nitrogenGroup = undefined;
- if (this.group) {
- this.model.clearGroup(this.group);
- }
- this.model = null;
- this.group = null;
- }
- }
- export default NitrogenOverground;
|