Search Unity

Question Transparent Video Shader, Video and Alpha in the same frame

Discussion in 'Shaders' started by juvelezm, Jul 15, 2022.

  1. juvelezm

    juvelezm

    Joined:
    May 23, 2018
    Posts:
    9
    Hi everyone,

    I need a Shader for Transparent Video where half of the frame is for the video and the other half for Alpha

    Something like this:



    I achieve it using Shader Graph, it is quite simple, but I need it for SRP and Unity 2019:



    I am pretty bad writing shader code, I will appreciate it a lot for any help

    The reason is due to the in-viability of streaming video with alpha channel, but extracting the alpha and creating a new video including the video and alpha in the same frame 50/50, made possible to stream using more compatible formats like H264.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Shader Graph only works with the SRP in 2019, so your work is done, unless you're thinking SRP means something else.

    SRP stands for Scriptable Render Pipeline, which is a new(ish) way to write custom rendering pipelines for Unity with c#. Both Unity's URP and HDRP are SRPs. Shader Graph is a tool Unity created to make shaders for those two SRPs.

    If you're looking to support the built in renderer, this isn't supported by Unity 2019. They added built in renderer support to Unity 2021.2's version of Shader Graph. You would need to write this shader using ShaderLab and HLSL, or buy Amplify Shader Editor.
     
  3. juvelezm

    juvelezm

    Joined:
    May 23, 2018
    Posts:
    9
    Thanks a lot for your reply, Sorry My Mistake, What I wanted to say with SRP was Standard Render Pipeline, but of course this doesn't exist, I mean Built-in Render PipeLine., Sorry for the confusion

    I think that Shader Graph for built in is only possible in the latest 2021 release

    I wrote this shader that works fine, but I am still not sure if this is the right way to do it:

    Code (CSharp):
    1. Shader "Transparent/HalfAlpha"
    2. {
    3.     Properties{
    4.     _Color("Main Color", Color) = (1,1,1,1)
    5.     _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    6.     }
    7.         SubShader {
    8.              Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    9.              LOD 200
    10.              Cull off
    11.  
    12.         CGPROGRAM
    13.         #pragma surface surf Lambert alpha
    14.         sampler2D _MainTex;
    15.         float4 _Color;
    16.         struct Input {
    17.              float2 uv_MainTex;
    18.         };
    19.         void surf(Input IN, inout SurfaceOutput o) {
    20.  
    21.             float2 cv1 = IN.uv_MainTex;
    22.             float2 cv2 = IN.uv_MainTex;
    23.  
    24.             cv1.x *= 0.5;
    25.             cv2.x *= 0.5;
    26.             cv2.x += 0.5;
    27.  
    28.             half4 c = tex2D(_MainTex, cv1) * _Color;
    29.             o.Albedo = c.rgb;
    30.             o.Alpha = c.a * tex2D(_MainTex, cv2).r;
    31.         }
    32.         ENDCG
    33.     }
    34.     Fallback "Transparent/VertexLit"
    35. }