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. Dismiss Notice

Question Video player as post proceessing effect with custom scriptable render pass

Discussion in 'Universal Render Pipeline' started by veteran_gamer, Sep 14, 2022.

  1. veteran_gamer

    veteran_gamer

    Joined:
    Dec 19, 2019
    Posts:
    14
    Hello , I've a shader effect that takes render texture from a video player component then use Graphics.Blit(source, destination, _material); .

    I need to enable /disable the effect at runtime , unfortunately I can't use ScriptableRenderPass andMonoBehaviour at the same time .

    can someone please help me convert this into urp .
    Code (CSharp):
    1.  
    2. //vhsprocesseffect.cs
    3.  
    4. using UnityEngine;
    5. using UnityEngine.Video;
    6. using UnityEngine.UI;
    7. namespace effect {
    8.     public class VHSPostProcessEffect : MonoBehaviour
    9.     {
    10.         public Shader shader;
    11.         public VideoClip VHSClip;
    12.  
    13.         private float _yScanline;
    14.         private float _xScanline;
    15.         private Material _material = null;
    16.         private VideoPlayer _player;
    17.         //public bool rewinding = false;
    18.  
    19.  
    20.         private void OnEnable()
    21.         {
    22.             _material = new Material(shader);
    23.             _player = GetComponent<VideoPlayer>();
    24.             _player.isLooping = true;
    25.             _player.renderMode = VideoRenderMode.APIOnly;
    26.             _player.audioOutputMode = VideoAudioOutputMode.None;
    27.             _player.clip = VHSClip;
    28.             _player.Play();
    29.  
    30.         }
    31.         void Start()
    32.         {
    33.        
    34.             //
    35.         }
    36.  
    37.         void OnRenderImage(RenderTexture source, RenderTexture destination)
    38.         {
    39.             _material.SetTexture("_VHSTex", _player.texture);
    40.  
    41.             _yScanline += Time.deltaTime * 0.01f;
    42.             _xScanline -= Time.deltaTime * 0.1f;
    43.  
    44.             if (_yScanline >= 1)
    45.             {
    46.                 _yScanline = Random.value;
    47.             }
    48.             if (_xScanline <= 0 || Random.value < 0.05)
    49.             {
    50.                 _xScanline = Random.value;
    51.             }
    52.             _material.SetFloat("_yScanline", _yScanline);
    53.             _material.SetFloat("_xScanline", _xScanline);
    54.             Graphics.Blit(source, destination, _material);
    55.         }
    56.  
    57.         protected void OnDisable()
    58.         {
    59.             if (_material)
    60.             {
    61.                 DestroyImmediate(_material);//
    62.             }
    63.         }
    64.  
    65.         void update()
    66.         {
    67.  
    68.         }
    69.     }
    70. }
    and here's the shader

    Code (CSharp):
    1. Shader "Hidden/VHSPostProcessEffect" {
    2.     Properties{
    3.         _MainTex("Base (RGB)", 2D) = "white" {}
    4.         _VHSTex("Base (RGB)", 2D) = "white" {}
    5.     }
    6.  
    7.         SubShader{
    8.             Pass {
    9.                 ZTest Always Cull Off ZWrite Off
    10.                 Fog { Mode off }
    11.  
    12.                 CGPROGRAM
    13.                 #pragma vertex vert_img
    14.                 #pragma fragment frag
    15.                 #pragma fragmentoption ARB_precision_hint_fastest
    16.                 #include "UnityCG.cginc"
    17.  
    18.                 uniform sampler2D _MainTex;
    19.                 uniform sampler2D _VHSTex;
    20.  
    21.                 float _yScanline;
    22.                 float _xScanline;
    23.                 float rand(float3 co) {
    24.                      return frac(sin(dot(co.xyz ,float3(12.9898,78.233,45.5432))) * 43758.5453);
    25.                 }
    26.  
    27.                 fixed4 frag(v2f_img i) : COLOR{
    28.                     fixed4 vhs = tex2D(_VHSTex, i.uv);
    29.  
    30.                     float dx = 1 - abs(distance(i.uv.y, _xScanline));
    31.                     float dy = 1 - abs(distance(i.uv.y, _yScanline));
    32.  
    33.                     //float x = ((int)(i.uv.x*320))/320.0;
    34.                     dy = ((int)(dy * 15)) / 15.0;
    35.                     dy = dy;
    36.                     i.uv.x += dy * 0.025 + rand(float3(dy,dy,dy)).r / 500;//0.025;
    37.  
    38.                     float white = (vhs.r + vhs.g + vhs.b) / 3;
    39.  
    40.                     if (dx > 0.99)
    41.                         i.uv.y = _xScanline;
    42.                     //i.uv.y = step(0.99, dy) * (_yScanline) + step(dy, 0.99) * i.uv.y;
    43.  
    44.                     i.uv.x = i.uv.x % 1;
    45.                     i.uv.y = i.uv.y % 1;
    46.  
    47.                     fixed4 c = tex2D(_MainTex, i.uv);
    48.  
    49.                     float bleed = tex2D(_MainTex, i.uv + float2(0.01, 0)).r;
    50.                     bleed += tex2D(_MainTex, i.uv + float2(0.02, 0)).r;
    51.                     bleed += tex2D(_MainTex, i.uv + float2(0.01, 0.01)).r;
    52.                     bleed += tex2D(_MainTex, i.uv + float2(0.02, 0.02)).r;
    53.                     bleed /= 6;
    54.  
    55.                     if (bleed > 0.1) {
    56.                         vhs += fixed4(bleed * _xScanline, 0, 0, 0);
    57.                     }
    58.  
    59.                     float x = ((int)(i.uv.x * 320)) / 320.0;
    60.                     float y = ((int)(i.uv.y * 240)) / 240.0;
    61.  
    62.                     c -= rand(float3(x, y, _xScanline)) * _xScanline / 5;
    63.                     return c + vhs;
    64.                 }
    65.                 ENDCG
    66.             }
    67.     }
    68.         Fallback off
    69. }
    70.  

    I tried to use a library from github which convnert any material into render feature but
    unfortunately it doesn't affect sprites (it render at the background ) the original effect does applys for gui too .
    also using render texture as overlay doesn't looks good as it isn't native post processing effect .
     
  2. veteran_gamer

    veteran_gamer

    Joined:
    Dec 19, 2019
    Posts:
    14