真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

本文實(shí)例為大家分享了Unity3D網(wǎng)格功能生成球體網(wǎng)格模型的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供長海網(wǎng)站建設(shè)、長海做網(wǎng)站、長海網(wǎng)站設(shè)計(jì)、長海網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、長海企業(yè)網(wǎng)站模板建站服務(wù),10余年長海做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

前面已經(jīng)講過怎樣使用mesh生成一個(gè)自己的網(wǎng)格,那么本文將會(huì)講述怎樣將這個(gè)網(wǎng)格變換成自己想要的形狀,比如一個(gè)球體。

我們需要知道一個(gè)從平面坐標(biāo)到球體坐標(biāo)的映射公式。假設(shè)平面坐標(biāo)是(x,y),球體坐標(biāo)是(x0,y0,z0),則

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

球體坐標(biāo)(x0,y0,z0)可以通過以下代碼得到,(x,y) 對(duì)應(yīng)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);

完整代碼在最后,將完整的腳本綁定到一個(gè)空物體上,把棋盤格圖案chessboard.jpg放到Assets\Resources下,還要在場景中生成一個(gè)sphere作為顯示網(wǎng)格頂點(diǎn)的輔助物體。一切就緒后,運(yùn)行效果如下:

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

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 < 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().mesh = mesh;
 
 }
 
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


當(dāng)前文章:Unity3D網(wǎng)格功能生成球體網(wǎng)格模型
轉(zhuǎn)載注明:http://weahome.cn/article/ggpjpg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部