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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ketan12345 · Jul 27, 2013 at 05:54 PM · meshcolorrendertriangle

Render mesh by specifying color of triangle

I have created an empty GameObject and assign the following script:

 using System;
 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
     public class Primitives : MonoBehaviour {
 
             Mesh mesh = new Mesh();

             Vector3[] vertices = new Vector3[totalVertices]; //totalVertices: no. of vertices 
             verticesList.CopyTo(vertices); //verticesList: list of vertices
             int[] triangles = new int[totalIndices]; //totalIndices: no. of indices
             indicesList.CopyTo(triangles); //incesList: list of triangles
             mesh.vertices = vertices;
             mesh.triangles = triangles;
         
        //create uvs
          Vector2[] uvs = new Vector2[vertices.Length];
            int u = 0;
             while (u < uvs.Length) {
             uvs[u] = new Vector2(vertices[u].x, vertices[u].z);
             u++;
             }
         mesh.uv = uvs;
     
         //create colors
         Vector3[] verticesM = mesh.vertices;
         Color[] colors = new Color[verticesM.Length];
           int c = 0;
         while (c < verticesM.Length) {
         colors[c] = Color.red;
         c++;
              }
             mesh.colors = colors;

             mesh.RecalculateBounds();
             mesh.Optimize();
             MeshFilter mFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
             mFilter.mesh = mesh;
         }
 }

When I press Run, the shape is fined except that it is displayed in pink. Any idea how to correct this?

Comment
Add comment · Show 11
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 AlucardJay · Jul 27, 2013 at 06:01 PM 1
Share

You still need to apply a material with a shader to see the colours. You can attach a material to the gameObject, or assign a material through script : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial-ctor.html

Please note for vertex colours, you'll need a vertex shader. Here's a couple on the Unify Community Wiki :

  • http://wiki.unity3d.com/index.php?title=VertexColor

  • http://wiki.unity3d.com/index.php/VertexColorUnlit (please note on this one : Both shaders allow for overbrightening, so using (.5, .5, .5) as vertex color values results in 100% brightness). I made the mistake of not reading that. The only modification you have to make to the shader to get it to render properly is delete the word DOUBLE from the combine line.

avatar image ketan12345 · Jul 27, 2013 at 07:25 PM 0
Share

@alucardj I have done Asset->Create->Shader which creates a shader file. I added the code from http://wiki.unity3d.com/index.php?title=VertexColor in the shader file. Now what do I do with the shader file?

avatar image AlucardJay · Jul 27, 2013 at 07:30 PM 0
Share

Create a new material, select the shader, then attach the material to the empty gameObject with the script.

avatar image ketan12345 · Jul 28, 2013 at 10:24 AM 0
Share

@alucardj I did that but now the whole shape gets the color of the new material. It seems that the codes in Lines 25-34 are not having any effect. Is is possible to get different colors for the different triangles in the mesh?

avatar image AlucardJay · Jul 28, 2013 at 07:34 PM 0
Share

Well yes, line 31 is only setting each vertex colour to one colour : red! so change to this to read from a texture atlas, heightmap, or even randomly :

 colors[c] = new Color( Random.value, Random.value * 0.2 + 0.6, Random.value * 0.5 + 0.5 );
Show more comments

1 Reply

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

Answer by AlucardJay · Jul 28, 2013 at 08:17 PM

Summary of my comments :

You still need to apply a material with a shader to see the colours. You can attach a material to the gameObject, or assign a material through script : http://docs.unity3d.com/Documentation/ScriptReference/Material-ctor.html

Please note for vertex colours, you'll need a vertex shader. Here's a couple on the Unify Community Wiki :

  • http://wiki.unity3d.com/index.php?title=VertexColor

  • http://wiki.unity3d.com/index.php/VertexColorUnlit (please note on this one : Both shaders allow for overbrightening, so using (.5, .5, .5) as vertex color values results in 100% brightness). I made the mistake of not reading that. The only modification you have to make to the shader to get it to render properly is delete the word DOUBLE from the combine line.

Create a new material, select the shader, then attach the material to the empty gameObject with the script.

Assign the shader to the material. Click on the material, in the drop-down box at the top, click on it, then browse to the vertex shader.

Also, I just noticed you have not declared or assigned normals anywhere. Before Optimize, add :

 mesh.RecalculateNormals();
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 ketan12345 · Jul 28, 2013 at 08:18 PM 0
Share

Thanks. It worked.

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

16 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

Related Questions

Change color of mesh triangle based on Y position in world space 1 Answer

Get average color of the texture assigned to a mesh triangle 2 Answers

Programatically create cube and assign different colors to each side 1 Answer

Checking the transparency of a mesh with a piece of a texture 1 Answer

How to make an ocean by modifying a mesh 3 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