Search Unity

Help with 3D holographic shader

Discussion in 'Shaders' started by mcmount, Sep 6, 2019.

  1. mcmount

    mcmount

    Joined:
    Nov 15, 2015
    Posts:
    83
    Hi,

    I really hope someone talented could help with this;

    I've created a shader, which creates really awesome 3D holographic surfaces. BUT, I've really never messed with shaders, so my understanding is somewhere between poor and really poor. The shader works really well, but I would need to get this optimized. Let me first explain how it works:

    I use MP4 video, where left side is RGB and right side is depth map:



    I'm using it with Unity's Video Player component. one problem is, that videoplayer resets the tiling and offset.

    Due my limited skills, I've attached 2 video players to the same material, one driving the RGB and another the depth map. Then I offset the RGB UV so it matches with the dephmap. This solution is insane + I would have to UV remap the holographic plane from x 0.5->1 to prevent the ghosting seen in scene view.

    So inside of the shader I would need to:

    1. scale the left RGB so it fills the whole canvas
    2. take the depth info from the right side and map it to the RGB UV coordinates
    3. Use the depth map as a transparency with a multiplier slider, so the pure black is the transparency (otherwise grey will be semi transparent)
    4. Use only one videoplayer

    Code:
    Code (CSharp):
    1. Shader "Custom/Hologram" {
    2.         Properties {
    3.          
    4.             _Color("Color", color) = (1,1,1,1)
    5.             _MainTex ("Base (RGB)", 2D) = "white" {}
    6.             _Power("Power", Range(0, 2.0)) = 0.3
    7.             _DispTex ("Displacement Texture (RGB)", 2D) = "gray" {}
    8.             _Displacement ("Displacement", Range(0, 0.01)) = 0.001
    9.             _Tess("Tessellation", Range(1,15)) = 4
    10.  
    11.         }
    12.  
    13.         SubShader {
    14.                 Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
    15.                 LOD 200
    16.          
    17.          
    18.             CGPROGRAM
    19.             #pragma surface surf  BlinnPhong addshadow fullforwardshadows vertex:disp decal:add tessellate:tessFixed //nolightmap //alpha:fade
    20.             #pragma target 4.6
    21.  
    22.          
    23.             struct appdata {
    24.                 float4 vertex : POSITION;
    25.                 float4 tangent : TANGENT;
    26.                 float3 normal : NORMAL;
    27.                 float2 texcoord : TEXCOORD0;
    28.              
    29.             };
    30.  
    31.             float _Tess;
    32.             half _Alpha;
    33.             half _Power;
    34.          
    35.  
    36.             float4 tessFixed()
    37.             {
    38.                 return _Tess;            
    39.             }
    40.  
    41.             sampler2D _DispTex;        
    42.             sampler2D _MainTex;
    43.             sampler2D _NormalMap;
    44.             fixed4 _Color;
    45.             float _Displacement;
    46.          
    47.  
    48.             void disp (inout appdata v)
    49.             {
    50.                 float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
    51.                 v.vertex.xyz += v.normal * d;
    52.             }
    53.  
    54.             struct Input {
    55.  
    56.                 float2 uv_MainTex;
    57.              
    58.             };
    59.  
    60.          
    61.             void surf (Input IN, inout SurfaceOutput o) {
    62.              
    63.                 fixed offsetX = -.5f;
    64.                 fixed2 offsetUV = fixed2(offsetX, 0);
    65.                 fixed2 mainUV = IN.uv_MainTex + offsetUV;
    66.  
    67.                 half4 c = tex2D(_MainTex, mainUV) *_Color;
    68.              
    69.                 o.Albedo = c.rgb*_Power;
    70.              
    71.             }
    72.             ENDCG
    73.         }
    74.         FallBack "Specular"
    75.     }

    You can download the runner video file from here: citor3.com/fd2/runner.mp4

    Here is a demo how it looks in runtime:citor3.com/fd2/RunnerDemo.mp4

    I REALLY appriciate any help to get this baby optimized!!
     
    Last edited: Sep 6, 2019
  2. mcmount

    mcmount

    Joined:
    Nov 15, 2015
    Posts:
    83
    Ok,
    I managed to get this forward and only thing missing now is the shifting and scaling of the final texture. I would need to shift the UV from x=0.5 to x=0 and scale the x width by 2, so the whole plane gets filled.

    I also added offset, which helps to bring the character even more outside, but clipping it to the plane level.

    Code:
    Code (CSharp):
    1. Shader "Custom/Hologram" {
    2.         Properties {
    3.            
    4.             _Color("Color", color) = (1,1,1,1)
    5.             _MainTex ("Base (RGB)", 2D) = "white" {}
    6.             _Power("Power", Range(0, 2.0)) = 0.3
    7.             _Displacement ("Displacement", Range(0, 0.01)) = 0.001
    8.             _Tess("Tessellation", Range(1,15)) = 4
    9.             _Offset("Offset", Range(-.005,.005)) = 0
    10.            
    11.         }
    12.  
    13.         SubShader {
    14.                 Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
    15.                 LOD 200
    16.            
    17.            
    18.             CGPROGRAM
    19.             #pragma surface surf  BlinnPhong addshadow fullforwardshadows vertex:disp decal:add tessellate:tessFixed  //alpha:fade //nolightmap
    20.             #pragma target 4.6
    21.  
    22.            
    23.             struct appdata {
    24.                 float4 vertex : POSITION;
    25.                 float4 tangent : TANGENT;
    26.                 float3 normal : NORMAL;
    27.                 float2 texcoord : TEXCOORD0;
    28.                
    29.                
    30.             };
    31.  
    32.             float _Tess;
    33.             half _Power;
    34.             float _Offset;
    35.            
    36.  
    37.             float4 tessFixed()
    38.             {
    39.                 return _Tess;              
    40.             }
    41.  
    42.                      
    43.             sampler2D _MainTex;
    44.             fixed4 _Color;
    45.             float _Displacement;
    46.            
    47.  
    48.             void disp (inout appdata v)
    49.             {
    50.                 float d = (tex2Dlod(_MainTex, float4(v.texcoord.xy,0,0)).r * _Displacement) + _Offset;
    51.                 if (d<0) d=0;
    52.                 v.vertex.xyz += v.normal * d;
    53.             }
    54.  
    55.             struct Input {
    56.  
    57.                 float2 uv_MainTex;  
    58.                
    59.             };
    60.  
    61.            
    62.             void surf (Input IN, inout SurfaceOutput o) {
    63.                
    64.                 fixed offsetX = -.5f;
    65.                 fixed2 offsetUV = fixed2(offsetX, 0);
    66.                 fixed2 mainUV = IN.uv_MainTex + offsetUV;
    67.  
    68.                 half4 c = tex2D(_MainTex, mainUV) *_Color;
    69.                
    70.                 o.Albedo = c.rgb*_Power;
    71.                
    72.  
    73.             }
    74.             ENDCG
    75.         }
    76.         FallBack "Specular"
    77.     }