Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     sky.vert nachocpol@gmail.com
  3. */
  4. #version 430 core
  5.  
  6. layout (location = 0)in vec3 aPosition;
  7. layout(std140)uniform uPass
  8. {
  9.     mat4 uView;
  10.     mat4 uProjection;
  11.     vec3 uCampos;
  12.     float uTime;
  13.     float uCamnear;
  14.     float uCamfar;
  15. };
  16.  
  17. uniform mat4 uModel;
  18.  
  19. out vec3 v3Direction;
  20. out vec3 c0;
  21. out vec3 c1;
  22. out vec3 iSunDir;
  23. float scale(float fCos)
  24. {
  25.     float fScaleDepth = 0.25f;      // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
  26.     float x = 1.0 - fCos;
  27.     return fScaleDepth * exp(-0.00287f + x*(0.459f + x*(3.83f + x*(-6.80f + x*5.25f))));
  28. }
  29.  
  30. void main()
  31. {
  32.     float Kr = 0.0025f;  // Rayleigh scattering constant
  33.     float ESun = 20.0f; // Sun brightness constant
  34.     float Km = 0.0010f;     // Mie scattering constant
  35.     vec3 wavelength = vec3(0.650f,0.570f,0.475f);
  36.     vec3 pow4WaveLength = pow(wavelength,vec3(4.0f));
  37.  
  38.     const float PI = 3.141517f;
  39.     const int nSamples = 4;
  40.     const float fSamples = 4.0;
  41.  
  42.     vec3  v3CameraPos = vec3(0.0f,0.956f,0.0f);     // The camera's current position
  43.     //float sunY = sin(uTime * 0.5f);
  44.     vec3  v3LightPos = vec3(0.0f,0.0f,1.0f);        // The direction vector to the light source
  45.     iSunDir = v3LightPos;
  46.     vec3  v3InvWavelength = 1.0f / pow4WaveLength;  // 1 / pow(wavelength, 4) for the red, green, and blue channels
  47.     float fCameraHeight = v3CameraPos.y;    // The camera's current height
  48.     float fCameraHeight2 = pow(fCameraHeight,2.0f); // fCameraHeight^2
  49.     float fOuterRadius = 1.0f;      // The outer (atmosphere) radius
  50.     float fOuterRadius2 = pow(fOuterRadius,2.0f);   // fOuterRadius^2
  51.     float fInnerRadius = 0.95f;     // The inner (planetary) radius
  52.     float fInnerRadius2 = pow(fInnerRadius,2.0f);   // fInnerRadius^2
  53.     float fKrESun = Kr * ESun;          // Kr * ESun
  54.     float fKmESun = Km * ESun;          // Km * ESun
  55.     float fKr4PI = Kr * 4.0f * PI;          // Kr * 4 * PI
  56.     float fKm4PI = Km * 4.0f * PI;          // Km * 4 * PI
  57.     float fScale = 1.0f / (fOuterRadius - fInnerRadius);            // 1 / (fOuterRadius - fInnerRadius)
  58.     float fScaleDepth = 0.1f;       // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
  59.     float fScaleOverScaleDepth = fScale/fScaleDepth;    // fScale / fScaleDepth
  60.  
  61.     // Get the ray from the camera to the vertex, and its length (which is the far point of the ray passing through the atmosphere)
  62.     vec3 v3Pos = (uModel * vec4(aPosition,0.0f)).xyz;
  63.     vec3 v3Ray = v3Pos - v3CameraPos;
  64.     float fFar = length(v3Ray);
  65.     v3Ray /= fFar;
  66.  
  67.     // Calculate the ray's starting position, then calculate its scattering offset
  68.     vec3 v3Start = v3CameraPos;
  69.     float fHeight = length(v3Start);
  70.     float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight));
  71.     float fStartAngle = dot(v3Ray, v3Start) / fHeight;
  72.     float fStartOffset = fDepth*scale(fStartAngle);
  73.  
  74.     // Initialize the scattering loop variables
  75.     //gl_FrontColor = vec4(0.0, 0.0, 0.0, 0.0);
  76.     float fSampleLength = fFar / fSamples;
  77.     float fScaledLength = fSampleLength * fScale;
  78.     vec3 v3SampleRay = v3Ray * fSampleLength;
  79.     vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
  80.  
  81.     // Now loop through the sample rays
  82.     vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
  83.     for(int i=0; i<nSamples; i++)
  84.     {
  85.         float fHeight = length(v3SamplePoint);
  86.         float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
  87.         float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
  88.         float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
  89.         float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));
  90.         vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
  91.         v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
  92.         v3SamplePoint += v3SampleRay;
  93.     }
  94.  
  95.     // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
  96.     c1 = v3FrontColor * fKmESun;
  97.     c0 = v3FrontColor * (v3InvWavelength * fKrESun);
  98.     v3Direction = v3CameraPos - v3Pos;
  99.  
  100.     mat3 v = mat3(uView);
  101.     vec3 displacedPos = aPosition;
  102.     displacedPos.y -= 0.2f;
  103.     gl_Position = uProjection * mat4(v)  *  vec4(displacedPos,1.0f);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement