199 lines
6.9 KiB
JavaScript
199 lines
6.9 KiB
JavaScript
|
|
const ATMOSPHERE = {
|
|
vertexUniforms: `
|
|
#define M_PI 3.1415926535897932384626433832795
|
|
uniform vec3 planetPosition; // Position of the planet
|
|
uniform vec3 lightDirection; // The direction vector to the light source
|
|
uniform vec3 invWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
|
|
uniform float outerRadius; // The outer (atmosphere) radius
|
|
uniform float innerRadius; // The inner (planetary) radius
|
|
uniform float ESun; // ESun
|
|
uniform float Km; // Km
|
|
uniform float Kr; // Kr
|
|
uniform float scale; // 1 / (outerRadius - innerRadius)
|
|
uniform float scaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
|
|
|
|
const int nSamples = 2;
|
|
const float fSamples = 1.0;
|
|
|
|
varying vec3 v3Direction;
|
|
varying vec3 c0;
|
|
varying vec3 c1;
|
|
`,
|
|
vertexFunctions: `
|
|
float dscale(float fCos) {
|
|
float x = 1.0 - fCos;
|
|
return scaleDepth * exp(-0.00287 + x * (0.459 + x * (3.83 + x * (-6.80 + x * 5.25))));
|
|
}
|
|
|
|
float calculateNearScatter(vec3 v3CameraPosition, vec3 v3Ray, float fCameraHeight, float fOuterRadius) {
|
|
float B = 2.0 * dot(v3CameraPosition, v3Ray);
|
|
float C = pow(fCameraHeight, 2.0) - pow(fOuterRadius, 2.0);
|
|
float fDet = max(0.0, B * B - 4.0 * C);
|
|
return 0.5 * (-B - sqrt(fDet));
|
|
}
|
|
`,
|
|
vertexAtmosphere: `
|
|
void main(void) {
|
|
// Initialize variables
|
|
float cameraHeight = length(planetPosition - cameraPosition);
|
|
float KmESun = Km * ESun;
|
|
float KrESun = Kr * ESun;
|
|
float Kr4PI = Kr * 4.0 * M_PI;
|
|
float Km4PI = Km * 4.0 * M_PI;
|
|
float scaleOverScaleDepth = scale / scaleDepth;
|
|
|
|
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
|
|
vec3 v3Ray = position - cameraPosition;
|
|
float fFar = length(v3Ray);
|
|
v3Ray /= fFar;
|
|
|
|
vec3 v3Start;
|
|
float fStartAngle;
|
|
float fStartDepth;
|
|
float fStartOffset;
|
|
|
|
if (cameraHeight > outerRadius) {
|
|
// Sky from space
|
|
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
|
|
float fNear = calculateNearScatter(cameraPosition, v3Ray, cameraHeight, outerRadius);
|
|
|
|
// Calculate the ray's starting position, then calculate its scattering offset
|
|
v3Start = cameraPosition + v3Ray * fNear;
|
|
fFar -= fNear;
|
|
fStartAngle = dot(v3Ray, v3Start) / outerRadius;
|
|
fStartDepth = exp(-1.0 / scaleDepth);
|
|
fStartOffset = fStartDepth * dscale(fStartAngle);
|
|
} else {
|
|
// Sky from within the atmosphere
|
|
v3Start = cameraPosition;
|
|
fStartDepth = exp(scaleOverScaleDepth * (innerRadius - cameraHeight));
|
|
fStartAngle = dot(v3Ray, v3Start) / length(v3Start);
|
|
fStartOffset = fStartDepth * dscale(fStartAngle);
|
|
}
|
|
|
|
// Initialize the scattering loop variables
|
|
float fSampleLength = fFar / fSamples;
|
|
float scaledLength = fSampleLength * scale;
|
|
vec3 v3SampleRay = v3Ray * fSampleLength;
|
|
vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
|
|
|
|
// Now loop through the sample rays
|
|
vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
|
|
for(int i=0; i<nSamples; i++)
|
|
{
|
|
float fHeight = length(v3SamplePoint);
|
|
float fDepth = exp(scaleOverScaleDepth * (innerRadius - fHeight));
|
|
float fLightAngle = dot(lightDirection, v3SamplePoint) / fHeight;
|
|
float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
|
|
float fScatter = (fStartOffset + fDepth * (dscale(fLightAngle) - dscale(fCameraAngle)));
|
|
vec3 v3Attenuate = exp(-fScatter * (invWavelength * Kr4PI + Km4PI));
|
|
v3FrontColor += v3Attenuate * (fDepth * scaledLength);
|
|
v3SamplePoint += v3SampleRay;
|
|
}
|
|
|
|
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
|
|
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1);
|
|
c0 = v3FrontColor * (invWavelength * KrESun);
|
|
c1 = v3FrontColor * KmESun;
|
|
v3Direction = cameraPosition - position;
|
|
}
|
|
`,
|
|
vertexGround: `
|
|
void main(void) {
|
|
// Initialize variables
|
|
float cameraHeight = length(planetPosition - cameraPosition);
|
|
float KmESun = Km * ESun;
|
|
float KrESun = Kr * ESun;
|
|
float Kr4PI = Kr * 4.0 * M_PI;
|
|
float Km4PI = Km * 4.0 * M_PI;
|
|
float scaleOverScaleDepth = scale / scaleDepth;
|
|
|
|
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
|
|
vec3 v3Ray = position - cameraPosition;
|
|
float fFar = length(v3Ray);
|
|
v3Ray /= fFar;
|
|
|
|
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
|
|
float fNear = calculateNearScatter(cameraPosition, v3Ray, cameraHeight, outerRadius);
|
|
|
|
// Calculate the ray's starting position, then calculate its scattering offset
|
|
vec3 v3Start = cameraPosition + v3Ray * fNear;
|
|
fFar -= fNear;
|
|
float fDepth = exp((innerRadius - outerRadius) / scaleDepth);
|
|
float fCameraAngle = dot(-v3Ray, position) / length(position);
|
|
float fLightAngle = dot(lightDirection, position) / length(position);
|
|
|
|
float fCameraScale = dscale(fCameraAngle);
|
|
float fLightScale = dscale(fLightAngle);
|
|
float fCameraOffset = fDepth*fCameraScale;
|
|
float fTemp = (fLightScale + fCameraScale);
|
|
|
|
// Initialize the scattering loop variables
|
|
float fSampleLength = fFar / fSamples;
|
|
float fScaledLength = fSampleLength * scale;
|
|
vec3 v3SampleRay = v3Ray * fSampleLength;
|
|
vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
|
|
|
|
// Now loop through the sample rays
|
|
vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
|
|
vec3 v3Attenuate;
|
|
for(int i=0; i<nSamples; i++)
|
|
{
|
|
float fHeight = length(v3SamplePoint);
|
|
float fDepth = exp(scaleOverScaleDepth * (innerRadius - fHeight));
|
|
float fScatter = fDepth*fTemp - fCameraOffset;
|
|
v3Attenuate = exp(-fScatter * (invWavelength * Kr4PI + Km4PI));
|
|
v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
|
|
v3SamplePoint += v3SampleRay;
|
|
}
|
|
|
|
// Calculate the attenuation factor for the ground
|
|
c0 = v3Attenuate;
|
|
c1 = v3FrontColor * (invWavelength * KrESun + KmESun);
|
|
|
|
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position,1);
|
|
}
|
|
`,
|
|
fragmentAtmosphere: `
|
|
uniform vec3 lightDirection;
|
|
uniform float g;
|
|
|
|
varying vec3 v3Direction;
|
|
varying vec3 c0;
|
|
varying vec3 c1;
|
|
|
|
// Calculates the Mie phase function
|
|
float getMiePhase(float fCos, float fCos2, float g, float g2){
|
|
return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0 * g * fCos, 1.5);
|
|
}
|
|
|
|
// Calculates the Rayleigh phase function
|
|
float getRayleighPhase(float fCos2) {
|
|
// return 0.75 + 0.75 * fCos2;
|
|
return 0.75 * (2.0 + 0.5 * fCos2);
|
|
}
|
|
|
|
void main (void) {
|
|
float fCos = dot(lightDirection, v3Direction) / length(v3Direction);
|
|
float fCos2 = fCos * fCos;
|
|
vec3 color = getRayleighPhase(fCos2) * c0 +
|
|
getMiePhase(fCos, fCos2, g, pow(g, 2.0)) * c1;
|
|
gl_FragColor = vec4(color, 1.0);
|
|
gl_FragColor.a = gl_FragColor.b;
|
|
}
|
|
`,
|
|
fragmentGround: `
|
|
uniform vec3 color;
|
|
|
|
varying vec3 c0;
|
|
varying vec3 c1;
|
|
|
|
void main (void) {
|
|
gl_FragColor = vec4(c1, 1.0) + vec4(color * c0, 1.0);
|
|
}
|
|
`
|
|
}
|
|
|
|
module.exports = ATMOSPHERE
|