遮挡显示
Shader "Unlit/MyOwnShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MarginColor ("_MarginColor",COLOR)=(1,1,1,1)
_Float ("_Float", Range(0.1,10)) = 0.1
_BrightFloat ("_BrightFloat", Range(0.1,50)) = 0.1
_WardColor ("_WardColor",COLOR)=(1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"} //向前渲染模式
LOD 100
Pass
{
ZWrite off
//将第一个通道的深度隐藏
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
//引用库"Lighting.cginc",而在以库类里面已引用了库"UnityLightingCommon.cginc";
//所以只需引用库"Lighting.cginc"即可;
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 worldViewDir:POSITION1; //物体顶点到相机的世界坐标
float3 worldNormDir:POSITION2; //物体顶点法线的世界坐标
float3 worldLightDir:POSITION3; //物体顶点到光源的世界坐标
float3 worldBrightLightFDir:POSITION4; //太阳光的反射光的世界坐标
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _MarginColor;
float _Float;
float _BrightFloat;
//基于顶点渲染
v2f vert (appdata_base v)
{
v2f o;
o.worldNormDir = normalize(UnityObjectToWorldNormal(v.normal));
//将顶点的法线的本地坐标转化成世界坐标 转化成单位向量用于点乘计算
o.worldViewDir = normalize(WorldSpaceViewDir(v.vertex));
//将顶点本地坐标到摄像机本地坐标的向量转化成世界的 转化成单位向量用于点乘计算
o.worldLightDir = normalize(UnityWorldSpaceLightDir(v.vertex));
//将顶点本地坐标到光源本地坐标的向量转化成世界的 转化成单位向量用于点乘计算
o.worldBrightLightFDir = normalize(reflect(-o.worldLightDir,o.worldNormDir));
//得到光线反射的世界坐标 转化成单位向量用于点乘计算
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
//基于像素渲染
fixed4 frag (v2f i) : SV_Target
{
fixed4 theCol = _MarginColor * pow(1 - (saturate(dot(i.worldViewDir,i.worldNormDir))), _Float);
fixed4 theDiffuseCol = _LightColor0 * saturate(dot(i.worldLightDir,i.worldNormDir)) + UNITY_LIGHTMODEL_AMBIENT;
// _LightColor0 是 "UnityLightingCommon.cginc" 里面的,他表示世界的灯光的颜色
//UNITY_LIGHTMODEL_AMBIENT是环境光
//渲染里面: + 代表颜色更深,* 代表颜色更浅
fixed4 theBrightLightCol =_LightColor0 * pow(saturate(dot(i.worldBrightLightFDir,i.worldViewDir)),_BrightFloat);
//高光颜色
fixed4 col = tex2D(_MainTex, i.uv) * theDiffuseCol + theCol + theBrightLightCol;
return col;
}
ENDCG
}
Pass
{
blend SrcAlpha OneMinusSrcAlpha
//透明通道
ZTest greater
//被挡住显示,被挡住就渲染这个通道
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
//引用库"Lighting.cginc",而在以库类里面已引用了库"UnityLightingCommon.cginc";
//所以只需引用库"Lighting.cginc"即可;
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 worldViewDir:POSITION1; //物体顶点到相机的世界坐标
float3 worldNormDir:POSITION2; //物体顶点法线的世界坐标
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _MarginColor;
float _Float;
fixed4 _WardColor;
//基于顶点渲染
v2f vert (appdata_base v)
{
v2f o;
o.worldNormDir = normalize(UnityObjectToWorldNormal(v.normal));
//将顶点的法线的本地坐标转化成世界坐标 转化成单位向量用于点乘计算
o.worldViewDir = normalize(WorldSpaceViewDir(v.vertex));
//将顶点本地坐标到摄像机本地坐标的向量转化成世界的 转化成单位向量用于点乘计算
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
//基于像素渲染
fixed4 frag (v2f i) : SV_Target
{
fixed4 theCol = _MarginColor * pow(1 - (saturate(dot(i.worldViewDir,i.worldNormDir))), _Float);
fixed4 col = tex2D(_MainTex, i.uv) * _WardColor + theCol;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
//设置回滚投机取巧偷懒,让物体的阴影为Diffuse的阴影
}
人物描边
Shader "Unlit/Outline"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("_Color",COLOR)=(1,1,1,1)
}
SubShader
{
Tags { "Queue"="Transparent"}
LOD 100
Pass
{
blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
v2f vert (appdata_base v)
{
v2f o;
v.vertex.x *= 1.08;
v.vertex.y *= 1.08;
v.vertex.z *= 1.08;
//放大了再将本地坐标转化成投影坐标
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return _Color;
}
ENDCG
}
Pass
{
ZTest greater
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}