Making A Transparent Shader in Unity
最终效果:
Shader源码:
Shader "Unlit/ManullyShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_TintColor("Tint Color", Color)=(1,1,1,1)
_Transparency("Transparency",Range(0.0,1.0))=0.5
_CutoutThresh("CutoutThresh",Range(0.0,1.0)) = 0.5
_Distance("Distance",float)=1
_Amplitude("Amplitude",float) = 1
_Speed("Speed",float) = 1
_Amount("Amount",float) = 1
//红色文字为自定义参数
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" } //更改Opaque为Transparent模式
LOD 100
ZWrite off
Blend SrcAlpha OneMinusSrcAlpha //此处有疑问可查Manual----https://docs.unity3d.com/Manual/SL-CullAndDepth.html
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION; // X Y Z W Model local speace
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION; // ScreenSpace postion
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _TintColor;
float _Transparency;
float _CutoutThresh;
float _Distance;
float _Amplitude;
float _Speed;
float _Amount;
//红色文字为自定义参数
v2f vert (appdata v)
{
v2f o;
v.vertex.x += sin(_Time.y*_Speed + v.vertex.y*_Amplitude)*_Distance*_Amount; // move model vertex with these num.
o.vertex = UnityObjectToClipPos(v.vertex); // This function is mean to translate model position to scence position
o.uv = TRANSFORM_TEX(v.uv, _MainTex); // Use UV info to put texture on it
return o; // return the struct
}
fixed4 frag (v2f i) : SV_Target // SV_Target is render target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv)+ _TintColor; // X Y Z W or RGBA
col.a = _Transparency;
clip(col.r - _CutoutThresh); // keep red channel
//if (col.r < _CutoutThresh) discard;
return col;
}
ENDCG
}
}
}
----------------------------------------代码分割线----------------------------------------------
PS:
Shader上的这张贴图需要红色通道很清晰,尽可能少用白色,会影响红色通道;可以没有alpha通道;代码中使用的是红色通道,也可以改成其他通道制作;
本文地址:https://blog.csdn.net/jennyhigh/article/details/110195326