我正在尝试从本教程中创建一个高斯模糊着色器: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);
)