Skip to content

Commit 815bd8d

Browse files
committed
Factor out the procedural sky shader
1 parent cc9c63a commit 815bd8d

File tree

3 files changed

+134
-49
lines changed

3 files changed

+134
-49
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Shader "Hidden/HDRenderLoop/SkyHDRI"
2727
#include "Common.hlsl"
2828
#include "CommonLighting.hlsl"
2929
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
30-
#include "AtmosphericScattering.hlsl"
3130

3231
TEXTURECUBE(_Cubemap);
3332
SAMPLERCUBE(sampler_Cubemap);
@@ -69,56 +68,8 @@ Shader "Hidden/HDRenderLoop/SkyHDRI"
6968
float3 rotDirY = float3(sinPhi, 0, cosPhi);
7069
dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir));
7170

72-
/*
73-
Coordinate coord = GetCoordinate(input.positionCS.xy, _ScreenSize.zw);
74-
75-
// If the sky box is too far away (depth set to 0), the resulting look is too foggy.
76-
const float skyDepth = 0.01;
77-
78-
#ifdef PERFORM_SKY_OCCLUSION_TEST
79-
// Determine whether the sky is occluded by the scene geometry.
80-
// Do not perform blending with the environment map if the sky is occluded.
81-
float rawDepth = max(skyDepth, LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).r);
82-
float skyTexWeight = (rawDepth > skyDepth) ? 0.0 : 1.0;
83-
#else
84-
float rawDepth = skyDepth;
85-
float skyTexWeight = 1.0;
86-
#endif
87-
88-
float3 positionWS = UnprojectToWorld(rawDepth, coord.positionSS, _InvViewProjMatrix);
89-
90-
float4 c1, c2, c3;
91-
VolundTransferScatter(positionWS, c1, c2, c3);
92-
93-
float4 coord1 = float4(c1.rgb + c3.rgb, max(0.f, 1.f - c1.a - c3.a));
94-
float3 coord2 = c2.rgb;
95-
96-
float sunCos = dot(normalize(dir), _SunDirection);
97-
float miePh = MiePhase(sunCos, _MiePhaseAnisotropy);
98-
99-
float2 occlusion = float2(1.0, 1.0); // TODO.
100-
float extinction = coord1.a;
101-
float3 scatter = coord1.rgb * occlusion.x + coord2 * miePh * occlusion.y;
102-
103-
#ifdef ATMOSPHERICS_DEBUG
104-
switch (_AtmosphericsDebugMode)
105-
{
106-
case ATMOSPHERICS_DBG_RAYLEIGH: return c1;
107-
case ATMOSPHERICS_DBG_MIE: return c2 * miePh;
108-
case ATMOSPHERICS_DBG_HEIGHT: return c3;
109-
case ATMOSPHERICS_DBG_SCATTERING: return float4(scatter, 0.0);
110-
case ATMOSPHERICS_DBG_OCCLUSION: return float4(occlusion.xy, 0.0, 0.0);
111-
case ATMOSPHERICS_DBG_OCCLUDEDSCATTERING: return float4(scatter, 0.0);
112-
}
113-
#endif
114-
115-
*/
116-
11771
float3 skyColor = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y);
11872
return float4(skyColor, 1.0);
119-
120-
// Apply extinction to the scene color when performing alpha-blending.
121-
//return float4(skyColor * (skyTexWeight * extinction) + scatter, extinction);
12273
}
12374

12475
ENDHLSL
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyProcedural.shader.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)