Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Position of the vertex not filled in Android?

Discussion in 'Shaders' started by mmerlange, Feb 10, 2016.

  1. mmerlange

    mmerlange

    Joined:
    Jan 22, 2013
    Posts:
    9
    Hi,

    I wrote this simple shader. It is supposed to display disqus on top of a texture: center and radius are input values.
    My problem is that is works nicely on my laptop. But strangely on my android. It displays either MainTex or the color of the disqus (on the whole material).
    Playing around with values, I'm under the impression that i.vertex in my code is (0 0 0).

    Any idea?
    Thanks a lot,
    Michaël

    Shader"Unlit/simpleUnlit2"
    {
    Properties
    {
    _MainTex ("Texture", 2D) = "white" {}
    _CentrePoint1 ("Centre1", Vector) = (0, 0, 0, 0)
    _CentrePoint2 ("Centre2", Vector) = (0, 0, 0, 0)
    _rayon ("rayon", Float) = 0.5
    _luminosity ("luminosity", Float) = 1
    _Color1 ("Color1", Color) = (0.3, 0.4, 0.7, 1.0)
    _Color2 ("Color2", Color) = (0.6, 0.2, 0.7, 1.0)
    }
    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 100

    Pass
    {
    CGPROGRAM
    #pragmavertexvert
    #pragmafragmentfragalpha


    #include "UnityCG.cginc"

    struct appdata
    {
    float4vertex : POSITION;
    float2 uv : TEXCOORD0;
    };

    struct v2f
    {
    float2 uv : TEXCOORD0;
    float4vertex : SV_POSITION;
    };

    sampler2D _MainTex;
    float4 _MainTex_ST;
    float4 _CentrePoint1;
    float4 _CentrePoint2;
    float _rayon;
    float _luminosity;
    float4 _Color1;
    float4 _Color2;

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

    float4 frag (v2f i) : SV_Target
    {
    //samplethe texture
    float4 col = tex2D(_MainTex, i.uv);

    float curDistance1 = float(distance(_CentrePoint1.xyz, i.vertex));
    float curDistance2 = float(distance(_CentrePoint2.xyz, i.vertex));
    int a1= int(max(float(0),sign(_rayon-curDistance1)));
    int a2= int(max(float(0),sign(_rayon-curDistance2)));
    inta=abs(a1-a2);

    float4 c1=_Color1.rgba*a1;
    c1.a=a1;
    float4 c2=_Color2.rgba*a2;
    c2.a=a2;
    float4 cF;
    //floatisHidden=float(abs(a1-a2));
    //floatisTransparent=1-isHidden;
    float isHidden=a1;
    float isTransparent=1-isHidden;
    cF.rgb=isHidden*(c1+c2)+col.rgb*isTransparent*_luminosity;
    col.rgb=cF.rgb;
    col.a = 1;

    return col;
    }
    ENDCG
    }
    }
    }
     
  2. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    OpenGL ES (used in most mobile devices) doesn't support reading screen position in pixel shader. You have to compute it in vertex shader and pass it over. There's a built in function for that - ComputeScreenPos.
    1) make room in your input structure
    Code (CSharp):
    1. float4 screenPos : TEXCOORD1;
    2) compute screen position in vertex shader
    Code (CSharp):
    1. o.screenPos = ComputeScreenPos(v.vertex);
    3) divide by w in pixel shader and use it instead of i.vertex
    Code (CSharp):
    1. float2 screenPos = i.screenPos.xy / i.screenPos.w;
     
    mmerlange likes this.
  3. mmerlange

    mmerlange

    Joined:
    Jan 22, 2013
    Posts:
    9
    THANK YOU, Michal. I was almost dancing when I saw it working