diff options
Diffstat (limited to 'src/sky.js')
| -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); +} |
