• 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 orcinusdev · Oct 21, 2014 at 07:59 AM · verticesvertexvertexcolor

Get vertices a material is assigned to

Hello! As the title states I am after a solution on how i can get all the vertices that a material is assigned to. T$$anonymous$$s because I want to be able to change the vertex colors just for that part. The reason being is that I'm creating a mobile game with cars, where I only want 1 draw call for the body, but i still want the ability to change color of the car at runtime, except windows, lights, grill etc (so I can't use a shader with a main color, and i don't want 2 materials for the body). So if I somehow can save a list with these vertices would be great.

Thanks in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by richyrich · Oct 21, 2014 at 02:59 PM

Your question relates to sub meshes...

In order to have multiple materials, your mesh will have multiple sub-meshes - one for each material. On that basis, you could use the function GetTriangles to obtain all the triangles for a given sub-mesh. You would then loop through and identify all unique vertex indices used wit$$anonymous$$n the array.

See http://docs.unity3d.com/ScriptReference/Mesh.GetTriangles.html for more information

Comment
Add comment · Show 5 · 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 _dns_ · Oct 21, 2014 at 03:23 PM 0
Share

Hi, this would work but I think having sub-meshes makes multiple drawcall (maybe not all the time but that's what happened on my projects/tests). If it does, I would then use your solution of creating sub-meshes for authoring the meshes. Then, I would create some editor code to merge the sub-meshes into one mesh that is used in the game. This merging code would have everything it needs to create an array of index of vertices that needs a color change. Maybe you can just store the vertices that needs a color change first in the mesh, then have to store only 1 index to know when to stop changing vertex colors later.

avatar image orcinusdev · Oct 21, 2014 at 03:27 PM 0
Share

Thank you for an answer. Yes i read about GetTriangles just after posting this. But GetTriangles returns a Int[] (I guess this is some kind of index) while mesh.vertices is a Vector3[] (actual positions). Which function let me loop though all vertex indices and store them in a vertices array?

avatar image orcinusdev · Oct 21, 2014 at 03:32 PM 0
Share

@_dns

It was exactly what I was thinking about. First by have them separate i can identify the vertices, and change specified vertex colors. It would be great. And then combine everything to one mesh with 1 material.

avatar image _dns_ · Oct 21, 2014 at 03:45 PM 0
Share

yes, GetTriangles returns a, int[] composed of indexes of vertices from the .vertices array, 3 indexes per triangle. Maybe you can check this code to create your own: http://wiki.unity3d.com/index.php?title=MeshMerger. You may also want to save your created mesh to disk using http://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html so you can use it like any other mesh.

avatar image Bunny83 · Oct 21, 2014 at 03:51 PM 0
Share

@orcinusdev: A mesh consists of vertices and indices. The vertices i talk about includes all the arrays related to vertex attributes. So the .vertices, .normals, .uv, .colors, ... arrays all made up the vertices list. A mesh only has one vertices array but might have multiple submeshes. GetTriangles will return the indices for the given material / submesh index. The returned indices array contains the actual triangle definition. So it's count is always a multiple of 3. Those indices index into the vertices arrays.

So if you want to change the vertex color of all vertices that belong to the submesh 1 just use GetTriangles(1) and iterate through that array and change the meshs colors[index] to your desired color.

Note: Keep in mind that if two submeshes share the same vertices (which is quite unusual but possible) you can't change the color just for one submesh without affecting the other. The only way in such a case is to manually duplicate the shared vertices.

To collapse all submeshes into one all you have to do is:

 mesh.triangles = mesh.triangles;

This will keep all triangles but merge them into one submesh and will remove all others.

avatar image
1

Answer by orcinusdev · Oct 21, 2014 at 07:38 PM

TY for the help, here is a working solution

 using UnityEngine;
 using System.Collections;
 
 public class ChangeColor : MonoBehaviour {
 
     // T$$anonymous$$s code replaces the indexed submesh and corresponding material color to a vertex color. Then it combines all materials to a single material (Only tested for 2 materials)
 
 
     Color newColor;
     public Shader vertexColoredShader;
     public Texture lightmap;
     public int subMeshNumber = 1;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         Mesh mesh = GetComponent<MeshFilter>().mesh;
 
         newColor = renderer.materials[subMeshNumber].GetColor("_Color");
 
         Debug.Log("Submeshes: " + mesh.subMeshCount);
         int[] t = mesh.GetTriangles(subMeshNumber);
         Vector3[] vertices = mesh.vertices;
         Color[] oldColors = mesh.colors;
         Color[] colors = new Color[vertices.Length];
         for (int i = 0; i < vertices.Length; i++)
         {
 
             bool foundSubMeshV = false;
 
             foreach (int index in t)
             {
 
                 if (i == index)
                 {
                     colors[i] = newColor;
                     foundSubMeshV = true;
                 }
 
             
 
             }
 
             if(foundSubMeshV)
                 continue;
 
 
             colors[i] = oldColors[i];
         }
             
 
 
         mesh.colors = colors;
         mesh.triangles = mesh.triangles;
 
         DestroyImmediate(renderer);
         gameObject.AddComponent<MeshRenderer>();
 
 
         Material newMat = new Material (vertexColoredShader);
         renderer.material = newMat;
         if(lightmap)
         renderer.material.mainTexture = lightmap;
 
 
 
     
 
 
     
     }
 }
 

Comment
Add comment · Show 1 · 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 orcinusdev · Oct 21, 2014 at 07:41 PM 0
Share

(The vertex shader that I used can be found at the wiki)

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityEngine.UI.Text characters mesh 0 Answers

Moving vertices in shaders 1 Answer

Vertex Shader problem with _WorldSpaceCameraPos 1 Answer

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

Creating a mesh for an overview map 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