gpt4 book ai didi

unity-game-engine - Unity 高斯模糊着色器只是使我的纹理变白 - 为什么?

转载 作者:行者123 更新时间:2023-12-02 04:17:19 25 4
gpt4 key购买 nike

我正在尝试根据本教程制作高斯模糊着色器: 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); )

关于unity-game-engine - Unity 高斯模糊着色器只是使我的纹理变白 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com