From 46026ab9b9704bfd8f31a26bd1ee88873e6e4fe3 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 31 Mar 2020 15:07:23 +0300 Subject: [PATCH] Sky from atmosphere! Fix normalization --- assets/shaders/atmosphere.vs | 50 ++++++++++++++-------- src/engine/components/planet/atmosphere.js | 7 +-- src/engine/utility.js | 2 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/assets/shaders/atmosphere.vs b/assets/shaders/atmosphere.vs index 9276554..a2ced5d 100644 --- a/assets/shaders/atmosphere.vs +++ b/assets/shaders/atmosphere.vs @@ -30,35 +30,50 @@ uniform mat4 uModelMatrix; uniform mat4 uViewMatrix; uniform mat4 uProjectionMatrix; -float scale(float fCos) -{ +float scale(float fCos) { float x = 1.0 - fCos; return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); } -void main(void) -{ + +void main(void) { // 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 = aVertexPosition - v3CameraPosition; 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 B = 2.0 * dot(v3CameraPosition, v3Ray); - float C = fCameraHeight2 - fOuterRadius2; - float fDet = max(0.0, B*B - 4.0 * C); - float fNear = 0.5 * (-B - sqrt(fDet)); - // Calculate the ray's starting position, then calculate its scattering offset - vec3 v3Start = v3CameraPosition + v3Ray * fNear; - fFar -= fNear; - float fStartAngle = dot(v3Ray, v3Start) / fOuterRadius; - float fStartDepth = exp(-1.0 / fScaleDepth); - float fStartOffset = fStartDepth * scale(fStartAngle); - //c0 = vec3(1.0, 0, 0) * fStartAngle; + + vec3 v3Start; + float fStartAngle; + float fStartDepth; + float fStartOffset; + + if (fCameraHeight > fOuterRadius) { + // 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 B = 2.0 * dot(v3CameraPosition, v3Ray); + float C = fCameraHeight2 - fOuterRadius2; + float fDet = max(0.0, B*B - 4.0 * C); + float fNear = 0.5 * (-B - sqrt(fDet)); + + // Calculate the ray's starting position, then calculate its scattering offset + v3Start = v3CameraPosition + v3Ray * fNear; + fFar -= fNear; + fStartAngle = dot(v3Ray, v3Start) / fOuterRadius; + fStartDepth = exp(-1.0 / fScaleDepth); + fStartOffset = fStartDepth * scale(fStartAngle); + } else { + // Sky from within the atmosphere + v3Start = v3CameraPosition; + fStartDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight)); + fStartAngle = dot(v3Ray, v3Start) / length(v3Start); + fStartOffset = fStartDepth*scale(fStartAngle); + } + // Initialize the scattering loop variables float fSampleLength = fFar / fSamples; float fScaledLength = fSampleLength * fScale; vec3 v3SampleRay = v3Ray * fSampleLength; vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5; - //gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0); + // Now loop through the sample rays vec3 v3FrontColor = vec3(0.0, 0.0, 0.0); for(int i=0; i