效果图
shader源码
Shader "ASESampleShaders/XRay"
{
Properties
{
_ASEOutlineWidth( "Outline Width", Float ) = 0
_TextureSample0("Texture Sample 0", 2D) = "white" {}
_XRayPower("XRayPower", Float) = 0
_XRayColor("XRayColor", Color) = (0,0,0,0)
_XRayScale("XRayScale", Float) = 0
_XRayBias("XRayBias", Float) = 0
_XRayIntensity("XRayIntensity", Float) = 0
[HideInInspector] _texcoord( "", 2D ) = "white" {}
[HideInInspector] __dirty( "", Int ) = 1
}
SubShader
{
Pass
{
ColorMask 0
ZWrite On
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent+0"}
ZWrite Off
// ZTest Always意味着该Pass始终通过深度测试,绘制在最前面
ZTest Always
Cull Back
CGPROGRAM
#pragma target 3.0
#pragma surface outlineSurf Outline nofog alpha:fade keepalpha noshadow noambient novertexlights nolightmap nodynlightmap nodirlightmap nometa noforwardadd vertex:outlineVertexDataFunc
struct Input
{
float3 worldPos;
float3 worldNormal;
INTERNAL_DATA
};
uniform float _XRayBias;
uniform float _XRayScale;
uniform float _XRayPower;
uniform float4 _XRayColor;
uniform float _XRayIntensity;
float _ASEOutlineWidth;
void outlineVertexDataFunc( inout appdata_full v, out Input o )
{
UNITY_INITIALIZE_OUTPUT( Input, o );
v.vertex.xyz += ( v.normal * _ASEOutlineWidth );
}
// 自定义的光照模型,直接输出了黑色,以及透明度用的s.Alpha
inline half4 LightingOutline( SurfaceOutput s, half3 lightDir, half atten ) { return half4 ( 0,0,0, s.Alpha); }
void outlineSurf( Input i, inout SurfaceOutput o )
{
float3 ase_worldPos = i.worldPos;
float3 ase_worldViewDir = normalize( UnityWorldSpaceViewDir( ase_worldPos ) );
float3 ase_worldNormal = WorldNormalVector( i, float3( 0, 0, 1 ) );
float fresnelNdotV4 = dot( ase_worldNormal, ase_worldViewDir );
// 菲涅尔得到模型边缘
float fresnelNode4 = ( _XRayBias + _XRayScale * pow( 1.0 - fresnelNdotV4, _XRayPower ) );
o.Emission = ( fresnelNode4 * _XRayColor ).rgb;
o.Alpha = ( fresnelNode4 * (_XRayColor).a * _XRayIntensity );
o.Normal = float3(0,0,-1);
}
ENDCG
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry+1" }
Cull Back
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma target 3.0
#pragma surface surf Standard keepalpha addshadow fullforwardshadows vertex:vertexDataFunc
struct Input
{
float2 uv_texcoord;
};
uniform sampler2D _TextureSample0;
uniform float4 _TextureSample0_ST;
void vertexDataFunc( inout appdata_full v, out Input o )
{
UNITY_INITIALIZE_OUTPUT( Input, o );
v.vertex.xyz += 0;
}
void surf( Input i , inout SurfaceOutputStandard o )
{
float2 uv_TextureSample0 = i.uv_texcoord * _TextureSample0_ST.xy + _TextureSample0_ST.zw;
o.Albedo = tex2D( _TextureSample0, uv_TextureSample0 ).rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "Diffuse"
CustomEditor "ASEMaterialInspector"
}
原理解析
整个核心的效果分为两个pass完成,步骤如下:
- 第一个pass,对半透明的XRay猴子脑袋进行绘制。需要注意的是,关掉深度写入,这是半透明绘制的常规操作;把ZTest设置为Always,这样做使得绘制的时候永远都会通过深度测试,永远绘制在所有物体的前面。这一步的绘制结果如下面的第一张图。
- 第二个pass,常规的对猴子脑袋进行绘制。这一步完成之后,就得到了最终的效果。