-
-
Save 3DI70R/52be1a8e917d476c0a953ed1a577876e to your computer and use it in GitHub Desktop.
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
#pragma kernel DeformPass | |
#pragma kernel FlattenPass | |
#pragma kernel PaintPass | |
#pragma kernel PostprocessDepthPass | |
int depthWidth; | |
int depthHeight; | |
float zNear; | |
float zSize; | |
float flattenSpeed; | |
float postprocessSpeed; | |
float4 paintColor; | |
Texture2D<float4> PaintDepthTexture; | |
Texture2D<float4> DeformDepthTexture; | |
Texture2D<float4> FlattenDepthTexture; | |
Texture2D<float4> EffectDepthTexture; | |
RWTexture2D<float4> MaskDepthTexture; | |
RWTexture2D<float4> MaskDepthPostprocessTexture; | |
RWTexture2D<float4> MaskColorTexture; | |
const static int BlurKernelSize = 25; | |
static int2 BlurKernelOffsets[BlurKernelSize] = { | |
int2(-2, -2), int2(-1, -2), int2(0, -2), int2(1, -2), int2(2, -2), | |
int2(-2, -1), int2(-1, -1), int2(0, -1), int2(1, -1), int2(2, -1), | |
int2(-2, 0), int2(-1, 0), int2(0, 0), int2(1, 0), int2(2, 0), | |
int2(-2, 1), int2(-1, 1), int2(0, 1), int2(1, 1), int2(2, 1), | |
int2(-2, 2), int2(-1, 2), int2(0, 2), int2(1, 2), int2(2, 2) | |
}; | |
static float BlurKernelWeights[BlurKernelSize] = { | |
0.002969, 0.013306, 0.021938, 0.013306, 0.002969, | |
0.013306, 0.059634, 0.098320, 0.059634, 0.013306, | |
0.021938, 0.098320, 0.162103, 0.098320, 0.021938, | |
0.013306, 0.059634, 0.098320, 0.059634, 0.013306, | |
0.002969, 0.013306, 0.021938, 0.013306, 0.002969, | |
}; | |
float InverseLerp(float a, float b, float t) | |
{ | |
return (t - a) / (b - a); | |
} | |
float Linear01Depth(float z) | |
{ | |
float zValue = (1 - z); | |
if(zValue < zNear - zSize) { | |
return 1; | |
} | |
return InverseLerp(zNear, zNear + zSize, zValue); | |
} | |
float GetCameraPixelDepth(Texture2D<float4> t, uint2 id) | |
{ | |
return Linear01Depth(t[int2(depthWidth - id.x, id.y)]); | |
} | |
[numthreads(8,8,1)] | |
void DeformPass(uint3 id : SV_DispatchThreadID) | |
{ | |
if(id.x > depthWidth || id.y > depthHeight) return; | |
float4 mask = MaskDepthTexture[id.xy]; | |
float cameraDepth = GetCameraPixelDepth(DeformDepthTexture, id.xy); | |
if(cameraDepth < mask.r) | |
{ | |
mask.r = lerp(mask.r, cameraDepth, 0.5); | |
MaskDepthTexture[id.xy] = mask; | |
} | |
} | |
[numthreads(8,8,1)] | |
void FlattenPass(uint3 id : SV_DispatchThreadID) | |
{ | |
if(id.x > depthWidth || id.y > depthHeight) return; | |
float4 mask = MaskDepthTexture[id.xy]; | |
float cameraDepth = GetCameraPixelDepth(FlattenDepthTexture, id.xy); | |
if(cameraDepth < mask.r - 0.1 || cameraDepth > 0.99f) return; | |
float result = mask.r; | |
for(int i = 0; i < BlurKernelSize; i++) | |
{ | |
result = max(result, MaskDepthTexture[id.xy + BlurKernelOffsets[i]]); | |
} | |
mask.r = lerp(mask.r, result, flattenSpeed); | |
MaskDepthTexture[id.xy] = mask; | |
} | |
[numthreads(8,8,1)] | |
void PaintPass(uint3 id : SV_DispatchThreadID) | |
{ | |
if(id.x > depthWidth || id.y > depthHeight) return; | |
float4 mask = MaskDepthTexture[id.xy]; | |
float cameraDepth = GetCameraPixelDepth(PaintDepthTexture, id.xy); | |
if(cameraDepth < mask.r) | |
{ | |
float4 color = paintColor; | |
color.a = 1; | |
MaskColorTexture[id.xy] = lerp(MaskColorTexture[id.xy], color, paintColor.a); | |
} | |
} | |
[numthreads(8,8,1)] | |
void PostprocessDepthPass(uint3 id : SV_DispatchThreadID) | |
{ | |
if(id.x > depthWidth || id.y > depthHeight) return; | |
float result = 0; | |
for(int i = 0; i < BlurKernelSize; i++) | |
{ | |
int2 position = id.xy + BlurKernelOffsets[i]; | |
float4 maskValue = MaskDepthTexture[position]; | |
float effectValue = GetCameraPixelDepth(EffectDepthTexture, position); | |
result += min(maskValue, max(effectValue, 0.4)) * BlurKernelWeights[i]; | |
} | |
MaskDepthPostprocessTexture[id.xy] = lerp(MaskDepthPostprocessTexture[id.xy], result, postprocessSpeed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment