using System.Collections.Generic; using UnityEngine; public class PlaneGenerator : MonoBehaviour { public Material material; private const int pqsResolution = 16; private const int sphereRadius = 10; private List quads; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { BuildRootQuads(); UpdateMeshes(); } // Update is called once per frame void Update() { } private void BuildRootQuads() { quads = new List(); var quad = BuildQuad("quadY"); quads.Add(quad); RotatePlaneY(quad.Vertices); quad = BuildQuad("quadY1"); quads.Add(quad); RotatePlaneY1(quad.Vertices); quad = BuildQuad("quadZ"); quads.Add(quad); RotatePlaneZ(quad.Vertices); quad = BuildQuad("quadZ1"); quads.Add(quad); RotatePlaneZ1(quad.Vertices); quad = BuildQuad("quadX"); quads.Add(quad); RotatePlaneX(quad.Vertices); quad = BuildQuad("quadX1"); quads.Add(quad); RotatePlaneX1(quad.Vertices); } private Quad BuildQuad(string name) { var (vertices, uv) = CreateVertices(); var triangles = CreateTriangles(); return new Quad { Name = name, Vertices = vertices, Triangles = triangles, Uv = uv }; } private void UpdateMeshes() { foreach (var quad in quads) { CreateMesh(quad.Name, quad.Vertices, quad.Uv, quad.Triangles); } } private (Vector3[] vertices, Vector2[] uv) CreateVertices() { var half = sphereRadius / 2f; var vertices = new Vector3[(pqsResolution + 1) * (pqsResolution + 1)]; Vector2[] uv = new Vector2[(pqsResolution + 1) * (pqsResolution + 1)]; for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(half - (float)x / pqsResolution * sphereRadius, half, half - (float)z / pqsResolution * sphereRadius); uv[vertex] = new Vector2((float)x / pqsResolution, (float)z / pqsResolution); } } return (vertices, uv); } private int[] CreateTriangles() { var triangles = new int[(pqsResolution) * (pqsResolution) * 6]; int triangle = 0; for (int x = 1, vertex = 0; x(); meshFilter.mesh.vertices = vertices; meshFilter.mesh.triangles = triangles; meshFilter.mesh.uv = uv; mesh.GetComponent().sharedMesh = meshFilter.mesh; meshFilter.mesh.RecalculateBounds(); meshFilter.mesh.RecalculateNormals(); mesh.GetComponent().material = material; } private void RotatePlaneZ(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(-vertices[vertex].x, vertices[vertex].z, vertices[vertex].y); vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } private void RotatePlaneY(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } private void RotatePlaneZ1(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(-vertices[vertex].x, -vertices[vertex].z, -vertices[vertex].y); vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } private void RotatePlaneX(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(vertices[vertex].y, vertices[vertex].x, -vertices[vertex].z); vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } private void RotatePlaneY1(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(-vertices[vertex].x, -vertices[vertex].y, vertices[vertex].z); vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } private void RotatePlaneX1(Vector3[] vertices) { for (int x = 0, vertex = 0; x < pqsResolution + 1; x++) { for (int z = 0; z < pqsResolution + 1; z++, vertex++) { vertices[vertex] = new Vector3(-vertices[vertex].y, -vertices[vertex].x, -vertices[vertex].z); vertices[vertex] = Vector3.Normalize(vertices[vertex]) * sphereRadius; } } } }