Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by WiS3 · Apr 05, 2014 at 03:30 PM · blurpost effects

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();
         }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OGNE · Jun 13, 2019 at 06:39 AM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jakhongir · Jan 16, 2016 at 06:24 AM 0
Share

Thanx, could't find it in documentation.

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tatanan · Dec 29, 2014 at 06:38 PM 0
Share

Thank you @taxvi, your answer was very appreciated. But why should we use the Plugins folder?

avatar image taxvi · Dec 30, 2014 at 07:04 AM 0
Share

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.

avatar image
0

Answer by Ermarrero · Apr 05, 2014 at 04:59 PM

Public Blur blur; // drag and drop the blur script from the editor on here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to blur a 2d sprite with transparency? 0 Answers

How to switch OFF motion vectors for SkinnedMeshRenderer ? 0 Answers

Post Process v2 Layer without effects causes weird blur (bad VR experience) 0 Answers

Modifying Image Effect values from script 1 Answer

GUI Texture blur 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges