Interpreting CG writing in Unity Shader series Three

Source: Internet
Author: User
Tags mul

Transfer from http://www.itnose.net/detail/6096068.html

In the previous example, we obtained the information passed by the mesh component that was mathematically converted to the appropriate color range to be colored to the object. This article will be based on the study of the Erase of the fragment (discarding fragments) and the front clipping, the back clipping (front face culling and the rear face culling) to achieve a transparent effect.

When the information of a mesh component is passed, we can determine which parts render (render) through the code, and which parts do not, the process is like removing the unwanted parts, we do not see him, although his mesh information is still there, but our GPU will not deal with it, Must be lower than the performance consumption of the GPU before culling.

This process is like our mesh component is a transparent membrane, we assume that this adhesive paper we do not see, and the fragment shader in the coloring of the time like a brush to selectively ground color, the final effect is that we may see a part of the film is visible, but the missing place, the film still exists, but we did not give him color, We neither look at them nor need them to paint precious ink (GPU parallel processing capability)

So we can change the longitude green false colored sphere in the previous example, erase its longitude >0.5 part, then the code should be modified accordingly:

  

So give this shader to material, and then give a sphere to see that the green fake color ball we saw last time only left the southern hemisphere:

Looks like solid from the front.

A little tilt. You can see from above that the inside of the sphere is hollow, so I use a film and a brush to describe the render process.

Let's change the sphere to a cube and see what it looks like:

It can be found that this is a strange cube, the cube's six faces were drawn only half, and are half the following.

Why are the effects on the cube and the sphere so much different?

Because the cube is a Cartesian coordinate system, the sphere is a polar coordinate system .... Did you give the teacher a slap?

Similarly we will change the >0.5 to <0.5, we can get the northern hemisphere of the sphere.

This is the simplest surface culling (cuteaway)

A better surface culling is to convert the position of the fragment from the object coordinate system to the world coordinate system, and then transform it based on the underlying matrix to calculate which fragments are inside the other spheres (the original radius is 0.5) and then reject the surface inside the other spheres, so that if two balls overlap one another, So even if two balls revolve around each other's sphere, no overlapping parts will be drawn, and the overlapping parts will not be drawn, which we do not see, so save performance. Because even if the sphere rotates, the coordinates of the object are transformed into world coordinates after Unity's built-in matrix, the overlapping parts of the world coordinates are fixed, so there will be no two spheres overlap part of the surface is clipped, after rotating a ball slowly see the cut of the hole. (Because the previous method is clipped by the object coordinate system)

Trim front and back

In the code we just saw cull off, this line of code precedes the Cgprogram tag, so he's not in the CG category. It's a shaderlab command in unity, so he doesn't need a semicolon to end it.

Cull off is to turn off the triangle cut (why suddenly came out of the triangle, brain repair, our stereoscopic image in the computer is a triangular patchwork, because so our three-dimensional graphics will produce sawtooth, it is the contribution of the triangle)

Cull Front for Front (outer) clipping

Cull back for the rear (inner) trim, and this is the default mode for all of our shader, that is, if shader is not written by you, it is possible to turn our hemisphere, you only see the front surface rather than the hemispherical surface, you can drag a model to see

As to why the default is the back clipping, because in most cases our rendering is on the entire three-dimensional surface of the body, then since the surface is all rendered, you will not see the part that is back to you, so the default after clipping will save a lot of physical performance Ah!

But now that we've erased the surface, we can see the inner surface of the back through the erased part, so we should modify the clipping mode, like a house with a roof, we can't see the floor in the house from above, so the floor should fall into the category of tailoring. But if we erase the roof (pushing the roof), and we can't see the floor, it's a bit scary, and it's going to change the clipping mode.

In order to understand these two patterns more intuitively, we modify the above code to trim the internal/external dual channel (pass), and the last color in each Pass is different (red and green)

To be clear, the shader in unity will only execute one subshader, but will execute all the pass

The Modified code:

Pass{cull Front//external trim, then this channel can be understood to be to color the inner surface of the basketball cgprogram#pragma vertex vert#pragma fragment Frag#include "Unitycg.cginc" The struct Vertexoutput {float4 pos:sv_position;//is output by the vertex shader to the texture coordinates in the mesh information, which is an object-coordinate float4 posinobjectcoords:texcoord0 ;}; Vertexoutput Vert (Appdata_full input) {Vertexoutput Output;output.pos = Mul (UNITY_MATRIX_MVP, Input.vertex);// Pass the Texcoord directly to the fragment shader output.posinobjectcoords = Input.texcoord;return output;} FLOAT4 Frag (vertexoutput input): color{//Erase Fragment if (Input.posinobjectcoords.y > 0.5) {discard;} when the Y value of the coordinate is greater than 0.5 The remainder still generates the longitude Green ball return float4 (0.0, Input.posinobjectcoords.y, 0.0, 1.0) by the Y-value size; }ENDCG} Pass{cull back//Internal trim, then this channel can be understood to be to color the outer surface of the basketball cgprogram#pragma vertex vert#pragma fragment Frag#include " Unitycg.cginc "struct Vertexoutput {float4 pos:sv_position;//the texture coordinates in the mesh information are output by the vertex shader, which is an object-coordinate system FLOAT4 posinobjectcoords:texcoord0;}; Vertexoutput Vert (Appdata_full input) {Vertexoutput Output;output.pos = Mul (UNITY_MATRIX_MVP, Input.vertex);// Pass the Texcoord directly to the fragment shader Output.posinobjEctcoords = Input.texcoord;return output;} FLOAT4 Frag (vertexoutput input): color{//Erase Fragment if (Input.posinobjectcoords.y > 0.5) {discard;} when the Y value of the coordinate is greater than 0.5 The remainder still generates a longitude red ball return Float4 (input.posinobjectcoords.y, 0.0, 0.0, 1.0) at the Y-value size;  }ENDCG}

We completed a shader with two passes and now look at what the sphere looks like:

Looking down from the top, we don't know if this sphere is recessed or convex, as if it were the green longitude ball in our last example.

We'll look at the bottom online:

We still do not know whether the red and black part is concave or convex, after all, it is a hemisphere, the vertical hemisphere to see nothing found

We look at the past from the front:

Visible green and black parts are recessed inside the surface, the red and black part is the convex outer surface ~

At this point, we have the freedom to control where our surfaces are visible or invisible.

The next CG has more magical places to wait for us to discover ~

Interpreting CG writing in Unity Shader series Three

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.