0
\$\begingroup\$

I'm drawing a rectangle based on input sizes and use it to mask the screen. Now I want to give it 1 pixel stroke but it doesn't rasterize to one pixel. I tried UnityPixelSnap but still no chance.

Here's My Shader:

Shader "MyShaders/Frame"
{
    Properties
    {
        [PerRendererData] _MainTex ("Frame Overlay", 2D) = "white" {}
        framesize ("Frame Sizes", Vector) = (0.5 , 0.5, 0.0 , 0.0) // z & w are just value holders
    }
    SubShader
    {
        Cull Off ZWrite Off ZTest Off Lighting Off
        Blend One OneMinusSrcAlpha
        
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

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

            struct v2f
            {
                float4 c : COLOR;
                float4 pos : SV_POSITION;
                float4 p : TEXCOORD;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.c = v.color;
                o.p = ComputeScreenPos(o.pos);
                return o;
            }

            sampler2D _MainTex;
            float4  framesize; 

            fixed4 frag (v2f i) : SV_Target
            {
                // Base Color
                fixed4 color = i.c;
                float2 screen = i.p.xy;
                float2 screenSize = _ScreenParams.xy;

                // Get Pixel Size
                framesize.z =  (1.0f  /  screenSize.x);
                framesize.w =  (1.0f /  screenSize.y);

                // Draw Stroke
                if(screen.x > (0.5f - (framesize.x/2) - framesize.z) && screen.x < (0.5f + (framesize.x/2) + framesize.z)
                    && screen.y > (0.5f - (framesize.y/2) - framesize.w) && screen.y < (0.5f + (framesize.y/2) + framesize.w))
                color = float4(1.0, 1.0, 1.0, 0.4f);

                // Delim Using Ratios
                if(screen.x > (0.5f - (framesize.x/2) - framesize.z*.2) && screen.x < (0.5f + (framesize.x/2) + framesize.z*.2)
                    && screen.y > (0.5f - (framesize.y/2) - framesize.w*.2) && screen.y < (0.5f + (framesize.y/2) + framesize.w*.2))
                color.a = 0;

                // Apply UI Alpha
                color.rgb *= color.a;
                return color;
            }
            ENDCG
        }
    }
}

It should be assigned to a full width/height panel in a canvas.

This is what happens :

Wrong Pixel

I've found this useful shader, It works perfect but I have no idea how can I use it in my shader.

I'll be very greatful if someone know how to make it work :)

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

OK, After days of struggling the solution was god damn easy... Using Int2/Int4 instead of Float2/Float4, Pixels are integer so when you have a pixel like 550.5 it goes invisible...

Hope this help someone else :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .