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
2
Question by smnerat · Dec 30, 2012 at 11:37 PM · colorrenderer

How to redner a single texture, multiple colors without increasing draw calls.

Here is some background: I'm using a single material and rendering it one of four colors depending on its tag. As a test, if I have 20 objects in my scene without a script attached, my draw calls are low because all objects are getting batched.

My problem arises after adding my script. I would expect my draw calls to increase by 4 (1 for each color). But the draw calls are over 350. I did some searching and saw that using renderer.sharedMaterial would lower the enormous amount of draw calls I have and using renderer.sharedMaterial on the first color seems to work. When I put it in my code for the rest of the colors, no matter what tag the object has, it is rendered the first color. So instead of 5/5/5/5 (5 objects for each color) I am getting 20/0/0/0. I'm hoping someone can see an error in my code or at least put me on the right path.

Also, before I forget, I am using a diffuse shader.

 #pragma strict
 
 
 static function RenderChange(first : Transform){
 
     var allChildren = first.GetComponentsInChildren(Transform);
     
     if (first.tag == "1"){    
         
         for (var child : Transform in allChildren) {
             
             child.renderer.sharedMaterial.color = Color.cyan;
             first.tag = "2";
             
         }
     }
         
     else if (first.tag == "2"){    
     
         for (var child : Transform in allChildren) {
 
             child.renderer.material.color = Color.magenta;
             first.tag = "3";
         
         }
     }
     
     else if (first.tag == "3"){    
     
         for (var child : Transform in allChildren) {
 
             child.renderer.material.color = Color.red;
             first.tag = "4";
         
         }
     }
 
     else if (first.tag == "4"){    
     
         for (var child : Transform in allChildren) {
 
             child.renderer.material.color = Color.green;
             first.tag = "1";
         
         }
     }
 
     first = null;
     
 }
Comment
Add comment · Show 6
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 smnerat · Dec 31, 2012 at 07:55 PM 0
Share

Update: I've created two separate objects and put a different material on each one. I was trying to see if I could get the code above to work with two different materials but the same problem occurs. Even with different materials, adding shared$$anonymous$$aterial in the code colors everything the same color.

avatar image whydoidoit · Dec 31, 2012 at 08:16 PM 0
Share

You will need a custom shader and use control textures that use uv2 for each object to lookup the tint colour. It's pretty advanced stuff and I'm not at my computer right now but I could possibly help.

Do you know how to write shaders?

avatar image smnerat · Dec 31, 2012 at 08:51 PM 0
Share

No, I'm pretty new to unity. I've gotten done some player control scripts, manipulated transforms and game objects, and some other basics. I'm now moving into optimization stuff like draw calls and occlusion culling for the first time and I'm kind of at a loss. I would appreciate any kind of help you could provide, I've been trying on and off for a day or two now and haven't been getting anywhere on this.

avatar image whydoidoit · Dec 31, 2012 at 11:04 PM 0
Share

So I'm also new to shader program$$anonymous$$g too but I wrote a Noobs Guide on Unity Gems that could serve as a starting point to learning. Using Uv2 or vertex colours is the way to go, shaders aren't actually so hard when you get the $$anonymous$$dset. Check that out and ill post more when at the laptop.

avatar image smnerat · Jan 08, 2013 at 01:47 AM 0
Share

So it's been a while, but I finally looked into this a bit more this weekend and my head almost exploded. I definitely don't know enough to write my own shader yet. I found a $$anonymous$$asked Tint - Vertex Lit shader online but wasn't able to get it working properly, but I'm thinking that its my best option at this point, I think I have an idea of how it works but I have no idea how to implement it based on tags as I mentioned in my question.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jogo13 · Dec 31, 2012 at 09:05 PM

renderer.sharedMaterial does lower the draw calls, but you can't set seperated material.color values because it's all the same single material (shared.)

To avoid needing seperate materials you could find/create a material that gets it's color from vertex color values. You'd need to set the vertex color values via script. They can be found at mesh.colors[];

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 smnerat · Jan 11, 2013 at 07:08 PM 0
Share

This is a link to the shader that I used: http://wiki.unity3d.com/index.php/$$anonymous$$asked_Tint

I used the second shader, which is "$$anonymous$$asked Tint - Vertex Colors" without any modifications. Then using the script in my original question, when a tag was changed, I just set vertex colors ins$$anonymous$$d of changing the material color.

I now have 5 draw calls with 340 batched.

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

10 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Renderer and Color are unknown to unity. Why? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Accessing material of particle renderer?? 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