Skip to content

Commit 4f8819b

Browse files
Merge pull request #36 from EvgeniiG/master
Fix bugs and refactor
2 parents 27cbbd5 + 2cc4907 commit 4f8819b

File tree

8 files changed

+144
-164
lines changed

8 files changed

+144
-164
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ public override void PrepareLightsForGPU(CullResults cullResults, Camera camera,
898898
if (areaLightCount >= k_MaxAreaLightsOnSCreen)
899899
continue;
900900
lightCategory = LightCategory.Area;
901-
gpuLightType = GPULightType.Rectangle;
901+
gpuLightType = GPULightType.Line;
902902
lightVolumeType = LightVolumeType.Box;
903903
++areaLightCount;
904904
break;

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

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,9 @@ Shader "Hidden/HDRenderLoop/SkyHDRI"
1515
#pragma vertex Vert
1616
#pragma fragment Frag
1717

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-
2618
#include "Color.hlsl"
2719
#include "Common.hlsl"
2820
#include "CommonLighting.hlsl"
29-
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
30-
#include "AtmosphericScattering.hlsl"
3121

3222
TEXTURECUBE(_Cubemap);
3323
SAMPLERCUBE(sampler_Cubemap);
@@ -69,56 +59,8 @@ Shader "Hidden/HDRenderLoop/SkyHDRI"
6959
float3 rotDirY = float3(sinPhi, 0, cosPhi);
7060
dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir));
7161

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-
11762
float3 skyColor = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y);
11863
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);
12264
}
12365

12466
ENDHLSL
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
Shader "Hidden/HDRenderLoop/SkyProcedural"
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.

Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ public bool IsSkyValid(SkyParameters parameters)
197197

198198
private void RenderSky(Matrix4x4 invViewProjectionMatrix, SkyParameters skyParameters, Mesh skyMesh, RenderLoop renderLoop)
199199
{
200-
Shader.EnableKeyword("PERFORM_SKY_OCCLUSION_TEST");
201-
202200
m_RenderSkyPropertyBlock.SetTexture("_Cubemap", skyParameters.skyHDRI);
203201
m_RenderSkyPropertyBlock.SetVector("_SkyParam", new Vector4(skyParameters.exposure, skyParameters.multiplier, skyParameters.rotation, 0.0f));
204202
m_RenderSkyPropertyBlock.SetMatrix("_InvViewProjMatrix", invViewProjectionMatrix);
@@ -211,8 +209,6 @@ private void RenderSky(Matrix4x4 invViewProjectionMatrix, SkyParameters skyParam
211209

212210
private void RenderSkyToCubemap(SkyParameters skyParameters, RenderTexture target, RenderLoop renderLoop)
213211
{
214-
Shader.DisableKeyword("PERFORM_SKY_OCCLUSION_TEST");
215-
216212
for (int i = 0; i < 6; ++i)
217213
{
218214
Utilities.SetRenderTarget(renderLoop, target, 0, (CubemapFace)i);
@@ -243,11 +239,9 @@ private void RenderCubemapGGXConvolution(Texture input, RenderTexture target, Re
243239
RenderSkyToCubemap(skyParams, target, renderLoop);
244240
// End temp
245241

246-
//
247242
//for (int f = 0; f < 6; f++)
248243
// Graphics.CopyTexture(input, f, 0, target, f, 0);
249244

250-
251245
// Do the convolution on remaining mipmaps
252246
float invOmegaP = (6.0f * input.width * input.width) / (4.0f * Mathf.PI); // Solid angle associated to a pixel of the cubemap;
253247

@@ -263,7 +257,7 @@ private void RenderCubemapGGXConvolution(Texture input, RenderTexture target, Re
263257
for (int face = 0; face < 6; ++face)
264258
{
265259
Utilities.SetRenderTarget(renderLoop, target, mip, (CubemapFace)face);
266-
260+
267261
var cmd = new CommandBuffer { name = "" };
268262
cmd.DrawMesh(m_CubemapFaceMesh[face], Matrix4x4.identity, m_GGXConvolveMaterial, 0, 0, propertyBlock);
269263
renderLoop.ExecuteCommandBuffer(cmd);
@@ -282,12 +276,13 @@ public void RenderSky(Camera camera, SkyParameters skyParameters, RenderTargetId
282276
{
283277
// Trigger a rebuild of cubemap / convolution
284278
// TODO: can we have some kind of hash value here ? +> use or override GetHashCode() + include a refresh rate value in parameters
285-
// TODO: we could apply multiplier/exposure and rotation on the final result (i.e on the sky ibl and on lightprobe / lightmap, but can be tricky as Unity seems to merge sky information with
279+
// TODO: we could apply multiplier/exposure and rotation on the final result (i.e on the sky ibl and on lightprobe / lightmap, but can be tricky as Unity seems to merge sky information with
286280
// other lighting into SH / lightmap.
287281
if (skyParameters.skyResolution != m_bakedSkyParameters.skyResolution ||
288282
skyParameters.exposure != m_bakedSkyParameters.exposure ||
289283
skyParameters.rotation != m_bakedSkyParameters.rotation ||
290-
skyParameters.multiplier != m_bakedSkyParameters.multiplier)
284+
skyParameters.multiplier != m_bakedSkyParameters.multiplier ||
285+
skyParameters.skyHDRI != m_bakedSkyParameters.skyHDRI)
291286
{
292287
using (new Utilities.ProfilingSample("Sky Pass: Render Cubemap", renderLoop))
293288
{
@@ -307,6 +302,7 @@ public void RenderSky(Camera camera, SkyParameters skyParameters, RenderTargetId
307302
}
308303

309304
// Cleanup all this...
305+
m_bakedSkyParameters.skyHDRI = skyParameters.skyHDRI;
310306
m_bakedSkyParameters.skyResolution = skyParameters.skyResolution;
311307
m_bakedSkyParameters.exposure = skyParameters.exposure;
312308
m_bakedSkyParameters.rotation = skyParameters.rotation;

Assets/ScriptableRenderLoop/ShaderLibrary/AreaLighting.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ float PolygonRadiance(float4x3 L, bool twoSided)
160160

161161
sum *= INV_TWO_PI; // Normalization
162162

163-
return twoSided ? abs(sum) : max(sum, 0.0);
163+
sum = twoSided ? abs(sum) : max(sum, 0.0);
164+
165+
return isfinite(sum) ? sum : 0.0;
164166
}
165167

166168
// For polygonal lights.

0 commit comments

Comments
 (0)