Skip to content

Instantly share code, notes, and snippets.

@enue
Created February 20, 2017 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enue/181d18412e407cf8f30fcc1bc5f3ac41 to your computer and use it in GitHub Desktop.
Save enue/181d18412e407cf8f30fcc1bc5f3ac41 to your computer and use it in GitHub Desktop.
【Unity】範囲指定可能なブラーエフェクト
using UnityEngine;
using UnityStandardAssets.ImageEffects;
namespace TSKT
{
[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class TextureBlur : PostEffectsBase
{
public float maxBlurSize = 2.0f;
public bool highResolution = false;
public enum BlurSampleCount {
Low = 0,
Medium = 1,
High = 2,
}
public BlurSampleCount blurSampleCount = BlurSampleCount.High;
public Shader dofHdrShader;
private Material dofHdrMaterial = null;
public Texture cocTexture;
public override bool CheckResources ()
{
CheckSupport(false);
if (!dofHdrShader)
{
dofHdrShader = Shader.Find("Hidden/Dof/DepthOfFieldHdr");
}
dofHdrMaterial = CheckShaderAndCreateMaterial (dofHdrShader, dofHdrMaterial);
if (!isSupported)
{
ReportAutoDisable();
}
return isSupported;
}
void OnDisable()
{
if (dofHdrMaterial)
{
DestroyImmediate(dofHdrMaterial);
}
dofHdrMaterial = null;
}
private void WriteCoc ( RenderTexture fromTo)
{
dofHdrMaterial.SetTexture("_FgOverlap", cocTexture);
fromTo.MarkRestoreExpected(); // only touching alpha channel, RT restore expected
Graphics.Blit (fromTo, fromTo, dofHdrMaterial, 13);
}
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
if (!CheckResources ())
{
Graphics.Blit (source, destination);
return;
}
var internalBlurWidth = Mathf.Max(maxBlurSize, 0.0f);
source.filterMode = FilterMode.Bilinear;
WriteCoc (source);
int blurPass = (blurSampleCount == BlurSampleCount.High || blurSampleCount == BlurSampleCount.Medium) ? 17 : 11;
if (highResolution)
{
internalBlurWidth *= 2.0f;
dofHdrMaterial.SetVector ("_Offsets", new Vector4 (0.0f, internalBlurWidth, 0.025f, internalBlurWidth));
Graphics.Blit (source, destination, dofHdrMaterial, blurPass);
}
else
{
var rtLow = RenderTexture.GetTemporary(source.width >> 1, source.height >> 1, 0, source.format);
var rtLow2 = RenderTexture.GetTemporary(source.width >> 1, source.height >> 1, 0, source.format);
dofHdrMaterial.SetVector ("_Offsets", new Vector4 (0.0f, internalBlurWidth, 0.1f, internalBlurWidth));
// blur
Graphics.Blit (source, rtLow, dofHdrMaterial, 6);
Graphics.Blit (rtLow, rtLow2, dofHdrMaterial, blurPass);
// cheaper blur in high resolution, upsample and combine
dofHdrMaterial.SetTexture("_LowRez", rtLow2);
dofHdrMaterial.SetTexture("_FgOverlap", null);
dofHdrMaterial.SetVector ("_Offsets", Vector4.one * ((1.0f*source.width)/(1.0f*rtLow2.width)) * internalBlurWidth);
Graphics.Blit (source, destination, dofHdrMaterial, blurSampleCount == BlurSampleCount.High ? 18 : 12);
RenderTexture.ReleaseTemporary(rtLow);
RenderTexture.ReleaseTemporary(rtLow2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment