Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.0 KiB

4 years ago
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  3. Shader "Atmosphere/SkyFromSpace"
  4. {
  5. SubShader
  6. {
  7. Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
  8. //Tags { "RenderType"="Opaque" }
  9. Pass
  10. {
  11. Blend SrcAlpha OneMinusSrcAlpha
  12. ZWrite Off
  13. Cull Front
  14. //Blend One One
  15. CGPROGRAM
  16. #include "UnityCG.cginc"
  17. #pragma target 3.0
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. uniform float3 v3Translate; // The objects world pos
  21. uniform float3 v3LightPos; // The direction vector to the light source
  22. uniform float3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
  23. uniform float fOuterRadius; // The outer (atmosphere) radius
  24. uniform float fOuterRadius2; // fOuterRadius^2
  25. uniform float fInnerRadius; // The inner (planetary) radius
  26. uniform float fInnerRadius2; // fInnerRadius^2
  27. uniform float fKrESun; // Kr * ESun
  28. uniform float fKmESun; // Km * ESun
  29. uniform float fKr4PI; // Kr * 4 * PI
  30. uniform float fKm4PI; // Km * 4 * PI
  31. uniform float fScale; // 1 / (fOuterRadius - fInnerRadius)
  32. uniform float fScaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
  33. uniform float fScaleOverScaleDepth; // fScale / fScaleDepth
  34. uniform float fHdrExposure; // HDR exposure
  35. uniform float g; // The Mie phase asymmetry factor
  36. uniform float g2; // The Mie phase asymmetry factor squared
  37. struct v2f
  38. {
  39. float4 pos : SV_POSITION;
  40. float2 uv : TEXCOORD0;
  41. float3 t0 : TEXCOORD1;
  42. float3 c0 : COLOR0;
  43. float3 c1 : COLOR1;
  44. };
  45. float scale(float fCos)
  46. {
  47. float x = 1.0 - fCos;
  48. return 0.25 * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
  49. }
  50. v2f vert(appdata_base v)
  51. {
  52. float3 v3CameraPos = _WorldSpaceCameraPos - v3Translate; // The camera's current position
  53. float fCameraHeight = length(v3CameraPos); // The camera's current height
  54. float fCameraHeight2 = fCameraHeight*fCameraHeight; // fCameraHeight^2
  55. // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
  56. float3 v3Pos = mul(unity_ObjectToWorld, v.vertex).xyz - v3Translate;
  57. float3 v3Ray = v3Pos - v3CameraPos;
  58. float fFar = length(v3Ray);
  59. v3Ray /= fFar;
  60. // Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
  61. float B = 2.0 * dot(v3CameraPos, v3Ray);
  62. float C = fCameraHeight2 - fOuterRadius2;
  63. float fDet = max(0.0, B*B - 4.0 * C);
  64. float fNear = 0.5 * (-B - sqrt(fDet));
  65. // Calculate the ray's start and end positions in the atmosphere, then calculate its scattering offset
  66. float3 v3Start = v3CameraPos + v3Ray * fNear;
  67. fFar -= fNear;
  68. float fStartAngle = dot(v3Ray, v3Start) / fOuterRadius;
  69. float fStartDepth = exp(-1.0/fScaleDepth);
  70. float fStartOffset = fStartDepth*scale(fStartAngle);
  71. const float fSamples = 2.0;
  72. // Initialize the scattering loop variables
  73. float fSampleLength = fFar / fSamples;
  74. float fScaledLength = fSampleLength * fScale;
  75. float3 v3SampleRay = v3Ray * fSampleLength;
  76. float3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
  77. // Now loop through the sample rays
  78. float3 v3FrontColor = float3(0.0, 0.0, 0.0);
  79. for(int i=0; i<int(fSamples); i++)
  80. {
  81. float fHeight = length(v3SamplePoint);
  82. float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
  83. float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
  84. float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
  85. float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));
  86. float3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
  87. v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
  88. v3SamplePoint += v3SampleRay;
  89. }
  90. v2f OUT;
  91. OUT.pos = UnityObjectToClipPos(v.vertex);
  92. OUT.uv = v.texcoord.xy;
  93. // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
  94. OUT.c0 = v3FrontColor * (v3InvWavelength * fKrESun);
  95. OUT.c1 = v3FrontColor * fKmESun;
  96. OUT.t0 = v3CameraPos - v3Pos;
  97. return OUT;
  98. }
  99. // Calculates the Mie phase function
  100. float getMiePhase(float fCos, float fCos2, float g, float g2)
  101. {
  102. return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
  103. }
  104. // Calculates the Rayleigh phase function
  105. float getRayleighPhase(float fCos2)
  106. {
  107. return 0.75 + 0.75*fCos2;
  108. }
  109. half4 frag(v2f IN) : COLOR
  110. {
  111. float fCos = dot(v3LightPos, IN.t0) / length(IN.t0);
  112. float fCos2 = fCos*fCos;
  113. float3 col = getRayleighPhase(fCos2) * IN.c0 + getMiePhase(fCos, fCos2, g, g2) * IN.c1;
  114. col = 1.0 - exp(col * -fHdrExposure);
  115. return float4(col,col.b);
  116. }
  117. ENDCG
  118. }
  119. }
  120. }