我正在尝试根据本教程制作高斯模糊着色器: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5
但是当我将其转换为 Unity 的着色器语言时,除了透明度(并且没有模糊以太)之外,我的纹理中除了白色什么也没有。
这是我的 Unity 模糊着色器代码:
Shader "Unlit/UnlitShaderTest"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
uniform float resolution = 800;
uniform float radius = 400;
uniform float2 dir = float2(0,1);
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 sum = float4(0.0, 0.0, 0.0, 0.0);
float2 tc = i.uv;
float blur = 0;// radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(4.0,blur),hstep), tc.y - mul(mul(4.0,blur),vstep))),0.0162162162);
sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(3.0,blur),hstep), tc.y - mul(mul(3.0,blur),vstep))),0.0540540541);
sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(2.0,blur),hstep), tc.y - mul(mul(2.0,blur),vstep))),0.1216216216);
sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(1.0,blur),hstep), tc.y - mul(mul(1.0,blur),vstep))),0.1945945946);
sum += mul(tex2D(_MainTex, float2(tc.x, tc.y)),0.2270270270);
sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(1.0,blur),hstep), tc.y + mul(mul(1.0,blur),vstep))),0.1945945946);
sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(2.0,blur),hstep), tc.y + mul(mul(2.0,blur),vstep))),0.1216216216);
sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(3.0,blur),hstep), tc.y + mul(mul(3.0,blur),vstep))),0.0540540541);
sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(4.0,blur),hstep), tc.y + mul(mul(4.0,blur),vstep))),0.0162162162);
// sample the texture
fixed4 col = mul(tex2D(_MainTex, i.uv),float4(sum.r, sum.g, sum.b, sum.a));
return col;
}
ENDCG
}
}
}
有人可以告诉我为什么我的着色器只给我全白色而不是正常颜色吗?另外,如果有人能告诉我为什么它不模糊那就太好了。
Shader "Custom/GaussianBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
radius ("Radius", Range(0,30)) = 15
resolution ("Resolution", float) = 800
hstep("HorizontalStep", Range(0,1)) = 0.5
vstep("VerticalStep", Range(0,1)) = 0.5
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
half2 texcoord : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float radius;
float resolution;
//the direction of our blur
//hstep (1.0, 0.0) -> x-axis blur
//vstep(0.0, 1.0) -> y-axis blur
//for example horizontaly blur equal:
//float hstep = 1;
//float vstep = 0;
float hstep;
float vstep;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color;
return OUT;
}
float4 frag(v2f i) : COLOR
{
float2 uv = i.texcoord.xy;
float4 sum = float4(0.0, 0.0, 0.0, 0.0);
float2 tc = uv;
//blur radius in pixels
float blur = radius/resolution/4;
sum += tex2D(_MainTex, float2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += tex2D(_MainTex, float2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += tex2D(_MainTex, float2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += tex2D(_MainTex, float2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += tex2D(_MainTex, float2(tc.x, tc.y)) * 0.2270270270;
sum += tex2D(_MainTex, float2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += tex2D(_MainTex, float2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += tex2D(_MainTex, float2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += tex2D(_MainTex, float2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
return float4(sum.rgb, 1);
}
ENDCG
}
}
Fallback "Sprites/Default"
}
您获得白色纹理的原因是因为
mul(tex2D(_MainTex, i.uv),float4(sum.r, sum.g, sum.b, sum.a));
返回 float1 ( float1 mul(float4 v, float4x1 M);
)
我想补充一下冥王星上面的精彩答案。我注意到我的精灵有奇怪的黑色轮廓。着色器输出 1 作为 alpha,而不是纹理 alpha。然而,添加这个很容易。如果我们将第 79 行更改为...
返回 float4(sum.rgb, 1);
到
返回float4(sum.rgba);
然后将考虑 alpha。
我只是将其添加为评论,但我没有足够的 StackOverflow 声誉。 🤷u200d♂️