Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vares · Aug 31, 2017 at 10:27 PM · 2dshadershadersshader programming

How do i get a shader to affect every object with the same material?

Hi!
I'm trying to implement a shader that affects all objects with a certain material. The basic idea is to have a GameObject Blurrer with a script that sets the radius and position for the shader, like so: alt text

The rock and tree and the area around them is blurred!

Everything in the scene has the same material applied.

My first approach was to have two variables in the shader for radius and position, which i update from a script attached to the Blurrer object:

 void Update () 
 {
     renderer.material.SetVector("_BlurPos", this.transform.position);
     renderer.material.SetFloat("_BlurRadius", 200);
 }

The blurrer object would be under both the Rock GameObject and the Tree GameObject in the example. The problem was that the shader only affects pixels within the object sprite itself, so the result was something like this: alt text

Rock and Tree are blurred within radius, but the surrounding area is not

Looks like a shader only iterates over the pixels within the sprite of the particular renderer. My workaround was to have global variables in the shader, which hold the position and radius of all Blurrer GameObjects and they would add themself to this list:

 //BlurScript.cs
 private static List<Vector4> blurPositions= new List<Vector4>(10);
 private static int index = -1;
 void Start ()
 {
     Shader.SetGlobalVectorArray("G_BlurPositions", blurPositions);
     index++;
 }
     
 void Update()
 { 
     blurPositions[index] = this.transform.position;);
     Shader.SetGlobalVectorArray("G_BlurPositions", blurPositions);
 }
 void OnDestroy()
 {
     blurPositions.RemoveAt(index);
     Shader.SetGlobalVectorArray("G_BlurPositions", blurPositions);
     index--;
 }

I was able to get the solution working as each instance had access to all the positional data. However, this solution feels very hacky and I have no idea how to get a dynamic size array for a shader (currently using uniform float4 G_BlurPositions[10] ) I am new to writing shaders and would like some help with this issue:

  1. Are shaders even the right thing to use in implementing something like this? Are there other options?

  2. Is there any other mechanism for sharing data between shaders? In my case positions and radiuses.

  3. What is the usual way of implementing something like this?

rock-and-tree-blurr.png (166.3 kB)
blurr-fail.png (141.6 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

176 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 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 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 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 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 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 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

Getting Color Generated from Shader to Script to Shader 0 Answers

Very basic shader problem 0 Answers

Converted ShaderToy shader showing up black 1 Answer

why outline is drawed only on front side of skinned mesh render 1 Answer

Render oject A when behind object B, but not if behind object B and object C 1 Answer


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