《Unity Shader 入门精要》MyProject: letsStartShader笔记 04

0. Z-Buffer and Color-Buffer

        There's always a Z-Buffer before a fragment's color can be used. Its color can be wrote into Color-Buffer only when current Z is smaller than Z-Buffer's value. At this moment you can decide whether to change the Z-Buffer value into this one or not.

        For a transparent object (A), Z-Write is Off. If a Blend Off object (B) renders after that, B can pass the Z-Test, write its Z into Z-Buffer, and finally write its Blend-Off Color into Color-Buffer. It causes that whether B is in front of A or not, B's opaque color will always replace the color of this pixel.

        If B is also transparent, their Blend On color will always write into Color-Buffer, it means that their position is no longer determined by themself, but by the rendering order of the system.

1. Alpha Test

        Not only here, when you render any transparent object, you need to declare something in your SubShader Tags.

// some cases:
        Tags{"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TranparentCutout"}
        Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TranparentCutout"}

        However, in alpha test, ZWrite doesn't need to Off. 

Clip() 

        discard current fragment if parameter < 0.

clip(objectValue - clipValue)

        Make the clipValue work as a threshold.

2. Alpha Blend

        Give each transparent fragment an alpha value.

        ...
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            ...

                return fixed4(ambient + diffuse, texColor.a * _Scale);
            ...

         If Z-Write is Off but the object's structure is complex, the rendering sequence will not perform the object structure well.

Double Pass

        The first pass is used only to fill the Z-Buffer.

        ColorMask 0 means that when fragment passes the Z-Test, fills the Z-Buffer and is ready to fill the Color-Buffer, you tell it to fill zero.

    ...
        // a new pass only used to write something in z-buffer
        pass{
            ZWrite On
            ColorMask 0
        }

        ...

        pass{
            ...
            
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            ...

3. BothSide 

        A general model certainly doesn't need to render the back, but we shader developers need to consider...

Cull

//AlphaTest
    pass{
            Cull Off

//AlphaBlend
    pass{
            Cull Front

    ...

    pass{
            Cull Back

HAPPY ENDING! :)

85af7b0cb5434dee9b5ca12d6c76216a.png

(Chapter 8 is much simpler than Chapter 7... Because I've learned something about blend mode in photoshop XD)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值