Skip to content

Commit 8a24fa3

Browse files
committed
add DirectionalBlur
add DirectionalBlur
1 parent af846bc commit 8a24fa3

File tree

9 files changed

+255
-0
lines changed

9 files changed

+255
-0
lines changed

Assets/X-PostProcessing/Effects/DirectionalBlur.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+

2+
//----------------------------------------------------------------------------------------------------------
3+
// X-PostProcessing Library
4+
// created by QianMo @ 2020
5+
//----------------------------------------------------------------------------------------------------------
6+
7+
using System;
8+
using UnityEngine;
9+
using UnityEngine.Rendering;
10+
using UnityEngine.Rendering.PostProcessing;
11+
12+
13+
namespace XPostProcessing
14+
{
15+
16+
[Serializable]
17+
[PostProcess(typeof(DirectionalBlurRenderer), PostProcessEvent.AfterStack, "X-PostProcessing/Blur/DirectionalBlur")]
18+
public class DirectionalBlur : PostProcessEffectSettings
19+
{
20+
21+
[Range(0.0f, 5.0f)]
22+
public FloatParameter BlurRadius = new FloatParameter { value = 1f };
23+
24+
[Range(1, 30)]
25+
public IntParameter Iteration = new IntParameter { value = 12 };
26+
27+
[Range(0.0f, 6.0f)]
28+
public FloatParameter Angle = new FloatParameter { value = 0.5f };
29+
30+
[Range(1.0f, 10.0f)]
31+
public FloatParameter RTDownScaling = new FloatParameter { value = 1.0f };
32+
33+
}
34+
35+
public sealed class DirectionalBlurRenderer : PostProcessEffectRenderer<DirectionalBlur>
36+
{
37+
38+
private const string PROFILER_TAG = "X-DirectionalBlur";
39+
private Shader shader;
40+
41+
42+
public override void Init()
43+
{
44+
shader = Shader.Find("Hidden/X-PostProcessing/DirectionalBlur");
45+
}
46+
47+
public override void Release()
48+
{
49+
base.Release();
50+
}
51+
52+
static class ShaderIDs
53+
{
54+
internal static readonly int Iteration = Shader.PropertyToID("_Iteration");
55+
internal static readonly int Direction = Shader.PropertyToID("_Direction");
56+
internal static readonly int BufferRT = Shader.PropertyToID("_BufferRT");
57+
}
58+
59+
public override void Render(PostProcessRenderContext context)
60+
{
61+
62+
CommandBuffer cmd = context.command;
63+
PropertySheet sheet = context.propertySheets.Get(shader);
64+
cmd.BeginSample(PROFILER_TAG);
65+
66+
67+
if (settings.RTDownScaling > 1)
68+
{
69+
int RTWidth = (int)(context.screenWidth / settings.RTDownScaling);
70+
int RTHeight = (int)(context.screenHeight / settings.RTDownScaling);
71+
cmd.GetTemporaryRT(ShaderIDs.BufferRT, RTWidth, RTHeight, 0, FilterMode.Bilinear);
72+
// downsample screen copy into smaller RT
73+
context.command.BlitFullscreenTriangle(context.source, ShaderIDs.BufferRT);
74+
}
75+
76+
float sinVal = (Mathf.Sin(settings.Angle) * settings.BlurRadius * 0.05f) / settings.Iteration;
77+
float cosVal = (Mathf.Cos(settings.Angle) * settings.BlurRadius * 0.05f) / settings.Iteration;
78+
sheet.properties.SetVector(ShaderIDs.Direction, new Vector2(sinVal, cosVal));
79+
sheet.properties.SetFloat(ShaderIDs.Iteration, settings.Iteration);
80+
81+
if (settings.RTDownScaling > 1)
82+
{
83+
cmd.BlitFullscreenTriangle(ShaderIDs.BufferRT, context.destination, sheet, 0);
84+
}
85+
else
86+
{
87+
cmd.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
88+
}
89+
90+
91+
cmd.ReleaseTemporaryRT(ShaderIDs.BufferRT);
92+
cmd.EndSample(PROFILER_TAG);
93+
}
94+
}
95+
}
96+

