Last active
July 3, 2021 12:01
-
-
Save ValerioMarty/09b6e8cc77bf92b85b23e929a13ef15f to your computer and use it in GitHub Desktop.
depth aware upsampling and compositing of the volumetric lighting tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pass | |
{ | |
Name "Compositing" | |
HLSLPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" | |
struct appdata | |
{ | |
real4 vertex : POSITION; | |
real2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
real2 uv : TEXCOORD0; | |
real4 vertex : SV_POSITION; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = TransformWorldToHClip(v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
sampler2D _MainTex; | |
TEXTURE2D (_volumetricTexture); | |
SAMPLER(sampler_volumetricTexture); | |
TEXTURE2D (_LowResDepth); | |
SAMPLER(sampler_LowResDepth); | |
real4 _SunMoonColor; | |
real _Intensity; | |
real3 frag (v2f i) : SV_Target | |
{ | |
real col = 0; | |
//based on https://eleni.mutantstargoat.com/hikiko/on-depth-aware-upsampling/ | |
int offset =0; | |
//depth at the high res fragment | |
real d0 = SampleSceneDepth(i.uv); | |
/* calculating the distances between the depths of the pixels | |
* in the lowres neighborhood and the full res depth value | |
* (texture offset must be compile time constant and so we | |
* can't use a loop) | |
*/ | |
//depth in the adjacent lower res pixels | |
real d1 = _LowResDepth.Sample(sampler_LowResDepth, i.uv, int2(0, 1)).x; | |
real d2 = _LowResDepth.Sample(sampler_LowResDepth, i.uv, int2(0, -1)).x; | |
real d3 =_LowResDepth.Sample(sampler_LowResDepth, i.uv, int2(1, 0)).x; | |
real d4 = _LowResDepth.Sample(sampler_LowResDepth, i.uv, int2(-1, 0)).x; | |
//difference between the two values | |
d1 = abs(d0 - d1); | |
d2 = abs(d0 - d2); | |
d3 = abs(d0 - d3); | |
d4 = abs(d0 - d4); | |
//choosing the closer one in depth | |
real dmin = min(min(d1, d2), min(d3, d4)); | |
if (dmin == d1) | |
offset= 0; | |
else if (dmin == d2) | |
offset= 1; | |
else if (dmin == d3) | |
offset= 2; | |
else if (dmin == d4) | |
offset= 3; | |
//sampling the chosen fragment | |
switch(offset){ | |
case 0: | |
col = _volumetricTexture.Sample(sampler_volumetricTexture, i.uv, int2(0, 1)); | |
break; | |
case 1: | |
col = _volumetricTexture.Sample(sampler_volumetricTexture, i.uv, int2(0, -1)); | |
break; | |
case 2: | |
col = _volumetricTexture.Sample(sampler_volumetricTexture, i.uv, int2(1, 0)); | |
break; | |
case 3: | |
col = _volumetricTexture.Sample(sampler_volumetricTexture, i.uv, int2(-1, 0)); | |
break; | |
default: | |
col = _volumetricTexture.Sample(sampler_volumetricTexture, i.uv); | |
break; | |
} | |
//color our rays and multiply by the intensity | |
real3 finalShaft =(saturate (col)*_Intensity)* normalize (_SunMoonColor); | |
real3 screen = tex2D(_MainTex,i.uv); | |
//regular sum (linear dodge/additive) | |
return screen+finalShaft; | |
} | |
ENDHLSL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment