如何统一动态创建网格 [英] How to create a mesh dynamically In unity
问题描述
我的顶点具有颜色值.
我想使用具有相同颜色值的顶点制作网格.
I'd like to make a mesh using vertex with the same color values.
这张照片是一个例子.
我用Android手机拍照,然后在对象上进行了图像分割
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.
但是我想要一个网格物体.
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();
如何制作网格物体?
推荐答案
1-设置带有MeshFilter和MeshRenderer的预制件.
1- Set a prefab with a MeshFilter and a MeshRenderer.
2-您需要在脚本中填充的变量.
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-初始化网格
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.
信息源是一个教程,用于为像minecrat之类的地形生成mensh,请检查
The source of information is a tutorial to generate mensh for a terrain like minecrat, check the link for more information.
这篇关于如何统一动态创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!