-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (204 loc) · 8.54 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import * as THREE from 'three';
import { Lensflare, LensflareElement } from './Lensflare.js';
// import metaversefile from 'metaversefile';
import {Cloud} from './cloud.js';
import {Sky} from './sky.js';
const baseUrl = import.meta.url.replace(/(\/)[^\/\\]*$/, '$1');
const textureLoader = new THREE.TextureLoader();
const cloudTexture1 = textureLoader.load(baseUrl + `./textures/cloud1.png`);
const cloudTexture2 = textureLoader.load(baseUrl + `./textures/cloud2.png`);
const cloudTexture3 = textureLoader.load(baseUrl + `./textures/cloud3.png`);
const cloudTexture4 = textureLoader.load(baseUrl + `./textures/cloud4.png`);
const moonTexture = textureLoader.load(baseUrl + `./textures/moon2.png`);
const starTexture = textureLoader.load(baseUrl + `./textures/star3.png`);
starTexture.wrapS = starTexture.wrapT = THREE.RepeatWrapping;
const noiseTexture = textureLoader.load(baseUrl + `./textures/noise.png`);
noiseTexture.wrapS = noiseTexture.wrapT = THREE.RepeatWrapping;
const galaxyTexture = textureLoader.load(baseUrl + `./textures/galaxy.png`);
const noiseTexture2 = textureLoader.load(baseUrl + `./textures/noise2.png`);
noiseTexture2.wrapS = noiseTexture2.wrapT = THREE.RepeatWrapping;
const textureFlare0 = textureLoader.load(baseUrl + `./textures/Flare32.png`);
const textureFlare3 = textureLoader.load(baseUrl + `./textures/lensflare3.png`);
export default (ctx) => {
const {
useApp,
useFrame,
useCamera,
// useInternals,
useLocalPlayer,
useSkyManager,
} = ctx;
const app = useApp();
// const {camera} = useInternals();
const camera = useCamera();
const localPlayer = useLocalPlayer();
const skyManager = useSkyManager();
// ######################################## set component ################################################################
let skylightSpeed = 1;
const _setSkyLight = (value) => {
skylightSpeed = value.speed;
}
const ambientLight = new THREE.AmbientLight('#fff', 0.5);
app.add(ambientLight);
const _setAmbientLight = (value) => {
const args = value.args;
ambientLight.color.set(ambientLight.color.fromArray(args[0]).multiplyScalar(1 / 255).getHex());
ambientLight.intensity = args[1];
}
const _setHemisphereLight = (value) => {
const args = value.args;
const hemiLight = new THREE.HemisphereLight(
new THREE.Color().fromArray(args[0]).multiplyScalar(1 / 255).getHex(),
new THREE.Color().fromArray(args[1]).multiplyScalar(1 / 255).getHex(),
args[2]
);
hemiLight.position.set(value.position[0], value.position[1], value.position[2]);
app.add(hemiLight);
}
let sunColor = new THREE.Color(0xffffff);
let sunColorHex = '#' + sunColor.getHexString();
let sunIntensity = 6;
const _setSunLight = (value) => {
const args = value.args;
sunColorHex = '#' + sunColor.fromArray(args[0]).multiplyScalar(1 / 255).getHexString();
sunIntensity = args[1];
}
let moonColor = new THREE.Color(0x98caf5);
let moonColorHex = '#' + moonColor.getHexString();
let moonIntensity = 2;
const _setMoonLight = (value) => {
const args = value.args;
moonColorHex = '#' + moonColor.fromArray(args[0]).multiplyScalar(1 / 255).getHexString();
moonIntensity = args[1];
}
for (const component of app.components) {
switch (component.key) {
case 'skyLight': {
_setSkyLight(component.value)
break;
}
case 'ambientLight': {
_setAmbientLight(component.value)
break;
}
case 'hemisphereLight': {
_setHemisphereLight(component.value)
break;
}
case 'sunLight': {
_setSunLight(component.value)
break;
}
case 'moonLight': {
_setMoonLight(component.value)
break;
}
default: {
break;
}
}
}
// skyManager.initSkyLight();
const skyLight = skyManager.getSkyLight();
app.add(skyLight);
app.add(skyLight.target);
//############################################################## sun position ##############################################################
let azimuth = 0.4;
const inclination = 0.;
const sunPosition = new THREE.Vector3();
const skyLightPosition = new THREE.Vector3();
useFrame(() => {
// ?* moves the skybox app so that player never passes the skybox's walls
app.position.copy(localPlayer.position);
azimuth = (0.05 + (Date.now() / 5000) * 0.1 * skylightSpeed) % 1;
const theta = Math.PI * (inclination - 0.5);
const phi = 2 * Math.PI * (azimuth - 0.5);
sunPosition.set(
Math.cos(phi),
Math.sin(phi) * Math.sin(theta),
Math.sin(phi) * Math.cos(theta)
);
if (azimuth < 0.5) {
// sun
skyLightPosition.copy(sunPosition);
skyManager.setSkyLightColor(sunColorHex);
skyManager.setSkyLightIntensity(sunIntensity);
} else {
// moon
skyLightPosition.copy(sunPosition).multiplyScalar(-1);
skyManager.setSkyLightColor(moonColorHex);
skyManager.setSkyLightIntensity(moonIntensity);
}
skyManager.setSkyLightPosition(skyLightPosition);
});
//############################################################## sky ##############################################################
{
const skyBoxRadius = 10000;
const sunMoonRotationRadius = skyBoxRadius / 2;
const sky = new Sky();
app.add(sky);
sky.material.uniforms.skyBoxRadius.value = skyBoxRadius;
sky.material.uniforms.starTexture.value = starTexture;
sky.material.uniforms.noiseTexture.value = noiseTexture;
sky.material.uniforms.galaxyTexture.value = galaxyTexture;
sky.material.uniforms.noiseTexture2.value = noiseTexture2;
const moonGeometry = new THREE.PlaneGeometry( 500, 500 );
const moonMaterial = new THREE.MeshBasicMaterial( { map: moonTexture, blending: THREE.AdditiveBlending, depthWrite: false, transparent: true} );
const moon = new THREE.Mesh(moonGeometry, moonMaterial);
app.add( moon );
const sun = new THREE.PointLight(0xffffff, 100, 2000);
const lensflare = new Lensflare();
const mainFlare = new LensflareElement(textureFlare0, 500, 0, sun.color, 0.2);
lensflare.addElement(mainFlare);
lensflare.addElement(new LensflareElement(textureFlare3, 60, 0.6));
lensflare.addElement(new LensflareElement(textureFlare3, 70, 0.7));
lensflare.addElement(new LensflareElement(textureFlare3, 120, 0.9));
lensflare.addElement(new LensflareElement(textureFlare3, 70, 1));
sun.add( lensflare );
app.add( sun );
sun.visible = false;
useFrame(({timestamp}) => {
const player = useLocalPlayer();
// update sun
if (azimuth < 0.5) {
sun.visible = true;
sun.position.set(sunPosition.x, sunPosition.y, sunPosition.z).multiplyScalar(sunMoonRotationRadius);
mainFlare.rotation = camera.rotation.y;
}
else {
sun.visible = false;
}
// update moon
moon.position.set(sunPosition.x, sunPosition.y, sunPosition.z).multiplyScalar(-sunMoonRotationRadius);
moon.rotation.copy(camera.rotation);
// update sky
sky.material.uniforms.sunPosition.value.set(sunPosition.x, sunPosition.y, sunPosition.z)
.multiplyScalar(skyBoxRadius)
.add(player.position);
sky.material.uniforms.moonPosition.value.set(sunPosition.x, sunPosition.y, sunPosition.z)
.multiplyScalar(-skyBoxRadius)
.add(player.position);
sky.material.uniforms.uTime.value = timestamp / 1000;
});
}
//############################################################## cloud ##############################################################
{
const cloud = new Cloud();
app.add(cloud);
cloud.material.uniforms.noiseTexture2.value = noiseTexture2
cloud.material.uniforms.cloudRadius.value = cloud.cloudRadius;
cloud.material.uniforms.cloudTexture1.value = cloudTexture1;
cloud.material.uniforms.cloudTexture2.value = cloudTexture2;
cloud.material.uniforms.cloudTexture3.value = cloudTexture3;
cloud.material.uniforms.cloudTexture4.value = cloudTexture4;
useFrame(({timestamp}) => {
const player = useLocalPlayer();
cloud.material.uniforms.uTime.value = timestamp / 1000;
cloud.material.uniforms.sunPosition.value.set(sunPosition.x * cloud.cloudRadius, sunPosition.y * cloud.cloudRadius, sunPosition.z * cloud.cloudRadius)
.add(player.position);
app.updateMatrixWorld();
});
}
app.setComponent('renderPriority', 'high');
return app;
};