How to create a mesh dynamically In unity

2020-04-20 11:25发布

I have vertex that have a color value.

I'd like to make a mesh using vertex with the same color values.

enter image description here

This picture is an example.

I took pictures with my Android Phone, and I did image segmentation on the object

So I got a color value corresponding to the coordinate value.

I succeeded in just making textures. please check the image. enter image description here

But I want a mesh object.

Below is making texture code.

var pixel = await this.segmentation.SegmentAsync(rotated, scaled.width, scaled.height);
// int pixel[][];                   // image segmentation using tensorflow

Color transparentColor = new Color32(255, 255, 255, 0);  // transparent
for (int y = 0; y < texture.height; y++)
{
      for (int x = 0; x < texture.width; x++)
      {
             int class_output = pixel[y][x];   

              texture.SetPixel(x, y, pixel[y][x] == 0 ? transparentColor : colors[class_output]);
      }
}
texture.Apply();

How can I make a mesh object?

1条回答
趁早两清
2楼-- · 2020-04-20 11:59

1- Set a prefab with a MeshFilter and a MeshRenderer.

2- Variables inside the script that you will need to fill.

// This first list contains every vertex of the mesh that we are going to render
public List<Vector3> newVertices = new List<Vector3>();

// The triangles tell Unity how to build each section of the mesh joining
// the vertices
public List<int> newTriangles = new List<int>();

// The UV list is unimportant right now but it tells Unity how the texture is
// aligned on each polygon
public List<Vector2> newUV = new List<Vector2>();


// A mesh is made up of the vertices, triangles and UVs we are going to define,
// after we make them up we'll save them as this mesh
private Mesh mesh;

3- Initialize the mesh

void Start () {

  mesh = GetComponent<MeshFilter> ().mesh;

  float x = transform.position.x;
  float y = transform.position.y;
  float z = transform.position.z;

  newVertices.Add( new Vector3 (x  , y  , z ));
  newVertices.Add( new Vector3 (x + 1 , y  , z ));
  newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
  newVertices.Add( new Vector3 (x  , y-1 , z ));

  newTriangles.Add(0);
  newTriangles.Add(1);
  newTriangles.Add(3);
  newTriangles.Add(1);
  newTriangles.Add(2);
  newTriangles.Add(3);

  newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y + tUnit));
  newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
  newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y));
  newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y));

  mesh.Clear ();
  mesh.vertices = newVertices.ToArray();
  mesh.triangles = newTriangles.ToArray();
  mesh.uv = newUV.ToArray(); // add this line to the code here
  mesh.Optimize ();
  mesh.RecalculateNormals ();
 }

This code will draw a square at the position of the prefab, if you keep adding vertices you can generate a more complex mesh.

The source of information is a tutorial to generate mensh for a terrain like minecrat, check the link for more information.

查看更多
登录 后发表回答