-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnvironmentManager.js
140 lines (114 loc) · 3.38 KB
/
EnvironmentManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader";
import { Assets, Colors } from "./utils/constants";
import { FBXLoader } from "three/addons/loaders/FBXLoader";
const ColorLerps = {
ToCyan: "LerpToCyan",
ToPink: "LerpToPink",
ToBlack: "LerpToBlack",
};
class EnvironmentManager {
constructor() {
this._GLTFLoader = new GLTFLoader();
this._FBXLoader = new FBXLoader();
this._lerpCounter = 0;
this.currLerp = ColorLerps.ToCyan;
this._FBXLoader.load(Assets.Environment, (obj) => {
/**
* @type { THREE.Mesh }
*/
this.envMesh = obj.children[0];
this.envMesh.rotation.z = Math.PI / 2;
this.envMesh.position.y = -120;
//Grid Color
/**
* @type {THREE.Material}
*/
const mainMat = this.envMesh.material[1];
mainMat.color = Colors.Pink;
mainMat.emissive = new THREE.Color(0.1, 0.1, 0.1);
mainMat.emissiveIntensity = 0.3;
this.envMesh.material[0].emissiveIntensity = 0;
});
this._GLTFLoader.load(Assets.GameTitleGLB, (gltf) => {
/**
* @type { THREE.Mesh }
*/
this.titleMesh = gltf.scene.children[0];
this.titleMesh.scale.set(3, 3, 3);
const box = new THREE.Box3().setFromObject(this.titleMesh);
const vector = new THREE.Vector3();
box.getCenter(vector);
vector.setY(-3);
vector.setZ(8);
this.titleMesh.position.sub(vector);
this.titleMesh.material = new THREE.MeshStandardMaterial({
color: 0xfbcb00,
});
this.titleMesh.material.emissive = new THREE.Color(0xff7800);
this.titleMesh.material.emissiveIntensity = 3;
});
this._lights = {
ambient: new THREE.AmbientLight(0xffffff, 0.4),
point: new THREE.PointLight(),
};
this._lights.point.position.set(2, 3, 4);
}
get lights() {
return Object.values(this._lights);
}
_handleLerpCount() {
setInterval(() => {
if (this._lerpCounter >= 1) {
this._lerpCounter = 0;
}
this._lerpCounter += 0.014;
}, 100);
}
loop(deltaTime) {
this._handleTerrain(deltaTime);
environment._handleLerpCount();
this.handleTerrainVFX();
}
_handleTerrain(deltaTime) {
const ENVMESH_RESET_POINT_Z = 3000;
if (!!this.envMesh) {
this.envMesh.position.z += 100 * deltaTime;
if (this.envMesh.position.z >= ENVMESH_RESET_POINT_Z) {
this.envMesh.position.z = 0;
}
}
}
get terrainLight() {
return this.envMesh.material[1];
}
handleTerrainVFX() {
switch (this.currLerp) {
case ColorLerps.ToCyan:
if (this._lerpCounter > 1) {
this._lerpCounter = 0;
this.currLerp = ColorLerps.ToPink;
return;
}
this.terrainLight.color.lerp(Colors.Cyan, this._lerpCounter);
break;
case ColorLerps.ToPink:
if (this._lerpCounter > 1) {
this._lerpCounter = 0;
this.currLerp = ColorLerps.ToBlack;
return;
}
this.terrainLight.color.lerp(Colors.Pink, this._lerpCounter);
break;
case ColorLerps.ToBlack:
if (this._lerpCounter > 1) {
this._lerpCounter = 0;
this.currLerp = ColorLerps.ToCyan;
return;
}
this.terrainLight.color.lerp(Colors.Black, this._lerpCounter);
break;
}
}
}
export const environment = new EnvironmentManager();