服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity3D网格功能生成球体网格模型

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

2022-03-10 14:05雁回晴空 C#

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

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

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

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

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

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

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

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

?
1
2
3
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放到assets\resources下,还要在场景中生成一个sphere作为显示网格顶点的辅助物体。一切就绪后,运行效果如下:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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<meshfilter>();
  gameobject.addcomponent<meshrenderer>();
  texture img = (texture)resources.load("tm");
  gameobject.getcomponent<renderer>().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 < vertices.length; i++)
  {
   float x = i % (n + 1);
   float y = i / (n + 1);
   float x_pos = x / n * width;
   float y_pos = y / m * height;
   vertices[i] = new vector3(x_pos, y_pos, 0);
   float u = x / n;
   float v = y / m;
   uv[i] = new vector2(u, v);
  }
  for (int i = 0; i < 2 * m * n; i++)
  {
   int[] triindex = new int[3];
   if (i % 2 == 0)
   {
    triindex[0] = i / 2 + i / (2 * n);
    triindex[1] = triindex[0] + 1;
    triindex[2] = triindex[0] + (n + 1);
   }
   else
   {
    triindex[0] = (i + 1) / 2 + i / (2 * n);
    triindex[1] = triindex[0] + (n + 1);
    triindex[2] = triindex[1] - 1;
 
   }
   triangles[i * 3] = triindex[0];
   triangles[i * 3 + 1] = triindex[1];
   triangles[i * 3 + 2] = triindex[2];
  }
  
  int r = 10;
  for (int i = 0; i < vertices.length; i++)
  {
   spheres[i] = instantiate( sphere,this.transform) as gameobject;
   vector3 v ;
   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);
   //v = vertices[i];
 
   vertices[i] = v;
   spheres[i].transform.localposition = v;
 
   normals[i] = new vector3(0,1,0);
  }
  
  mesh.vertices = vertices;
  mesh.normals = normals;
  mesh.uv = uv;
  mesh.triangles = triangles;
  this.getcomponent<meshfilter>().mesh = mesh;
 
 }
 
}

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

原文链接:https://blog.csdn.net/zzlyw/article/details/53256916

延伸 · 阅读

精彩推荐
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18