Search Unity

Camera glitch image effect

Discussion in 'Image Effects' started by whidzee, Feb 8, 2019.

  1. whidzee

    whidzee

    Joined:
    Nov 20, 2012
    Posts:
    166
    Hello gang, I am looking to get some camera glitching effects for when my player is getting out of bounds. I'd like to recreate something like this (from 6.29 - 6:47)


    I am new to dealing with image effects but if you could point me in the right direction that would be great.

    The best I can come up with in the post processing that is built into Unity is this. The problem is it doesn't look like the video breakup I am seeing in the video above. Also even when it's cranked all the way up I am still able to see, where as in the video you can see that when it's at maximum breakup you cannot see. also the image comes in and out of the static too.
    upload_2019-2-8_10-44-29.png

    Can you help point me in the right direction?
     
  2. HappyRobot

    HappyRobot

    Joined:
    Sep 9, 2015
    Posts:
    2
    hi, I was able to modify the legacy image effects "Screen Overlay" script and the "BlendModesOverlay" shader to randomly offset the positioning of a texture, and then just use the intensity slider to control the power of the effect,

    And to completely lose vision, just add the script again, turn off the random boolean, and put in a blank texture, you might have to play around with it a bit

    Screen Overlay:

    using System;
    using UnityEngine;
    namespace UnityStandardAssets.ImageEffects
    {
    [ExecuteInEditMode]
    [RequireComponent (typeof(Camera))]
    [AddComponentMenu ("Image Effects/Other/Screen Overlay")]
    public class ScreenOverlay : PostEffectsBase
    {
    public enum OverlayBlendMode
    {
    Additive = 0,
    ScreenBlend = 1,
    Multiply = 2,
    Overlay = 3,
    AlphaBlend = 4,
    }
    public OverlayBlendMode blendMode = OverlayBlendMode.Overlay;
    public float intensity = 1.0f;
    public Texture2D texture = null;
    public Vector2 offset;
    public Boolean random = true;
    public Shader overlayShader = null;
    private Material overlayMaterial = null;

    public override bool CheckResources ()
    {
    CheckSupport (false);
    overlayMaterial = CheckShaderAndCreateMaterial (overlayShader, overlayMaterial);
    if (!isSupported)
    ReportAutoDisable ();
    return isSupported;
    }
    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {
    if (random)
    {
    offset.x = UnityEngine.Random.Range(-1f, 1f);
    offset.y = UnityEngine.Random.Range(-1f, 1f);
    }
    if (CheckResources() == false)
    {
    Graphics.Blit (source, destination);
    return;
    }
    Vector4 UV_Transform = new Vector4(1, 0, 0, 1);
    #if UNITY_WP8
    // WP8 has no OS support for rotating screen with device orientation,
    // so we do those transformations ourselves.
    if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    UV_Transform = new Vector4(0, -1, 1, 0);
    }
    if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    UV_Transform = new Vector4(0, 1, -1, 0);
    }
    if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    UV_Transform = new Vector4(-1, 0, 0, -1);
    }
    #endif
    overlayMaterial.SetVector("_UV_Transform", UV_Transform);
    overlayMaterial.SetFloat ("_Intensity", intensity);
    overlayMaterial.SetTexture ("_Overlay", texture);
    overlayMaterial.SetVector("offset", offset);
    Graphics.Blit (source, destination, overlayMaterial, (int) blendMode);
    }
    }
    }

    BlendModesOverlay (shader)

    Shader "Hidden/BlendModesOverlay" {
    Properties {
    _MainTex ("Screen Blended", 2D) = "" {}
    _Overlay ("Color", 2D) = "grey" {}
    }

    CGINCLUDE
    #include "UnityCG.cginc"

    struct v2f {
    float4 pos : SV_POSITION;
    float2 uv[2] : TEXCOORD0;
    };

    sampler2D _Overlay;
    half4 _Overlay_ST;
    sampler2D _MainTex;
    half4 _MainTex_ST;

    half _Intensity;
    half4 _MainTex_TexelSize;
    half4 _UV_Transform = half4(1, 0, 0, 1);
    half3 offset = half3(1, 0, 0);
    v2f vert( appdata_img v ) {
    v2f o;
    o.pos = UnityObjectToClipPos(v.vertex);

    o.uv[0] = UnityStereoScreenSpaceUVAdjust(float2(
    dot(v.texcoord.xy + offset.xy, _UV_Transform.xy),
    dot(v.texcoord.xy + offset.xy, _UV_Transform.zw)
    ), _Overlay_ST);

    #if UNITY_UV_STARTS_AT_TOP
    if(_MainTex_TexelSize.y<0.0)
    o.uv[0].y = 1.0-o.uv[0].y;
    #endif

    o.uv[1] = UnityStereoScreenSpaceUVAdjust(v.texcoord.xy, _MainTex_ST);
    return o;
    }

    half4 fragAddSub (v2f i) : SV_Target {
    half4 toAdd = tex2D(_Overlay, i.uv[0]) * _Intensity;
    return tex2D(_MainTex, i.uv[1]) + toAdd;
    }
    half4 fragMultiply (v2f i) : SV_Target {
    half4 toBlend = tex2D(_Overlay, i.uv[0]) * _Intensity;
    return tex2D(_MainTex, i.uv[1]) * toBlend;
    }

    half4 fragScreen (v2f i) : SV_Target {
    half4 toBlend = (tex2D(_Overlay, i.uv[0]) * _Intensity);
    return 1-(1-toBlend)*(1-(tex2D(_MainTex, i.uv[1])));
    }
    half4 fragOverlay (v2f i) : SV_Target {
    half4 m = (tex2D(_Overlay, i.uv[0]));// * 255.0;
    half4 color = (tex2D(_MainTex, i.uv[1]));//* 255.0;
    // overlay blend mode
    //color.rgb = (color.rgb/255.0) * (color.rgb + ((2*m.rgb)/( 255.0 )) * (255.0-color.rgb));
    //color.rgb /= 255.0;

    /*
    if (Target > ½) R = 1 - (1-2x(Target-½)) x (1-Blend)
    if (Target <= ½) R = (2xTarget) x Blend
    */

    float3 check = step(half3(0.5,0.5,0.5), color.rgb);
    float3 result = 0;

    result = check * (half3(1,1,1) - ( (half3(1,1,1) - 2*(color.rgb-0.5)) * (1-m.rgb)));
    result += (1-check) * (2*color.rgb) * m.rgb;

    return half4(lerp(color.rgb, result.rgb, (_Intensity)), color.a);
    }

    half4 fragAlphaBlend (v2f i) : SV_Target {
    half4 toAdd = tex2D(_Overlay, i.uv[0]) ;
    return lerp(tex2D(_MainTex, i.uv[1]), toAdd, toAdd.a * _Intensity);
    }
    ENDCG

    Subshader {
    ZTest Always Cull Off ZWrite Off
    ColorMask RGB

    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment fragAddSub
    ENDCG
    }
    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment fragScreen
    ENDCG
    }
    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment fragMultiply
    ENDCG
    }
    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment fragOverlay
    ENDCG
    }

    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment fragAlphaBlend
    ENDCG
    }
    }
    Fallback off

    } // shader
     
    Last edited: Apr 10, 2019
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Last edited: Apr 19, 2019
  4. Unicode77

    Unicode77

    Joined:
    Sep 8, 2020
    Posts:
    2
    Who can i change the Intensity of them trough script?