Skip to content

Instantly share code, notes, and snippets.

@divide-by-zero
Last active May 30, 2016 20:02
Show Gist options
  • Save divide-by-zero/02f05a76c073a9ce5e0d to your computer and use it in GitHub Desktop.
Save divide-by-zero/02f05a76c073a9ce5e0d to your computer and use it in GitHub Desktop.
Animation Sprite Blur Effect
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class BlurTest : MonoBehaviour {
private SpriteRenderer _spriteRenderer;
private Dictionary<Sprite, Sprite> _blurSprites = new Dictionary<Sprite, Sprite>();
private SpriteRenderer _blurSpriteRenderer;
private Texture2D _blurTexture;
// Use this for initialization
void Start ()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
_blurTexture = BuildTexture.CreateBlurTexture(_spriteRenderer.sprite.texture, 3.0f);
var obj = new GameObject("BlurSprite");
obj.transform.SetParent(this.transform);
obj.transform.localPosition = Vector3.zero;
_blurSpriteRenderer = obj.AddComponent<SpriteRenderer>();
_blurSpriteRenderer.sortingOrder = -1;
_blurSpriteRenderer.color = Color.yellow;
StartCoroutine(AnimationIterator());
}
private IEnumerator AnimationIterator()
{
float alpha = 1.0f;
while (true)
{
while (alpha < 1.0f) {
alpha += Time.deltaTime;
var color = _blurSpriteRenderer.color;
color.a = alpha;
_blurSpriteRenderer.color = color;
yield return null;
}
while (alpha > 0.5f) {
alpha -= Time.deltaTime;
var color = _blurSpriteRenderer.color;
color.a = alpha;
_blurSpriteRenderer.color = color;
yield return null;
}
}
}
// Update is called once per frame
void Update () {
if (_blurSprites.ContainsKey(_spriteRenderer.sprite))
{
_blurSpriteRenderer.sprite = _blurSprites[_spriteRenderer.sprite];
}
else
{
var spr = _spriteRenderer.sprite;
_blurSprites.Add(spr,Sprite.Create(_blurTexture,spr.rect,new Vector2(0.5f,0.5f),16.0f));
}
}
}
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
public static class BuildTexture {
private static Dictionary<Texture2D, Texture2D> _textureCache = new Dictionary<Texture2D, Texture2D>();
public static Texture2D CreateBlurTexture(Texture2D tex, float sig, bool isCache = true) {
if (isCache && _textureCache.ContainsKey(tex)) return _textureCache[tex];
int W = tex.width;
int H = tex.height;
int Wm = (int)(Mathf.Ceil(3.0f * sig) * 2 + 1);
int Rm = (Wm - 1) / 2;
//フィルタ
float[] msk = new float[Wm];
sig = 2 * sig * sig;
float div = Mathf.Sqrt(sig * Mathf.PI);
//フィルタの作成
for (int x = 0; x < Wm; x++) {
int p = (x - Rm) * (x - Rm);
msk[x] = Mathf.Exp(-p / sig) / div;
}
var src = tex.GetPixels(0).Select(x => x.a).ToArray();
var tmp = new float[src.Length];
var dst = new Color[src.Length];
//垂直方向
for (int x = 0; x < W; x++) {
for (int y = 0; y < H; y++) {
float sum = 0;
for (int i = 0; i < Wm; i++) {
int p = y + i - Rm;
if (p < 0 || p >= H) continue;
sum += msk[i] * src[x + p * W];
}
tmp[x + y * W] = sum;
}
}
//水平方向
for (int x = 0; x < W; x++) {
for (int y = 0; y < H; y++) {
float sum = 0;
for (int i = 0; i < Wm; i++) {
int p = x + i - Rm;
if (p < 0 || p >= W) continue;
sum += msk[i] * tmp[p + y * W];
}
dst[x + y * W] = new Color(1, 1, 1, sum);
}
}
var createTexture = new Texture2D(W, H);
createTexture.SetPixels(dst);
createTexture.Apply();
if (isCache) {
_textureCache.Add(tex, createTexture);
}
return createTexture;
}
public static void Release() {
_textureCache.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment