Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Forked from jimfleming/Blur.cs
Created April 8, 2024 18:09
Show Gist options
  • Select an option

  • Save unitycoder/fa8301b46f5f58f3ada76f499dfb593d to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/fa8301b46f5f58f3ada76f499dfb593d to your computer and use it in GitHub Desktop.

Revisions

  1. @jimfleming jimfleming renamed this gist May 21, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jimfleming jimfleming revised this gist May 15, 2015. 1 changed file with 36 additions and 39 deletions.
    75 changes: 36 additions & 39 deletions HasteBlur.cs
    Original file line number Diff line number Diff line change
    @@ -7,55 +7,52 @@
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    namespace Haste {
    public class Blur {

    public class HasteBlur {
    Color tint = Color.black;
    float tinting = 0.4f;
    float blurSize = 4.0f;
    int passes = 8;

    Color tint = Color.black;
    float tinting = 0.4f;
    float blurSize = 4.0f;
    int passes = 8;
    Material blurMaterial;
    RenderTexture destTexture;

    Material blurMaterial;
    RenderTexture destTexture;
    public Blur(int width, int height) {
    blurMaterial = new Material(Shader.Find("Hidden/Blur"));
    blurMaterial.SetColor("_Tint", tint);
    blurMaterial.SetFloat("_Tinting", tinting);
    blurMaterial.SetFloat("_BlurSize", blurSize);

    public HasteBlur(int width, int height) {
    blurMaterial = new Material(Shader.Find("Hidden/Haste/Blur"));
    blurMaterial.SetColor("_Tint", tint);
    blurMaterial.SetFloat("_Tinting", tinting);
    blurMaterial.SetFloat("_BlurSize", blurSize);

    destTexture = new RenderTexture(width, height, 0);
    destTexture.Create();
    }
    destTexture = new RenderTexture(width, height, 0);
    destTexture.Create();
    }

    public Texture BlurTexture(Texture sourceTexture) {
    RenderTexture active = RenderTexture.active; // Save original RenderTexture
    public Texture BlurTexture(Texture sourceTexture) {
    RenderTexture active = RenderTexture.active; // Save original RenderTexture so we can restore when we're done.

    try {
    RenderTexture tempA = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
    RenderTexture tempB = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
    try {
    RenderTexture tempA = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
    RenderTexture tempB = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);

    for (int i = 0; i < passes; i++) {
    if (i == 0) {
    Graphics.Blit(sourceTexture, tempA, blurMaterial, 0);
    } else {
    Graphics.Blit(tempB, tempA, blurMaterial, 0);
    }
    Graphics.Blit(tempA, tempB, blurMaterial, 1);
    for (int i = 0; i < passes; i++) {
    if (i == 0) {
    Graphics.Blit(sourceTexture, tempA, blurMaterial, 0);
    } else {
    Graphics.Blit(tempB, tempA, blurMaterial, 0);
    }

    Graphics.Blit(tempB, destTexture, blurMaterial, 2);

    RenderTexture.ReleaseTemporary(tempA);
    RenderTexture.ReleaseTemporary(tempB);
    } catch (Exception e) {
    Debug.LogException(e);
    } finally {
    RenderTexture.active = active; // Restore
    Graphics.Blit(tempA, tempB, blurMaterial, 1);
    }

    return destTexture;
    Graphics.Blit(tempB, destTexture, blurMaterial, 2);

    RenderTexture.ReleaseTemporary(tempA);
    RenderTexture.ReleaseTemporary(tempB);
    } catch (Exception e) {
    Debug.LogException(e);
    } finally {
    RenderTexture.active = active; // Restore
    }

    return destTexture;
    }
    }
  3. @jimfleming jimfleming created this gist Dec 7, 2014.
    61 changes: 61 additions & 0 deletions HasteBlur.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    using UnityEngine;
    using UnityEditor;
    using System;
    using System.IO;
    using System.Linq;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    namespace Haste {

    public class HasteBlur {

    Color tint = Color.black;
    float tinting = 0.4f;
    float blurSize = 4.0f;
    int passes = 8;

    Material blurMaterial;
    RenderTexture destTexture;

    public HasteBlur(int width, int height) {
    blurMaterial = new Material(Shader.Find("Hidden/Haste/Blur"));
    blurMaterial.SetColor("_Tint", tint);
    blurMaterial.SetFloat("_Tinting", tinting);
    blurMaterial.SetFloat("_BlurSize", blurSize);

    destTexture = new RenderTexture(width, height, 0);
    destTexture.Create();
    }

    public Texture BlurTexture(Texture sourceTexture) {
    RenderTexture active = RenderTexture.active; // Save original RenderTexture

    try {
    RenderTexture tempA = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
    RenderTexture tempB = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);

    for (int i = 0; i < passes; i++) {
    if (i == 0) {
    Graphics.Blit(sourceTexture, tempA, blurMaterial, 0);
    } else {
    Graphics.Blit(tempB, tempA, blurMaterial, 0);
    }
    Graphics.Blit(tempA, tempB, blurMaterial, 1);
    }

    Graphics.Blit(tempB, destTexture, blurMaterial, 2);

    RenderTexture.ReleaseTemporary(tempA);
    RenderTexture.ReleaseTemporary(tempB);
    } catch (Exception e) {
    Debug.LogException(e);
    } finally {
    RenderTexture.active = active; // Restore
    }

    return destTexture;
    }
    }
    }