Giter Site home page Giter Site logo

glsl-atmosphere's Introduction

glsl-atmosphere

Renders sky colors with Rayleigh and Mie scattering.

Demo

Install

npm install glsl-atmosphere

Example

Javascript

var quad = Geometry(gl)
    .attr('aPosition', [
        -1, -1, -1,
         1, -1, -1,
         1,  1, -1,
        -1, -1, -1,
         1,  1, -1,
        -1,  1, -1
    ]);

program.bind();
quad.bind(program);
program.uniforms.uSunPos = [0, 0.1, -1];
quad.draw();

Vertex Shader

attribute vec3 aPosition;

varying vec3 vPosition;

void main() {
    gl_Position = vec4(aPosition, 1.0);
    vPosition = aPosition;
}

Fragment Shader

varying vec3 vPosition;

uniform vec3 uSunPos;

#pragma glslify: atmosphere = require(glsl-atmosphere)

void main() {
    vec3 color = atmosphere(
        normalize(vPosition),           // normalized ray direction
        vec3(0,6372e3,0),               // ray origin
        uSunPos,                        // position of the sun
        22.0,                           // intensity of the sun
        6371e3,                         // radius of the planet in meters
        6471e3,                         // radius of the atmosphere in meters
        vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient
        21e-6,                          // Mie scattering coefficient
        8e3,                            // Rayleigh scale height
        1.2e3,                          // Mie scale height
        0.758                           // Mie preferred scattering direction
    );

    // Apply exposure.
    color = 1.0 - exp(-1.0 * color);

    gl_FragColor = vec4(color, 1);
}

API

#pragma glslify: atmosphere = require(glsl-atmosphere)

vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g)

Returns a vec3 representing the color of the sky along a view direction.

Takes:

  • vec3 r normalized ray direction, typically a ray cast from the observers eye through a pixel
  • vec3 r0 ray origin in meters, typically the position of the viewer's eye
  • vec3 pSun the position of the sun
  • float iSun intensity of the sun
  • float rPlanet radius of the planet in meters
  • float rAtoms radius of the atmosphere in meters
  • vec3 kRlh Rayleigh scattering coefficient
  • vec3 kMie Mie scattering coefficient
  • float shRlh Rayleigh scale height in meters
  • float shMie Mie scale height in meters
  • float g Mie preferred scattering direction

For an Earth-like atmosphere, see the settings in the example above.

Credits

glsl-atmosphere's People

Contributors

miyu avatar wwwtyro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.