Kaynağa Gözat

Генератор куба + нормализация до сферы

Andrey Ushakov 2 hafta önce
ebeveyn
işleme
4f6df87d06

+ 6 - 5
SpaceCraft.Unity/Assets/_Project/Scenes/Game.unity

@@ -938,6 +938,7 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 1e97fde5b479ad744928cbb9fb6b22a2, type: 3}
   m_Name: 
   m_EditorClassIdentifier: '::'
+  material: {fileID: 2100000, guid: bdf04b1dd42b18241a6b974938d5e840, type: 2}
 --- !u!33 &1579309478
 MeshFilter:
   m_ObjectHideFlags: 0
@@ -1005,7 +1006,7 @@ Transform:
   serializedVersion: 2
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
   m_LocalPosition: {x: -10.01, y: 0.09, z: -19.95}
-  m_LocalScale: {x: 10, y: 10, z: 10}
+  m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_Children: []
   m_Father: {fileID: 0}
@@ -1049,7 +1050,7 @@ GameObject:
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
   m_StaticEditorFlags: 0
-  m_IsActive: 1
+  m_IsActive: 0
 --- !u!114 &1606415198
 MonoBehaviour:
   m_ObjectHideFlags: 0
@@ -1072,7 +1073,7 @@ Transform:
   m_GameObject: {fileID: 1606415197}
   serializedVersion: 2
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalPosition: {x: -6.67, y: -2.37, z: -10.66}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_Children:
@@ -1398,7 +1399,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 6716779143748086990, guid: 6ae2d67c9c5eb9d4bb380a95834c904f, type: 3}
       propertyPath: m_LocalPosition.y
-      value: 6.08
+      value: 15.5
       objectReference: {fileID: 0}
     - target: {fileID: 6716779143748086990, guid: 6ae2d67c9c5eb9d4bb380a95834c904f, type: 3}
       propertyPath: m_LocalPosition.z
@@ -1488,7 +1489,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 6236465134544768010, guid: 9b786479c80c9de47ad5dabf196b90d6, type: 3}
       propertyPath: m_LocalPosition.y
-      value: 1.599
+      value: 11.35
       objectReference: {fileID: 0}
     - target: {fileID: 6236465134544768010, guid: 9b786479c80c9de47ad5dabf196b90d6, type: 3}
       propertyPath: m_LocalPosition.z

+ 147 - 14
SpaceCraft.Unity/Assets/_Project/Scripts/Game/PlaneGenerator.cs

@@ -1,29 +1,87 @@
+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<Quad> quads;
 
     // Start is called once before the first execution of Update after the MonoBehaviour is created
     void Start()
     {
+        BuildRootQuads();
+    }
+
+    // Update is called once per frame
+    void Update()
+    {
+        
+    }
+
+    private void BuildRootQuads()
+    {
+        var (vertices, uv) = CreateVertices();
+        var triangles = CreateTriangles();
+        RotatePlaneY(vertices);
+        CreateMesh("mesh_Y", vertices, uv, triangles);
+
+        (vertices, uv) = CreateVertices();
+        triangles = CreateTriangles();
+        RotatePlaneY1(vertices);
+        CreateMesh("mesh_Y1", vertices, uv, triangles);
+
+        (vertices, uv) = CreateVertices();
+        triangles = CreateTriangles();
+        RotatePlaneZ(vertices);
+        CreateMesh("mesh_Z", vertices, uv, triangles);
+
+        (vertices, uv) = CreateVertices();
+        triangles = CreateTriangles();
+        RotatePlaneZ1(vertices);
+        CreateMesh("mesh_Z1", vertices, uv, triangles);
+
+        (vertices, uv) = CreateVertices();
+        triangles = CreateTriangles();
+        RotatePlaneX(vertices);
+        CreateMesh("mesh_X", vertices, uv, triangles);
+
+        (vertices, uv) = CreateVertices();
+        triangles = CreateTriangles();
+        RotatePlaneX1(vertices);
+        CreateMesh("mesh_X1", vertices, uv, 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)];
-        var triangles = new int[(pqsResolution) * (pqsResolution) * 6];
 
         for (int x = 0, vertex = 0; x < pqsResolution + 1; x++)
         {
             for (int z = 0; z < pqsResolution + 1; z++, vertex++)
             {
-                vertices[vertex] = new Vector3(x, 0, z);
+                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 x = 1, vertex = 0; x<pqsResolution + 1; x++, vertex++)
         {
-            for (int z = 1; z < pqsResolution + 1; z++, vertex++)
+            for (int z = 1; z<pqsResolution + 1; z++, vertex++)
             {
                 triangles[triangle] = vertex;
                 triangles[triangle + 1] = vertex + pqsResolution + 2;
@@ -37,19 +95,94 @@ public class PlaneGenerator : MonoBehaviour
             }
         }
 
-        var mesh = GetComponent<MeshFilter>();
-        mesh.mesh.vertices = vertices;
-        mesh.mesh.triangles = triangles;
-        mesh.mesh.uv = uv;
+        return triangles;
+    }
 
-        GetComponent<MeshCollider>().sharedMesh = mesh.mesh;
-        mesh.mesh.RecalculateBounds();
-        mesh.mesh.RecalculateNormals();
+    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;
     }
 
-    // Update is called once per frame
-    void Update()
+    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;
+            }
+        }
     }
 }

+ 14 - 0
SpaceCraft.Unity/Assets/_Project/Scripts/Game/Quad.cs

@@ -0,0 +1,14 @@
+using UnityEngine;
+
+public struct Quad
+{
+    public int id { get; set; }
+
+    public int parentId { get; set; }
+
+    public int level { get; set; }
+
+    public Vector3 position { get; set; }
+
+    public Quaternion rotation { get; set; }
+}

+ 2 - 0
SpaceCraft.Unity/Assets/_Project/Scripts/Game/Quad.cs.meta

@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 04c077cc76ca8cc4eab902fac21175ce