UnityShader Basics – Surface Shader

Ox00 Surface Shader Syntax

Three commonly used output data formats:

//Standard output structure of surface shaders is this:

struct SurfaceOutput

{

< p style="background: #e7e6e6"> 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 and above, support 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 highlight output< /span>

struct SurfaceOutputStandardSpecular

{ p>

fixed3 Albedo; // diffuse color

fixed3 Specular; // specular color

fixed3 Normal; // tangent space normal, if written

< span style="font-family:宋体; font-size:12pt"> 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 is coded in CGPROGRAM…ENDCG block,

  • must be in the Subshader block but not in the pass, Surface shader compile itself into multiple Passage.
  • You must use the #pragma surface surfaceFunction lightModel [optionalparams] command to indicate that this is a surface shader.

Split #pragma surface surfaceFunction lightModel [optionalparams]

Required parameters:

< tbody valign="top">

surfaceFunction

format void surf (Input IN, inout SurfaceOutput o), Input custom structure often must include texture coordinates and other independent variables

lightModel

< /colgroup>

Standard lighting

Use SurfaceOutputStandard

StandardSpecular lighting

Use SurfaceOutputStandardSpecular

Lambert and BlinnPhong

does not support physical lighting based on low-end equipment

span>

Input parameter texture coordinates in surfaceFunction must be prefixed with uv or uv2

struct Input

{ < /span>

????float2 uv_MainTex;

????floatX other variables;

}

Other variables

Description

float3 viewDir

View direction In order to calculate time difference, edge lighting and other effects, Input needs to include the view direction span>

float4 color

The color value of each vertex

float4 screenPos

The screen space position, in order to obtain the reflection effect

float3 worldPos

world coordinate space

float3 worldRefl

world space reflection vector, surface shader cannot write o.Normal parameters

float3 worldNormal

world space normal vector, surface shader cannot write o.Normal parameter< /span>

float3 worldRefl;INTERNAL_DATA

< /td>

< p>The world coordinate reflection vector, the surface shader must write the o.Normal parameter. To obtain the reflection vector based on the pixel-by-pixel normal map, please use WorldReflectionVector(IN,o.Normal)

float3 worldNormal;INTERNAL_DATA

world coordinate normal vector, surface shader must Write the o.Normal parameter. To obtain the reflection vector based on the pixel-by-pixel normal map, please use WorldReflectionVector(IN,o.Normal)

Optional parameters: optionalparams

Transparency and alpha testing

Usealpha and alphatest command

?

alpha

alpha :auto

fade -transparency or premultiplied transparency

alpha:blend

Mixed

alpha:fade

Fade out

alpha:premul

Pre-multiplication (make the translucent surface retain proper specular reflection)

alphatest:VariableName

Execute cutout based on the given variable value

keepalpha

Always write 1.0 in the alpha channel

decal:add

Superimpose on the surface and perform superimposition and blending

decal:blend

Alpha Blend

Custom modifier functions are used to calculate and change the input vertex data, or change The final calculated fragment color

< td style="padding-left: 7px; padding-right: 7px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt">

Valid in deferred path

vertex:VertexFunction

Modify calculation p er-vertex data

finalcolor:ColorFunction< /p>

?

finalgbuffer:ColorFunction

finalprepass:ColorFunction

Effective in the forward path. base path

Shadows and Tessellation

addshadow

?

fullforwardshadows

Under the forward path, In addition to supporting the default one-directional light shadow, it can also support point light and spotlight shadows

tessellate:TessFunction

//to be detailed Supplement

Code generation options

Adjust the generated shader Code options to make the shader smaller and faster to load

< tr>

?

exclude_path:deferred,

exclude_path:forward,

exclude_path:prepass

Do not generate passes for the specified rendering path

noshadow

Turn off accepting all shadows p>

noambient

Do not use ambient light and probe

novertexlights

Disable the probe and per-vertex lighting under the forward path

nolightmap

Turn off all lightmaps

nodynlightmap< /span>

Disable runtime dynamic GI

nodirlightmap

No Support directional light light map

nofog

禁用所有内置fog

nometa

不生成meta pass(提取光照贴图和GI信息用)

noforwardadd

禁用forward path中additive pass,支持一个完整的方向光和所有逐顶点/SH计算的光

nolppv

不再支持Light Probe Proxy Volume组件

noshadowmask

禁用阴影遮罩

Miscellaneous options 各种其他选项

softvegetation

only be rendered Soft Vegetation

interpolateview

在顶点着色器中计算视图方向并进行插值;而不是在像素着色器中计算它。这可以使像素着色更快,但会消耗更多的纹理插值器。

halfasview

传递半角向量而不是视图向量到光照函数。半角向量将被逐顶点计算和标准化。这更快,但不完全正确。

approxview

被遗弃了。 用interpolateview

dualforward

基本不用(官方内置shader没搜到)

dithercrossfade

使表面着色器支持抖动效果。然后,您可以将这个着色器应用到GameObjects中,这些GameObjects使用为交叉淡入过渡模式配置的LOD组组件。

?

表面着色器的工作原理

分享图片

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 (SurfaceOutput s, UnityGI gi);forward path,不依赖视图方向

half4 Lighting (SurfaceOutput s, half3 viewDir, UnityGI gi); forward path,依赖视图方向

half4 Lighting_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal); deferred lighting paths

half4 Lighting_PrePass (SurfaceOutput s, half4 light); Use this in light prepass (legacy deferred) lighting paths. in light prepass (legacy deferred) lighting paths

half4 Lighting_GI (SurfaceOutput s, UnityGIInput data, inout UnityGI gi);自定义GI解析光照贴图和探针数据

自定义光照函数:

?

0x03 Example

surfaceFunction

格式void surf (Input IN, inout SurfaceOutput o),Input自定义结构常必须包含纹理坐标和其他自需变量

lightModel

Standard lighting

使用SurfaceOutputStandard

StandardSpecular lighting

使用SurfaceOutputStandardSpecular

Lambert and BlinnPhong

不支持基于物理照明,低端设备用

其他变量

说明

float3 viewDir

视图方向为了计算时差、边缘光照等效果,Input需要包含视图方向< /p>

float4 color

每个顶点颜色值

float4 screenPos

屏幕空间位置,为了获得反射效果

float3 worldPos

世界坐标空间

float3 worldRefl

世界空间反射向量,surface shader不能写入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)

