边缘光材质
边缘光材质原理和xray材质相类似,shader代码也就长得几乎一样了,一般游戏中做一些角色的背光效果,或者宝石材质(如下图所示),总之看怎么灵活运用了。
shader代码实现
VF版本代码01:
Shader "PengLu/RimLightSimpleVF" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_RimColor("RimColor",Color) = (0,1,1,1)
_RimPower ("Rim Power", Range(0.1,8.0)) = 1.0
}
SubShader {
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
LOD 200
Pass
{
// Blend SrcAlpha One
// ZWrite off
Lighting off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _RimColor;
float _RimPower;
sampler2D _MainTex;
float4 _MainTex_ST;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 color:COLOR;
float4 normal:NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float2 texcoord : TEXCOORD0;
float4 color:COLOR;
} ;
v2f vert (appdata_t v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
float rim = 1 - saturate(dot(viewDir,v.normal ));
o.color = _RimColor*pow(rim,_RimPower);
return o;
}
float4 frag (v2f i) : COLOR
{
fixed4 col = tex2D(_MainTex, i.texcoord);
return i.color+col;
}
ENDCG
}
}
FallBack "Diffuse"
}
VF版本代码01效果:
VF版本代码02:法线贴图
Shader "PengLu/RimLightBumpVF"{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_RimColor("RimColor",Color) = (0,1,1,1)
_RimPower ("Rim Power", Range(0.1,8)) = 1.0
}
SubShader {
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
LOD 200
Pass
{
// Blend SrcAlpha One
// ZWrite off
// Lighting off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _RimColor;
sampler2D _BumpMap;
sampler2D _MainTex;
float _RimPower;
float4 _BumpMap_ST;
float4 _MainTex_ST;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewDir:TEXCOORD1;
float3 tangent:TEXCOORD2;
float3 binormal:TEXCOORD3;
float3 normal:TEXCOORD4;
float2 mainuv:TEXCOORD5;
} ;
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv = TRANSFORM_TEX( v.texcoord, _BumpMap );
o.mainuv = TRANSFORM_TEX( v.texcoord, _MainTex );
o.tangent=v.tangent.xyz;
o.binormal=cross(v.normal,v.tangent)*v.tangent.w;
o.normal=v.normal;
o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
return o;
}
float4 frag (v2f i) : COLOR
{
float3x3 rotation=float3x3 (i.tangent.xyz,i.binormal,i.normal);
// float4 packedN=tex2D(_BumpMap,i.uv);
// float3 N=float3(2.0*packedN.wy-1,1.0);
// N.z=sqrt(1-N.x*N.x-N.y*N.y);
float4 col =tex2D(_MainTex,i.mainuv);
float3 N = UnpackNormal(tex2D(_BumpMap,i.uv));
N=normalize(mul(N,rotation));//把法线体贴图中的法线转换到世界坐标中
float rim=1 - saturate(dot(i.viewDir,N));
float4 c=_RimColor*pow(rim,_RimPower);
c+=col;
return c;
}
ENDCG
}
}
FallBack "PengLu/XRaySimpleVF"
}
VF版本代码02效果
Surf版本代码03:
Shader "PengLu/RimLightSimpleSurf"{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_RimColor("RimColor",Color) = (0,1,1,1)
_RimPower ("Rim Power", Range(0.1,8)) = 1.0
}
SubShader {
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
LOD 200
// Blend SrcAlpha One
// Zwrite off
CGPROGRAM
#pragma surface surf Lambert
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _RimColor;
float _RimPower;
struct Input {
float2 uv_MainTex;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o) {
float4 col =tex2D(_MainTex,IN.uv_MainTex);
o.Albedo =0;
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
float4 c=_RimColor * pow (rim, _RimPower);
// o.Alpha=c.a;
o.Emission =c.rgb+col.rgb;
}
ENDCG
}
FallBack "PengLu/XRaySimpleVF"
}
Surf版本代码03效果
Surf版本代码04:法线贴图
Shader "PengLu/RimLightBumpSurf"{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_RimColor("RimColor",Color) = (1,1,0,1)
_RimPower ("Rim Power", Range(0.1,8)) = 3.0
}
SubShader {
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
LOD 200
// Blend SrcAlpha One
// Zwrite off
CGPROGRAM
#pragma surface surf Lambert
float4 _RimColor;
float _RimPower;
sampler2D _MainTex;
sampler2D _BumpMap;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
float4 c=_RimColor * pow (rim, _RimPower);
// o.Alpha=c.a;
o.Emission =c.rgb+tex2D(_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
FallBack "PengLu/XRaySimpleSurf"
}
Surf版本代码03效果