Enfisk

Spline Level Generation (17 apr 2015)

Apr 17th, 2015
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. /********************
  2. * LevelGenerator.cs *
  3. ********************/
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8.  
  9. public class LevelGenerator : MonoBehaviour {
  10.     public GameObject copyFrom;
  11.  
  12.     public List<LevelPart> parts = new List<LevelPart>();
  13.     public List<BezierSpline> splines = new List<BezierSpline>();
  14.  
  15.     public void PopulateLists() {
  16.         parts.Clear();
  17.         splines.Clear();
  18.  
  19.         LevelPart[] levelparts = FindObjectsOfType<LevelPart>();
  20.         BezierSpline[] existingSplines = FindObjectsOfType<BezierSpline>();
  21.        
  22.         parts.AddRange(levelparts);
  23.         splines.AddRange(existingSplines);
  24.     }
  25.  
  26.     public void GenerateObjects() {
  27.         for (int i = 0; i < splines.Count; ++i) {
  28.             CreateFromSpline(splines[i]);
  29.         }
  30.     }
  31.  
  32.     private void CreateFromSpline(BezierSpline spline) {
  33.         for (int i = 0; i < spline.NumPoints - 1; i += 3) {
  34.             Vector3[] points = Handles.MakeBezierPoints(spline.GetControlPoint(i),
  35.                 spline.GetControlPoint(i + 3),
  36.                 spline.GetControlPoint(i + 1),
  37.                 spline.GetControlPoint(i + 2), 20);
  38.  
  39.             for (int j = 0; j < points.Length; ++j) {
  40.                 GameObject temp = Instantiate(copyFrom);
  41.                 temp.transform.position = spline.transform.position + points[j];
  42.                
  43.                 parts.Add(temp.GetComponent<LevelPart>());
  44.             }
  45.         }
  46.     }
  47.  
  48.     public void ConnectAllVertices() {
  49.         for (int i = 0; i < parts.Count; ++i) {
  50.             if (i + 1 >= parts.Count) {
  51.                 parts[0].ConnectMeshes(parts[i].gameObject);
  52.                 break;
  53.             }
  54.             parts[i + 1].ConnectMeshes(parts[i].gameObject);
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60. /***************
  61. * LevelPart.cs *
  62. ***************/
  63.  
  64. using UnityEngine;
  65. using UnityEditor;
  66. using System.Collections;
  67.  
  68. public class LevelPart : MonoBehaviour {
  69.     private Mesh mesh;
  70.     private Vector3[] vertices;
  71.  
  72.     //public GameObject otherObject;
  73.     // Use this for initialization
  74.     void Start() {
  75.         mesh = GetComponent<MeshFilter>().mesh;
  76.         vertices = mesh.vertices;
  77.         //ConnectMeshes(otherObject);
  78.     }
  79.  
  80.     // Update is called once per frame
  81.     void Update() {
  82.  
  83.     }
  84.  
  85.     public void ConnectMeshes(GameObject p_connectTo) {
  86.         Vector3[] otherVertices = p_connectTo.GetComponent<MeshFilter>().mesh.vertices;
  87.         //Back: 2/8/21 (topleft), 0/15/22 (bottomleft), 4/10/23 (topright), 6/12/20 (bottomright)
  88.         //Front: 3/9/19 (topleft), 1/13/16 (bottomleft), 5/11/17 (topright), 7/14/18 (bottomright)
  89.  
  90.         //topleft
  91.         vertices[3] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[2]));
  92.         vertices[9] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[8]));
  93.         vertices[19] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[21]));
  94.  
  95.         //bottomleft
  96.         vertices[1] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[0]));
  97.         vertices[13] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[15]));
  98.         vertices[16] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[22]));
  99.  
  100.         //topright
  101.         vertices[5] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[4]));
  102.         vertices[11] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[10]));
  103.         vertices[17] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[23]));
  104.  
  105.         //bottomright
  106.         vertices[7] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[6]));
  107.         vertices[14] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[12]));
  108.         vertices[18] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[20]));
  109.  
  110.  
  111.         mesh.vertices = vertices;
  112.         mesh.RecalculateBounds();
  113.         mesh.RecalculateNormals();
  114.  
  115.         //take the front 4 vertices, set their position to the back 4 of p_connectTo
  116.         //should result in a pointy-ass mesh, but should work
  117.     }
  118. }
  119.  
  120.  
  121. /********************
  122. * LevelInspector.cs *
  123. ********************/
  124.  
  125. using UnityEngine;
  126. using UnityEditor;
  127. using System.Collections;
  128. using System.Collections.Generic;
  129.  
  130. [CustomEditor(typeof(LevelPart))]
  131. public class LevelInspector : Editor {
  132.     //private LevelGenerator level;
  133.  
  134.     [DrawGizmo(GizmoType.NotSelected | GizmoType.SelectedOrChild)]
  135.     static void RenderCustomGizmo(GameObject go, GizmoType gizmoType) {
  136.         MeshFilter meshFilter = go.GetComponent<MeshFilter>();
  137.  
  138.         if (meshFilter != null) {
  139.             Mesh mesh = meshFilter.sharedMesh;
  140.             Dictionary<Vector3, float> dupes = new Dictionary<Vector3, float>();
  141.             Vector3[] vertices = mesh.vertices;
  142.             for (int i = 0; i < mesh.vertexCount; ++i) {
  143.                 if (dupes.ContainsKey(vertices[i])) {
  144.                     dupes[vertices[i]] += 0.05f;
  145.                 }
  146.                 else {
  147.                     dupes.Add(vertices[i], 0.0f);
  148.                 }
  149.  
  150.                 Handles.Label(vertices[i] + go.transform.position + new Vector3(0f, dupes[vertices[i]], 0f), "Vertex " + i);
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156. [CustomEditor(typeof(LevelGenerator))]
  157. public class LevelGeneratorInspector : Editor {
  158.     public override void OnInspectorGUI() {
  159.         LevelGenerator lg = target as LevelGenerator;
  160.         DrawDefaultInspector();
  161.  
  162.         if (GUILayout.Button("Find objects")) {
  163.             lg.PopulateLists();
  164.         }
  165.  
  166.         if (GUILayout.Button("Generate Objects")) {
  167.             lg.GenerateObjects();
  168.         }
  169.  
  170.         if (GUILayout.Button("Connect Meshes")) {
  171.             lg.ConnectAllVertices();
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment