Create a gist now

Instantly share code, notes, and snippets.

@enue /ShakeColor.cs
Last active Feb 28, 2017

[Unity] ShakeColor
using System;
using UnityEngine;
using UnityStandardAssets.ImageEffects;
namespace TSKT
{
[RequireComponent(typeof(Camera))]
public class ShakeColor : ImageEffectBase
{
[Range(0.01f, 1f)]
public float intensity = 0.01f;
int _RedOffsetPropertyId;
int _BlueOffsetPropertyId;
int _GreenOffsetPropertyId;
void Awake()
{
_RedOffsetPropertyId = Shader.PropertyToID("_RedOffset");
_BlueOffsetPropertyId = Shader.PropertyToID("_BlueOffset");
_GreenOffsetPropertyId = Shader.PropertyToID("_GreenOffset");
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
material.SetVector(_RedOffsetPropertyId, GenerateOffset(intensity));
material.SetVector(_BlueOffsetPropertyId, GenerateOffset(intensity));
material.SetVector(_GreenOffsetPropertyId, GenerateOffset(intensity));
Graphics.Blit(source, destination, material);
}
static Vector2 GenerateOffset(float intensity)
{
return new Vector2(
UnityEngine.Random.Range(-intensity, intensity),
UnityEngine.Random.Range(-intensity, intensity));
}
}
}
Shader "TSKT/Hidden/ShakeColor Effect"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_RedOffset("RedOffset", Vector) = (0.0, 0.0, 0.0, 0.0)
_BlueOffset("BlueOffset", Vector) = (0.0, 0.0, 0.0, 0.0)
_GreenOffset("GreenOffset", Vector) = (0.0, 0.0, 0.0, 0.0)
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
half4 _RedOffset;
half4 _BlueOffset;
half4 _GreenOffset;
fixed4 frag (v2f_img i) : SV_Target
{
fixed a = tex2D(_MainTex, i.uv).a;
fixed r = tex2D(_MainTex, i.uv + _RedOffset.xy).r;
fixed g = tex2D(_MainTex, i.uv + _GreenOffset.xy).g;
fixed b = tex2D(_MainTex, i.uv + _BlueOffset.xy).b;
return fixed4(r, g, b, a);
}
ENDCG
}
}
Fallback off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment