如何在Unity中动态创建网格

3
我有一个具有颜色值的顶点。
我想使用具有相同颜色值的顶点制作一个网格。

enter image description here

这张图片是一个例子。
我用我的安卓手机拍了照片,并对物体进行了图像分割。
因此,我得到了与坐标值相对应的颜色值。
我成功地制作了纹理。请检查图片。 enter image description here 但我想要一个网格对象。
下面是制作纹理的代码。
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();

如何制作网格对象?

使用Unity3D的Mesh类... https://docs.unity3d.com/ScriptReference/Mesh.html - Dave
如果你有轮廓线,你需要的过程叫做三角剖分(如果是2D的话)。 - zambari
显然你正在使用ARCore。你可以像@Dave提到的那样使用提取的点来创建网格。但是,我非常怀疑你最终能得到一个合适的网格。 - Ali Kanat
2个回答

4
被选为最佳答案的回答,在我看来有四个问题。首先,它已经被弃用了。其次,它比必要的更复杂。第三,它提供的解释很少。最后,它大部分只是从别人的博客文章中复制过来的。因此,我提供一个新的建议。详细信息请查看这里的文档
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class meshmaker : MonoBehaviour {

    Mesh mesh;
    MeshFilter meshFilter;
    Vector3[] newVertices;
    int[] newTriangles;

    // Use this for initialization
    void Start () {
        //First, we create an array of vector3's. Each vector3 will 
        //represent one vertex in our mesh. Our shape will be a half 
        //cube (probably the simplest 3D shape we can make.

        newVertices = new Vector3[4];

        newVertices [0] = new Vector3 (0, 0, 0);
        newVertices [1] = new Vector3 (1, 0, 0);
        newVertices [2] = new Vector3 (0, 1, 0);
        newVertices [3] = new Vector3 (0, 0, 1);

        //Next, we create an array of integers which will represent 
        //triangles. Triangles are built by taking integers in groups of 
        //three, with each integer representing a vertex from our array of 
        //vertices. Note that the integers are in a certain order. The order 
        //of integers determines the normal of the triangle. In this case, 
        //connecting 021 faces the triangle out, while 012 faces the 
        //triangle in.

        newTriangles = new int[12];

        newTriangles[0] = 0;
        newTriangles[1] = 2;
        newTriangles[2] = 1;

        newTriangles[3] = 0;
        newTriangles[4] = 1;
        newTriangles[5] = 3;

        newTriangles[6] = 0;
        newTriangles[7] = 3;
        newTriangles[8] = 2;

        newTriangles[9] = 1;
        newTriangles[10] = 2;
        newTriangles[11] = 3;


        //We instantiate our mesh object and attach it to our mesh filter
        mesh = new Mesh ();
        meshFilter = gameObject.GetComponent<MeshFilter> ();
        meshFilter.mesh = mesh;


        //We assign our vertices and triangles to the mesh.
        mesh.vertices = newVertices;
        mesh.triangles = newTriangles;

}

Ta da!您拥有自己的半立方体。

那个被弃用的答案有什么问题吗?据我所知,他们基本上使用了与您相同的方法,但是多了一些方法调用... - Shadow
可以坦白地说吗?我很少上这个网站,而且我回答这个问题的时间已经久远,我甚至不记得自己回答过它。也许我说它被弃用了是错的。如果我没有理由,我可能不会这么说。他们的答案后来被编辑过吗?抱歉我不能提供更多帮助。 - Truth
说实话总是好的。虽然没有编辑,但谁知道选定的答案是否改变。只是确保我没有漏掉什么。 - Shadow

3

1- 设置一个包含MeshFilter和MeshRenderer的预制件。

2- 脚本内需要填写的变量。

// 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- 初始化网格

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 ();
 }

这段代码将在预制件位置绘制一个正方形,如果继续添加顶点,则可以生成更复杂的网格。
信息来源是一篇关于生成像Minecraft一样地形的网格教程,请查看链接获取更多信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接