核心
运用到核心知识:边缘光,描边外拓,颜色阶级
效果图
Shader "Unlit/019"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("漫反射", Color) = (1,1,1,1)
_Outline ("外廓粗细", Range(0,0.05)) = 0.02
_OutlineColor ("外廓颜色", Color) = (0,0,0,0)
_Steps("颜色阶级", Range(1,30)) = 1
_ToonEffect("卡通效果", Range(0,1) ) = 0.5
_RampTex("渐进纹理",2D)="while"{}
_RimColor("边缘光", Color) = (1,1,1,1)
_RimPower("边缘光强度",Range(0,3)) =1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
//定义Pass名,用于重复使用该Pass
Name "Outline"
//渲染前面
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Outline;
fixed4 _OutlineColor;
struct v2f
{
float4 vertex :SV_POSITION;
};
v2f vert(appdata_base v)
{
v2f o;
//描边=顶点在法线上偏移 * 颜色
//物体空间法线外拓
//v.vertex.xyz += v.normal * _Outline;
//o.vertex = UnityObjectToClipPos(v.vertex);
//视角空间法线外拓
//float4 pos = mul(UNITY_MATRIX_V,mul(unity_ObjectToWorld,v.vertex));
//float3 normal = normalize (mul((float3x3)UNITY_MATRIX_IT_MV,v.normal));
//pos = pos + float4(normal,0) * _Outline;
//o.vertex = mul(UNITY_MATRIX_P,pos);
//裁剪空间法线外拓
o.vertex = UnityObjectToClipPos(v.vertex);
float3 normal = normalize (mul((float3x3)UNITY_MATRIX_IT_MV,v.normal));
float2 viewNoraml = TransformViewToProjection(normal.xy);
o.vertex.xy += viewNoraml * _Outline;
return o;
}
float4 frag(v2f i):SV_Target
{
return _OutlineColor;
}
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;
float _Steps;
float _ToonEffect;
sampler2D _RampTex;
fixed4 _RimColor;
float _RimPower;
v2f vert (appdata_base v)
{
v2f o;
//顶点位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//世界坐标
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
//纹理坐标缩放偏移
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);
//视角方向
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//半兰伯特模型=映射值为正数(表面法线方向 · 光源方向)
float difLight = dot(worldLightDir,i.worldNormal)*0.5+0.5;
////渐进纹理采样
//fixed4 rampColor = tex2D(_RampTex, fixed2(difLight,difLight));
////漫反射光=入射光线强度*纹素值*材质的漫反射系数*渐进纹理
//fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * rampColor;
//颜色平滑在【0,1】之间
difLight = smoothstep(0,1,difLight);
//颜色离散化
float toon = floor(difLight * _Steps) / _Steps;
difLight = lerp(difLight,toon,_ToonEffect);
//漫反射光=入射光线强度*纹素值*材质的漫反射系数*半兰伯特模型
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * difLight;
//边缘光
float rim = 1-dot(i.worldNormal,viewDir);
fixed3 rimColor = _RimColor * pow(rim,1/_RimPower);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse + rimColor;
return fixed4(color,1);
}
ENDCG
}
}
}
- 带入XRay Shader
Shader "Unlit/019"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("漫反射", Color) = (1,1,1,1)
_Outline ("外廓粗细", Range(0,0.05)) = 0.02
_OutlineColor ("外廓颜色", Color) = (0,0,0,0)
_Steps("颜色阶级", Range(1,30)) = 1
_ToonEffect("卡通效果", Range(0,1) ) = 0.5
_RampTex("渐进纹理",2D)="while"{}
_RimColor("边缘光", Color) = (1,1,1,1)
_RimPower("边缘光强度",Range(0,3)) =1
_XRayColor("XRay光", Color) = (1,1,1,1)
_XRayPower("XRay光强度",Range(0,3)) =1
}
SubShader
{
Tags {"Queue"="Geometry+1000" "RenderType"="Opaque" }
LOD 100
Pass
{
//关闭阴影
Tags{"ForceNoShadowCastiong"="true"}
//开启因子混合
Blend SrcAlpha One
//关闭深度写入
ZWrite Off
//深度测试开启为 大于
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;
//顶点位置
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
{
//定义Pass名,用于重复使用该Pass
Name "Outline"
//渲染前面
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Outline;
fixed4 _OutlineColor;
struct v2f
{
float4 vertex :SV_POSITION;
};
v2f vert(appdata_base v)
{
v2f o;
//描边=顶点在法线上偏移 * 颜色
//物体空间法线外拓
//v.vertex.xyz += v.normal * _Outline;
//o.vertex = UnityObjectToClipPos(v.vertex);
//视角空间法线外拓
//float4 pos = mul(UNITY_MATRIX_V,mul(unity_ObjectToWorld,v.vertex));
//float3 normal = normalize (mul((float3x3)UNITY_MATRIX_IT_MV,v.normal));
//pos = pos + float4(normal,0) * _Outline;
//o.vertex = mul(UNITY_MATRIX_P,pos);
//裁剪空间法线外拓
o.vertex = UnityObjectToClipPos(v.vertex);
float3 normal = normalize (mul((float3x3)UNITY_MATRIX_IT_MV,v.normal));
float2 viewNoraml = TransformViewToProjection(normal.xy);
o.vertex.xy += viewNoraml * _Outline;
return o;
}
float4 frag(v2f i):SV_Target
{
return _OutlineColor;
}
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;
float _Steps;
float _ToonEffect;
sampler2D _RampTex;
fixed4 _RimColor;
float _RimPower;
v2f vert (appdata_base v)
{
v2f o;
//顶点位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//世界坐标
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
//纹理坐标缩放偏移
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);
//视角方向
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
//半兰伯特模型=映射值为正数(表面法线方向 · 光源方向)
float difLight = dot(worldLightDir,i.worldNormal)*0.5+0.5;
////渐进纹理采样
//fixed4 rampColor = tex2D(_RampTex, fixed2(difLight,difLight));
////漫反射光=入射光线强度*纹素值*材质的漫反射系数*渐进纹理
//fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * rampColor;
//颜色平滑在【0,1】之间
difLight = smoothstep(0,1,difLight);
//颜色离散化
float toon = floor(difLight * _Steps) / _Steps;
difLight = lerp(difLight,toon,_ToonEffect);
//漫反射光=入射光线强度*纹素值*材质的漫反射系数*半兰伯特模型
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * difLight;
//边缘光
float rim = 1-dot(i.worldNormal,viewDir);
fixed3 rimColor = _RimColor * pow(rim,1/_RimPower);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse + rimColor;
return fixed4(color,1);
}
ENDCG
}
}
}