Unity3D网格功能生成球体网格模型

这篇文章主要为大家详细介绍了Unity3D网格功能生成球体网格模型,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity3D网格功能生成球体网格模型的具体代码,供大家参考,具体内容如下

前面已经讲过怎样使用mesh生成一个自己的网格,那么本文将会讲述怎样将这个网格变换成自己想要的形状,比如一个球体。

我们需要知道一个从平面坐标到球体坐标的映射公式。假设平面坐标是(x,y),球体坐标是(x0,y0,z0),则

球体坐标(x0,y0,z0)可以通过以下代码得到,(x,y) 对应vertices[i].x和vertices[i].y。

 v.x = r * Mathf.Cos(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2); v.y = r * Mathf.Sin(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2); v.z = r * Mathf.Sin(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);

完整代码在最后,将完整的脚本绑定到一个空物体上,把棋盘格图案chessboard.jpg-600放到Assets\Resources下,还要在场景中生成一个sphere作为显示网格顶点的辅助物体。一切就绪后,运行效果如下:

 using UnityEngine; using System.Collections; public class BallMesh : MonoBehaviour { Mesh mesh; Vector3[] vertices; Vector2[] uv; int[] triangles; Vector3[] normals; public GameObject sphere; GameObject[] spheres; void Start() { gameObject.AddComponent(); gameObject.AddComponent(); Texture img = (Texture)Resources.Load("tm"); gameObject.GetComponent().material.mainTexture = img; mesh = new Mesh(); int m = 25; //row int n = 50; //col float width = 8; float height = 6; vertices = new Vector3[(m + 1) * (n + 1)];//the positions of vertices spheres = new GameObject[(m + 1) * (n + 1)]; uv = new Vector2[(m + 1) * (n + 1)]; normals = new Vector3[(m + 1) * (n + 1)]; triangles = new int[6 * m * n]; for (int i = 0; i ().mesh = mesh; } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Unity3D网格功能生成球体网格模型的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程