Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by dkfister · Jan 17, 2014 at 03:46 AM · c#meshcolorvertexcolor

Vertex coloring isn't working

So, I'm making a custom mesh directly from scripting, setting up the vertices, triangles, normals and UVs, and now also trying to change the vertices colors (Vertex colors).

However, even tho I don't have any errors when I run the script, no results shows up. The mesh aren't colored or anything, but the colors ARE getting actually getting inserted to the mesh.

This is my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class RoomCreator : MonoBehaviour {
 
     private List<Vector3> Mesh_Vertices = new List<Vector3>();
     private List<Vector2> Mesh_Uvs = new List<Vector2>();
     private List<int> Mesh_Triangles = new List<int>();
 
     private MeshFilter meshFilter;
 
     void Start() {
         meshFilter = GetComponent<MeshFilter>();
 
         CreateRoom();
     }
 
     private void CreateRoom() {
         meshFilter.mesh = CreateMesh();
     }
 
     private Mesh CreateMesh() {
         Mesh m = new Mesh();
         m.name = gameObject.name + "_Mesh";
         
         CreateFace(new Vector3(0,0,0), 10,1,0);
         CreateFace(new Vector3(10,0,0), 0,1,10);
 
         m.vertices = Mesh_Vertices.ToArray();
         m.triangles = Mesh_Triangles.ToArray();
         m.uv = Mesh_Uvs.ToArray();
         m.RecalculateNormals();
 
         print("Vertices: " + Mesh_Vertices.Count); // Vertices: 8
         print("Triangles: " + Mesh_Triangles.Count); // Triangles: 12
 
         Color[] colors = ColorizeFaces(m.vertices, m.triangles);
         m.colors = colors;
 
         print("Colors: " + m.colors.Length); // Colors: 8
 
         return m;
     }
     
     private Color[] ColorizeFaces(Vector3[] vertices, int[] triangles) {
         Color[] colors = new Color[vertices.Length];
         Color triColor = getRandomColor();
 
         for (int i = 0; i < triangles.Length; i++)
         {
             int vertIndex = triangles[i];
             if (i % 3 == 0)
                 triColor = getRandomColor();
             colors[vertIndex] = triColor;
         }
 
         return colors;
     }
 
     private Color getRandomColor()
     {
         float red = Random.Range(0.0f, 1.0f);
         float green = Random.Range(0.0f, 1.0f);
         float blue = Random.Range(0.0f, 1.0f);
         return new Color(red, green, blue);
     }
     
     private void CreateFace(Vector3 position,int width, int height, int depth) {
         Mesh_Vertices.Add(new Vector3(position.x, position.y, position.z));
         Mesh_Vertices.Add(new Vector3(position.x+width, position.y, position.z+depth));
         Mesh_Vertices.Add(new Vector3(position.x, position.y+height, position.z));
         Mesh_Vertices.Add(new Vector3(position.x+width, position.y+height, position.z+depth));
 
         int a = Mesh_Vertices.Count-1;
         int b = a-1;
         int c = a-2;
         int d = a-3;
 
         Mesh_Triangles.Add(a);
         Mesh_Triangles.Add(b);
         Mesh_Triangles.Add(d);
 
         Mesh_Triangles.Add(d);
         Mesh_Triangles.Add(c);
         Mesh_Triangles.Add(a);
 
         Mesh_Uvs.Add(new Vector2(0,0));
         Mesh_Uvs.Add(new Vector2(1,0));
         Mesh_Uvs.Add(new Vector2(0,1));
         Mesh_Uvs.Add(new Vector2(1,1));
     }
 }
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
0
Best Answer

Answer by Eric5h5 · Jan 17, 2014 at 03:57 AM

You need to use a shader that utilizes vertex colors; most of the built-in shaders don't.

Comment
Add comment · Show 2 · 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 dkfister · Jan 17, 2014 at 04:08 AM 0
Share

I see.. Is there shaders you could recommend for this then?

avatar image Eric5h5 · Jan 17, 2014 at 05:26 AM 0
Share

You can make a custom shader that uses vertex colors...it depends on what you want the shader to do.

avatar image
0

Answer by HappyMoo · Jan 18, 2014 at 06:43 PM

Use a Shader like this:

 Shader "Custom/Vertex Colored Lighted" {
 Properties {
 }
     SubShader {
             Pass {
                     ColorMaterial AmbientAndDiffuse
                     Lighting On
             }
     } 
 }
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

20 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Set UVs depending on input 1 Answer

Generate mesh from faces, invisible 1 Answer

calculate normals 2 Answers

Change Halo Color with Code 1 Answer


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