当前位置:网站首页>Generate a slice of mesh Foundation
Generate a slice of mesh Foundation
2022-06-30 04:58:00 【Bigfield】
unity Tutorial summary address https://zhuanlan.zhihu.com/p/151238164
https://zhuanlan.zhihu.com/p/151238164
Basics mesh Tutorial address https://zhuanlan.zhihu.com/p/188545267
https://zhuanlan.zhihu.com/p/188545267
Ideas :
1、 Dependent components MeshFilter and MeshRenderer
2、 Specify width =xSize, high =ySize, Generate vertices based on width and height , As shown in the figure below , The number of fixed points needed to generate a rectangle is (xSize + 1) * (ySize + 1),( The picture comes from the screenshot of the tutorial web page )

3、 Creating Vertices , And pass the vertex information to MeshFilter, Be careful , Vertex information is stored in a one-dimensional array , Therefore, when traversing the rows and columns, the vertex index should be specified in addition to the row and column index , as follows :
for (int y = 0, i = 0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++, i++)
{
vertices[i] = new Vector3(x, y);
}
}
4、 Render a triangle ,mesh The smallest element of is a triangle , Which side of a triangle is visible is determined by the direction of its vertex order .
The original text of the course :

Personal understanding : Here is an Abe rule in the house , Make the bending direction of four fingers consistent with the order of vertices , The thumb direction is the normal direction

int[] triangles = new int[3];
triangles[0] = 0;
triangles[1] = xSize + 1;
triangles[2] = 1;
mesh.triangles = triangles;4、 Render a rectangle , Render a triangle again based on the rendered triangle , Form a rectangle , Be careful , Two vertices are reused , So rendering a rectangle requires six vertices .


