Search Unity

Weird shader behaviour on just one Android Device

Discussion in 'Shaders' started by benacler, Apr 10, 2017.

  1. benacler

    benacler

    Joined:
    Oct 26, 2013
    Posts:
    6
    Hi there,

    I wrote a custom shader to simulate a camera circular close effect with a custom blur. Basically it works well on IOS, on the Editor, and on some Android devices, but on a PhonePad S501, it's like the coordinates of the distance between the pixel and the center of the screen are reported wrong, making the filter starting at 50% of it's effect and then blurring in the opposite direction after the 50% of time... I think this has something to do with the differences between the platforms and the GFX card but I'm not sure where to begin to debug it.
    I'm attaching the shader:

    Shader "Camera/Camera Close Effect" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader
    {
    Pass
    {
    ZTest Always Cull Off ZWrite Off

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    uniform sampler2D _MainTex;
    uniform float _radius;
    uniform float _blur;

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

    v2f vert (appdata_img v)
    {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.uv = v.texcoord.xy ;

    return o;
    }

    float4 frag (v2f i) : SV_Target
    {
    float4 sampledColor = tex2D(_MainTex, i.uv);
    float2 screen = _ScreenParams.xy;
    float2 screenPosition = i.uv * screen.xy;
    float2 screenCenter = screen / 2.0f;
    float ar = screen.y / screen.x;
    float dist = distance(screenPosition, screenCenter);
    float normalizedRadius = (screen.x * _radius * ar) * (1.0f + _blur);
    float normalizedBlur = screen.x * _blur;
    if (screen.y > screen.x) {
    normalizedRadius = screen.y * _radius / ar * (1.0f + _blur);
    normalizedBlur = screen.y * _blur;
    }

    if (dist < normalizedRadius- normalizedBlur) {
    return sampledColor;
    } else {
    float offset = normalizedRadius - dist;
    float amount = offset/normalizedBlur;
    float4 mask = float4(amount, amount, amount, 1.0f);
    sampledColor.rgb = lerp(sampledColor.rgb, lerp(mask.rgb, sampledColor.rgb, amount), mask.a);
    return sampledColor;
    }
    }
    ENDCG
    }
    }

    Fallback off

    }