我已经尝试了两个多星期了,但还是不行,我尝试了多种方法,甚至询问聊天 GPT 但没有任何效果,我试图制作一个简单的着色器来勾勒出 NPC 的精灵
这是我尝试过的一些事情:
这个是由聊天GPT制作的,这是我的最新尝试,此时我感到绝望:
float4 PixelShaderFunction(float4 sampleColor : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
float4 color = tex2D(uImage0, coords);
if (any(color))
return color.rgba = 1,1,1,1;
color.xy = uTargetPosition;
float dx = 1 / uImageSize1.x;
float dy = 1 / uImageSize1.y;
bool flag = false;
for (float i = 0; i <= 0.5; i++)
{
for (float j = 0; j <= 1; j++)
{
color.xy = float2(dx * i, dy * j);
if (any(color.a))
{
flag = true;
}
}
}
if (flag == true)
{
return color.rgba = 0, 1, 0, 1;
}
return color;
}
这个是从中文教程中删除的,它充满了错误,甚至无法编译,所以我尽力修复它,它确实可以编译,但没有给出我想要的结果:
float4 PixelShaderFunction(float4 sampleColor : COLOR0, float2 coords : TEXCOORD0) : COLOR0
{
float4 color = tex2D(uImage0, coords);
if (any(color))
return color.rgba = 1,1,1,1;
color.xy = uTargetPosition;
float dx = 1 / uImageSize1.x;
float dy = 1 / uImageSize1.y;
bool flag = false;
for (float i = 0; i <= 0.5; i++)
{
for (float j = 0; j <= 1; j++)
{
color.xy = float2(dx * i, dy * j);
if (any(color.a))
{
flag = true;
}
}
}
if (flag == true)
{
return color.rgba = 0, 1, 0, 1;
}
return color;
}
我认为问题出在循环中的 i 和 j 以及着色器透明度设置。
Shader "Hidden/Example"{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_uImageSize ("ImageSize", Vector) = (300,300,0,0)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float2 _uImageSize;
float4 PixelShaderFunction(float2 coords : TEXCOORD0) : COLOR0
{
float4 color = tex2D(_MainTex, coords);
float4 empty = float4(0,0,0,0);
float4 border = float4(0,0,1,1);
if (any(color.a)) return color;
float dx = 1 / _uImageSize.x;
float dy = 1 / _uImageSize.y;
bool flag = false;
for(int i =-1; i <=1; i++)
{
for(int j=-1; j<=1; j++)
{
float4 nearColor = tex2D(_MainTex, coords + float2(dx * i, dy * j));
if (any(nearColor.a))
{
flag = true;
}
}
}
if (flag) return border;
return empty;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = PixelShaderFunction(i.uv);
return col;
}
ENDCG
}
}}