forked from keijiro/Pix2Pix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
31 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
94 changes: 19 additions & 75 deletions
94
Assets/Pix2Pix/PostProcessing/Resources/TemporalReprojection.hlsl
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,48 @@ | ||
#include "PostProcessing/Shaders/StdLib.hlsl" | ||
#include "PostProcessing/Shaders/Colors.hlsl" | ||
|
||
#define SAMPLE_TEX2D(name, uv) SAMPLE_TEXTURE2D(name, sampler##name, uv) | ||
#define SAMPLE_DEPTH(name, uv) SAMPLE_DEPTH_TEXTURE(name, sampler##name, uv).x | ||
|
||
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex); | ||
TEXTURE2D_SAMPLER2D(_RemapTex, sampler_RemapTex); | ||
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture); | ||
TEXTURE2D_SAMPLER2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture); | ||
TEXTURE2D_SAMPLER2D(_UVRemap, sampler_UVRemap); | ||
TEXTURE2D_SAMPLER2D(_PrevUVRemap, sampler_PrevUVRemap); | ||
TEXTURE2D_SAMPLER2D(_PrevMoDepth, sampler_PrevMoDepth); | ||
|
||
float4 _CameraDepthTexture_TexelSize; | ||
|
||
float _DepthWeight; | ||
float _MotionWeight; | ||
float2 _DeltaTime; | ||
|
||
struct FragmentOutput | ||
{ | ||
half4 uvRemap : SV_Target0; | ||
half4 moDepth : SV_Target1; | ||
half4 color : SV_Target0; | ||
half2 remap : SV_Target1; | ||
}; | ||
|
||
// Converts a raw depth value to a linear 0-1 depth value. | ||
float LinearizeDepth(float z) | ||
{ | ||
// A little bit complicated to take the current projection mode | ||
// (perspective/orthographic) into account. | ||
float isOrtho = unity_OrthoParams.w; | ||
float isPers = 1.0 - unity_OrthoParams.w; | ||
z *= _ZBufferParams.x; | ||
return (1.0 - isOrtho * z) / (isPers * z + _ZBufferParams.y); | ||
} | ||
|
||
float3 CompareDepth(float3 min_uvd, float2 uv) | ||
bool DepthMask(float depth) | ||
{ | ||
float d = SAMPLE_DEPTH(_CameraDepthTexture, uv); | ||
return d < min_uvd ? float3(uv, d) : min_uvd; | ||
} | ||
|
||
float2 SearchClosest(float2 uv) | ||
{ | ||
float4 duv = _CameraDepthTexture_TexelSize.xyxy * float4(1, 1, -1, 0); | ||
|
||
float3 min_uvd = float3(uv, SAMPLE_DEPTH(_CameraDepthTexture, uv)); | ||
|
||
min_uvd = CompareDepth(min_uvd, uv - duv.xy); | ||
min_uvd = CompareDepth(min_uvd, uv - duv.wy); | ||
min_uvd = CompareDepth(min_uvd, uv - duv.zy); | ||
|
||
min_uvd = CompareDepth(min_uvd, uv + duv.zw); | ||
min_uvd = CompareDepth(min_uvd, uv + duv.xw); | ||
|
||
min_uvd = CompareDepth(min_uvd, uv + duv.zy); | ||
min_uvd = CompareDepth(min_uvd, uv + duv.wy); | ||
min_uvd = CompareDepth(min_uvd, uv + duv.xy); | ||
|
||
return min_uvd.xy; | ||
const float epsilon = 1e-5; | ||
#if defined(UNITY_REVERSED_Z) | ||
return depth > epsilon; | ||
#else | ||
return depth < 1 - epsilon; | ||
#endif | ||
} | ||
|
||
FragmentOutput FragInitialize(VaryingsDefault i) | ||
{ | ||
half2 m = SAMPLE_TEX2D(_CameraMotionVectorsTexture, i.texcoord).xy; | ||
half d = LinearizeDepth(SAMPLE_DEPTH(_CameraDepthTexture, i.texcoord)); | ||
|
||
FragmentOutput o; | ||
o.uvRemap = half4(i.texcoord, d < 0.9, 1); | ||
o.moDepth = half4(m, d, 0); | ||
o.color = SAMPLE_TEX2D(_MainTex, i.texcoord); | ||
o.remap = i.texcoord; | ||
return o; | ||
} | ||
|
||
FragmentOutput FragUpdate(VaryingsDefault i) | ||
{ | ||
float2 uv1 = i.texcoord; | ||
half2 m1 = SAMPLE_TEX2D(_CameraMotionVectorsTexture, uv1).xy; | ||
half d1 = LinearizeDepth(SAMPLE_DEPTH(_CameraDepthTexture, uv1)); | ||
|
||
float2 uv0 = uv1 - m1; | ||
half3 md0 = SAMPLE_TEX2D(_PrevMoDepth, uv0).xyz; | ||
half4 c0 = SAMPLE_TEX2D(_PrevUVRemap, uv0); | ||
half2 mv = SAMPLE_TEX2D(_CameraMotionVectorsTexture, i.texcoord).xy; | ||
half2 remap = SAMPLE_TEX2D(_RemapTex, i.texcoord - mv).xy; | ||
|
||
// Disocclusion test | ||
float docc = abs(1 - d1 / md0.z) * 20;//_DepthWeight; | ||
|
||
// Out of screen test | ||
float oscr = any(uv0 < 0) + any(uv0 > 1); | ||
|
||
float alpha = 1 - saturate(docc + oscr); | ||
half4 c1 = SAMPLE_TEX2D(_MainTex, i.texcoord); | ||
half4 c2 = SAMPLE_TEX2D(_MainTex, remap); | ||
bool mask = DepthMask(SAMPLE_DEPTH(_CameraDepthTexture, i.texcoord)); | ||
|
||
FragmentOutput o; | ||
o.uvRemap = half4(c0.xy, d1 < 0.9, min(c0.a, alpha)); | ||
o.moDepth = half4(m1, d1, 0); | ||
o.color = lerp(c1, c2, mask); | ||
o.remap = remap; | ||
return o; | ||
} | ||
|
||
half4 FragComposite(VaryingsDefault i) : SV_Target | ||
{ | ||
half4 remap = SAMPLE_TEX2D(_UVRemap, i.texcoord); | ||
half4 c = SAMPLE_TEX2D(_MainTex, remap.xy); | ||
c = lerp(SAMPLE_TEX2D(_MainTex,i.texcoord), c, remap.z); | ||
return c; | ||
} |