Search Unity

[IOS] AlphaTest issues

Discussion in 'Shaders' started by Superflat, Mar 1, 2015.

  1. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Hello,

    I'm trying to get a video with an alpha channel running on IOS. I have a plane with the texture playing and I use the following shader that works fine in the editor if set to stand alone. Should i switch the platform to IOS the shader stops working. Since alpha channels arent supported on ios with video. I have a movie with the alpha channel next to it that i sample from.

    Adding 'alpha' to the pragma tags will invert my mask :(

    Code (CSharp):
    1. Shader "Custom/VideoAlpha" {
    2.    Properties {
    3.       _MainTex ("Base (RGB)", 2D) = "white" {}
    4.       _AlphaOffsetX ("alpha offset x", float) = 0.5
    5.       _AlphaOffsetY ("alpha offset y", float) = 0
    6.       _Cutoff ("Cutoff", Range (0,1)) = .5
    7.    }
    8.    SubShader {
    9.     Tags { "Queue" = "Transparent" }
    10.    AlphaTest Less [_Cutoff]
    11.          CGPROGRAM
    12.          #pragma surface surf Lambert
    13.  
    14.          sampler2D _MainTex;
    15.          float _AlphaOffsetX;
    16.          float _AlphaOffsetY;
    17.  
    18.          struct Input {
    19.             float2 uv_MainTex;
    20.          };
    21.  
    22.          void surf (Input IN, inout SurfaceOutput o) {
    23.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    24.             IN.uv_MainTex.x += _AlphaOffsetX;
    25.             IN.uv_MainTex.y += _AlphaOffsetY;
    26.             half4 d = tex2D (_MainTex, IN.uv_MainTex);
    27.             o.Emission = c.rgb;
    28.             o.Alpha = (d.r*-1)+1;
    29.          }
    30.      
    31.      
    32.          ENDCG
    33.  
    34.    }
    35.  
    36.  
    37.  
    38.    FallBack "Diffuse"
    39. }


     
    Last edited: Mar 1, 2015
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    It seems to me the inversion is done right there. Without inversion it's just:
    Code (csharp):
    1. o.Alpha = d.r;
    I'm also wondering a bit why you are using a surface shader for this. Do you need any lighting on the video?
     
  3. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    d.r makes everything disappear : /. This is a shader posted in a chroma keying thread.
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    It seems to me that d.r should lead to an inverted mask compared to 1-d.r.

    Anyway, I noticed the line AlphaTest Less [_Cutoff]. Less mode might not be (properly) supported on iOs. So just use:
    Code (csharp):
    1.  
    2. AlphaTest Greater [_Cutoff]
    3.  
    Combined with o.Alpha = d.r;

    If that doesn't work, just remove the alphatest and change o.Alpha = d.r to:
    Code (csharp):
    1.  
    2. clip(d.r - _Cutoff);
    3.  
    (Don't forget to add float _Cutoff as input.)
     
  5. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    Somehow the shader fixed itself or perhaps i misspelt Less as Lesser and it simply broke. But the shader works like a charm now. Thanks Jvo3dc
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    That would be a great addition :):
    Code (csharp):
    1.  
    2. SelfHealing Always;
    3.  
     
    KenAtGlu likes this.