Assets/X-PostProcessing/Effects/DirectionalBlur/DirectionalBlur.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/X-PostProcessing/Effects/DirectionalBlur/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+

2+
//----------------------------------------------------------------------------------------------------------
3+
// X-PostProcessing Library
4+
// created by QianMo @ 2020
5+
//----------------------------------------------------------------------------------------------------------
6+
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
using UnityEngine;
10+
using UnityEditor;
11+
12+
using UnityEditor.Rendering.PostProcessing;
13+
using UnityEngine.Rendering.PostProcessing;
14+
15+
namespace XPostProcessing
16+
{
17+
[PostProcessEditor(typeof(DirectionalBlur))]
18+
public sealed class DirectionalBlurEditor : PostProcessEffectEditor<DirectionalBlur>
19+
{
20+
21+
SerializedParameterOverride BlurRadius;
22+
SerializedParameterOverride Iteration;
23+
SerializedParameterOverride Angle;
24+
SerializedParameterOverride RTDownScaling;
25+
26+
27+
public override void OnEnable()
28+
{
29+
BlurRadius = FindParameterOverride(x => x.BlurRadius);
30+
Iteration = FindParameterOverride(x => x.Iteration);
31+
Angle = FindParameterOverride(x => x.Angle);
32+
RTDownScaling = FindParameterOverride(x => x.RTDownScaling);
33+
}
34+
35+
public override string GetDisplayTitle()
36+
{
37+
return XPostProcessingEditorUtility.DISPLAY_TITLE_PREFIX + base.GetDisplayTitle();
38+
}
39+
40+
public override void OnInspectorGUI()
41+
{
42+
PropertyField(BlurRadius);
43+
PropertyField(Iteration);
44+
PropertyField(Angle);
45+
PropertyField(RTDownScaling);
46+
}
47+
48+
}
49+
}
50+

Assets/X-PostProcessing/Effects/DirectionalBlur/Editor/DirectionalBlurEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/X-PostProcessing/Effects/DirectionalBlur/Shader.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
//----------------------------------------------------------------------------------------------------------
3+
// X-PostProcessing Library
4+
// created by QianMo @ 2020
5+
//----------------------------------------------------------------------------------------------------------
6+
7+
Shader "Hidden/X-PostProcessing/DirectionalBlur"
8+
{
9+
HLSLINCLUDE
10+
11+
#include "../../../Shaders/StdLib.hlsl"
12+
#include "../../../Shaders/XPostProcessing.hlsl"
13+
14+
uniform half _Iteration;
15+
uniform half2 _Direction;
16+
17+
half4 DirectionalBlur(VaryingsDefault i)
18+
{
19+
half4 color = half4(0.0, 0.0, 0.0, 0.0);
20+
21+
for (int k = -_Iteration; k < _Iteration; k++)
22+
{
23+
color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord - _Direction * k);
24+
}
25+
half4 finalColor = color / (_Iteration * 2.0);
26+
27+
return finalColor;
28+
}
29+
30+
half4 Frag(VaryingsDefault i): SV_Target
31+
{
32+
return DirectionalBlur(i);
33+
}
34+
35+
ENDHLSL
36+
37+
38+
SubShader
39+
{
40+
Cull Off ZWrite Off ZTest Always
41+
42+
Pass
43+
{
44+
HLSLPROGRAM
45+
46+
#pragma vertex VertDefault
47+
#pragma fragment Frag
48+
49+
ENDHLSL
50+
51+
}
52+
}
53+
}
54+
55+

Assets/X-PostProcessing/Effects/DirectionalBlur/Shader/DirectionalBlur.shader.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)