视频
Shader "Unlit/ShileShader"
{
Properties
{
[HDR]_Color ("六边形边缘颜色", Color) = (1,1,1,1) //控制每一个蜂巢网格的边缘光颜色
[HDR]_Color1 ("条带颜色", Color) = (1,1,1,1) //控制每一个蜂巢网格的主体颜色
[HDR]_FresnelColor ("菲尼尔颜色", Color) = (1,1,1,1) //控制防护罩的边缘高光颜色
_FresnelPower ("FresnelPower", Range(0.1, 11)) = 1 //控制防护罩的边缘高光强度
_FresnelAlpha ("FresnelAlpha", Range(0, 50)) = 1 //控制防护罩的不透明度
_MainTex ("MainTex", 2D) = "white" {}
_ShieldTex ("ShieldTex", 2D) = "white" {}
_OffsetFactor ("OffsetFactor", Float) = 0.0001 //控制网格偏移程度
_RollSpeed ("RollSpeed", Range(0, 5)) = 1 //控制滚动速度
_AlphaTex("AlphaTex", 2D) = "white" {}
_ScaleOffset ("ScaleOffset", Range(0, 5)) = 1 //控制防护罩缩放的程度
_ScaleSpeed ("ScaleSpeed", Range(0, 10)) = 1 //控制防护罩缩放的速度
}
SubShader
{
Tags{"Queue" = "Transparent"/*模型渲染队列*/ "IngnoreProjector" = "True" "RanderType" = "Transparent"
"DisableBatching" = "True"}//Pass块参数标签
LOD 100
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
float4 _Color;
float4 _Color1;
float4 _FresnelColor;
float _FresnelPower;
float _FresnelAlpha;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _ShieldTex;
float4 _ShieldTex_ST;
float _OffsetFactor;
float _RollSpeed;
sampler2D _AlphaTex;
fixed _ScaleOffset;
fixed _ScaleSpeed;
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv1 : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float3 worldNormal:TEXCOORD2;
float3 worldPos:TEXCOORD3;
};
v2f vert (appdata v)
{
v2f o;
//注意uv的范围是0~1
float2 uv = v.normal.xy * 0.5 + 0.5;
//加上_MainTex贴图的缩放和偏移
uv = uv * _MainTex_ST.xy + _MainTex_ST.zw;
//用_RollSpeed来控制滚动速度
uv.y += _Time.y * _RollSpeed;
//利用texDlod函数来进行采样
float3 offset = tex2Dlod(_MainTex, float4(uv.x, uv.y, 0,0));
offset = smoothstep(0, 1, offset);
v.vertex.xyz += normalize(v.normal) * (offset * _OffsetFactor);
v.vertex.xyz += normalize(v.normal) * sin(_Time.y * _ScaleSpeed) * _ScaleOffset * 0.0005;
o.vertex = UnityObjectToClipPos(v.vertex);
//uv1和uv相同
o.uv1 = uv;
//uv2为物体本身的uv
o.uv2 = v.uv * _ShieldTex_ST.xy + _ShieldTex_ST.zw;
o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal));
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//菲尼尔系数Fresnel:F = pow(1-max(dot(v.n),0),_FresnelPower)
float NdotV = max(dot(i.worldNormal, normalize(UnityWorldSpaceViewDir(i.worldPos))),0);//模型边缘NdotV接近0
NdotV = 1 - NdotV;//取反:模型边缘为1
fixed3 fresnelCol = pow(NdotV * _FresnelColor, _FresnelPower);//模型边缘颜色
//Single Shield Color
fixed3 sCol = tex2D(_ShieldTex, i.uv2) * _Color.rgb;//六边形边缘光颜色
fixed4 col = tex2D(_MainTex, i.uv1) * _Color1;//偏移UV采样六边形颜色
fixed colAlpha = tex2D(_AlphaTex, i.uv1).a;//偏移UV采样遮罩
return fixed4(fresnelCol + col + sCol, pow(NdotV, _FresnelAlpha) + colAlpha);
//(模型边缘颜色+六边形本身颜色+添加边线颜色,边缘不透明+采样遮罩部分不透明)
}
ENDCG
}
}
FallBack "Transparent/VertexLit"//混合透明物体无法投射与接收阴影
}