核心
利用上章所用的邊緣光加上深度測(cè)試設(shè)置與透明混合
效果展示
效果展示
Shader "Unlit/019"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("漫反射", Color) = (1,1,1,1)
_XRayColor("XRay光", Color) = (1,1,1,1)
_XRayPower("XRay光強(qiáng)度",Range(0,3)) =1
}
SubShader
{
Tags {"Queue"="Geometry+1000" "RenderType"="Opaque" }
LOD 100
Pass
{
//關(guān)閉陰影
Tags{"ForceNoShadowCastiong"="true"}
//開(kāi)啟因子混合
Blend SrcAlpha One
//關(guān)閉深度寫(xiě)入
ZWrite Off
//深度測(cè)試開(kāi)啟為 大于
ZTest Greater
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _XRayColor;
float _XRayPower;
struct v2f
{
float4 vertex : SV_POSITION;
float3 viewDir : TEXCOORD0;
float3 normal : TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
//頂點(diǎn)位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法線方向
o.normal = v.normal;
//視角方向
o.viewDir = ObjSpaceViewDir(v.vertex);
return o;
}
fixed4 frag(v2f i): SV_Target
{
float3 normal = normalize(i.normal);
float3 viewDir = normalize(i.viewDir);
float rim = 1 - dot(normal,viewDir);
return _XRayColor * pow(rim,1/ _XRayPower);
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//引入光照
#include "Lighting.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Diffuse;
v2f vert (appdata_base v)
{
v2f o;
//頂點(diǎn)位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法線方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//世界坐標(biāo)
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
//紋理坐標(biāo)縮放偏移
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// 紋理采樣
fixed4 albedo = tex2D(_MainTex, i.uv);
//光源方向
fixed3 worldLightDir = UnityWorldSpaceLightDir (i.worldPos);
//漫反射光=入射光線強(qiáng)度*紋素值*材質(zhì)的漫反射系數(shù)* 映射值為正數(shù)(表面法線方向 · 光源方向)
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * (dot(worldLightDir,i.worldNormal)*0.5+0.5);
//環(huán)境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse;
return fixed4(color,1);
}
ENDCG
}
}
}