您现在的位置是:首页 > 数码 > 

径向模糊shader

2025-07-20 13:47:57
径向模糊shader 这效果在鬼泣4中切换场景时见过, 极品飞车12的运动模糊也有这种感觉. 原理: 确定一个中心点(如0.5, 0.5), 跟当前像素连一条线. 以当前像素为中心, 在线上的附近像素进行采样, 最后取一下平均值. 代码翻译成HLSL: // This texture should hold the image to blur. sampler2D Texture

径向模糊shader

这效果在鬼泣4中切换场景时见过, 极品飞车12的运动模糊也有这种感觉.

原理:

确定一个中心点(如0.5, 0.5), 跟当前像素连一条线. 以当前像素为中心, 在线上的附近像素进行采样, 最后取一下平均值.

代码翻译成HLSL:

// This texture should hold the image to blur.
sampler2D Texture0;

// some ct, tweak for best look
ct float fSampleDist;
ct float fSampleStrength;

// some sample positi
float samples[10] =
{
-0.08,
-0.05,
-0.0,
-0.02,
-0.01,
0.01,
0.02,
0.0,
0.05,
0.08
};

float4 ps_main( float2 texCoord : TEXCOORD0 ) : COLOR
{
// 0.5,0.5 is the center of the screen
// so substracting uv from it will result in
// a vector pointing to the middle of the screen
float2 dir = 0.5 - texCoord;
// calculate the distance to the center of the screen
float dist = length(dir);
// normalize the direction (reuse the distance)
dir /= dist;

// this is the original colour of this pixel
// using only this would result in a nonblurred version
float4 color = tex2D(Texture0, texCoord);

float4 sum = color;
// take 10 additional blur samples in the direction towards
// the center of the screen
for (int i = 0; i < 10; i)
{
sum = tex2D(Texture0, texCoord  dir * samples[i] * fSampleDist);
}

// we have taken eleven samples
sum /= 11.0;

// weighten the blur effect with the distance to the
// center of the screen ( further out is blurred more)
float t = saturate(dist * fSampleStrength);

//Blend the original color with the averaged pixels
return lerp(color, sum, t);
}

Unity shaderLab:

按 CtrlC 复制代码

//径向模糊后处理
Shader “RadialBlur” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
_fSampleDist(“SampleDist”, Float) = 1 //采样距离
_fSampleStrength(“SampleStrength”, Float) = 2.2 //采样力度
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

                     #include struct appdata_t {float4 vertex : POSITIO;float2 texcoord : TEXCOORD;};struct v2f {float4 vertex : POSITIO;float2 texcoord : TEXCOORD;};float4 _MainTex_ST;v2f vert (appdata_t v){v2f o;o.vertex = mul(UITY_MATRIX_MVP, v.vertex); = TRASFORM_TEX(, _MainTex);return o;}sampler2D _MainTex;float _fSampleDist;float _fSampleStrength;// some sample positi  static ct float samples[6] =   {   -0.05,  -0.0,    -0.01,  0.01,    0.0,  0.05,  }; half4 frag (v2f i) : SV_Target{//0.5,0.5屏幕中心float2 dir = float2(0.5, 0.5) - ;//从采样中心到uv的方向向量float2 texcoord = ;float dist = length(dir);  dir = normalize(dir); float4 color = tex2D(_MainTex, texcoord);  float4 sum = color;//    6次采样for (int i = 0; i < 6; i)  {  sum = tex2D(_MainTex, texcoord  dir * samples[i] * _fSampleDist);    }  //求均值sum /= 7.0f;  //越离采样中心近的地方,越不模糊float t = saturate(dist * _fSampleStrength);  //插值return lerp(color, sum, t);}EDCG }} Fallback off

}

两个参数, 动态调整的话可以产生极品飞车12那种速度感(也算是第一人称运动模糊的简单实现吧).

这是RM里的效果:

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/shuma/794007.html

相关标签:无
上传时间: 2024-01-09 22:17:50
留言与评论(共有 9 条评论)
本站网友 重庆整形医院
18分钟前 发表
// This texture should hold the image to blur. sampler2D Texture0; // some ct
本站网友 鳌峰公园
16分钟前 发表
-0.0
本站网友 国泰君安大智慧5
26分钟前 发表
0.0
本站网友 曲婷婷
7分钟前 发表
Float) = 1 //采样距离 _fSampleStrength(“SampleStrength”
本站网友 长春牙科
18分钟前 发表
0.5屏幕中心float2 dir = float2(0.5
本站网友 大鼻头整形
26分钟前 发表
0.5)
本站网友 安西信行
28分钟前 发表
t);}EDCG }} Fallback off } 两个参数
本站网友 德意名居
19分钟前 发表
动态调整的话可以产生极品飞车12那种速度感(也算是第一人称运动模糊的简单实现吧). 这是RM里的效果