T.TAO
返回博客
/2 min read/Technical Art

Unity Shader #2.1 基础指令

#Unity#Shader#TechnicalArt

In the process of the render pipeline we do have several other commands for some options.

Face Culling

The backside of a surface is by default culled during the process.

By addingCull Offbefore CGPROGRAM, we can turn off the backside face culling.

Plain TextPass
{
    Cull Off
    CGPROGRAM
    //...
    ENDCG
}

The other options here:

-Cull Off: turn off the backside face culling.

-Cull Back: [Default] cull the backside of a face.

-Call Front: cull the frontside of a face.

If we want to set the cull mode in the inspector, we can add a line in the Properties part, and then useCull[_CullMode].

Plain TextProperties
{
    [Enum(UnityEngine.Rendering.CullMode)]_CullMode("CullMode", float) = 2
}

Pass
{
    Cull [_CullMode]
    CGPROGRAM
    //...
    ENDCG
}

CullMode:

  • 0: Off

  • 1: Front

  • 2: Back

Time

We can use the_Timeparameter to use the time value.

_Time.x/y/z/w:t/20, t, 2t, 3t

_SinTime.w:the cos value of the time

_CosTime.w:the cos value of the time

Alpha Test

In the fragment shader, theclip(n)function has the following behavior:

n < 0:hide the fragment

n >= 0:show the fragment.

We can use the clip() function to realize some resolution/blend/wave effect, and we will do that in future notes.

For blending/semi-transparency, we need to add aBlendcommand like theCullabove before the CGPROGRAM.

Plain TextPass
{
    Blend SrcAlpha OneMinusSrcAlpha
    CGPROGRAM
    //...
    ENDCG
}

Besides OneMinusSrcAlpha, we do have other blending modes. Another frequently used mode is theBlend SrcAlpha One.

Also, remember to turn offZWrite. Otherwise there might be some obvious and serious issues with sorting/rendering order.

Plain TextPass
{
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    CGPROGRAM
    //...
    ENDCG
}

In some cases we probably need ZWrite to be on, and we will cover these cases in later notes.

Recall the semi-transparent objects should be after 2500 in Render Queue(by default 3000), so we add aTagsinSubShaderpart before anyPass's.

Plain TextSubShader
{
    Tags{"Queue" = "Transparent"}
    Pass
    {
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
        //...
        ENDCG
    }
}

Just one more thing to add, if you want to manually set the alpha value of a color (or a float), always make sure that the alpha value does fall in the range of (0,1). You can always use thesaturate()function as a safe guard.saturate()makes sure that it does not return any value out of range(0,1).

Plain Texthalf alpha = saturate(tex2D(_MainTex, i.uv).r * _Color.a * _Emiss);

本系列文章

Unity Shader
  1. 01Unity Shader #0 数学基础
  2. 02Unity Shader #1 渲染管线
  3. 03Unity Shader #2 代码基础
  4. 04Unity Shader #2.1 基础指令
  5. 05Unity Shader #3 升级到 URP
  6. 06Unity Shader #4 在 UV 上画图形
  7. 07Unity Shader #5 边缘光
  8. 08Unity Shader #6 扫描与全息
  9. 09Unity Shader #7 日式卡通渲染
  10. 10Unity Shader #8 溶解
  11. 11Unity Shader #9 镜头上的雨滴
  12. 12Unity Shader #13 屏幕后处理
  13. 13URP #1 Universal Lit 与 URP ShaderLab