| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using System.Collections.Generic;
- using UnityEngine;
- public class PlaneGenerator : MonoBehaviour
- {
- public Material material;
- public float noiseLevel = 0.1f;
- private const int pqsResolution = 16;
- private const int sphereRadius = 10;
- private readonly int[] matrixRotate0 = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; // поворот на 0 градусов
- private readonly int[] matrixRotateX = { 1, 0, 0, 0, 0, -1, 0, 1, 0 }; // поворот на 90 градусов вокруг X
- private readonly int[] matrixRotateX1 = { 1, 0, 0, 0, 0, 1, 0, -1, 0 }; // поворот на -90 градусов вокруг X
- private readonly int[] matrixRotateX2 = { 1, 0, 0, 0, -1, 0, 0, 0, -1 }; // поворот на 180 градусов вокруг X
- private readonly int[] matrixRotateY = { 0, 0, 1, 0, 1, 0, -1, 0, 0 }; // поворот на 90 градусов вокруг Y
- private readonly int[] matrixRotateY1 = { 0, 0, -1, 0, 1, 0, 1, 0, 0 }; // поворот на -90 градусов вокруг Y
- private readonly int[] matrixRotateZ = { 0, -1, 0, 1, 0, 0, 0, 0, 1 }; // поворот на 90 градусов вокруг Z
- private readonly int[] matrixRotateZ1 = { 0, 1, 0, -1, 0, 0, 0, 0, 1 }; // поворот на -90 градусов вокруг Z
- private List<Quad> 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<Quad>
- {
- BuildQuad("quadY", matrixRotate0),
- BuildQuad("quadY1", matrixRotateX2),
- BuildQuad("quadZ", matrixRotateX),
- BuildQuad("quadZ1", matrixRotateX1),
- BuildQuad("quadX", matrixRotateZ),
- BuildQuad("quadX1", matrixRotateZ1)
- };
- }
- private Quad BuildQuad(string name, int[] matrix)
- {
- var (vertices, uv) = CreateVertices();
- RotateWithMatrix(vertices, matrix);
- 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<pqsResolution + 1; x++, vertex++)
- {
- for (int z = 1; z<pqsResolution + 1; z++, vertex++)
- {
- triangles[triangle] = vertex;
- triangles[triangle + 1] = vertex + pqsResolution + 2;
- triangles[triangle + 2] = vertex + pqsResolution + 1;
- triangles[triangle + 3] = vertex;
- triangles[triangle + 4] = vertex + 1;
- triangles[triangle + 5] = vertex + pqsResolution + 2;
- triangle += 6;
- }
- }
- return triangles;
- }
- private void CreateMesh(string name, Vector3[] vertices, Vector2[] uv, int[] triangles)
- {
- var mesh = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider));
- mesh.transform.localScale = new Vector3(1, 1, 1);
- var meshFilter = mesh.GetComponent<MeshFilter>();
- meshFilter.mesh.vertices = vertices;
- meshFilter.mesh.triangles = triangles;
- meshFilter.mesh.uv = uv;
- mesh.GetComponent<MeshCollider>().sharedMesh = meshFilter.mesh;
- meshFilter.mesh.RecalculateBounds();
- meshFilter.mesh.RecalculateNormals();
- mesh.GetComponent<MeshRenderer>().material = material;
- }
- private void RotateWithMatrix(Vector3[] vertices, int[] matrix)
- {
- Vector3 v;
- for (int x = 0, vertex = 0; x < pqsResolution + 1; x++)
- {
- for (int z = 0; z < pqsResolution + 1; z++, vertex++)
- {
- v = vertices[vertex];
- vertices[vertex].x = v.x * matrix[0] + v.y * matrix[1] + v.z * matrix[2];
- vertices[vertex].y = v.x * matrix[3] + v.y * matrix[4] + v.z * matrix[5];
- vertices[vertex].z = v.x * matrix[6] + v.y * matrix[7] + v.z * matrix[8];
- //vertices[vertex] = Vector3.Normalize(vertices[vertex]);
- }
- }
- }
- private float GetNoise(float x, float y)
- {
- return 1 + Mathf.PerlinNoise(x, y) * noiseLevel;
- }
- private void AddHeights(Vector3[] vertices)
- {
- for (int x = 0, vertex = 0; x < pqsResolution + 1; x++)
- {
- for (int z = 0; z < pqsResolution + 1; z++, vertex++)
- {
- var noise = 1; // GetNoise(vertices[vertex].x, vertices[vertex].y);
- vertices[vertex] = noise * sphereRadius * vertices[vertex];
- }
- }
- vertices[pqsResolution + 3] *= 2f;
- }
- }
|