Unity ZTest Depth Test & zwrite Depth Write

Source: Internet
Author: User

Beginner shader, a start for the rendering queue, ZTest and Zwrite confused, through the multi-view and experiment, with some of their own understanding. This article and the novice shader friends to share, also is to make a note for themselves. The wrong or the lack of the place is welcome to correct.

Don't talk nonsense, go straight to the chase.

The first is the experimental scene, a blue and a red two cube. Blue in front of the red.

The shader used by two blocks are the simplest v&f coloring program, the difference is that the Blue Block Alpha return value is 0.6 and the red is 1.

But the blue squares do not have the slightest effect of transparency, when we add such a line of code to the shader of the Blue square:

Blend Srcalpha Oneminussrcalpha.

To explain a little bit, this line of instruction means that the color value computed by this Shader (source color value, i.e. blue) * Source alpha value (0.6) + Target color value (which can be understood as the background color) * (1-0.6), so that the blue squares show 40% transparency.

Then we see the effect:

It seems to be a little transparent, but the red squares still don't show up. Let's add this line of code:

Tags {"Queue" = "Transparent"}

It means setting its value in the render queue to Transparent (transparent) = 3000, the smaller the value is the first render, and then the rendered (large queue value) object overrides the first rendered object (the red block does not set the queue value, the default is Geometry (geometry) = 2000). In an ideal world, we should let distant objects be rendered first, and then rendered near objects so that distant objects do not obscure near objects.

And then we see the right result:

Queue other predefined values are: Background = +, alphatest = 2450,overlay = 4000. The default value is geometry.

For example, if we want a distant red block to block the nearby Blue Square, that is, let the blue first render, the red after the rendering, just the red block of the queue is also set to Transparent, the blue block of the queue value-1.

Then we look at the effect:

There's no such thing as an egg. What is this for?

The reason for this is that two instructions:

Although we do not have these two lines in our code, they are the default.

ZTest, depth test; lequal, less than equals.

Zwrite, depth write, on, open.

ZTest values are: Greater, gequal, less, lequal, Equal, notequal, always, never, off, by default lequal,ztest off equals ZTest alway S

The Zwrite value is: On, OFF, default is on.

There is a color buffer and a depth buffer in the system that stores the color values and depth values to determine what color should be displayed on the screen.

The depth value is the distance of the object from the camera in world space. The closer the distance, the lower the depth value, and the farther the distance, the greater the depth value.

For example, in our scenario, blue squares are closer to the camera than red, and the depth of the blue block is smaller than the red block.

Suppose the blue block has a depth value of 0.5 and a red block of 0.7. Remember in the above example, we let the blue block in the render queue in front of the red block, the system first put the blue value in the color buffer corresponding to the region, the depth value of 0.5 in the depth buffer corresponding to the region. Next, the red block is rendered, and the system compares the depth value of the red block to the value in the depth buffer (this process is the depth test), because the default ZTest depth test is lequal less than or equal, that is, a color with a depth value of less than or equal to 0.5 passes the test. If the test is passed and the Zwirte is on, the depth value of the color overrides the value in the depth buffer, and the color value overrides the value in the color buffer to display the new color.

Obviously, 0.7 > 0.5, so red does not pass the test, the red block can not be displayed in front of the blue block.

If we are hard to block the nearby blocks of red, it is clear that we should change or close the depth test, or close the deep write (after the depth test or deep write is closed, the object's color occlusion will be consistent with the render queue, which will block the front).

Next we try to turn off the deep write of the Blue block:

Get the results you want:

Try closing the Red Block's depth test separately, and note the depth write of the blue block:

The result is still the same. But what if we change the way the red block is tested in depth:

That is, the depth value is greater than the value in the depth buffer can pass the test, remember we assume that the red block depth value is 0.7, the blue block is 0.5. Theoretically we will get the result we want:

Oddly enough, where did the other half of the red block go?

The answer is blocked by the background.

Depending on the distance from the camera, we can assume that the depth value of the background is 1. The depth value in the depth buffer of the vanishing other half should be the depth value of the background 1. And we set the ZTest greater,0.7 < 1, so the other half of the red block did not pass the depth test.

This is the end of this article, because it is the first time to write a blog, but also hope that everyone can correct me!

Unity ZTest Depth Test & zwrite Depth Write

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.