diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-09-21 15:26:49 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-09-21 15:26:49 +0200 |
| commit | fd4b35eba2656f16be13219867e94ca30f96a699 (patch) | |
| tree | 078f4a94170f96e024927e944c937d59d6c1b136 /src | |
| parent | 48374c440e344745ecbf96a3e2803fdc2bf35626 (diff) | |
hover
Diffstat (limited to 'src')
| -rw-r--r-- | src/sky.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/sky.js b/src/sky.js new file mode 100644 index 0000000..9f74aea --- /dev/null +++ b/src/sky.js @@ -0,0 +1,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); +} |
