接上文中使用的shader编辑器Matcap,是需要掌握一定的Cg语言和unity shader开发能力的;这方面可以自行检索学习。现将博主和小伙伴处理的模型示图上传供大家参考如果觉得和自己的需求相似可以直接套用。(脚本在文末有分享,特点是光影看起来极其的假,但对电脑性能消耗比较低),在我的理解里,采用这种降低对性能消耗的非静态模型上的光影折射,已经不在受到场景光源的影响,而是模型自身产生的“死光”;对于场景内其他静态的模型可以选择布置光源进行实时烘焙,在项目打包前在对静态模型进行光影烘焙,以在保持场景相对真实性、让人拥有沉浸体验感的同时降低对电脑性能的消耗。
Shader "TJia/Matcap_PBR" {
Properties{
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Albedo Tex", 2D) = "white" {}
_BumpMap("Normal Tex", 2D) = "bump" {}
_BumpValue("Normal Value", Range(0,10)) = 1
_MatCapDiffuse("MatCap Diffuse (RGB)", 2D) = "white" {}
_DiffuseValue("Diffuse Value", Range(0,5)) = 1
_MatCapSpec("MatCap Spec (RGB)", 2D) = "white" {}
_SpecValue("Spec Value", Range(0,2)) = 0
}
Subshader{
Tags { "RenderType" = "Opaque" }
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 TtoV0 : TEXCOORD1;
float3 TtoV1 : TEXCOORD2;
};
uniform float4 _BumpMap_ST;
uniform float4 _MainTex_ST;
v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord,_BumpMap);
TANGENT_SPACE_ROTATION;
o.TtoV0 = normalize(mul(rotation, UNITY_MATRIX_IT_MV[0].xyz));
o.TtoV1 = normalize(mul(rotation, UNITY_MATRIX_IT_MV[1].xyz));
return o;
}
uniform fixed4 _Color;
uniform sampler2D _BumpMap;
uniform sampler2D _MatCapDiffuse;
uniform sampler2D _MainTex;
uniform sampler2D _MatCapSpec;
uniform fixed _BumpValue;
uniform fixed _DiffuseValue;
uniform fixed _SpecValue;
float4 frag(v2f i) : COLOR
{
fixed4 c = tex2D(_MainTex, i.uv.xy);
float3 normal = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
normal.xy *= _BumpValue;
normal.z = sqrt(1.0 - saturate(dot(normal.xy ,normal.xy)));
normal = normalize(normal);
half2 vn;
vn.x = dot(i.TtoV0, normal);
vn.y = dot(i.TtoV1, normal);
vn = vn * 0.5 + 0.5;
fixed4 matcapDiffuse = tex2D(_MatCapDiffuse, vn) * _DiffuseValue;
fixed4 matcapSpec = tex2D(_MatCapSpec, vn) * _SpecValue;
fixed4 finalColor = matcapDiffuse * c * _Color + matcapSpec;
return finalColor;
}
ENDCG
}
}
}
|