unity-shader-post-cameraFocus

开启前

开启后

ScreenRipple.shader

ScreenRipple.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef __CAMERA_FOCUS_HLSL__
#define __CAMERA_FOCUS_HLSL__

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 positionCS : SV_POSITION;
float4 positionSS : TEXCOORD1;
};

// 计算模糊颜色
// @uvXFac:uvX方向的偏移系数
// @uvYFac:uvY方向的偏移系数
// @blurSize:模糊程度值
half4 CalcBlurColor(v2f i, Texture2D _MainTex, SamplerState sampler_MainTex, half uvXFac, half uvYFac, half blurSize)
{
float blurSize2 = blurSize * 0.001; // 这里缩小,外部拖动小数太小不好拖
float uvXFac2 = uvXFac * blurSize2;
float uvYFac2 = uvYFac * blurSize2;

float2 uv = i.uv;

half4 color = half4(0,0,0,0);
color += 0.01621622 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - float2(uvXFac2 * 4.0, uvYFac2 * 4.0));
color += 0.05405405 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - float2(uvXFac2 * 3.0, uvYFac2 * 3.0));
color += 0.12162162 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - float2(uvXFac2 * 2.0, uvYFac2 * 2.0));
color += 0.19459459 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - float2(uvXFac2 * 1.0, uvYFac2 * 1.0));
color += 0.22702703 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv );
color += 0.19459459 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(uvXFac2 * 1.0, uvYFac2 * 1.0));
color += 0.12162162 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(uvXFac2 * 2.0, uvYFac2 * 2.0));
color += 0.05405405 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(uvXFac2 * 3.0, uvYFac2 * 3.0));
color += 0.01621622 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(uvXFac2 * 4.0, uvYFac2 * 4.0));

return color;
}

// 计算聚焦颜色
// @blurColor:模糊颜色
// @blurDist:有效的模糊距离,超过这个值才会模糊
half4 CalcFocusColor(v2f i, Texture2D _MainTex, SamplerState sampler_MainTex, Texture2D _CameraDepthTexture, SamplerState sampler_CameraDepthTexture,
half4 blurColor, half blurDist)
{
// 计算深度值
float2 screenUV = i.positionCS.xy/_ScreenParams.xy;
half4 depthMap = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV);
half depthZ = Linear01Depth(depthMap.x, _ZBufferParams);

// 计算模糊系数
half blur = step(depthZ, blurDist * 0.01f);

// 输出颜色处理
half4 mainColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
half4 outColor = mainColor * blur + blurColor * (1 - blur);

return outColor;
}

v2f vert (appdata v)
{
v2f o;
o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.positionSS = ComputeScreenPos(o.positionCS);
o.uv = v.uv;

return o;
}

#endif // __CAMERA_FOCUS_HLSL__
CameraFocus.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Shader "HHF/Post/CameraFocus"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FocusParam ("Focus Param(depth, blurSize)", Vector) = (1, 1, 1, 0)
}

SubShader
{
Tags { "RenderPipeline" = "UniversalPipeline" }
Cull Off ZWrite Off ZTest Always

Pass
{
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#include "CameraFocus.hlsl"

CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _FocusParam;
CBUFFER_END

TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex);
TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);

half4 frag (v2f i) : SV_Target
{
half4 blurColor = CalcBlurColor(i, _MainTex, sampler_MainTex, 1, 1, _FocusParam.y);
half4 outColor = CalcFocusColor(i, _MainTex, sampler_MainTex, _CameraDepthTexture, sampler_CameraDepthTexture, blurColor, _FocusParam.x );

return outColor;
}

ENDHLSL
}

Pass
{
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#include "CameraFocus.hlsl"

CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _FocusParam;
CBUFFER_END

TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex);
TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);

half4 frag (v2f i) : SV_Target
{
half4 blurColor = CalcBlurColor(i, _MainTex, sampler_MainTex, -1, 1, _FocusParam.y);
half4 outColor = CalcFocusColor(i, _MainTex, sampler_MainTex, _CameraDepthTexture, sampler_CameraDepthTexture, blurColor, _FocusParam.x );

return outColor;
}

ENDHLSL
}
}

}