Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 77f30bd

Browse files
committed
Fixed noise sampling
1 parent b1c68b4 commit 77f30bd

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

PostProcessing/Runtime/Effects/ScreenSpaceReflections.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ internal void Render(PostProcessRenderContext context)
9494
CheckRT(ref m_Resolve, size, size, context.sourceFormat, FilterMode.Trilinear, true);
9595
CheckRT(ref m_History, size, size, context.sourceFormat, FilterMode.Bilinear, false);
9696

97+
var noiseTex = context.resources.blueNoise256[0];
9798
var sheet = context.propertySheets.Get(context.resources.shaders.screenSpaceReflections);
98-
sheet.properties.SetTexture(ShaderIDs.Noise, context.resources.blueNoise256[0]);
99+
sheet.properties.SetTexture(ShaderIDs.Noise, noiseTex);
99100

100101
var screenSpaceProjectionMatrix = new Matrix4x4();
101102
screenSpaceProjectionMatrix.SetRow(0, new Vector4(size * 0.5f, 0f, 0f, size * 0.5f));
@@ -111,6 +112,7 @@ internal void Render(PostProcessRenderContext context)
111112
sheet.properties.SetMatrix(ShaderIDs.InverseProjectionMatrix, projectionMatrix.inverse);
112113
sheet.properties.SetMatrix(ShaderIDs.ScreenSpaceProjectionMatrix, screenSpaceProjectionMatrix);
113114
sheet.properties.SetVector(ShaderIDs.Params, new Vector4(attenuation, distanceFade, maximumMarchDistance, lodCount));
115+
sheet.properties.SetVector(ShaderIDs.Params2, new Vector4((float)context.width / (float)context.height, (float)size / (float)noiseTex.width, 0f, 0f));
114116

115117
// TOOD: Hardcode these in shader variants (quality levels) for much improved performances
116118
sheet.properties.SetFloat("_Bandwidth", bandwidth);

PostProcessing/Runtime/Utils/ShaderIDs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static class ShaderIDs
3131
internal static readonly int InverseViewMatrix = Shader.PropertyToID("_InverseViewMatrix");
3232
internal static readonly int InverseProjectionMatrix = Shader.PropertyToID("_InverseProjectionMatrix");
3333
internal static readonly int ScreenSpaceProjectionMatrix = Shader.PropertyToID("_ScreenSpaceProjectionMatrix");
34+
internal static readonly int Params2 = Shader.PropertyToID("_Params2");
3435

3536
internal static readonly int FogColor = Shader.PropertyToID("_FogColor");
3637
internal static readonly int FogParams = Shader.PropertyToID("_FogParams");

PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define SSR_FINAL_BLEND_STATIC_FACTOR 0.95
1818
#define SSR_FINAL_BLEND_DYNAMIC_FACTOR 0.7
1919

20+
#define SSR_ENABLE_CONTACTS 0
21+
2022
//
2123
// Helper structs
2224
//
@@ -72,10 +74,13 @@ float4x4 _InverseProjectionMatrix;
7274
float4x4 _ScreenSpaceProjectionMatrix;
7375

7476
float4 _Params; // x: attenuation, y: distance fade, z: maximum march distance, w: blur pyramid lod count
77+
float2 _Params2; // x: aspect ratio, y: noise tiling
7578
#define _Attenuation _Params.x
7679
#define _DistanceFade _Params.y
7780
#define _MaximumMarchDistance _Params.z
7881
#define _BlurPyramidLODCount _Params.w
82+
#define _AspectRatio _Params2.x
83+
#define _NoiseTiling _Params2.y
7984

8085
// TODO: hardcode these
8186
int _MaximumIterationCount;
@@ -179,8 +184,8 @@ Result march(Ray ray, VaryingsDefault input)
179184

180185
float stride = 1.0 - min(1.0, -ray.origin.z * 0.01);
181186

182-
float2 uv = input.texcoord * 10.0;
183-
uv.y *= 0.5625;
187+
float2 uv = input.texcoord * _NoiseTiling;
188+
uv.y *= _AspectRatio;
184189

185190
float jitter = _Noise.SampleLevel(sampler_Noise, uv + _WorldSpaceCameraPos.xz, 0).a;
186191
stride *= _Bandwidth;
@@ -351,9 +356,13 @@ float4 FragComposite(VaryingsDefault i) : SV_Target
351356
float3 eye = mul((float3x3)_InverseViewMatrix, normalize(position));
352357
position = mul(_InverseViewMatrix, float4(position, 1.0)).xyz;
353358

354-
//float4 test = _Test.SampleLevel(sampler_Test, i.texcoord, 0);
359+
#if SSR_ENABLE_CONTACTS
360+
float4 test = _Test.SampleLevel(sampler_Test, i.texcoord, 0);
361+
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoord, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) * test.z + 1.0);
362+
#else
363+
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoord, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) + 1.0);
364+
#endif
355365

356-
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoord, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) /* * test.z */ + 1.0);
357366
float confidence = saturate(2.0 * dot(-eye, normalize(reflect(-eye, normal))));
358367

359368
UnityLight light;

0 commit comments

Comments
 (0)