We came to a point in our mobile game where texture maps became costly so we started looking for alternatives. We wanted to look at using vertex colors, but were unsure if they would offer the fidelity we were looking for. Luckily one of our outsourcing partners Ocellus is able to create beautiful art that you wouldn't even suspect uses vertex colors. But we also wanted to use multiple color sets which is no problem in Maya, but Unity only sees the first color set. So how to get multiple color sets into our shader?
We first contemplated the obvious, save out multiple objects but that's just wasteful. We thought about saving out very small textures with a color per vertex but that kind of defeats the purpose of vertex colors. What we came up with is storing the additional color sets in extra UV sets. Since Unity supports up to 8 UV sets, we potentially have 16 float values at our disposal. Since each color is a float 3, we can store 16/3 = 5 additional color sets without alpha or 4 with alpha in our model! Since we didn't need that much we decided to assign each color set 2 uv sets, 1 for Red and Green and 1 for Blue and Alpha. Although alpha is usually 1 (and I made a specific version of the shader with alpha) I didn't want to store channels of 2 different color sets in 1 uv set for clarity.
What the script does is read out the selected object's color sets and create 2 uv sets per color set (with base names "UVrg_colorSet" and "UVba_colorSet") Then each color per vertex is converted to a float 4 (RGBA) and stored in the corresponding uv set.
Then it clicked. In Maya you're able to assign colors on a per vertex face level, so 1 vertex can potentially contain multiple colors. But Unity sees just one color per vertex so everything is blended. The solution was to split the vertices where this was an issue, making sure the correct output was shown. Vertex count will increase a little of course, but nothing dramatic.This can be done by accessing each vertex face like this through a simple polyListConversion:
When all the vertex face vertices that belong to a vertex are checked and they are not all the same value we split the vertex. Below a video of the script and the shader in action:
No comments:
Post a Comment