• 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
0
Question by · Oct 19, 2016 at 04:05 PM · colorvertexbatchingdrawcalls

Changing vertex colors break batching?

Hello everyone, i have to recolor some meshes on my scene, and since shader i use can work with vertex color, so i decided to try just change colors of vertexes runtime. But then i faced with increasing batches. Recolored mesh doesn't batch with others, despite they share one material. I decided to check it again and create empty project with two standart unity's quads. Material use shader (Particles/AlphaBlended) and "Default particle" as Texture. Then i attached to one of quads script, that change its colors.

     void Start () {
     mesh = this.GetComponent<MeshFilter>().mesh;
     Color[] colors = new Color[mesh.vertexCount];
     for (int i = 0; i < colors.Length; i++)
         colors[i] = Color.black;
     mesh.colors = colors;
 }
     

But after all i get 2 batches, instead of 1. Enable script = 2 batches, disable script = 1batch. Same material, only 4 verts in each object. What have i missed? Where is my mistake? Shouldn't it be batched?

UPDATE I managed to get quads batched. First of all i changed in script mesh property, to sharedMesh, which is according to Unitydocs actuall asset in library. After that, as expected, all my quads became black. Then i changed script again and put mesh thing back. And here we are, 1 batch for at least 6 squads colored with different colors. Anybody have a suggestions why is it works like that? I suspect, that i forget to turn off some checkbox somewhere, or read some manual article. Looks like, that changing shared mesh colors change something else as well. Any help?

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 tanoshimi · Oct 20, 2016 at 10:02 PM 0
Share

That's.... weird! $$anonymous$$aybe changing to shared$$anonymous$$esh and back again to mesh woke up something in the rendering pipeline? Can you recreate that fix in a new project?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Oct 19, 2016 at 06:26 PM

There's nothing wrong with your understanding or your code. Vertex colours are assigned to the mesh, not material properties, so you can batch together meshes with different coloured verts just fine.

I can produce any number of different coloured quads using the steps you describe, as shown below (note that in order to get down to just one batch, I had to set the camera flag to "Don't Clear" - drawing the background/skybox takes at least one batch on its own - I wonder if that is skewing your figures?) alt text

Also be sure to have batching enabled under Project -> Player Settings.


batching.jpg (105.8 kB)
Comment
Add comment · Show 7 · 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 · Oct 19, 2016 at 08:26 PM 0
Share

Hi, thank you for answer. I used camera flag background, but with using "don't clear" i get same results. Only if change flag to skybox i see additional drawcall. Batching is enabled. Here what i get.

alt text

alt text

batching-scene.png (149.4 kB)
batching-player-settings.png (38.8 kB)
avatar image tanoshimi · Oct 19, 2016 at 08:31 PM 0
Share

But you will get additional drawcalls for the skybox - that's to be expected. But, choose a solid colour background, say, and you'll get two batches - one for the background, and one for as many vertex-coloured meshes as share the same material as you want.

avatar image tanoshimi · Oct 19, 2016 at 08:56 PM 0
Share

I chose don't clear and get 2 batches as well, that what i wanted to say. And 3 batches, if i choose skybox, since skybox also do drawcall, as you mentioned above. Sorry for misunderstanding

avatar image · Oct 19, 2016 at 08:39 PM 0
Share

$$anonymous$$ore than that. This recolored quads don't even want to batch with each other. Each additional quad adds 1 drawcall. Enabling and disabling dynamic batching in player settings affects immediately with 2 drawcalls for non-colored quads. alt text

alt text

batching-scene-2.png (54.1 kB)
batching-scene-3.png (61.3 kB)
avatar image tanoshimi · Oct 19, 2016 at 08:59 PM 1
Share

If I recreate that scene (I'm using 5.3.3f1) I get 2 batches as expected, so there must be something else different about your scene, project settings, or something that's preventing batching occurring.

avatar image tanoshimi · Oct 19, 2016 at 10:01 PM 0
Share

I also tried to recreate same situation in Unity 4.6 and of course it works perfectly well there. 1 drawcall for all quads. In Unity 5.4.0 problem remains. Well, anyway, thank you for help and patience

Show more comments
avatar image
-1

Answer by flaviusxvii · Oct 19, 2016 at 05:08 PM

Ignore these lies.. ---Changing the vertex colors is building a new set of data that has to be sent to the gpu separately. That means a new batch.---

Comment
Add comment · Show 3 · 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 tanoshimi · Oct 19, 2016 at 06:26 PM 0
Share

Huh? No it doesn't. It's just adding properties to vertices of the existing mesh data.

avatar image flaviusxvii tanoshimi · Oct 19, 2016 at 10:41 PM 0
Share

You're right. I was remembering this: http://answers.unity3d.com/questions/525327/batch-drawcalls-of-gameobjects-with-a-script-attac.html incorrectly.

avatar image · Oct 19, 2016 at 08:21 PM 0
Share

I saw related topics here, where people said that it should work well.

avatar image
0

Answer by Lilius · Oct 19, 2016 at 05:30 PM

You can still batch them after you have done changing vertex colors:

https://docs.unity3d.com/ScriptReference/StaticBatchingUtility.html

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 · Oct 19, 2016 at 08:22 PM 0
Share

Thanks for answer. This is quite useful tool, but unfortunately they said it blocks transform for objects, which is not acceptable for me :(

avatar image Noxury · Jan 16, 2018 at 05:54 PM 0
Share

It's sad that every approach works only when the objects are static, as in Unity Cookies Tutorial.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex colours - can they improve performance? Do they affect batching? 1 Answer

Dynamic Batching not working even adhering to all rules 3 Answers

How to assign different colorsets to objects that are using the same mesh? 0 Answers

How can I change the color of an object's material, not generate another instance of the material, and still keep batching? 2 Answers

How performance heavy is dynamic batching? 0 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