Unity3d shader official tutorial translation (7) -- shader Syntax: Pass culling & depth Testing

Source: Internet
Author: User
Shaderlab Syntax: culling & depth testing Syntax: Surface Removal and deep Test

 

 

Culling is an optimization that does not render polygons facing away from the viewer. all polygons have a front and a back side. culling makes use of the fact that most objects are closed; if you have a cube, you will never see the sides facing away from you
(There is always a side facing you in front of it) So we don't need to draw the sides facing away. Hence the term: backface culling.

Surface Removal is an option that is not rendered by the polygon that is not observed by the audience. All polygon have one front and one back. Removal is often used to remove faces that are away from you. For example, the back is usually invisible to you. Therefore, the back is usually removed, but you can also choose to remove the front.

The other feature that makes rendering looks correct is depth testing. Depth testing makes sure that only the closest surfaces objects are drawn in a scene.

Another thing that makes rendering of objects look normal is deep testing. The deep test determines that only the surface closest to the observer is drawn in the scene.

Syntax
CullBack | front | off: front-end removal
Controls which sides of polygons shocould be culled (not drawn) controls which side is removed (not drawn)

BackDon't render polygons facing away from the viewer (Default). Do not render the back that the observer cannot see FrontDon't render polygons facing towards the viewer. Used for turning objects inside-out. Don't render the front of the observer. It is often used to display objects from the inside out. OffDisables culling-All faces are drawn. Used for special effects. Disable and remove. All faces are visible for special effects.
ZwriteOn | off
Controls whether pixels from this object are written to the depth buffer (default is On). If you're drawng solid objects, leave this on. If you're drawing semitransparent effects, switch Zwrite
Off
. For more details read below.
Controls whether to write pixels of an object into the depth buffer (enabled by default ). If you want to draw a fixed object, this is useless. If you want to draw a translucent effect, disable zwrite. For more information, see the following section.
 
ZtestLess | greater | lequal | gequal | equal | notequal | always
How shocould depth testing be already med. Default is Lequal(Draw objects in from or at the distance as existing objects; hide objects behind them ).
Controls how deep tests are performed. The default value is lequal (draw an object based on the distance of the existing object and hide the object smaller than the set value ).
 
Offset  Factor  ,  UnitsDepth offset
Allows you specify a depth offset with two parameters. FactorAnd Units. FactorScales the maximum Z slope, with respect to X or Y of the polygon, and UnitsScale
The minimum resolvable depth buffer value. This allows you to force one polygon to be drawn on top of another although they are actually in the same position. For example Offset 0,-1Pulls the polygon closer to the camera ignoring the Polygon's slope,
Whereas Offset-1,-1Will pull the polygon even closer when looking at a grazing angle.
You can specify two parameters for the depth offset. Factor and units, factor sets the maximum slope of Z, about X or Y of the polygon. Units sets the minimum resolution depth buffer value. When two polygon are in the same depth, this can force a polygon to give priority to the other polygon. For example, offset 0 and-1 can force the polygon to be closer to the camera (ignore the polygon slope ). Offset-1 and-1 bring the polygon closer to the camera when they are at the grazing angle.
Refer: Original: unity3d shader official tutorial translation (6) ---- shader Syntax: Pass color, material, Lighting/Html/xwzx/TY/3317.html
Examples

This object will render only the backfaces of an object:

The front of the object is removed and only the back is displayed during rendering.

Shader "Show Insides" {    SubShader {        Pass {            Material {                Diffuse (1,1,1,1)            }            Lighting On            Cull Front        }    }} 

Try to apply it to a cube, and notice how the geometry feels all wrong when you orbit around it. This is because you're only seeing the inside parts of the cube.

Try to apply it to a cube and notice if there is a geometric illusion when rotating around it. This is because you only see the inside of the cube.

Debugging normals debug normal

The next one is more interesting; First we render the object with normal vertex lighting, then we render the backfaces in bright pink. this has the effects of highlighting anywhere your normals need to be flipped. if you see physically-controlled objects getting
'Sucked in' by any meshes, try to assign this shader to them. If any pink parts are visible, these parts will pull in anything unfortunate enough to touch it.

The following will become very interesting. First we will render an object with vertex normal illumination, and then we will render the back of it in a bright pink color. In this way, the highlight effect will be available anywhere, and your normal needs to be flipped. If you observe that an object controlled by a physical system is swallowed up by a grid (a collision occurs or a part of the object is inserted into the grid), assign the shader to the object. If any pink part can be seen, this part can be pushed anywhere, and we cannot touch it. (Physical system simulation problem)

Here we go: Example

Shader "Reveal Backfaces" {    Properties {        _MainTex ("Base (RGB)", 2D) = "white" { }    }    SubShader {        // Render the front-facing parts of the object.        // We use a simple white material, and apply the main texture.        Pass {            Material {                Diffuse (1,1,1,1)            }            Lighting On            SetTexture [_MainTex] {                Combine Primary * Texture            }        }        // Now we render the back-facing triangles in the most        // irritating color in the world: BRIGHT PINK!        Pass {            Color (1,0,1,1)            Cull Front        }    }} 
Glass culling glass Removal

Controlling culling is useful for more than debugging backfaces. If you have transparent objects, you quite often want to show the backfacing side of an object. If you render without any culling (Cull off), You'll most likely have some rear
Faces overlapping some of the front faces.

Controllable removal is more useful than debugging on the back. If you have a transparent object, you often need to display the back of the object. If you disable the trim function, it is very likely that some faces behind the object overlap with those above.

 

Here is a simple shader that will work for convex objects (spheres, cubes, car windscreens ).

Here there is a simple shader that will act on some convex objects (sphere, cube, car windshield ).

Shader "simple glass" {properties {_ color ("main color", color) = (,) _ speccolor ("spec color", color) =) _ emission ("emmisive color", color) = (0.01, 0.7) _ shininess ("shininess", range (, 1) = _ maintex ("base (RGB) ", 2d) =" white "{}} subshader {// we use the material in your passes by defining them in the subshader. // anything defined here becomes default values for Ll contained passes. Anything defined here will become the default value (valid for all passes ). Material {diffuse [_ color] ambient [_ color] shininess [_ shininess] specular [_ speccolor] emission [_ emission]} lighting on separatespecular on // set up alpha blending to set alpha hybrid blend srcalpha oneminussrcalpha // render the back facing parts of the object. // if the object is convex, these will always be further away // than the front-faces.
// If the object is a convex object, these will be farther away than the front surface pass {cull front settexture [_ maintex] {combine primary * texture} // render the parts of the object facing us. // if the object is convex, these will be closer than the // back-faces. // if the object is a convex object, these will be closer to the rear surface.
         Pass {            Cull Back            SetTexture [_MainTex] {                Combine Primary * Texture            }        }    }} 
Original at www.j2megame.com. For more information, see.

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.