Advertisement
Guest User

Skybox.shader

a guest
Jun 9th, 2019
4,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/SkyboxProc"
  2. {
  3.     Properties
  4.     {
  5.          [Header(Stars Settings)]
  6.         _Stars("Stars Texture", 2D) = "black" {}
  7.         _StarsCutoff("Stars Cutoff",  Range(0, 1)) = 0.08
  8.         _StarsSpeed("Stars Move Speed",  Range(0, 1)) = 0.3
  9.         _StarsSkyColor("Stars Sky Color", Color) = (0.0,0.2,0.1,1)
  10.  
  11.  
  12.          [Header(Horizon Settings)]
  13.         _OffsetHorizon("Horizon Offset",  Range(-1, 1)) = 0
  14.         _HorizonIntensity("Horizon Intensity",  Range(0, 10)) = 3.3
  15.         _SunSet("Sunset/Rise Color", Color) = (1,0.8,1,1)
  16.         _HorizonColorDay("Day Horizon Color", Color) = (0,0.8,1,1)
  17.         _HorizonColorNight("Night Horizon Color", Color) = (0,0.8,1,1)
  18.  
  19.          [Header(Sun Settings)]
  20.          _SunColor("Sun Color", Color) = (1,1,1,1)
  21.         _SunRadius("Sun Radius",  Range(0, 2)) = 0.1
  22.  
  23.         [Header(Moon Settings)]
  24.         _MoonColor("Moon Color", Color) = (1,1,1,1)
  25.         _MoonRadius("Moon Radius",  Range(0, 2)) = 0.15
  26.         _MoonOffset("Moon Crescent",  Range(-1, 1)) = -0.1
  27.  
  28.         [Header(Day Sky Settings)]
  29.         _DayTopColor("Day Sky Color Top", Color) = (0.4,1,1,1)
  30.         _DayBottomColor("Day Sky Color Bottom", Color) = (0,0.8,1,1)
  31.  
  32.         [Header(Main Cloud Settings)]
  33.         _BaseNoise("Base Noise", 2D) = "black" {}
  34.         _Distort("Distort", 2D) = "black" {}
  35.         _SecNoise("Secondary Noise", 2D) = "black" {}
  36.         _BaseNoiseScale("Base Noise Scale",  Range(0, 1)) = 0.2
  37.         _DistortScale("Distort Noise Scale",  Range(0, 1)) = 0.06
  38.         _SecNoiseScale("Secondary Noise Scale",  Range(0, 1)) = 0.05
  39.         _Distortion("Extra Distortion",  Range(0, 1)) = 0.1
  40.         _Speed("Movement Speed",  Range(0, 10)) = 1.4
  41.         _CloudCutoff("Cloud Cutoff",  Range(0, 1)) = 0.3
  42.         _Fuzziness("Cloud Fuzziness",  Range(0, 1)) = 0.04
  43.         _FuzzinessUnder("Cloud Fuzziness Under",  Range(0, 1)) = 0.01
  44.         [Toggle(FUZZY)] _FUZZY("Extra Fuzzy clouds", Float) = 1
  45.  
  46.         [Header(Day Clouds Settings)]
  47.         _CloudColorDayEdge("Clouds Edge Day", Color) = (1,1,1,1)
  48.         _CloudColorDayMain("Clouds Main Day", Color) = (0.8,0.9,0.8,1)
  49.         _CloudColorDayUnder("Clouds Under Day", Color) = (0.6,0.7,0.6,1)
  50.         _Brightness("Cloud Brightness",  Range(1, 10)) = 2.5
  51.         [Header(Night Sky Settings)]
  52.         _NightTopColor("Night Sky Color Top", Color) = (0,0,0,1)
  53.         _NightBottomColor("Night Sky Color Bottom", Color) = (0,0,0.2,1)
  54.  
  55.         [Header(Night Clouds Settings)]
  56.         _CloudColorNightEdge("Clouds Edge Night", Color) = (0,1,1,1)
  57.         _CloudColorNightMain("Clouds Main Night", Color) = (0,0.2,0.8,1)
  58.         _CloudColorNightUnder("Clouds Under Night", Color) = (0,0.2,0.6,1)
  59.     }
  60.         SubShader
  61.         {
  62.             Tags { "RenderType" = "Opaque" }
  63.             LOD 100
  64.  
  65.             Pass
  66.             {
  67.                 CGPROGRAM
  68.                 #pragma vertex vert
  69.                 #pragma fragment frag
  70.                 // make fog work
  71.                 #pragma multi_compile_fog
  72.                 #pragma shader_feature FUZZY
  73.                 #include "UnityCG.cginc"
  74.  
  75.                 struct appdata
  76.                 {
  77.                     float4 vertex : POSITION;
  78.                     float3 uv : TEXCOORD0;
  79.                 };
  80.  
  81.                 struct v2f
  82.                 {
  83.                     float3 uv : TEXCOORD0;
  84.                     UNITY_FOG_COORDS(1)
  85.                     float4 vertex : SV_POSITION;
  86.                     float3 worldPos : TEXCOORD1;
  87.                 };
  88.  
  89.                 sampler2D _Stars, _BaseNoise, _Distort, _SecNoise;
  90.  
  91.                 float _SunRadius, _MoonRadius, _MoonOffset, _OffsetHorizon;
  92.                 float4 _SunColor, _MoonColor;
  93.                 float4 _DayTopColor, _DayBottomColor, _NightBottomColor, _NightTopColor;
  94.                 float4 _HorizonColorDay, _HorizonColorNight, _SunSet;
  95.                 float _StarsCutoff, _StarsSpeed, _HorizonIntensity;
  96.                 float _BaseNoiseScale, _DistortScale, _SecNoiseScale, _Distortion;
  97.                 float _Speed, _CloudCutoff, _Fuzziness, _FuzzinessUnder, _Brightness;
  98.                 float4 _CloudColorDayEdge, _CloudColorDayMain, _CloudColorDayUnder;
  99.                 float4 _CloudColorNightEdge, _CloudColorNightMain, _CloudColorNightUnder, _StarsSkyColor;
  100.  
  101.                 v2f vert(appdata v)
  102.                 {
  103.                     v2f o;
  104.                     o.vertex = UnityObjectToClipPos(v.vertex);
  105.                     o.uv = v.uv;
  106.                     o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  107.                     UNITY_TRANSFER_FOG(o,o.vertex);
  108.                     return o;
  109.                 }
  110.  
  111.                 fixed4 frag(v2f i) : SV_Target
  112.                 {
  113.  
  114.                     float horizon = abs((i.uv.y * _HorizonIntensity) - _OffsetHorizon);
  115.  
  116.                 // uv for the sky
  117.                 float2 skyUV = i.worldPos.xz / i.worldPos.y;
  118.  
  119.                 // moving clouds
  120.                 float baseNoise = tex2D(_BaseNoise, (skyUV - _Time.x) * _BaseNoiseScale).x;
  121.  
  122.                 float noise1 = tex2D(_Distort, ((skyUV + baseNoise) - (_Time.x * _Speed)) * _DistortScale);
  123.  
  124.                 float noise2 = tex2D(_SecNoise, ((skyUV + (noise1 * _Distortion)) - (_Time.x * (_Speed * 0.5))) * _SecNoiseScale);
  125.  
  126.                 float finalNoise = saturate(noise1 * noise2) * 3 * saturate(i.worldPos.y);
  127.  
  128.  
  129. #if FUZZY
  130.                 float clouds = saturate(smoothstep(_CloudCutoff * baseNoise, _CloudCutoff * baseNoise + _Fuzziness, finalNoise));
  131.                 float cloudsunder = saturate(smoothstep(_CloudCutoff* baseNoise, _CloudCutoff * baseNoise + _FuzzinessUnder + _Fuzziness, noise2) * clouds);
  132.  
  133. #else
  134.                 float clouds = saturate(smoothstep(_CloudCutoff, _CloudCutoff + _Fuzziness, finalNoise));
  135.                 float cloudsunder = saturate(smoothstep(_CloudCutoff, _CloudCutoff + _Fuzziness + _FuzzinessUnder , noise2) * clouds);
  136.  
  137.  
  138. #endif
  139.                 float3 cloudsColored = lerp(_CloudColorDayEdge, lerp(_CloudColorDayUnder, _CloudColorDayMain, cloudsunder), clouds) * clouds;
  140.                 float3 cloudsColoredNight = lerp(_CloudColorNightEdge, lerp(_CloudColorNightUnder,_CloudColorNightMain , cloudsunder), clouds) * clouds;
  141.                 cloudsColoredNight *= horizon;
  142.  
  143.  
  144.                 cloudsColored = lerp(cloudsColoredNight, cloudsColored, saturate(_WorldSpaceLightPos0.y)); // lerp the night and day clouds over the light direction
  145.                 cloudsColored += (_Brightness * cloudsColored * horizon); // add some extra brightness
  146.  
  147.  
  148.                 float cloudsNegative = (1 - clouds) * horizon;
  149.                 // sun
  150.                 float sun = distance(i.uv.xyz, _WorldSpaceLightPos0);
  151.                 float sunDisc = 1 - (sun / _SunRadius);
  152.                 sunDisc = saturate(sunDisc * 50);
  153.  
  154.                 // (crescent) moon
  155.                 float moon = distance(i.uv.xyz, -_WorldSpaceLightPos0);
  156.                 float crescentMoon = distance(float3(i.uv.x + _MoonOffset, i.uv.yz), -_WorldSpaceLightPos0);
  157.                 float crescentMoonDisc = 1 - (crescentMoon / _MoonRadius);
  158.                 crescentMoonDisc = saturate(crescentMoonDisc * 50);
  159.                 float moonDisc = 1 - (moon / _MoonRadius);
  160.                 moonDisc = saturate(moonDisc * 50);
  161.                 moonDisc = saturate(moonDisc - crescentMoonDisc);
  162.  
  163.                 float3 sunAndMoon = (sunDisc * _SunColor) + (moonDisc * _MoonColor);
  164.                 sunAndMoon *= cloudsNegative;
  165.  
  166.                 //stars
  167.                 float3 stars = tex2D(_Stars, skyUV + (_StarsSpeed * _Time.x));
  168.                 stars *= saturate(-_WorldSpaceLightPos0.y);
  169.                 stars = step(_StarsCutoff, stars);
  170.                 stars += (baseNoise * _StarsSkyColor);
  171.                 stars *= cloudsNegative;
  172.  
  173.                 // gradient day sky
  174.                 float3 gradientDay = lerp(_DayBottomColor, _DayTopColor, saturate(horizon));
  175.  
  176.                 // gradient night sky
  177.                 float3 gradientNight = lerp(_NightBottomColor, _NightTopColor, saturate(horizon));
  178.  
  179.                 float3 skyGradients = lerp(gradientNight, gradientDay, saturate(_WorldSpaceLightPos0.y)) * cloudsNegative;
  180.  
  181.                 // horizon glow / sunset/rise
  182.                 float sunset = saturate((1 - horizon) * saturate(_WorldSpaceLightPos0.y * 5));
  183.  
  184.  
  185.                 float3 sunsetColoured = sunset * _SunSet;
  186.  
  187.                 float3 horizonGlow = saturate((1 - horizon * 5) * saturate(_WorldSpaceLightPos0.y * 10)) * _HorizonColorDay;//
  188.                 float3 horizonGlowNight = saturate((1 - horizon * 5) * saturate(-_WorldSpaceLightPos0.y * 10)) * _HorizonColorNight;//
  189.                 horizonGlow += horizonGlowNight;
  190.  
  191.                 // apply fog
  192.  
  193.                float3 combined = skyGradients + sunAndMoon + sunsetColoured + stars + cloudsColored + horizonGlow;
  194.                UNITY_APPLY_FOG(i.fogCoord, combined);
  195.                return float4(combined,1);
  196.  
  197.            }
  198.            ENDCG
  199.        }
  200.         }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement