Ox00 Surface Shader Syntax
Three common output data format:
//Standard output structure of surface shaders is this:
struct SurfaceOutput
{
fixed3 Albedo; // diffuse color
fixed3 Normal; // tangent space normal, if written
fixed3 Emission;
half Specular; // specular power in 0..1 range
fixed Gloss; // specular intensity
fixed Alpha; // alpha for transparencies
};
// Unity 5 or later, support for physically based lighting models.
struct SurfaceOutputStandard
{
fixed3 Albedo; // base (diffuse or specular) color
fixed3 Normal; // tangent space normal, if written
half3 Emission;
half Metallic; // 0=non-metal, 1=metal
half Smoothness; // 0=rough, 1=smooth
half Occlusion; // occlusion (default 1)
fixed Alpha; // alpha for transparencies
};
// specular light output
struct SurfaceOutputStandardSpecular
{
fixed3 Albedo; // diffuse color
fixed3 Specular; // specular color
fixed3 Normal; // tangent space normal, if written
half3 Emission;
half Smoothness; // 0=rough, 1=smooth
half Occlusion; // occlusion (default 1)
fixed Alpha; // alpha for transparencies
};
0x01 Surface Shader compile directives
Surface shader CGPROGRAM ... ENDCG encoded within the block,
- Must not block the Subshader inside pass, Surface shader themselves compiled into a plurality of channels .
- Must #pragma surface surfaceFunction lightModel [optionalparams] command to indicate that this is a surface shader.
Split #pragma surface surfaceFunction lightModel [optionalparams]
Necessary parameters:
surfaceFunction |
Format void surf (Input IN, inout SurfaceOutput o), Input structures must often contain custom texture coordinates and other variables required from |
||||||
lightModel |
|
Input parameters surfaceFunction the texture coordinates must be prefixed with uv or uv2
struct Input
{
float2 uv_MainTex;
floatX other variables;
}
Other variables |
Explanation |
float3 viewDir |
To calculate the direction of view of the time difference, the edge lighting and other effects, Input need to include view direction |
float4 color |
Each vertex color values |
float4 screenPos |
Screen space positions, in order to obtain the reflection effect |
float3 worldPos |
World space |
float3 worldRefl |
World Space reflection vector, surface shader parameters can not be written o.Normal |
float3 worldNormal |
世界空间法向量,surface shader不能写入o.Normal参数 |
float3 worldRefl;INTERNAL_DATA |
世界坐标反射向量,surface shader必须写入o.Normal参数。基于逐像素法线贴图获得反射向量,请使用WorldReflectionVector(IN,o.Normal) |
float3 worldNormal;INTERNAL_DATA |
世界坐标法线向量,surface shader必须写入o.Normal参数。基于逐像素法线贴图获得反射向量,请使用WorldReflectionVector(IN,o.Normal) |
可选参数:optionalparams
Transparency and alpha testing 使用alpha and alphatest指令 |
|
|||||||||||||||||||||||||||
Custom modifier functions用于计算更改输入的顶点数据,或更改最终计算的片元色 |
|
|||||||||||||||||||||||||||
Shadows and Tessellation 阴影和网格细分 |
|
|||||||||||||||||||||||||||
Code generation options 调整生成着色器代码选项,使得shader更小加载更快 |
|
|||||||||||||||||||||||||||
Miscellaneous options 各种其他选项 |
|
表面着色器的工作原理
0x02 Surface Shader Light Model
Lighting.cginc
UnityLambertLight |
基于非物理的漫反射 |
LightingLambert |
|
LightingLambert_Deferred |
//.. |
LightingLambert_PrePass |
//.. |
UnityBlinnPhongLight |
基于非物理的镜面高光 |
LightingBlinnPhong |
//.. |
LightingBlinnPhong_Deferred |
//.. |
LightingBlinnPhong_PrePass |
//.. |
LightingLambert_GI |
//.. |
LightingBlinnPhong_GI |
//.. |
half4 Lighting<Name> (SurfaceOutput s, UnityGI gi);forward path,不依赖视图方向
half4 Lighting<Name> (SurfaceOutput s, half3 viewDir, UnityGI gi); forward path,依赖视图方向
half4 Lighting<Name>_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal); deferred lighting paths
half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light); Use this in light prepass (legacy deferred) lighting paths. in light prepass (legacy deferred) lighting paths
half4 Lighting<Name>_GI (SurfaceOutput s, UnityGIInput data, inout UnityGI gi);自定义GI解析光照贴图和探针数据
自定义光照函数:
0x03 Example