unity – Ignore ZWrite for translucent pixels

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]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply