Simple UV Mapping for Color?

I’m trying to bring down the draw calls in my game a bit. My 3d models are very simple, made up of just two or three shapes of different colors. The trouble is that I can’t find a simple way to color the shapes without creating new materials. Using different materials breaks batching and increases my draw calls.

I’m thinking of creating a texture atlas with just colored squares and using UV mapping to color the individual shapes within each model. I’ve done a lot of looking around but I can’t seem to find an example of a script that would handle this.

I have a few questions:

  1. Do I need to use uv mapping to handle something as simple as coloring models?
  2. If I use uv mapping, can I select a single color square from my atlas and apply that to an entire mesh, regardless of its shape? Does the square need to be as large as the unwrapped mesh or could it be as small as 1 pixel and applied to the entire mesh?
  3. Can someone point me to an example script that actually applies the texture atlas coordinates to the mesh? Something that basically goes “Here is the mesh. Here is the atlas. Apply this portion of the atlas to the mesh.”

Hi, I've never used this (yet) but kept the link for later, when optimizations will be on the menu: http://docs.unity3d.com/Documentation/ScriptReference/MaterialPropertyBlock.html

2 Answers

2

If what you need is one color per object or couple base colors for different parts, theoretically you can have all those object under 1 draw call. For that you will need a single texture, somewhat similar like this:

http://i.msdn.microsoft.com/dynimg/IC154693.gif

You will have to scale every objects uv close to couple pixels, i still wouldn’t suggest using 1x1 pixel per color (uv can be close to 1 pixel, but not the color itself), because of the padding, so if the texture resolution will be low, nearest object will also start to color in neighbour colors. But maybe if would have 32 bit targa uncompressed 4096x4096, trucolor, max antialsing and trilinear, maybe there would be no strech for large meshes and 1 pixel would be enough. But i would use at least 2x2 or 4x4 pixels for a color, i would just feel more better ;).

Thanks, I think this is what I'm looking for. Do you happen to know of an example script that applies the texture coordinates to the mesh?

No, sorry, i would do it manually though, in modelling software.

UV mapping by hand can be a real pain. Typically people use third-party tools for creating and/or manipulating a texture atlas. Here is one question where a texture atlas is done by hand for the built-in cube:

http://answers.unity3d.com/questions/542787/change-texture-of-cube-sides.html

As an alternate solution, consider using Vertex colors. Vertex colors do not break batching. You assign the colors in the ‘colors’ array of the mesh. Vertex colors require a shader that supports them. Only a fraction of the shaders support vertex colors.

I may try Vertex colors. Thanks!

If the mesh or parts will be in single color, you dont have to correctly uv map it, it can be ugly and parts can overlap, main thing is to scale it very small. So that process will be very fast.