- Home /
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?
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/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.
@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?
Create a new material, select the shader, then attach the material to the empty gameObject with the script.
@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?
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 );
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/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();
Your answer
Follow this Question
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