12-07 346 views
虽然urp管线下使用hlsl语言, 但是不妨碍我们小白使用cg入门, 前一阵子看了纹理蒙版, 今天突然就想到很早之前配合TA做的流光效果, 当时一脸懵逼, 现在自己重新实现一遍
//shader 纹理蒙版练习 流光
Shader "Custom/Unlit/liuguang"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_lg_mask ("lg_mask", 2D) = "white" {}
_lg("lg", 2D) = "white" {}
_lg_color ("lg_color", Color) = (1, 0.9, 0.7, 1)
_speed ("speed", Float ) = 0.3
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
//unity 默认全局变量, 可省略uniform关键字
sampler2D _MainTex; float4 _MainTex_ST;
//需要流光的部位蒙版纹理, 黑白图
sampler2D _lg_mask; float4 _lg_mask_ST;
//流光条带样式纹理, 黑白图
sampler2D _lg; float4 _lg_ST;
fixed4 _lg_color;
half _speed;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
//存储流光和蒙版的uv
float4 uv_mask : TEXCOORD1;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//float4 _Time : Time (t/20, t, t*2, t*3), use to animate things inside the shaders
o.uv_mask.xy = TRANSFORM_TEX(v.uv, _lg_mask);
//因为我这里是竖着的条带, 所以做x移动
o.uv_mask.zw = TRANSFORM_TEX(v.uv + half2(_Time.y * _speed, 0), _lg);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 maskCol = tex2D(_lg_mask,i.uv_mask.xy);
fixed4 lgCol = tex2D(_lg,i.uv_mask.zw);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
//基础颜色 + 流光蒙版*条带蒙版*条带颜色
fixed3 color = (col.rgb + maskCol.rgb * lgCol.rgb * _lg_color.rgb);
//全身流光
//fixed3 color = (col.rgb + lgCol.rgb * _lg_color.rgb);
return fixed4(color, 1);
}
ENDCG
}
}
}
效果如下

版权属于: CrazyStone Entertainment
原文地址: https://www.crazystonent.com/2020/12/07/unity-shader-exercise%e4%b8%80/
转载时必须以链接形式注明原始出处及本声明。