Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by greg725 · Jul 19, 2016 at 08:02 PM · shadersmaterialstexturesproceduraldraw calls

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.

Comment
Add comment · Show 3
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 Glurth · Jul 20, 2016 at 12:01 AM 0
Share

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.

avatar image greg725 · Jul 23, 2016 at 07:47 AM 0
Share

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?

avatar image Glurth greg725 · Jul 23, 2016 at 12:20 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
         }
     
     }
 }


Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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