unity urp 16 高斯模糊
一、原理高斯模糊得核心方程就是高斯函数
由这个函数计算出来得值计算出高斯核
另外,二维高斯核的计算复杂度为NxNxWxH,二维高斯核可以分解成两个一维的核进行两边卷积操作,来降低计算复杂度,为2xNxWxH。
二、代码
2.1 二维高斯核直接计算
Shader "Unlit/18_GaussianBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurSize("Blur Size", Float) = 1.0
}
SubShader
{
Tags
{
"RenderPipeline"="UniversalRenderPipeline"
}
Cull Off
ZWrite Off
ZTest Always
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial)
float _BlurSize;
float4 _MainTex_TexelSize;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
struct Attributes
{
float4 positionOS: POSITION;
float2 texcoord: TEXCOORD0;
};
struct Varyings
{
float4 positionCS:SV_POSITION;
float2 uv:TEXCOORD0;
};
ENDHLSL
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
Varyings vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS);
output.uv = input.texcoord;
return output;
}
half4 frag (Varyings input) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
float2 uv = input.uv;
half4 col = float4(0,0,0,0);
col += 0.060 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(-1,-1)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.098 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(0,-1)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.060 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1,-1)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.098 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(-1,0)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.162 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
col += 0.098 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1,0)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.060 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1,-1)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.022 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1,0)*_MainTex_TexelSize.xy*_BlurSize);
col += 0.060 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1,1)*_MainTex_TexelSize.xy*_BlurSize);
return col;
}
ENDHLSL
}
}
}
2.2
三、结果
二维高斯核的结果,可以发现明度明显的降低
四、参考
小小:机器视觉学习(二)高斯卷积
页:
[1]