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
2
Question by Clawiste · Aug 19, 2014 at 01:05 PM · meshrandomvertextriangles

Color triangle on mesh

Hi, I'm trying to vertex color every triangle on mesh with random color, but I'm getting this:

alt text

but I'm want to get this:

alt text

The red triangle is always one-colored and other ones on mesh aren't.


 using UnityEngine;
 using System.Collections;
 
 public void VertsColor(GameObject gameObject)
 {
     Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;

     int[] triangles = mesh.triangles;
     Vector3[] vertices = mesh.vertices;

     Vector3[] verticesModified = new Vector3[triangles.Length];
     int[] trianglesModified = new int[triangles.Length];

     Color32 currentColor = new Color32();
     Color32[] colors = new Color32[triangles.Length];

     for (int i = 0; i < trianglesModified.Length; i++) {

         // Makes every vertex unique
         verticesModified[i] = vertices[triangles[i]];
         trianglesModified[i] = i;

         // Every third vertex randomly chooses new color
         colors[triangles[i]] = currentColor;
         if(triangles[i] % 3 == 0)
             currentColor = new Color(
                                 Random.Range (0.0f, 1.0f),
                                 Random.Range (0.0f, 1.0f),
                                 Random.Range (0.0f, 1.0f),
                                 1.0f);
     }

     // Applyes changes to mesh
     mesh.vertices = verticesModified;
     mesh.triangles = trianglesModified;
     mesh.colors32 = colors;
 }

Thanks for your help!

correct.jpg (15.5 kB)
wrong.jpg (15.7 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by _dns_ · Aug 19, 2014 at 01:41 PM

Hi, I think you have to index with "i" in lines 24 and 25 instead of "triangle[i]" (there "i" is your new vertex + triangle + colors index)

Line24: colors[i] = currentColor;

Line25: if( i % 3 == 0 )

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 Scribe · Aug 19, 2014 at 01:46 PM 1
Share

Indeed, though you also need to swap the order of line 24 and the if statement:

 if(i % 3 == 0){
     currentColor = new Color(
         Random.Range (0.0f, 1.0f),
         Random.Range (0.0f, 1.0f),
         Random.Range (0.0f, 1.0f),
         1.0f
     );
 }
 colors[i] = currentColor;

avatar image
0

Answer by Owen-Reynolds · Aug 19, 2014 at 01:50 PM

Seems way to specific a Q to be here, but:

In the "every 3rd vert change color," isn't triangles[i] just a randomish number? Try a Debug to see when you change colors.

Shouldn't it be simply if(i%3==0)? And assign the color afterwards? i.e.: tri indexes 3,4,5 form a tri, so you pick a color for 3, use it for 3,4,5; then pick a new color just before assigning 6,7,8.

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
avatar image
0

Answer by technofeeliak · Oct 17, 2020 at 05:34 PM

This Brackeys video may help. https://www.youtube.com/watch?v=lNyZ9K71Vhc

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

7 People are following this question.

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

Related Questions

Having trouble implementing edge collapse operation with half-edge data structure 0 Answers

Optimized vertex manipulation 1 Answer

Understanding verts and triangles in Unity 1 Answer

Procedural mesh creation issue 1 Answer

Mesh triangles don't match wireframe view? 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