#ifndef BEAUTIFY_PPSDOF_FX #define BEAUTIFY_PPSDOF_FX // Copyright 2020-2021 Kronnect - All Rights Reserved. #include "BeautifyCommon.hlsl" TEXTURE2D_X(_DoFTransparentDepth); //TEXTURE2D_X(_DofExclusionTexture); TEXTURE2D_X(_MainTex); float4 _MainTex_TexelSize; float4 _MainTex_ST; float4 _BokehData; float4 _BokehData2; float3 _BokehData3; float _BlurScale; #if BEAUTIFY_EDGE_AA_DOF #include "BeautifyAA.hlsl" #endif struct VaryingsDoFCross { float4 positionCS : SV_POSITION; float2 uv: TEXCOORD0; BEAUTIFY_VERTEX_CROSS_UV_DATA UNITY_VERTEX_OUTPUT_STEREO }; float getCocFromDepth(float depth) { if (depth > _BokehData3.y) return 0; float xd = abs(depth - _BokehData.x) - _BokehData2.x * (depth < _BokehData.x); return min(_BokehData3.z, 0.5 * _BokehData.y * xd / depth); } float getCocFromUV(float2 uv) { #if BEAUTIFY_DOF_TRANSPARENT float depthTex = BEAUTIFY_GET_CUSTOM_DEPTH_01(_DoFTransparentDepth, uv); float depth = BEAUTIFY_GET_SCENE_DEPTH_01(uv); depth = min(depth, depthTex); depth *= _ProjectionParams.z; #else float depth = BEAUTIFY_GET_SCENE_DEPTH_EYE(uv); #endif return getCocFromDepth(depth); } float getCoc(VaryingsSimple i) { return getCocFromUV(i.uv); } float4 FragCoC (VaryingsSimple i) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); i.uv = UnityStereoTransformScreenSpaceTex(i.uv); float3 pixelRGB = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, i.uv).rgb; float coc = getCoc(i) / COC_BASE; #if BEAUTIFY_EDGE_AA_DOF float2 uv = i.uv; float2 texelSize = _MainTex_TexelSize.xy; float2 uvN = uv + float2(0, texelSize.y); float2 uvS = uv - float2(0, texelSize.y); float2 uvW = uv - float2(texelSize.x, 0); float2 uvE = uv + float2(texelSize.x, 0); float depthN = BEAUTIFY_GET_SCENE_DEPTH_01(uvN); float depthS = BEAUTIFY_GET_SCENE_DEPTH_01(uvS); float depthW = BEAUTIFY_GET_SCENE_DEPTH_01(uvW); float depthE = BEAUTIFY_GET_SCENE_DEPTH_01(uvE); float depthCenter = BEAUTIFY_GET_SCENE_DEPTH_01(uv); float dDepth = abs(depthN - depthS) + abs(depthW - depthE); if (dDepth > ANTIALIAS_THRESHOLD) { float3 rgbN = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, uvN).rgb; float3 rgbS = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, uvS).rgb; float3 rgbW = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, uvW).rgb; float3 rgbE = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, uvE).rgb; float lumaN = getLuma(rgbN); float lumaS = getLuma(rgbS); float lumaW = getLuma(rgbW); float lumaE = getLuma(rgbE); float maxLuma = max(max(lumaN, lumaS), max(lumaW, lumaE)); float minLuma = min(min(lumaN, lumaS), min(lumaW, lumaE)); pixelRGB = ApplyEdgeAA(uv, pixelRGB, dDepth, depthCenter, lumaN, lumaS, lumaW, lumaE, minLuma, maxLuma, texelSize); float farClip = _ProjectionParams.z; float cocN = getCocFromDepth(depthN * farClip) / COC_BASE; float cocS = getCocFromDepth(depthS * farClip) / COC_BASE; float cocW = getCocFromDepth(depthW * farClip) / COC_BASE; float cocE = getCocFromDepth(depthE * farClip) / COC_BASE; float maxNeighborCoC = max(max(cocN, cocS), max(cocW, cocE)); coc = lerp(coc, maxNeighborCoC, 0.5); } #endif pixelRGB = clamp(pixelRGB, 0.0.xxx, _BokehData3.xxx); #if UNITY_COLORSPACE_GAMMA pixelRGB = GAMMA_TO_LINEAR(pixelRGB); #endif return float4(pixelRGB, coc); } float4 FragCoCDebug (VaryingsSimple i) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); i.uv = UnityStereoTransformScreenSpaceTex(i.uv); float4 pixel = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, i.uv); float CoC = min(getCoc(i) / 16.0, 1.0); return float4(CoC.xxx, 1.0); } float4 FragDoFDebugTransparent (VaryingsSimple i) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); i.uv = UnityStereoTransformScreenSpaceTex(i.uv); float depthTex = BEAUTIFY_GET_CUSTOM_DEPTH_01(_DoFTransparentDepth, i.uv); return float4(depthTex.xxx, 1.0); } float4 FragBlur (VaryingsSimple i): SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); i.uv = UnityStereoTransformScreenSpaceTex(i.uv); float4 sum = SAMPLE_TEXTURE2D_X(_MainTex, sampler_LinearClamp, i.uv ); float samples = ceil(sum.a * COC_BASE); float4 dir = float4(_BokehData.zw * _MainTex_TexelSize.xy, 0, 0); dir *= max(1.0, samples / _BokehData2.y); #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED) || defined(SINGLE_PASS_STEREO) dir.x *= 2.0; #endif float jitter = dot(float2(2.4084507, 3.2535211), i.uv * _MainTex_TexelSize.zw); float2 disp0 = dir.xy * (frac(jitter) + 0.5); float4 disp1 = float4(i.uv + disp0, 0, 0); float4 disp2 = float4(i.uv - disp0, 0, 0); float w = 1.0; const int sampleCount = (int)min(_BokehData2.y, samples); UNITY_UNROLL for (int k=1;k<16;k++) { if (k