@[toc](Unity Shader (二) 最基础的Diffuse(漫反射)和Normal(法线贴图)样例)
1.Shader代码
Shader "Custom/BumpShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
//什么是"bump"{},通过实验可以知道,它是指蓝色“bump” (RGBA: 0.5,0.5,1,0.5)
_Normal("Normal", 2D) = "bump"{}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
/*其中这里的光照模型是Lambert模型,也就是环境光+散射光+反射高光+放射光*/
#pragma surface surf Standard Lambert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Normal;
struct Input
{
float2 uv_MainTex;
float2 uv_Normal;
};
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
//利用UnpackNormal(tex2D(_Normal, IN.uv_Normal))方法获取解包后的法方向
o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
}
ENDCG
}
FallBack "Diffuse"
}