- Home /
Multiple Shaders vs. Single Texture - Please help me overthink this.
I'm using Maya to model low-poly meshes for a mobile game. I'm assigning materials per-face to color the models. Since each model has several colors, I end up with multiple materials per mesh.
I've read on the Unity3d art assets best practices section that single textures on a mesh is recommended because it limits draw calls and optimizes performance. I've also read that procedural shaders generated at runtime are a lower performance hit than texture load-ins.
BUT, I don't have textures - just materials. Since I'm just setting RGB values and not dealing with bitmaps, that makes them procedural, right?
My question is, would I be okay running 5 to 6 unlit/color shaders on a skinned mesh renderer? My model meshes are generally in the 700 triangle range. Or is that going to land me in draw call purgatory once I have 10-12 of these models moving around?
I could bake the materials to a texture map by suffering through some kind of UV unwrap/replace nightmare, but it's 2016 and how are we even still dealing with UVs anyways. And even after figuring out how exactly to do that, I would end up with an atlas of flat colored polygons, which seems wrong even to me.
Or, I can continue to do what I'm doing, which is importing .fbx models from Maya with per-face coloring and not having to do anything else since it already just works.
Will someone with some knowledge on this please justify my decision to stick with the easier option? Thanks.
Sounds like you COULD use a colored vertex shader. Since you don't actually have any images on your textures, just colors, why use textures at all? You can simply color each vertex that makes up a face, the same color. This way, all faces can use the same material (and a single fairly-simple shader) https://docs.unity3d.com/ScriptReference/$$anonymous$$esh-colors.html
Just (over)thinking here: I've never tried this with a mesh before, just lines and other GL function drawn stuff.
Hey @Glurth, thanks for responding!
Like you say, I'm not using images or textures, just colors. But I'm applying them per-face. I'll try to follow the instructions in the link you provided, but I'm better with art and animation and pretty much instantly out of my depth with any kind of scripting, so it may take me a while to get any kind of results.
The per-face coloring imports a bunch of materials with the mesh. Is the advantage of vertex coloring over what I'm doing is that it supports multiple colors with only one shader?
Correct, a single shader can be used to draw the various vertex colors.
A $$anonymous$$esh defines a 3-d shape, using a set of verticies, which specify the corner of each face. Usually you will have an additional vertex for each FACE (triangle) a particular corner touches(e.g. 3 vertices for each corner of a cube). Each vertex defines, not just the position, but also things like the "normal" (facing direction), a texture UV (if used), and a color (if used). That is how you can use vertex-coloring, to color a face.
The tricky part is going to be setting the color of those verticies. If your drawing software does not have the ability to generate a vertex-colored mesh, I think some scripting may be necessary to process the mesh into one.
Answer by Glurth · Jul 23, 2016 at 01:42 PM
The code to create a colored vertex mesh, out of a mesh with multiple textures, is pretty simple, if you know what your doing, but is a nightmare if you don't. Here is a script that can be attached to a game object. It will allow you to select a mesh, and set of "replacement" colors, that will be assigned to the vertices based on their sub-mesh (material). When you click "process", a new mesh called "ColorVert"+originalMeshName+".asset", will be created in your asset folder. I do NOT have an model with multiple textures in a single file to test, so I've only done very limited tests. (post one and I can test it.)
You will also need a vertex-color shader to actually make this work. I used this one, with default settings, which worked well in my tests: http://forum.unity3d.com/threads/standard-shader-with-vertex-colors.316529/
Here is the script code to colorize a mesh's vertexes:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[ExecuteInEditMode]
public class ColorizeMesh : MonoBehaviour {
public Mesh meshToProcess;
public List<Color> colorsToAssign;
public bool processNow=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (processNow)
{
processNow = false;
if (!meshToProcess) return;
if (colorsToAssign == null) return;
Mesh tempMesh = (Mesh)UnityEngine.Object.Instantiate(meshToProcess);
int subMeshCount = tempMesh.subMeshCount;
List<int> finalTrinagleList= new List<int>();
Color[] vertexColorArray = new Color[tempMesh.vertexCount];
for (int i = 0; i < subMeshCount; i++)
{
int[] subMeshTrinagles = tempMesh.GetTriangles(i);
int subMeshTrinagleCount = subMeshTrinagles.Length;
for (int j = 0; j < subMeshTrinagleCount; j++)
{
int vertexIndex = subMeshTrinagles[j];
finalTrinagleList.Add(vertexIndex);
vertexColorArray[vertexIndex] = colorsToAssign[i];
}
tempMesh.SetTriangles(new int[0], i);
}
tempMesh.SetTriangles(finalTrinagleList, 0);
tempMesh.SetColors(new List<Color>(vertexColorArray));
AssetDatabase.CreateAsset(tempMesh, "Assets/ColoredVert" + tempMesh.name + ".asset");
AssetDatabase.SaveAssets();
}
}
}
Your answer
Follow this Question
Related Questions
Graphics Performance Question 2 Answers
Materials: unlit transparent texture vs standard shader's albedo texture 0 Answers
Why are most of my materials displayed as black or transparent? 1 Answer
How to create a shader that makes the texture look far away? 0 Answers
Two textures one Material 0 Answers