Unity3D 격자 기능 생 성 구체 격자 모형

4304 단어 Unity3D격자구체
본 논문 의 사례 는 유 니 티 3D 격자 기능 이 구체 적 인 격자 모델 을 만 드 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
앞에서 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 를 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<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;
 
 }
 
} 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기