// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' Shader "TENKOKU/Tenkoku_Sky_Legacy" { Properties { _SunSize ("Sun size", Range(0,1)) = 0.04 _AtmosphereThickness ("Atmoshpere Thickness", Range(0,5)) = 1.0 _SkyTint ("Sky Tint", Color) = (.5, .5, .5, 1) _GroundColor ("Ground", Color) = (.369, .349, .341, 1) _MieColor ("Mie Color", Color) = (1,1,1,1) _NightColor ("Night Color", Color) = (1,1,1,1) _MoonColor ("Moon Mie Color", Color) = (1,1,1,1) _Exposure("Exposure", Range(0, 8)) = 1.3 } SubShader { Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" } Cull Off ZWrite Off Pass { CGPROGRAM #pragma target 3.0 #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" #pragma multi_compile __ UNITY_COLORSPACE_GAMMA uniform half _Exposure; uniform half4 _GroundColor; uniform half _SunSize; uniform half3 _SkyTint; uniform half3 _MieColor; uniform half3 _MoonColor; uniform half3 _NightColor; uniform half _AtmosphereThickness; float _Tenkoku_SkyBright; float _Tenkoku_NightBright; float4 _TenkokuAmbientColor; float4 _TenkokuSunColor; float _Tenkoku_SunSize; float4 tenkoku_globalTintColor; float4 tenkoku_globalSkyColor; float _Tenkoku_AtmosphereDensity; float _Tenkoku_HorizonDensity; float _Tenkoku_HorizonHeight; float4 _Tenkoku_overcastColor; float _Tenkoku_overcastAmt; float _TenkokuExposureFac; float _TenkokuColorFac; float4 _TenkokuSkyColor; float _Tenkoku_Ambient; float _Tenkoku_AmbientGI; float4 _Tenkoku_SkyHorizonColor; sampler2D _Tenkoku_SkyTex; sampler2D _GTex; #if defined(UNITY_COLORSPACE_GAMMA) #define GAMMA 2 #define COLOR_2_GAMMA(color) color #define COLOR_2_LINEAR(color) color*color #define LINEAR_2_OUTPUT(color) sqrt(color) #else #define GAMMA 2.2 // HACK: to get gfx-tests in Gamma mode to agree until UNITY_ACTIVE_COLORSPACE_IS_GAMMA is working properly #define COLOR_2_GAMMA(color) ((unity_ColorSpaceDouble.r>2.0) ? pow(color,1.0/GAMMA) : color) #define COLOR_2_LINEAR(color) color #define LINEAR_2_LINEAR(color) color #endif // RGB wavelengths static const float3 kDefaultScatteringWavelength = float3(.65, .57, .475); static const float3 kVariableRangeForScatteringWavelength = float3(.15, .15, .15); #define OUTER_RADIUS 1.025 static const float kOuterRadius = OUTER_RADIUS; static const float kOuterRadius2 = OUTER_RADIUS*OUTER_RADIUS; static const float kInnerRadius = 1.0; static const float kInnerRadius2 = 1.0; static const float kCameraHeight = 0.0001; #define kMIE 0.0010 // Mie constant #define kSUN_BRIGHTNESS 20.0 // Sun brightness #define kMAX_SCATTER 50.0 // Maximum scattering value, to prevent math overflows on Adrenos static const half kSunScale = 100.0 * kSUN_BRIGHTNESS; static const float kKmESun = kMIE * kSUN_BRIGHTNESS; static const float kKm4PI = kMIE * 4.0 * 3.14159265; static const float kScale = 1.0 / (OUTER_RADIUS - 1.0); static const float kScaleDepth = 0.25; static const float kSamples = 2.0; // THIS IS UNROLLED MANUALLY, DON'T TOUCH #define MIE_G (-0.990) #define MIE_G2 0.9801 float _Tenkoku_MieAmt; float _Tenkoku_MnMieAmt; float4 Tenkoku_Vec_SunFwd; float4 Tenkoku_Vec_MoonFwd; float _tenkoku_rainbowFac1; //float4 Tenkoku_Vec_LightningFwd; //float Tenkoku_LightningIntensity; float _Tenkoku_EclipseFactor; struct appdata_t { float4 vertex : POSITION; }; struct v2f { float4 pos : SV_POSITION; half3 rayDir : TEXCOORD0; // Vector for incoming ray, normalized ( == -eyeRay ) half3 cIn : TEXCOORD1; // In-scatter coefficient half3 cOut : TEXCOORD2; // Out-scatter coefficient half3 texcoord : TEXCOORD3; }; float scale(float inCos) { float x = 1.0 - inCos; return 0.25 * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); } v2f vert (appdata_t v) { v2f OUT; UNITY_INITIALIZE_OUTPUT(v2f, OUT) OUT.pos = UnityObjectToClipPos(v.vertex); float3 kSkyTintInGammaSpace = COLOR_2_GAMMA(_SkyTint); // convert tint from Linear back to Gamma float3 kScatteringWavelength = lerp ( kDefaultScatteringWavelength-kVariableRangeForScatteringWavelength, kDefaultScatteringWavelength+kVariableRangeForScatteringWavelength, half3(1,1,1) - kSkyTintInGammaSpace); // using Tint in sRGB gamma allows for more visually linear interpolation and to keep (.5) at (128, gray in sRGB) point float3 kInvWavelength = 1.0 / pow(kScatteringWavelength, 4); float3 cameraPos = float3(0,kInnerRadius + kCameraHeight,0); // The camera's current position // Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere) float3 eyeRay = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex.xyz)); OUT.rayDir = half3(-eyeRay); half atFac = saturate( lerp( half(_Tenkoku_HorizonHeight), 1.0, half(dot(half3(0,3,0), (-eyeRay))) )); atFac = lerp(saturate(lerp(half(_Tenkoku_HorizonHeight),1.0,half(dot(lerp(half3(0,3,0),Tenkoku_Vec_SunFwd+half3(0.1,2.8,-0.1),_Tenkoku_Ambient), (-eyeRay))))), atFac, _Tenkoku_Ambient); _Tenkoku_AtmosphereDensity = lerp(_Tenkoku_AtmosphereDensity,_Tenkoku_AtmosphereDensity+_Tenkoku_HorizonDensity,atFac); float kRAYLEIGH = lerp(0.0, 0.0025, float(pow(_Tenkoku_AtmosphereDensity, 2.5))); float kScaleOverScaleDepth = (1.0 / (OUTER_RADIUS - 1.0)) / 0.075; float kKrESun = kRAYLEIGH * kSUN_BRIGHTNESS; float kKr4PI = kRAYLEIGH * 4.0 * 3.14159265; float far = 0.0; if(eyeRay.y >= 0.0) { // Sky // Calculate the length of the "atmosphere" far = sqrt(kOuterRadius2 + kInnerRadius2 * eyeRay.y * eyeRay.y - kInnerRadius2) - kInnerRadius * eyeRay.y; float3 pos = cameraPos + far * eyeRay; // Calculate the ray's starting position, then calculate its scattering offset float height = kInnerRadius + kCameraHeight; float depth = exp(kScaleOverScaleDepth * (-kCameraHeight)); float startAngle = dot(eyeRay, cameraPos) / height; float startOffset = depth*scale(startAngle); // Initialize the scattering loop variables float sampleLength = far / kSamples; float scaledLength = sampleLength * kScale; float3 sampleRay = eyeRay * sampleLength; float3 samplePoint = cameraPos + sampleRay * 0.5; // Now loop through the sample rays float3 frontColor = float3(0.0, 0.0, 0.0); // Weird workaround: WP8 and desktop FL_9_1 do not like the for loop here // (but an almost identical loop is perfectly fine in the ground calculations below) // Just unrolling this manually seems to make everything fine again. // for(int i=0; i