Browse Source

Вращение квадрантов матрицами

Andrey Ushakov 2 weeks ago
parent
commit
aca07fd6d6

+ 4 - 1
SpaceCraft.Unity/Assets/_Project/Scenes/Game.unity

@@ -276,7 +276,9 @@ MonoBehaviour:
   m_EditorClassIdentifier: Assembly-CSharp::CameraController
   player: {fileID: 1579309476}
   offset: {x: -20, y: 20, z: -20}
-  distance: 40
+  distance: 5
+  minDistance: 2
+  mouseSensitivity: 0.5
 --- !u!1 &410087039
 GameObject:
   m_ObjectHideFlags: 0
@@ -940,6 +942,7 @@ MonoBehaviour:
   m_Name: 
   m_EditorClassIdentifier: '::'
   material: {fileID: 2100000, guid: bdf04b1dd42b18241a6b974938d5e840, type: 2}
+  noiseLevel: 0.1
 --- !u!33 &1579309478
 MeshFilter:
   m_ObjectHideFlags: 0

+ 5 - 4
SpaceCraft.Unity/Assets/_Project/Scripts/Game/CameraController.cs

@@ -5,10 +5,11 @@ public class CameraController : MonoBehaviour
 {
     public GameObject player;
     public Vector3 offset;
-    public float distance = 20;
+    public float distance = 5;
+    public float minDistance = 2;
+    public float mouseSensitivity = 0.5f;
 
     private Vector2 mouseOrigin;
-    private Vector3 cameraOrigin;
 
     private bool drag = false;
 
@@ -23,6 +24,7 @@ public class CameraController : MonoBehaviour
         if (Mouse.current.scroll.value != Vector2.zero)
         {
             distance -= Mouse.current.scroll.value.y;
+            distance = Mathf.Max(distance, minDistance);
             UpdateCamera();
         }
     }
@@ -38,7 +40,6 @@ public class CameraController : MonoBehaviour
             {
                 drag = true;
                 mouseOrigin = mousePosition;
-                cameraOrigin = Camera.main.transform.position;
             }
 
         }
@@ -53,7 +54,7 @@ public class CameraController : MonoBehaviour
             var mouseDiff = mouseOrigin - mousePosition;
             var mouseDiff3 = (Vector3)mouseDiff;
             mouseOrigin = mousePosition;
-            Camera.main.transform.Translate(mouseDiff3);
+            Camera.main.transform.Translate(mouseDiff3 * mouseSensitivity * distance / 50);
             Camera.main.transform.rotation = Quaternion.LookRotation(player.transform.position - Camera.main.transform.position);
 
             UpdateCamera();

+ 33 - 86
SpaceCraft.Unity/Assets/_Project/Scripts/Game/PlaneGenerator.cs

@@ -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;
     }
 }