如果代码中有什么不清楚请查看以下基础知识
Shader基础知识
unity3d 中 七种坐标知识详解
替换颜色的着色器
指定一个颜色,并设置范围。所有满足条件的像素都会被替换成另一个颜色
大致效果如下,我这里是选择了一个灰色替换成红色
Shader "Custom/MiniMap_s_new"
{
Properties
{
//当然就是材质啦
_MainTex ("Texture", 2D) = "white" {}
//要被替换的目标颜色
_TargetColor ("Target Color", Color) = (1, 1, 1, 1)
//要被替换的颜色范围
_TargetRange ("Target Color Range", Range(0, 1)) = 0.01
//替换成什么颜色
_ReplaceColor ("Replace Color", Color) = (1, 1, 1, 1)
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
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;
fixed4 _TargetColor;
fixed _TargetRange;
fixed4 _ReplaceColor;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
//替换颜色
_TargetColor = _TargetColor + _TargetRange;
if(col.r < abs(_TargetColor.r) && col.g < abs(_TargetColor.g) && col.b < abs(_TargetColor.b)){
col = _ReplaceColor;
//如果透明度小于等于0.就不显示
if(col.a <= 0){
discard;
}
}
return col;
}
ENDCG
}
}
}