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
|
import { Mesh, BackSide, SphereGeometry, ShaderMaterial, Color } from "three";
const SKY_COLOR = 0x999999;
const GROUND_COLOR = 0x242424;
const SKY_SIZE = 40000;
export function addSkyGradient(scene) {
const vertexShader = `
varying vec3 vWorldPosition;
void main() {
vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
vWorldPosition = worldPosition.xyz;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`;
const fragmentShader = `
uniform vec3 topColor;
uniform vec3 bottomColor;
varying vec3 vWorldPosition;
void main() {
float h = (normalize(vWorldPosition).y ) / 2.0 + 0.25;
gl_FragColor = vec4( mix( bottomColor, topColor, h ), 1.0 );
}`;
const uniforms = {
topColor: { value: new Color(SKY_COLOR) },
bottomColor: { value: new Color(GROUND_COLOR) },
};
const skyGeo = new SphereGeometry(SKY_SIZE, 32, 15);
const skyMat = new ShaderMaterial({
uniforms,
vertexShader,
fragmentShader,
side: BackSide,
});
const sky = new Mesh(skyGeo, skyMat);
scene.add(sky);
}
|