int[] triangles = new int[6];
//triangles[0] = 0;
//triangles[1] = xSize + 1;
//triangles[2] = 1;
//triangles[3] = 1;
//triangles[4] = xSize + 1;
//triangles[5] = xSize + 2;
triangles[0] = 0;
triangles[1] = triangles[4] = xSize + 1;
triangles[2] = triangles[3] = 1;
triangles[5] = xSize + 2;
mesh.triangles = triangles;5、 Draw a line of rectangles
int[] triangles = new int[xSize * 6];
for (int i = 0, vi = 0; i < xSize; i++)
{
triangles[vi] = i;//0 xSize+1
triangles[vi + 1] = triangles[vi + 4] = xSize + i + 1;
triangles[vi + 2] = triangles[vi + 3] = i + 1;
triangles[vi + 5] = xSize + i + 2;
vi += 6;
}
mesh.triangles = triangles;6、 Draw multiline rectangles
int[] triangles = new int[xSize * ySize * 6];
for (int y = 0, ti = 0; y < ySize; y++)
{
for (int x = 0; x < xSize; x++)
{
triangles[ti] = y * (xSize + 1) + x;
triangles[ti + 1] = triangles[ti + 4] = (y + 1) * (xSize + 1) + x;
triangles[ti + 2] = triangles[ti + 3] = y * (xSize + 1) + 1 + x;
triangles[ti + 5] = (y + 1) * (xSize + 1) + 1 + x;
ti += 6;
}
}
mesh.triangles = triangles;The source tutorial code is implemented in
int[] triangles = new int[xSize * ySize * 6];
for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++) {
for (int x = 0; x < xSize; x++, ti += 6, vi++) {
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
triangles[ti + 5] = vi + xSize + 2;
}
}
mesh.triangles = triangles;The overall code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class MyGrid : MonoBehaviour
{
public int xSize = 10;
public int ySize = 5;
private Vector3[] vertices;
public Material mat;
private void Awake()
{
Generate();
GetComponent<MeshRenderer>().material = mat;
}
private void Generate()
{
mesh = new Mesh();
mesh.name = "MyMesh";
GetComponent<MeshFilter>().mesh = mesh;
vertices = new Vector3[(xSize + 1) * (ySize + 1)];
Vector4[] tangents = new Vector4[vertices.Length];
Vector2[] uv = new Vector2[vertices.Length];
Vector4 tangent = new Vector4(1, 0, 0, 0);
for (int y = 0, i = 0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++, i++)
{
vertices[i] = new Vector3(x, y);
uv[i] = new Vector2((float)x / xSize, (float)y / ySize);
tangents[i] = tangent;
}
}
mesh.vertices = vertices;
mesh.uv = uv;
mesh.tangents = tangents;
int[] triangles = new int[xSize * ySize * 6];
for (int y = 0, ti = 0; y < ySize; y++)
{
// Rectangle index x
for (int x = 0; x < xSize; x++)
{
triangles[ti] = y * (xSize + 1) + x;
triangles[ti + 1] = triangles[ti + 4] = (y + 1) * (xSize + 1) + x;
triangles[ti + 2] = triangles[ti + 3] = y * (xSize + 1) + 1 + x;
triangles[ti + 5] = (y + 1) * (xSize + 1) + 1 + x;
ti += 6;
}
}
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
Mesh mesh;
IEnumerator GenerateVertices()
{
mesh = new Mesh();
mesh.name = "MyMesh";
GetComponent<MeshFilter>().mesh = mesh;
vertices = new Vector3[(xSize + 1) * (ySize + 1)];
for (int i = 0, y = 0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++, i++)
{
yield return new WaitForSeconds(0.15f);
vertices[i] = new Vector3(x, y);
}
}
mesh.RecalculateNormals();
yield return 0;
}
private void Update()
{
// Draw a rectangle
if (Input.GetKeyDown(KeyCode.Alpha1))
{
int[] triangles = new int[6];
//triangles[0] = 0;
//triangles[1] = xSize + 1;
//triangles[2] = 1;
//triangles[3] = 1;
//triangles[4] = xSize + 1;
//triangles[5] = xSize + 2;
triangles[0] = 0;
triangles[1] = triangles[4] = xSize + 1;
triangles[2] = triangles[3] = 1;
triangles[5] = xSize + 2;
mesh.triangles = triangles;
}
// Draw a line of rectangles
if (Input.GetKeyDown(KeyCode.Alpha2))
{
int[] triangles = new int[xSize * 6];
for (int i = 0, vi = 0; i < xSize; i++)
{
triangles[vi] = i;//0 xSize+1
triangles[vi + 1] = triangles[vi + 4] = xSize + i + 1;
triangles[vi + 2] = triangles[vi + 3] = i + 1;
triangles[vi + 5] = xSize + i + 2;
vi += 6;
}
mesh.triangles = triangles;
}
// Draw multiline rectangles
if (Input.GetKeyDown(KeyCode.Alpha3))
{
// Rectangle index
// Row index
// Column index
// triangles Indexes
int[] triangles = new int[xSize * ySize * 6];
for (int y = 0, ti = 0; y < ySize; y++)
{
for (int x = 0; x < xSize; x++)
{
triangles[ti] = y * (xSize + 1) + x;
triangles[ti + 1] = triangles[ti + 4] = (y + 1) * (xSize + 1) + x;
triangles[ti + 2] = triangles[ti + 3] = y * (xSize + 1) + 1 + x;
triangles[ti + 5] = (y + 1) * (xSize + 1) + 1 + x;
ti += 6;
}
}
mesh.triangles = triangles;
}
// Recalculate normals
if (Input.GetKeyDown(KeyCode.Alpha4))
{
Debug.LogError(" Recalculate normals ");
mesh.RecalculateNormals();
}
// add to uv coordinate
if (Input.GetKeyDown(KeyCode.Alpha5))
{
Vector3[] verts = new Vector3[vertices.Length];
Vector2[] uv = new Vector2[vertices.Length];
for (int y = 0,i=0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++,i++)
{
verts[i] = new Vector3(x, y);
uv[i] = new Vector2((float)x / xSize, (float)y / ySize);
}
}
mesh.uv = uv;
}
}
private void OnDrawGizmos()
{
if (vertices != null)
{
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], 0.2f);
}
}
}
}
边栏推荐
- 图的一些表示方式、邻居和度的介绍
- 力扣27. 移除元素
- National Museum of Singapore - give you spiritual and physical satisfaction
- Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
- Unity script life cycle and execution sequence
- Collective system
- Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
- harbor api 2.0查询
- z-index属性在什么情况下会失效?
- Solution to Autowired annotation warning
猜你喜欢

【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator

Meet in Bangkok for a romantic trip on Valentine's Day

Deeply understand the function calling process of C language

pycharm 数据库工具

Connect to the database and run node JS running database shows that the database is missing

Ripple effect of mouse click (unity & shader)

Why does the computer have no network after win10 is turned on?

On mask culling of unity

Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok

力扣27. 移除元素
随机推荐
Output directory of log files after unity3d packaging
图的一些表示方式、邻居和度的介绍
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
C # Foundation
力扣349. 两个数组的交集
PBR material: basic principle and simple fabrication
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Create a simple battle game with photon pun
Singapore parent-child tour, these popular attractions must be arranged
Force buckle 704 Binary search
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
One command to run rancher
Passing values between classes using delegates and events
Unreal 4 unavigationsystemv1 compilation error
003-JS-DOM-Attr-innerText
PS1 Contemporary Art Center, Museum of modern art, New York
JS 数组的排序 sort方法详解
Unity3d lookat parameter description
Connect to the database and run node JS running database shows that the database is missing