|
|
@@ -10,15 +10,6 @@ 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
|
|
|
@@ -38,19 +29,19 @@ public class PlaneGenerator : MonoBehaviour
|
|
|
{
|
|
|
quads = new List<Quad>
|
|
|
{
|
|
|
- BuildQuad("quadY", matrixRotate0),
|
|
|
- BuildQuad("quadY1", matrixRotateX2),
|
|
|
- BuildQuad("quadZ", matrixRotateX),
|
|
|
- BuildQuad("quadZ1", matrixRotateX1),
|
|
|
- BuildQuad("quadX", matrixRotateZ),
|
|
|
- BuildQuad("quadX1", matrixRotateZ1)
|
|
|
+ BuildQuad("quadY", new Vector3(0, 0, 0)),
|
|
|
+ BuildQuad("quadY1", new Vector3(180, 0, 0)),
|
|
|
+ BuildQuad("quadZ", new Vector3(90, 0, 0)),
|
|
|
+ BuildQuad("quadZ1", new Vector3(-90, 0, 0)),
|
|
|
+ BuildQuad("quadX", new Vector3(0, 0, 90)),
|
|
|
+ BuildQuad("quadX1", new Vector3(0, 0, -90))
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private Quad BuildQuad(string name, int[] matrix)
|
|
|
+ private Quad BuildQuad(string name, Vector3 eulerAngles)
|
|
|
{
|
|
|
var (vertices, uv) = CreateVertices();
|
|
|
- RotateWithMatrix(vertices, matrix);
|
|
|
+ Rotate(vertices, eulerAngles);
|
|
|
var triangles = CreateTriangles();
|
|
|
|
|
|
return new Quad
|
|
|
@@ -130,23 +121,63 @@ public class PlaneGenerator : MonoBehaviour
|
|
|
mesh.GetComponent<MeshRenderer>().material = material;
|
|
|
}
|
|
|
|
|
|
- private void RotateWithMatrix(Vector3[] vertices, int[] matrix)
|
|
|
+ private void Rotate(Vector3[] vertices, Vector3 eulerAngles)
|
|
|
{
|
|
|
- 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]);
|
|
|
+ vertices[vertex] = Rotate(vertices[vertex], eulerAngles);
|
|
|
+ vertices[vertex] = Vector3.Normalize(vertices[vertex]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private Vector3 Rotate(Vector3 vertex, Vector3 eulerAngles)
|
|
|
+ {
|
|
|
+ var result = vertex;
|
|
|
+
|
|
|
+ if (eulerAngles.x != 0)
|
|
|
+ result = RotateX(result, eulerAngles.x);
|
|
|
+
|
|
|
+ if (eulerAngles.y != 0)
|
|
|
+ result = RotateY(result, eulerAngles.y);
|
|
|
+
|
|
|
+ if (eulerAngles.z != 0)
|
|
|
+ result = RotateZ(result, eulerAngles.z);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Vector3 RotateX(Vector3 vertex, float angle)
|
|
|
+ {
|
|
|
+ var angleDeg = Mathf.Deg2Rad * angle;
|
|
|
+
|
|
|
+ return new Vector3(
|
|
|
+ x: vertex.x,
|
|
|
+ y: vertex.y * Mathf.Cos(angleDeg) - vertex.z * Mathf.Sin(angleDeg),
|
|
|
+ z: vertex.y * Mathf.Sin(angleDeg) + vertex.z * Mathf.Cos(angleDeg));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Vector3 RotateY(Vector3 vertex, float angle)
|
|
|
+ {
|
|
|
+ var angleDeg = Mathf.Deg2Rad * angle;
|
|
|
+
|
|
|
+ return new Vector3(
|
|
|
+ x: vertex.x * Mathf.Cos(angleDeg) + vertex.z * Mathf.Sin(angleDeg),
|
|
|
+ y: vertex.y,
|
|
|
+ z: -vertex.x * Mathf.Sin(angleDeg) + vertex.z * Mathf.Cos(angleDeg));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Vector3 RotateZ(Vector3 vertex, float angle)
|
|
|
+ {
|
|
|
+ var angleDeg = Mathf.Deg2Rad * angle;
|
|
|
+
|
|
|
+ return new Vector3(
|
|
|
+ x: vertex.x * Mathf.Cos(angleDeg) - vertex.y * Mathf.Sin(angleDeg),
|
|
|
+ y: vertex.x * Mathf.Sin(angleDeg) + vertex.y * Mathf.Cos(angleDeg),
|
|
|
+ z: vertex.z);
|
|
|
+ }
|
|
|
|
|
|
private float GetNoise(float x, float y)
|
|
|
{
|