|
| 1 | +Shader "Hidden/HDRenderLoop/SkyHDRI" |
| 2 | +{ |
| 3 | + SubShader |
| 4 | + { |
| 5 | + Pass |
| 6 | + { |
| 7 | + ZWrite Off |
| 8 | + ZTest LEqual |
| 9 | + Blend One Zero |
| 10 | + |
| 11 | + HLSLPROGRAM |
| 12 | + #pragma target 5.0 |
| 13 | + #pragma only_renderers d3d11 // TEMP: unitl we go futher in dev |
| 14 | + |
| 15 | + #pragma vertex Vert |
| 16 | + #pragma fragment Frag |
| 17 | + |
| 18 | + #pragma multi_compile _ ATMOSPHERICS_DEBUG |
| 19 | + #pragma multi_compile _ ATMOSPHERICS_OCCLUSION_FULLSKY |
| 20 | + #pragma multi_compile _ PERFORM_SKY_OCCLUSION_TEST |
| 21 | + |
| 22 | + #ifndef PERFORM_SKY_OCCLUSION_TEST |
| 23 | + #define IS_RENDERING_SKY |
| 24 | + #endif |
| 25 | + |
| 26 | + #include "Color.hlsl" |
| 27 | + #include "Common.hlsl" |
| 28 | + #include "CommonLighting.hlsl" |
| 29 | + #include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl" |
| 30 | + #include "AtmosphericScattering.hlsl" |
| 31 | + |
| 32 | + TEXTURECUBE(_Cubemap); |
| 33 | + SAMPLERCUBE(sampler_Cubemap); |
| 34 | + |
| 35 | + float4x4 _InvViewProjMatrix; |
| 36 | + float4 _SkyParam; // x exposure, y multiplier, z rotation |
| 37 | + |
| 38 | + struct Attributes |
| 39 | + { |
| 40 | + float3 positionCS : POSITION; |
| 41 | + float3 eyeVector : NORMAL; |
| 42 | + }; |
| 43 | + |
| 44 | + struct Varyings |
| 45 | + { |
| 46 | + float4 positionCS : SV_POSITION; |
| 47 | + float3 eyeVector : TEXCOORD0; |
| 48 | + }; |
| 49 | + |
| 50 | + Varyings Vert(Attributes input) |
| 51 | + { |
| 52 | + // TODO: implement SV_vertexID full screen quad |
| 53 | + Varyings output; |
| 54 | + output.positionCS = float4(input.positionCS.xy, UNITY_RAW_FAR_CLIP_VALUE, 1.0); |
| 55 | + output.eyeVector = input.eyeVector; |
| 56 | + |
| 57 | + return output; |
| 58 | + } |
| 59 | + |
| 60 | + float4 Frag(Varyings input) : SV_Target |
| 61 | + { |
| 62 | + float3 dir = normalize(input.eyeVector); |
| 63 | + |
| 64 | + // Rotate direction |
| 65 | + float phi = DegToRad(_SkyParam.z); |
| 66 | + float cosPhi, sinPhi; |
| 67 | + sincos(phi, sinPhi, cosPhi); |
| 68 | + float3 rotDirX = float3(cosPhi, 0, -sinPhi); |
| 69 | + float3 rotDirY = float3(sinPhi, 0, cosPhi); |
| 70 | + dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir)); |
| 71 | + |
| 72 | + Coordinate coord = GetCoordinate(input.positionCS.xy, _ScreenSize.zw); |
| 73 | + |
| 74 | + // If the sky box is too far away (depth set to 0), the resulting look is too foggy. |
| 75 | + const float skyDepth = 0.01; |
| 76 | + |
| 77 | + #ifdef PERFORM_SKY_OCCLUSION_TEST |
| 78 | + // Determine whether the sky is occluded by the scene geometry. |
| 79 | + // Do not perform blending with the environment map if the sky is occluded. |
| 80 | + float rawDepth = max(skyDepth, LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).r); |
| 81 | + float skyTexWeight = (rawDepth > skyDepth) ? 0.0 : 1.0; |
| 82 | + #else |
| 83 | + float rawDepth = skyDepth; |
| 84 | + float skyTexWeight = 1.0; |
| 85 | + #endif |
| 86 | + |
| 87 | + float3 positionWS = UnprojectToWorld(rawDepth, coord.positionSS, _InvViewProjMatrix); |
| 88 | + |
| 89 | + float4 c1, c2, c3; |
| 90 | + VolundTransferScatter(positionWS, c1, c2, c3); |
| 91 | + |
| 92 | + float4 coord1 = float4(c1.rgb + c3.rgb, max(0.f, 1.f - c1.a - c3.a)); |
| 93 | + float3 coord2 = c2.rgb; |
| 94 | + |
| 95 | + float sunCos = dot(normalize(dir), _SunDirection); |
| 96 | + float miePh = MiePhase(sunCos, _MiePhaseAnisotropy); |
| 97 | + |
| 98 | + float2 occlusion = float2(1.0, 1.0); // TODO. |
| 99 | + float extinction = coord1.a; |
| 100 | + float3 scatter = coord1.rgb * occlusion.x + coord2 * miePh * occlusion.y; |
| 101 | + |
| 102 | + #ifdef ATMOSPHERICS_DEBUG |
| 103 | + switch (_AtmosphericsDebugMode) |
| 104 | + { |
| 105 | + case ATMOSPHERICS_DBG_RAYLEIGH: return c1; |
| 106 | + case ATMOSPHERICS_DBG_MIE: return c2 * miePh; |
| 107 | + case ATMOSPHERICS_DBG_HEIGHT: return c3; |
| 108 | + case ATMOSPHERICS_DBG_SCATTERING: return float4(scatter, 0.0); |
| 109 | + case ATMOSPHERICS_DBG_OCCLUSION: return float4(occlusion.xy, 0.0, 0.0); |
| 110 | + case ATMOSPHERICS_DBG_OCCLUDEDSCATTERING: return float4(scatter, 0.0); |
| 111 | + } |
| 112 | + #endif |
| 113 | + |
| 114 | + float3 skyColor = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y); |
| 115 | + |
| 116 | + // Apply extinction to the scene color when performing alpha-blending. |
| 117 | + return float4(skyColor * (skyTexWeight * extinction) + scatter, extinction); |
| 118 | + } |
| 119 | + |
| 120 | + ENDHLSL |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + Fallback Off |
| 125 | +} |
0 commit comments