- Home /
Change Blur effect at runtime
How i can access the Blur script attached to a camera in order to enable/disable/change parameters at runtime?
If i use GetComponent() i can only access the old BlurEffect, but not Blur.
Edit
I have found the problem.
Since i am using C# for my scripts, the new Blur effect was Javascript, and i couldn't access it from C# script.
I have read something about Compilation order, and the possibility to access JS script from C#, but also that is a bas idea to use Both.
So i converted the Blur.js to C# along PostImageEffect.js, because Blur inherit from it.
And here they are, if you need them :)
Blur.cs
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Blur/Blur (Optimized) C#")]
[RequireComponent(typeof(Camera))]
class Blur : PostEffectsBase
{
[Range(0, 2)]
public int downsample = 1;
public enum BlurType {
StandardGauss = 0,
SgxGauss = 1,
}
[Range(0.0f, 10.0f)]
public float blurSize = 3.0f;
[Range(1, 4)]
public int blurIterations = 2;
public int blurType= (int)BlurType.StandardGauss;
public Shader blurShader;
private Material blurMaterial;
bool CheckResources (){
CheckSupport (false);
blurMaterial = CheckShaderAndCreateMaterial (blurShader, blurMaterial);
if(!isSupported)
ReportAutoDisable ();
return isSupported;
}
void OnDisable (){
if(blurMaterial)
DestroyImmediate (blurMaterial);
}
void OnRenderImage ( RenderTexture source , RenderTexture destination ){
if(CheckResources() == false) {
Graphics.Blit (source, destination);
return;
}
float widthMod = 1.0f / (1.0f * (1<<downsample));
blurMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod, -blurSize * widthMod, 0.0f, 0.0f));
source.filterMode = FilterMode.Bilinear;
int rtW = source.width >> downsample;
int rtH = source.height >> downsample;
// downsample
RenderTexture rt = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
rt.filterMode = FilterMode.Bilinear;
Graphics.Blit (source, rt, blurMaterial, 0);
int passOffs = blurType == (int) BlurType.StandardGauss ? 0 : 2;
for(int i = 0; i < blurIterations; i++) {
float iterationOffs = (i*1.0f);
blurMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod + iterationOffs, -blurSize * widthMod - iterationOffs, 0.0f, 0.0f));
// vertical blur
RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
rt2.filterMode = FilterMode.Bilinear;
Graphics.Blit (rt, rt2, blurMaterial, 1 + passOffs);
RenderTexture.ReleaseTemporary (rt);
rt = rt2;
// horizontal blur
rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);
rt2.filterMode = FilterMode.Bilinear;
Graphics.Blit (rt, rt2, blurMaterial, 2 + passOffs);
RenderTexture.ReleaseTemporary (rt);
rt = rt2;
}
Graphics.Blit (rt, destination);
RenderTexture.ReleaseTemporary (rt);
}
}
PostEffectsBase.cs
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {
protected bool supportHDRTextures = true;
protected bool supportDX11 = false;
protected bool isSupported = true;
public Material CheckShaderAndCreateMaterial ( Shader s , Material m2Create ){
if (!s) {
Debug.Log("Missing shader in " + this.ToString ());
enabled = false;
return null;
}
if (s.isSupported && m2Create && m2Create.shader == s)
return m2Create;
if (!s.isSupported) {
NotSupported ();
Debug.Log("The shader " + s.ToString() + " on effect "+this.ToString()+" is not supported on this platform!");
return null;
}
else {
m2Create = new Material (s);
m2Create.hideFlags = HideFlags.DontSave;
if (m2Create)
return m2Create;
else return null;
}
}
Material CreateMaterial ( Shader s , Material m2Create ){
if (!s) {
Debug.Log ("Missing shader in " + this.ToString ());
return null;
}
if (m2Create && (m2Create.shader == s) && (s.isSupported))
return m2Create;
if (!s.isSupported) {
return null;
}
else {
m2Create = new Material (s);
m2Create.hideFlags = HideFlags.DontSave;
if (m2Create)
return m2Create;
else return null;
}
}
void OnEnable (){
isSupported = true;
}
public bool CheckSupport (){
return CheckSupport (false);
}
bool CheckResources (){
Debug.LogWarning ("CheckResources () for " + this.ToString() + " should be overwritten.");
return isSupported;
}
void Start (){
CheckResources ();
}
public bool CheckSupport ( bool needDepth ){
isSupported = true;
supportHDRTextures = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf);
supportDX11 = SystemInfo.graphicsShaderLevel >= 50 && SystemInfo.supportsComputeShaders;
if (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures) {
NotSupported ();
return false;
}
if(needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth)) {
NotSupported ();
return false;
}
if(needDepth)
camera.depthTextureMode |= DepthTextureMode.Depth;
return true;
}
bool CheckSupport ( bool needDepth , bool needHdr ){
if(!CheckSupport(needDepth))
return false;
if(needHdr && !supportHDRTextures) {
NotSupported ();
return false;
}
return true;
}
bool Dx11Support (){
return supportDX11;
}
public void ReportAutoDisable (){
Debug.LogWarning ("The image effect " + this.ToString() + " has been disabled as it's not supported on the current platform.");
}
// deprecated but needed for old effects to survive upgrading
bool CheckShader ( Shader s ){
Debug.Log("The shader " + s.ToString () + " on effect "+ this.ToString () + " is not part of the Unity 3.2f+ effects suite anymore. For best performance and quality, please ensure you are using the latest Standard Assets Image Effects (Pro only) package.");
if (!s.isSupported) {
NotSupported ();
return false;
}
else {
return false;
}
}
void NotSupported (){
enabled = false;
isSupported = false;
return;
}
void DrawBorder ( RenderTexture dest , Material material ){
float x1;
float x2;
float y1;
float y2;
RenderTexture.active = dest;
bool invertY = true; // source.texelSize.y < 0.0ff;
// Set up the simple Matrix
GL.PushMatrix();
GL.LoadOrtho();
for (int i = 0; i < material.passCount; i++)
{
material.SetPass(i);
float y1_; float y2_;
if (invertY)
{
y1_ = 1.0f; y2_ = 0.0f;
}
else
{
y1_ = 0.0f; y2_ = 1.0f;
}
// left
x1 = 0.0f;
x2 = 0.0f + 1.0f/(dest.width*1.0f);
y1 = 0.0f;
y2 = 1.0f;
GL.Begin(GL.QUADS);
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
// right
x1 = 1.0f - 1.0f/(dest.width*1.0f);
x2 = 1.0f;
y1 = 0.0f;
y2 = 1.0f;
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
// top
x1 = 0.0f;
x2 = 1.0f;
y1 = 0.0f;
y2 = 0.0f + 1.0f/(dest.height*1.0f);
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
// bottom
x1 = 0.0f;
x2 = 1.0f;
y1 = 1.0f - 1.0f/(dest.height*1.0f);
y2 = 1.0f;
GL.TexCoord2(0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
GL.TexCoord2(1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
GL.TexCoord2(1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
GL.TexCoord2(0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
GL.End();
}
GL.PopMatrix();
}
}
Thanks man...my assets had posteffectsbase.cs missing and it was causing me so many errors. Thanks to you and some editing I got $$anonymous$$e to work
Answer by vfxjex · Jul 20, 2015 at 01:11 AM
To those who are using v5 or higher you might need this
using UnityStandardAssets.ImageEffects;
to access image effects like Blur;
Answer by taxvi · Dec 25, 2014 at 09:04 AM
mate, you are overkilling it. you can just drag the whole folder of the Image Effects Pro in the Plugins folder. Everything in Plugins folder will compile first and get available for use in the C# scripts (and vice versa). But if you are using MonoDevelop than most probably it won't detect the change in compilation order and will still give you red lines when using the new classes from Plugins but you can just ignore them and hardcode everything, unity will compile OK.
Thank you @taxvi, your answer was very appreciated. But why should we use the Plugins folder?
there is an order in which unity is reading your classes. as far as I know the c# classes are compiled before JS classes so sometimes the c# scrip is requesting a class that is JS and the compiler has not yet gone through it thus throwing an exception thinking that that class does not exist. now when you put your scripts in Plugins folder you are pushing Unity to compile those first to avoid the exceptions.
Answer by Ermarrero · Apr 05, 2014 at 04:59 PM
Public Blur blur; // drag and drop the blur script from the editor on here.