|
|
@@ -1,6 +1,5 @@
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
-using UnityEngine.UIElements;
|
|
|
|
|
|
public class PlaneGenerator : MonoBehaviour
|
|
|
{
|
|
|
@@ -11,6 +10,15 @@ public class PlaneGenerator : MonoBehaviour
|
|
|
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
|
|
|
@@ -28,36 +36,21 @@ public class PlaneGenerator : MonoBehaviour
|
|
|
|
|
|
private void BuildRootQuads()
|
|
|
{
|
|
|
- quads = new List<Quad>();
|
|
|
-
|
|
|
- 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);
|
|
|
+ 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)
|
|
|
+ private Quad BuildQuad(string name, int[] matrix)
|
|
|
{
|
|
|
var (vertices, uv) = CreateVertices();
|
|
|
+ RotateWithMatrix(vertices, matrix);
|
|
|
var triangles = CreateTriangles();
|
|
|
|
|
|
return new Quad
|
|
|
@@ -137,86 +130,40 @@ public class PlaneGenerator : MonoBehaviour
|
|
|
mesh.GetComponent<MeshRenderer>().material = material;
|
|
|
}
|
|
|
|
|
|
- private void RotatePlaneX(Vector3[] vertices)
|
|
|
+ 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++)
|
|
|
{
|
|
|
- vertices[vertex] = new Vector3(vertices[vertex].y, vertices[vertex].x, -vertices[vertex].z);
|
|
|
- var noise = GetNoise(vertices[vertex].y, vertices[vertex].z);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[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];
|
|
|
|
|
|
- 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);
|
|
|
- var noise = GetNoise(-vertices[vertex].y, -vertices[vertex].z);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[vertex]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void RotatePlaneY(Vector3[] vertices)
|
|
|
- {
|
|
|
- for (int x = 0, vertex = 0; x < pqsResolution + 1; x++)
|
|
|
- {
|
|
|
- for (int z = 0; z < pqsResolution + 1; z++, vertex++)
|
|
|
- {
|
|
|
- var noise = GetNoise(vertices[vertex].x, vertices[vertex].z);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[vertex]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- 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);
|
|
|
- var noise = GetNoise(-vertices[vertex].x, -vertices[vertex].z);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[vertex]);
|
|
|
+ //vertices[vertex] = Vector3.Normalize(vertices[vertex]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- private void RotatePlaneZ(Vector3[] vertices)
|
|
|
+ private float GetNoise(float x, float y)
|
|
|
{
|
|
|
- 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);
|
|
|
- var noise = GetNoise(-vertices[vertex].x, vertices[vertex].y);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[vertex]);
|
|
|
- }
|
|
|
- }
|
|
|
+ return 1 + Mathf.PerlinNoise(x, y) * noiseLevel;
|
|
|
}
|
|
|
|
|
|
- private void RotatePlaneZ1(Vector3[] vertices)
|
|
|
+ private void AddHeights(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);
|
|
|
- var noise = GetNoise(-vertices[vertex].x, -vertices[vertex].y);
|
|
|
- vertices[vertex] = noise * sphereRadius * Vector3.Normalize(vertices[vertex]);
|
|
|
+ var noise = 1; // GetNoise(vertices[vertex].x, vertices[vertex].y);
|
|
|
+ vertices[vertex] = noise * sphereRadius * vertices[vertex];
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- private float GetNoise(float x, float y)
|
|
|
- {
|
|
|
- return 1 + Mathf.PerlinNoise(x, y) * noiseLevel;
|
|
|
+ vertices[pqsResolution + 3] *= 2f;
|
|
|
}
|
|
|
}
|