Transparency and alpha testing

使用alpha and alphatest指令

?

alpha

alpha:auto

fade-transparency或premultiplied transparency

alpha:blend

混合

alpha:fade

淡出

alpha:premul

预乘(使半透明表面保留适当的镜面反射)

alphatest:VariableName

基于给定变量值执行cutout

keepalpha

始终在alpha channel写入1.0

decal:add

叠加在表面上,并执行叠加混合

decal:blend

Alpha混合

Custom modifier functions用于计算更改输入的顶点数据,或更改最终计算的片元色

vertex:VertexFunction

修改计算per-vertex data

finalcolor:ColorFunction

?

finalgbuffer:ColorFunction

在deferred path生效

finalprepass:ColorFunction

在forward path生效。 base path

Shadows and Tessellation 阴影和网格细分

addshadow

?

fullforwardshadows

在forward path下,除了支持默认的一个方向光阴影,还可支持点光和聚光阴影

tessellate:TessFunction

//待详细补充

Code generation options

调整生成着色器代码选项,使得shader更小加载更快

?

exclude_path:deferred,

exclude_path:forward,

exclude_path:prepass

不给指定的渲染路径生成passes

noshadow

关闭接受所有阴影

noambient

不使用环境光和探头

novertexlights

在forward path下禁用探头和逐顶点光照

nolightmap

关闭所有光照贴图

nodynlightmap

禁用运行时动态GI

nodirlightmap

不支持方向光光照贴图

nofog

禁用所有内置fog

nometa

不生成meta pass(提取光照贴图和GI信息用)

noforwardadd

禁用forward path中additive pass,支持一个完整的方向光和所有逐顶点/SH计算的光

nolppv

不再支持Light Probe Proxy Volume组件

noshadowmask

禁用阴影遮罩

Miscellaneous options 各种其他选项

softvegetation

only be rendered Soft Vegetation

interpolateview

在顶点着色器中计算视图方向并进行插值;而不是在像素着色器中计算它。这可以使像素着色更快,但会消耗更多的纹理插值器。

halfasview

传递半角向量而不是视图向量到光照函数。半角向量将被逐顶点计算和标准化。这更快,但不完全正确。

approxview

被遗弃了。 用interpolateview

dualforward

基本不用(官方内置shader没搜到)

dithercrossfade

使表面着色器支持抖动效果。然后,您可以将这个着色器应用到GameObjects中,这些GameObjects使用为交叉淡入过渡模式配置的LOD组组件。

alpha

alpha:auto

exclude_path:deferred,

exclude_path:forward,

exclude_path:prepass

UnityLambertLight

基于非物理的漫反射

LightingLambert

//..待补充

LightingLambert_Deferred

//..

LightingLambert_PrePass

//..

UnityBlinnPhongLight

基于非物理的镜面高光

LightingBlinnPhong

//..

LightingBlinnPhong_Deferred

//..

LightingBlinnPhong_PrePass

//..

LightingLambert_GI

//..

LightingBlinnPhong_GI

//..

Leave a Comment

Your email address will not be published.