Shader "Example2"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_MatCap ("Mat (RGB)", 2D) = "white" {}
_Strongth ("Strongth", Range(0, 2)) = 1
_Rotate ("Rotate", float) = 0
_LightTex ("Light Texture(A)", 2D) = "black" {}
_MaskTex ("Mask Texture(A)", 2D) = "black" {}
_Brightness ("Brightness", Range(0, 5)) = 1
_uSpeed ("U Speed", float) = 0
_vSpeed ("V Speed", float) = 0
}
SubShader {
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _MatCap;
float _Strongth;
float _Rotate;
float4 _Color;
float4 _MainTex_ST;
uniform float4 _TimeEditor;
sampler2D _LightTex;
sampler2D _MaskTex;
float4 _LightTex_ST;
float _uSpeed;
float _vSpeed;
float _Brightness;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 cap : TEXCOORD1;
float2 light : TEXCOORD2;
};
float2 rotate2d (in float2 v, in float a) {
float sinA = sin(a);
float cosA = cos(a);
// 就是旋转矩阵
return float2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);
}
float2x2 rotmatrix (in float a) {
float sinA = sin(a);
float cosA = cos(a);
return float4(cosA, sinA, -sinA, cosA);
}
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
half2 capCoord;
/ Matrices in unity are column major
// 这里就是把v.normal变换到view空间
//capCoord.x = dot(UNITY_MATRIX_IT_MV[0].xyz, v.normal);
//capCoord.y = dot(UNITY_MATRIX_IT_MV[1].xyz, v.normal);
// 这样是同上面的一样的结果
capCoord = mul(UNITY_MATRIX_IT_MV, v.normal).xy;
// 这个是的做法就相当于把v.normal 乘上了一个_Object2World的逆转置矩阵
//capCoord = mul(v.normal, _World2Object).xy;
// 如果是v.normal是单位向量,那么,dot的结果的区间就是[-1, 1]
// 这里就是把[-1, 1] 变换到[0, 1]
o.cap = capCoord * 0.5 + 0.5;
//o.cap = mul(capCoord - float2(0.5, 0.5), rotmatrix(_Rotate)) + float2(0.5, 0.5);
//o.cap = mul( rotmatrix(_Rotate), o.cap).xy;
// 先把UV变换到[-0.5, 0.5], 再旋转,旋转完之后,再变换到[0, 1]
// 这样做的效果才是正确的,画图就知道了
o.cap = (o.cap + 0.5) - 1;
o.cap = rotate2d(o.cap, _Rotate);
o.cap = o.cap + 0.5;
o.light = TRANSFORM_TEX(v.texcoord, _LightTex);// * uvSpeed(_uSpeed, _vSpeed);
float4 moveUv = _Time + _TimeEditor;
o.light = (o.light + moveUv.g * float2(_uSpeed, _vSpeed));
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 tex = tex2D(_MainTex, i.uv);
fixed4 mc = tex2D(_MatCap, i.cap);
fixed4 lightC = tex2D(_LightTex, i.light) * _Brightness;
fixed4 c = (tex + (((mc * 2.0) - 1.0) * _Color) * _Strongth);
c.rgb += lightC.rgb * mask.rgb;
return c;
}
ENDCG
}
}
}
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Color", Color) = (1, 1, 1, 1)
_MatCap ("Mat (RGB)", 2D) = "white" {}
_Strongth ("Strongth", Range(0, 2)) = 1
_Rotate ("Rotate", float) = 0
_LightTex ("Light Texture(A)", 2D) = "black" {}
_MaskTex ("Mask Texture(A)", 2D) = "black" {}
_Brightness ("Brightness", Range(0, 5)) = 1
_uSpeed ("U Speed", float) = 0
_vSpeed ("V Speed", float) = 0
}
SubShader {
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _MatCap;
float _Strongth;
float _Rotate;
float4 _Color;
float4 _MainTex_ST;
uniform float4 _TimeEditor;
sampler2D _LightTex;
sampler2D _MaskTex;
float4 _LightTex_ST;
float _uSpeed;
float _vSpeed;
float _Brightness;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 cap : TEXCOORD1;
float2 light : TEXCOORD2;
};
float2 rotate2d (in float2 v, in float a) {
float sinA = sin(a);
float cosA = cos(a);
// 就是旋转矩阵
return float2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);
}
float2x2 rotmatrix (in float a) {
float sinA = sin(a);
float cosA = cos(a);
return float4(cosA, sinA, -sinA, cosA);
}
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
half2 capCoord;
/ Matrices in unity are column major
// 这里就是把v.normal变换到view空间
//capCoord.x = dot(UNITY_MATRIX_IT_MV[0].xyz, v.normal);
//capCoord.y = dot(UNITY_MATRIX_IT_MV[1].xyz, v.normal);
// 这样是同上面的一样的结果
capCoord = mul(UNITY_MATRIX_IT_MV, v.normal).xy;
// 这个是的做法就相当于把v.normal 乘上了一个_Object2World的逆转置矩阵
//capCoord = mul(v.normal, _World2Object).xy;
// 如果是v.normal是单位向量,那么,dot的结果的区间就是[-1, 1]
// 这里就是把[-1, 1] 变换到[0, 1]
o.cap = capCoord * 0.5 + 0.5;
//o.cap = mul(capCoord - float2(0.5, 0.5), rotmatrix(_Rotate)) + float2(0.5, 0.5);
//o.cap = mul( rotmatrix(_Rotate), o.cap).xy;
// 先把UV变换到[-0.5, 0.5], 再旋转,旋转完之后,再变换到[0, 1]
// 这样做的效果才是正确的,画图就知道了
o.cap = (o.cap + 0.5) - 1;
o.cap = rotate2d(o.cap, _Rotate);
o.cap = o.cap + 0.5;
o.light = TRANSFORM_TEX(v.texcoord, _LightTex);// * uvSpeed(_uSpeed, _vSpeed);
float4 moveUv = _Time + _TimeEditor;
o.light = (o.light + moveUv.g * float2(_uSpeed, _vSpeed));
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 tex = tex2D(_MainTex, i.uv);
fixed4 mc = tex2D(_MatCap, i.cap);
fixed4 lightC = tex2D(_LightTex, i.light) * _Brightness;
fixed4 mask = tex2D(_MaskTex, i.uv);
// ((mc * 2.0) - 1.0)的意思就是把mc从区间[0, 1]变换到[-1, 1]fixed4 c = (tex + (((mc * 2.0) - 1.0) * _Color) * _Strongth);
c.rgb += lightC.rgb * mask.rgb;
return c;
}
ENDCG
}
}
}