Last active
February 28, 2017 15:30
-
-
Save enue/6b8335f97e9fa24c61dd8e06e7837ea1 to your computer and use it in GitHub Desktop.
[Unity] ShakeColor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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