Search Unity

Video VideoPlayer - black is transparent: is it possible?

Discussion in 'Audio & Video' started by danielesuppo, Jan 30, 2018.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Hello!
    I'm working on a GearVr app, and I'd like to put some videos with "holographic style".
    Do you know if is there a way to have black=transparent for VideoPlayer for videos without Alpha channel (Android, as far as I know, doesn't support it) and, most important, without transcoding?
    P.s. I've posted this thread twice ("Audio & Video" and "Graphic" forum) because I don't know to whom is related, sorry...
    Many thanks!
     
  2. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Yessss! I've found this shader that's almost what I need. Unfortunately it's not unlit. Any suggestion to make it unlit?
    Code (CSharp):
    1.  
    2. Shader "Custom/AlphaIsBlack" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _Threshold ("Cutout threshold", Range(0,1)) = 0.1
    6.         _Softness ("Cutout softness", Range(0,0.5)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    10.         LOD 200
    11.         CGPROGRAM
    12.         #pragma surface surf Lambert alpha
    13.         sampler2D _MainTex;
    14.         float _Threshold;
    15.         float _Softness;
    16.         struct Input {
    17.             float2 uv_MainTex;
    18.         };
    19.         void surf (Input IN, inout SurfaceOutput o) {
    20.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    21.             o.Albedo = c.rgb;
    22.             o.Alpha = smoothstep(_Threshold, _Threshold + _Softness,
    23.                0.333 * (c.r + c.g + c.b));
    24.         }
    25.         ENDCG
    26.     }
    27.     FallBack "Diffuse"
    28. }
     
    joelosiris91 likes this.
  3. Ricez

    Ricez

    Joined:
    May 30, 2017
    Posts:
    1
    In case you still need it or someone finds this thread, here's an unlit version of the shader you found.

    Code (CSharp):
    1. Shader "Unlit/AlphaIsBlack"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Threshold("Cutout threshold", Range(0,1)) = 0.1
    7.         _Softness("Cutout softness", Range(0,0.5)) = 0.0
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Transparent" "Queue" = "Transparent" }
    12.         LOD 100
    13.  
    14.         ZWrite Off
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.  
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 float4 vertex : SV_POSITION;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.             float _Threshold;
    40.             float _Softness;
    41.  
    42.             v2f vert(appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 return o;
    48.             }
    49.  
    50.             fixed4 frag(v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = tex2D(_MainTex, i.uv);
    54.                 col.a = smoothstep(_Threshold, _Threshold + _Softness,
    55.                     0.333 * (col.r + col.g + col.b));
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }
    62.  
     
  4. TimothyAppel

    TimothyAppel

    Joined:
    May 28, 2017
    Posts:
    3
    How would you apply the shader to the video?
     
    hi_ribeiro likes this.
  5. Khelgar

    Khelgar

    Joined:
    Mar 13, 2019
    Posts:
    16
    You need to create a material and a RenderTexture.
    Select the shader of the material and make the RenderTexture on Texture Area.
    On the video player you need to make the RenderTexture on Target Texture.
     
    Fenikkel and hi_ribeiro like this.
  6. aleisa_art

    aleisa_art

    Joined:
    Feb 7, 2019
    Posts:
    1
    Hey, thanks for the shader. Works pretty well. Could I also change the alpha color to white?
     
  7. Sisanda

    Sisanda

    Joined:
    Oct 3, 2017
    Posts:
    10
    Thank you for the shader, it helped me on my project
     
  8. joelosiris91

    joelosiris91

    Joined:
    May 2, 2016
    Posts:
    4

    thanks daniel, this helped me a lot!