Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

unity – Ignore ZWrite for translucent pixels

[ad_1]

Been just lately moving into HLSL and Shaders in Unity. I am attempting to create a shader that has transparency, however that also writes to the ZWrite. For instance, as of proper now, if I wished to make use of an impact like Depth of Discipline. I mainly have 2 choices. I can create a blurred model of every texture earlier than the sport hundreds, then interchange between when essential, which is sluggish and makes use of a whole lot of reminiscence. Or I can blur the textures in photoshop, which I do not actually need to do because it causes a whole lot of issues afterward which I do not need to get into an excessive amount of element about.

In my present shader, for alpha to work, I’ve to show ZWrite off at first. However is there a means I can nonetheless use the ZWrite for opaque pixels?

One other means this might work is a customized blur impact, however that appears overkill.

Right here is my code for some reference, presently, it makes use of a Display impact to take away the “purple/inexperienced/blue display screen” discovered on the feel atlases.

Shader "Uber Shaders/SuperUberShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        [KeywordEnum(Red, Green, Blue)] _Screen ("Display Colour", Float) = 1
        _ScreenThreshold ("Display Threshold", Vary (0.0, 1.0)) = 0.9
        _ScreenMaskThreshold("Display Masks Threshold", Vary(0.0, 1.0)) = 0.9
    }
    SubShader
    {
        Tags { "Queue"="Clear" "RenderType"="Clear" }
        LOD 1000
        ZWrite Off
        Mix SrcAlpha OneMinusSrcAlpha

        Move
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #embody "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            #pragma multi_compile _SCREEN_RED _SCREEN_GREEN _SCREEN_BLUE
            float _ScreenThreshold;
            float _ScreenMaskThreshold;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // pattern the feel
                fixed4 col = tex2D(_MainTex, i.uv);
                if (_SCREEN_RED)
                {
                    if (col.r > _ScreenThreshold && col.g < _ScreenMaskThreshold && col.b < _ScreenMaskThreshold) col.a = 0.0;
                } else if (_SCREEN_GREEN)
                {
                    if (col.g > _ScreenThreshold && col.r < _ScreenMaskThreshold && col.b < _ScreenMaskThreshold) col.a = 0.0;
                } else
                {
                    if (col.b > _ScreenThreshold && col.g < _ScreenMaskThreshold && col.r < _ScreenMaskThreshold) col.a = 0.0;
                }
                return col;
            }
            ENDCG
        }
    }
}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *