请写一个Sprite2D边缘发光的shader
时间: 2024-03-16 12:42:25 浏览: 105
好的,以下是一个简单的 Unity Shader 代码实现 2D Sprite 边缘发光的效果:
```
Shader "Custom/SpriteOutlineGlow" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (1, 1, 1, 1)
_OutlineWidth ("Outline Width", Range(0, 1)) = 0.1
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f_t {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _OutlineColor;
float _OutlineWidth;
v2f_t vert (appdata_t v) {
v2f_t o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag (v2f_t i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 outlineColor = _OutlineColor * col.a;
float2 pixelSize = 1 / _ScreenParams.xy;
float2 uv2 = i.uv + pixelSize * _OutlineWidth * 2.0;
fixed4 col2 = tex2D(_MainTex, uv2);
outlineColor *= (col2.a - col.a);
col.rgb += outlineColor.rgb;
return col;
}
ENDCG
}
}
FallBack "Sprites/Default"
}
```
这个 Shader 首先通过传递的 `_MainTex` 纹理进行着色,之
阅读全文