Unity内置Shader解读10——Self-Illumin/
2021-05-12 本文已影响0人
ShawnWeasley
1.Shader在什么情况下使用
Self-Illumin(自身照明),即自发光材质,乍看一下这玩意还真不太熟,仔细研究了下源码发现其实也就是最简单的颜色混合。本系列主要是针对不愿意用光照的系统自行发光,通过光照贴图来渲染颜色,这样可以避免复杂的光照困扰。
2.Shader的价值(用的多不多),Shader的难度
个人几乎没见过用官方的这个shader,难度低。
3.代码详细注释
Shader "Legacy Shaders/Self-Illumin/Bumped Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Illum ("Illumin (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Emission ("Emission (Lightmapper)", Float) = 1.0
}
CGINCLUDE
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Illum;
fixed4 _Color;
fixed _Emission;
struct Input {
float2 uv_MainTex;
float2 uv_Illum;
float2 uv_BumpMap;
};
void surf (Input IN, inout SurfaceOutput o) {
//主纹理采样
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
//混合_Color
fixed4 c = tex * _Color;
//设置Albedo颜色
o.Albedo = c.rgb;
//自发光颜色由纹理颜色和_Illum采样颜色的a通道混合
o.Emission = c.rgb * tex2D(_Illum, IN.uv_Illum).a;
//主要用于meta pass,用于烘焙Lightmap
//详细看这篇https://blog.csdn.net/u012871784/article/details/81179432
#if defined (UNITY_PASS_META)
//让自发光的强度与_Emission做倍率乘法
o.Emission *= _Emission.rrr;
#endif
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
//两个SubShader按顺序执行,执行成功则后面的不执行,适配低端机型
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf Lambert
#pragma target 3.0
ENDCG
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf Lambert nodynlightmap //禁用动态光照贴图
ENDCG
}
FallBack "Legacy Shaders/Self-Illumin/Diffuse"
//详细看这篇https://blog.csdn.net/u012871784/article/details/81179432
CustomEditor "LegacyIlluminShaderGUI"
}
4.Shader编写思路,用到的知识点
本系列烘焙的部分其实还比较复杂,可以看这篇