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 를 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;
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Unity 공부 일지~블렌드 셰이프 조작 방법 그 ①게임을 만들고 싶다 ~라고 생각하고 마지막 날부터 Unity를 만지기 시작했습니다 HITOMI2236입니다. 이번 블렌드 셰이프에 대해 조사했으므로 여기에 기록하려고 합니다. 개인용 메모입니다만, 만약 같은 곳에서 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.