1.
Shader "Custom/RimLightAndScan"
{
Properties
{
_RimColor ("RimColor", Color) = (1,1,1,1)
_InnerColor("InnerColor",Color)=(0,0,0,0)
_RimTransparency("RimTransparency",Range(0,1))=1
_RimStrength("RimStrength",Range(0,2))=1
_RimPowStrength("RimPowStrength",Range(1,5))=3
_RimBase("RimBase",Range(0,2))=0.5
_ScanTex ("ScanTex", 2D) = "white" {}
_ScanTexStrength ("ScanTexStrength", Range(0,1)) = 0.5
_ScanSpeed("ScanSpeed",Range(0,5))=1
_ScanTransparency("ScanTransparency",Range(0,1))=0.5
_FlowTilling("FlowTilling",Vector)=(1,1,0,0)
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 200
pass{
//这个pass块开启深度写入,但不输出颜色,目的仅为把该模型的深度值写入深度缓冲
//这样可以得到逐像素级别的正确深度信息(否则,模型有交叉结构时无法得到正确效果)
Zwrite On
ColorMask 0
}
pass {
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _RimColor;
fixed4 _InnerColor;
float _RimTransparency;
float _RimStrength;
float _RimPowStrength;
float _RimBase;
sampler2D _ScanTex;
float4 _ScanTex_ST;
float _ScanTexStrength;
float _ScanSpeed;
float _ScanTransparency;
float4 _FlowTilling;
struct a2v {
float4 vertex : POSITION;//语义POSITION指模型空间下的顶点位置
float3 normal :NORMAL;//语义指模型空间下法线
float4 texcoord:TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 WorldNormal:TEXCOORD0;
float3 WorldPos : TEXCOORD1;
//float3 WorldViewDir:TEXCOORD3;
float3 WorldPivot:TEXCOORD2;
float2 uv:TEXCOORD3;
};
v2f vert(a2v v) {
v2f f;
f.pos = UnityObjectToClipPos(v.vertex);
f.WorldNormal= UnityObjectToWorldNormal(v.normal);
f.WorldPos = mul(unity_ObjectToWorld, v.vertex);
//f.WorldViewDir=UnityWorldSpaceViewDir(f.WorldPos);
f.WorldPivot=mul(unity_ObjectToWorld,float4(0,0,0,0)).xyz;
f.uv=TRANSFORM_TEX(v.texcoord,_ScanTex);
return f;
}
fixed4 frag(v2f f) :SV_TARGET0{
fixed3 worldNormal=normalize(f.WorldNormal);
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(f.WorldPos));//单位化视线方向。需要知道点的世界坐标
float4 RimLight=
lerp(_RimColor,_InnerColor,saturate( pow(dot(worldNormal,viewDir),_RimPowStrength)*_RimStrength+_RimBase));
RimLight.a=_RimTransparency;
//half2 uv=f.uv;
//half2 uv=f.WorldPos.xy;
half2 uv=f.WorldPos.xy-f.WorldPivot.xy;
uv.y+=_ScanSpeed*_Time.y;
fixed4 Scan = tex2D(_ScanTex, uv)*_ScanTexStrength;
Scan.a=_ScanTransparency;
fixed4 finalColor=RimLight+Scan;
return finalColor;
}
ENDCG //end
}
}
FallBack "Diffuse"
}
2.
Shader "01"{
Properties{
_MatCapTex("MatCapTex",2D)="write"{}
_RampTex("RampTex",2D)="write"{}
}
SubShader{
Tags {"RenderType" = "Opaque" }
pass {
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM //Begin
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MatCapTex;
//float4 _MatCapTex_ST;
sampler2D _RampTex;
//float4 _RampTex_ST;
struct a2v {
float4 vertex : POSITION;
float3 normal :NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 WorldNormal:TEXCOORD0;
float3 WorldPos:TEXCOORD1;
//float3 VectorPos:TEXCOORD2;
//float2 uv:TEXCOORD1;
};
v2f vert(a2v v) {
v2f f;
f.pos = UnityObjectToClipPos(v.vertex);
f.WorldNormal= UnityObjectToWorldNormal(v.normal);
f.WorldPos = mul(unity_ObjectToWorld, v.vertex);
//f.VectorPos=v.vertex;
return f;
}
fixed4 frag(v2f f) :SV_TARGET0{
//这一块内容一定要去看Unity内置变化矩阵的知识点.UNITY_MATRIX_V,UNITY_MATRIX_IT_MV都是什么变化要记牢
float3 ViewNormal=mul(UNITY_MATRIX_V,normalize(f.WorldNormal));
float2 MatCapUV=0.5*(ViewNormal.xy+float2(1,1));
//MatCapUV.x = dot(normalize(UNITY_MATRIX_IT_MV[0].xyz), normalize(f.VectorPos));
//MatCapUV.y= dot(normalize(UNITY_MATRIX_IT_MV[1].xyz), normalize(f.VectorPos));
//MatCapUV = MatCapUV.xy * 0.5 + 0.5;
float4 MatCapColor=tex2D(_MatCapTex,MatCapUV);
//关于这里的uv采样原理,用大脑模拟一下相机空间你看到的物体法线向量(由(x,y,z )表示),取xy
//例如,与视线平行的法线(正对视线的法线),向量表示为(0,0,1)(这里的法线取起始点为眼睛)取xy即为(0,0)
//类似的,考虑非平行的其他法线
fixed3 ViewDir = normalize(UnityWorldSpaceViewDir(f.WorldPos));
float NDotV=dot(f.WorldNormal,ViewDir);
float4 RampColor=tex2D(_RampTex,float2(NDotV,NDotV));
return MatCapColor*RampColor;
}
ENDCG //end
}
}
FallBack "Diffuse"//用来指定一个后备方案。
}
3.
|