其实一提到Shader 这个概念,对于大家来讲,可以说熟悉而陌生,熟悉是因为这个名词出现在游戏,出现在图形学中,是比较多的;陌生是因为我们很多人只能止步于知道它的存在,但是没有深究它的功能和用处。
1、Shader 简述
Shader(着色器):实际上是控制GPU的一堆指令集,它负责将输入Mesh(网格)以指定方式和输入的贴图或者颜色等组合作用,然后输出。
2、Shader 三种基本类型
2.1、固定功能着色器(Fixed Function Shader)
适用于所有显卡,常用于高级Shader在老显卡无法显示时Fallback
Shader "Examples/Alpha Blended Textures" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
}
SubShader {
Pass {
// Apply base texture
// 应用主纹理
SetTexture [_MainTex] {
combine texture
}
// Blend in the alpha texture using the lerp operator
// 使用差值操作混合Alpha纹理
SetTexture [_BlendTex] {
combine texture lerp (texture) previous
}
}
}
}
2.2、表面着色器(Surface Shader)
易于编写光照的Shader,受光照和阴影的影响,使用基于物理的照明模型:
Shader "Example/Diffuse Simple" {
SubShader {
Tags { "RenderType"="Opaque" }
CGPROGRAM#pragmasurface surf Lambertstruct Input
{
float4 color : COLOR;
};
void surf(Input IN, inout SurfaceOutput o)
{
o.Albedo =1;
}
ENDCG
}
Fallback "Diffuse" }
2.3、顶点着色器&片段着色器(Vertex Shader & Fragment Shader)
Shader "Custom/NewShader"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{ Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
struct Input { float2 uv_MainTex; };
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb; o.Alpha = c.a; }
ENDCG
}
FallBack "Diffuse"
}
网友评论