blue
blue

Reputation: 7385

Unity shader/C# - how to change colors of effect?

Ok so Im using this glitch effect (the analog one) that features a color "drift"

https://github.com/keijiro/KinoGlitch

It comes with a shader and this Analog Glitch script (all in link):

 [ExecuteInEditMode]
    [RequireComponent(typeof(Camera))]
    [AddComponentMenu("Kino Image Effects/Analog Glitch")]
    public class AnalogGlitch : MonoBehaviour
    {
        #region Public Properties

        // Scan line jitter

        [SerializeField, Range(0, 1)]
        float _scanLineJitter = 0;

        public float scanLineJitter {
            get { return _scanLineJitter; }
            set { _scanLineJitter = value; }
        }

        // Vertical jump

        [SerializeField, Range(0, 1)]
        float _verticalJump = 0;

        public float verticalJump {
            get { return _verticalJump; }
            set { _verticalJump = value; }
        }

        // Horizontal shake

        [SerializeField, Range(0, 1)]
        float _horizontalShake = 0;

        public float horizontalShake {
            get { return _horizontalShake; }
            set { _horizontalShake = value; }
        }

        // Color drift

        [SerializeField, Range(0, 1)]
        float _colorDrift = 0;

        public float colorDrift {
            get { return _colorDrift; }
            set { _colorDrift = value; }
        }

        #endregion

        #region Private Properties

        [SerializeField] Shader _shader;

        Material _material;

        float _verticalJumpTime;

        #endregion

        #region MonoBehaviour Functions

        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (_material == null)
            {
                _material = new Material(_shader);
                _material.hideFlags = HideFlags.DontSave;
            }

            _verticalJumpTime += Time.deltaTime * _verticalJump * 11.3f;

            var sl_thresh = Mathf.Clamp01(1.0f - _scanLineJitter * 1.2f);
            var sl_disp = 0.002f + Mathf.Pow(_scanLineJitter, 3) * 0.05f;
            _material.SetVector("_ScanLineJitter", new Vector2(sl_disp, sl_thresh));

            var vj = new Vector2(_verticalJump, _verticalJumpTime);
            _material.SetVector("_VerticalJump", vj);

            _material.SetFloat("_HorizontalShake", _horizontalShake * 0.2f);

            var cd = new Vector2(_colorDrift * 0.04f, Time.time * 606.11f);
            _material.SetVector("_ColorDrift", cd);

            Graphics.Blit(source, destination, _material);
        }

        #endregion

I need to find out how to change the colors that it "glitches" in - as you can see right now it glitches green and magenta. I need red and blue but cant find specific color values.

I can post shader as needed - how can I achieve this glitch but in red/blue in unity?

SHADER:

Shader "Hidden/Kino/Glitch/Analog"
{
    Properties
    {
        _MainTex ("-", 2D) = "" {}
    }
    CGINCLUDE

    #include "UnityCG.cginc"

    sampler2D _MainTex;
    float2 _MainTex_TexelSize;

    float2 _ScanLineJitter; // (displacement, threshold)
    float2 _VerticalJump;   // (amount, time)
    float _HorizontalShake;
    float2 _ColorDrift;     // (amount, time)

    float nrand(float x, float y)
    {
        return frac(sin(dot(float2(x, y), float2(12.9898, 78.233))) * 43758.5453);
    }

    half4 frag(v2f_img i) : SV_Target
    {
        float u = i.uv.x;
        float v = i.uv.y;

        // Scan line jitter
        float jitter = nrand(v, _Time.x) * 2 - 1;
        jitter *= step(_ScanLineJitter.y, abs(jitter)) * _ScanLineJitter.x;

        // Vertical jump
        float jump = lerp(v, frac(v + _VerticalJump.y), _VerticalJump.x);

        // Horizontal shake
        float shake = (nrand(_Time.x, 2) - 0.5) * _HorizontalShake;

        // Color drift
        float drift = sin(jump + _ColorDrift.y) * _ColorDrift.x;

        half4 src1 = tex2D(_MainTex, frac(float2(u + jitter + shake, jump)));
        half4 src2 = tex2D(_MainTex, frac(float2(u + jitter + shake + drift, jump)));

        return half4(src1.r, src2.g, src1.b, 1);
    }

    ENDCG
    SubShader
    {
        Pass
        {
            ZTest Always Cull Off ZWrite Off
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
            #pragma target 3.0
            ENDCG
        }
    }

Upvotes: 2

Views: 1778

Answers (1)

_ColorDrift is used to generate a local variable in the shader called drift, this value is then only used in one location to generate the src2 texture.

This texture is then used once, return half4(src1.r, src2.g, src1.b, 1);

Notice that the src2 texture only supplies its Green value. This means that the green channel is offset (generating Green and Anti-Green (Magenta) distortions).

You can adjust this return line to generate Red/Anti-Red (Cyan) distortions or Blue/Anti-Blue (Yellow) distortions. Just change which color value the src2 variable applies to.

Red/Cyan:
return half4(src2.r, src1.g, src1.b, 1);

Blue/Yellow:
return half4(src1.r, src1.g, src2.b, 1);

Note that applying src2 to two of the color channels just flip-flops the distortion (Red/Anti-Red -> Anti-Red/Red) and makes it more intense (the apparent distance the texture jumps is greater).

Upvotes: 2

Related Questions