Unity3D绘制物体表面三角形网格

Posted little_fat_sheep

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D绘制物体表面三角形网格相关的知识,希望对你有一定的参考价值。

1 仅绘制三角形网格

        1)创建游戏对象 

        创建一个空对象,重命名为 Grid,并在其下添加需要绘制网格的对象,如下:

         场景显示如下:

        2)添加脚本组件

        GridController.cs

using System;
using UnityEngine;

public class GridController : MonoBehaviour 

	private void Start () 
		ForAllChildren(transform, RebuildMesh);
	

	private void RebuildMesh(Transform transform) 
		MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
		if (meshFilter != null && meshFilter.mesh != null) 
			int[] indices = MakeIndices(meshFilter.mesh.triangles);
			meshFilter.mesh.SetIndices(indices, MeshTopology.Lines, 0);
		
	

	private int[] MakeIndices(int[] triangles) 
		int[] indices = new int[2 * triangles.Length];
		for( int i = 0; i < triangles.Length; i += 3 ) 
			for (int j = 0; j < 6; j++) 
				// 每个三角形转换为3条线段, 每个顶点使用2次, 对应的顶点序列是: 0, 1, 1, 2, 2, 0, 通式: (j + 1) % 6 / 2, j 的范围: 1 ~ 5
				indices[2 * i + j] = triangles[i + (j + 1) % 6 / 2];
			
		
		return indices;
	

	private void ForAllChildren(Transform transform, Action<Transform> action) 
		action.Invoke(transform);
		for (int i = 0; i < transform.childCount; i++) 
			ForAllChildren(transform.GetChild(i), action);
		
	

        说明: GridController 脚本组件挂在 Grid 对象上。

        运行效果:

        3)给网格添加新材质

        GridController.cs

using System;
using UnityEngine;

public class GridController : MonoBehaviour 
	public Material material;

	private void Start () 
		ForAllChildren(transform, RebuildMesh);
	

	private void RebuildMesh(Transform transform) 
		MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
		MeshRenderer meshRenderer = transform.GetComponent<MeshRenderer>();
		if (meshFilter != null && meshFilter.mesh != null && meshRenderer != null) 
			meshRenderer.material = material;
			int[] indices = MakeIndices(meshFilter.mesh.triangles);
			meshFilter.mesh.SetIndices(indices, MeshTopology.Lines, 0);
		
	

	private int[] MakeIndices(int[] triangles) 
		int[] indices = new int[2 * triangles.Length];
		for( int i = 0; i < triangles.Length; i += 3 ) 
			for (int j = 0; j < 6; j++) 
				// 每个三角形转换为3条线段, 每个顶点使用2次, 对应的顶点序列是: 0, 1, 1, 2, 2, 0, 通式: (j + 1) % 6 / 2, j 的范围: 1 ~ 5
				indices[2 * i + j] = triangles[i + (j + 1) % 6 / 2];
			
		
		return indices;
	

	private void ForAllChildren(Transform transform, Action<Transform> action) 
		action.Invoke(transform);
		for (int i = 0; i < transform.childCount; i++) 
			ForAllChildren(transform.GetChild(i), action);
		
	

        运行效果:

2 绘制表面和三角形网格

        场景中所有对象同第 1 节,脚本组件如下:

        GridController.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;

