Unity's checkerboard culling shader

Source: Internet
Author: User

Unity appears to have updated the document page after 5.3, where a checkerboard culling script

Https://docs.unity3d.com/Manual/SL-ShaderSemantics.html

I added a bit of comment and parameter control, the code is as follows:

Shader"Unlit/screen Position"{Properties {_maintex ("Texture", 2D) =" White"{} _sizefactor ("Sizefactor",float) =0.25} subshader {cull Off Blend srcalpha oneminussrcalpha tags {"Queue"="Transparent"            "Rendertype"="Transparent"} Pass {Cgprogram#pragmaVertex vert#pragmaFragment Frag#pragmaTarget 3.0structv2f {float2 uv:texcoord0;            }; v2f Vert (float4 vertex:position, Float2 uv:texcoord0, outfloat4 outpos:sv_position)                {v2f o; O.uv=UV; Outpos=Mul (UNITY_MATRIX_MVP, vertex); returno;        } sampler2d _maintex; float_sizefactor; Fixed4 Frag (v2f i, Unity_vpos_type screenpos:vpos): sv_target {//Presumably, after unity5.3, a set of parameter passes is added, which can pass in the vertex ID in the vertex shader, or pass in the screen position and so on. //The Screenpos used here is not a 0-1 range, but a real place like 960x480. //There are a number of new additions, specific to the documentation.Screenpos.xy= Floor (SCREENPOS.XY * _sizefactor) *0.5;//multiplying by 0.5 is divided by 2, so there are only two remainder results. 5 and 0.             floatChecker =-frac (SCREENPOS.R + SCREENPOS.G);//here Frac is to obtain the remainder part, take negative number is because of the following clip function. //So the result is-5 and 0. //The clip function only determines positive or negative numbers, and does not render if negative. This way-the value of 5 will not be rendered.Clip (checker); Fixed4 C=tex2d (_maintex, I.UV); returnC; } ENDCG} }}

Final effect (change _sizefactor to adjust checkerboard size):

This culling effect can be used either near the camera or on objects with low Lod in the distance (trees, etc.) to improve performance.

Unity's checkerboard culling shader

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.