public class GridController : MonoBehaviour 
	public Material material;

	private void Start () 
		ForAllChildren(transform, RebuildMesh);
	

	private void RebuildMesh(Transform transform) 
		MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
		MeshRenderer meshRenderer = transform.GetComponent<MeshRenderer>();
		if (meshFilter != null && meshFilter.mesh != null && meshRenderer != null) 
			InitMaterials(meshRenderer);
			int[] indices = MakeIndices(meshFilter.mesh.triangles);
			InitSubMesh(meshFilter.mesh, indices, indices.Length / 3);
		
	

	private void InitMaterials(MeshRenderer meshRenderer) 
		Material[] materials = new Material[meshRenderer.materials.Length + 1];
		meshRenderer.materials.CopyTo(materials, 0);
		materials[materials.Length - 1] = material;
		meshRenderer.materials = materials;
	

	private void InitSubMesh(Mesh mesh, int[] indices, int divide) 
		mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt32);
        mesh.SetIndexBufferData(indices, 0, 0, indices.Length);
        mesh.subMeshCount = 2; // 设置2个子网格
		// 第一部分绘制三角形内部
        SubMeshDescriptor subMeshDescriptor1 = new SubMeshDescriptor(0, divide, MeshTopology.Triangles);
        mesh.SetSubMesh(0, subMeshDescriptor1);
		// 第二部分绘制线段
        SubMeshDescriptor subMeshDescriptor2 = new SubMeshDescriptor(divide, indices.Length - divide, MeshTopology.Lines);
        mesh.SetSubMesh(1, subMeshDescriptor2);
	

	private int[] MakeIndices(int[] triangles) 
		int[] indices = new int[3 * triangles.Length];
		triangles.CopyTo(indices, 0);
		for( int i = 0; i < triangles.Length; i += 3 ) 
			for (int j = 0; j < 6; j++) 
				// 每个三角形转换为3条线段, 每个顶点使用2次, 对应的顶点序列是: 0, 1, 1, 2, 2, 0, 通式: (j + 1) % 6 / 2, j 的范围: 1 ~ 5
				indices[triangles.Length + 2 * i + j] = triangles[i + (j + 1) % 6 / 2];
			
		
		return indices;
	

	private void ForAllChildren(Transform transform, Action<Transform> action) 
		action.Invoke(transform);
		for (int i = 0; i < transform.childCount; i++) 
			ForAllChildren(transform.GetChild(i), action);
		
	

        运行效果:

Unity - 绘制正五边形网格

本文简述了Unity中绘制正五边形网格的基本方法:计算顶点信息、设置三角形覆盖信息、创建配置mesh

绘制方法

  • 基本思路:计算出五边形顶点坐标信息作为数组,设置三角形包围方式,再创建新的mesh配置vertices、triangle参数,最终赋值到当前mesh上
  • 项目实现:
    • 创建DrawPentagon.cs,挂在于带有mesh的物体上(本例为Quad
    • 编写代码如下:
    • 查看所创建的mesh信息
public class DrawPentagon : MonoBehaviour
{
    private Vector3[] newVertices;      //五边形顶点数组
    private int[] newTriangles;         //五边形网格内的三角形网格信息

    void Start()
    {
        //1. 创建五边形顶点坐标数组:顶点编号0~4
        newVertices = new Vector3[5] {
            Vector3.zero,
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 36), 0, Mathf.Sin(Mathf.Deg2Rad * 36)),
            new Vector3(2 * Mathf.Cos(Mathf.Deg2Rad * 36), 0, 0),
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 72) + 1, 0, -Mathf.Sin(Mathf.Deg2Rad * 72)),
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 72), 0, -Mathf.Sin(Mathf.Deg2Rad * 72))
            /*
             newVertices[0] = (0.0, 0.0, 0.0)
             newVertices[1] = (0.8, 0.0, 0.6)
             newVertices[2] = (1.6, 0.0, 0.0)
             newVertices[3] = (1.3, 0.0, -1.0)
             newVertices[4] = (0.3, 0.0, -1.0)          
             */
        };

        //2. 设根据已有的顶点编号设置三角形包围顺序,例如0,1,2顶点围成一个三角形;0,2,3顶点围成另一三角形
        newTriangles = new int[9] { 0, 1, 2, 0, 2, 3, 0, 3, 4 };


        //错误情况:三角形数量不足
        //newTriangles = new int[6] { 0, 1, 2, 0, 2, 3 };

        //错误情况:三角形覆盖面不全
        //newTriangles = new int[9] { 0, 1, 2, 1, 2, 3, 0, 3, 4 };


        //3. 创建mesh信息:顶点数据、三角形
        Mesh mesh = new Mesh
        {
            name = "Pentagon",
            vertices = newVertices,
            triangles = newTriangles           
        };
        GetComponent<MeshFilter>().mesh = mesh;
    }
}

示意图及错误示例:
技术图片

参考

以上是关于Unity3D绘制物体表面三角形网格的主要内容,如果未能解决你的问题,请参考以下文章

Unity - 绘制正五边形网格

《逐梦旅程 WINDOWS游戏编程之从零开始》笔记6——Direct3D中的顶点缓存和索引缓存

为啥绘制我的 OpenGL-ES VBO 网格会阻止其他三角形显示?

DirectX学习笔记:利用D3DX网格数据结构绘制可旋转茶壶

如何有效地渲染具有多边形边缘的网格表面?

绘制三角形