Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************
- * LevelGenerator.cs *
- ********************/
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- public class LevelGenerator : MonoBehaviour {
- public GameObject copyFrom;
- public List<LevelPart> parts = new List<LevelPart>();
- public List<BezierSpline> splines = new List<BezierSpline>();
- public void PopulateLists() {
- parts.Clear();
- splines.Clear();
- LevelPart[] levelparts = FindObjectsOfType<LevelPart>();
- BezierSpline[] existingSplines = FindObjectsOfType<BezierSpline>();
- parts.AddRange(levelparts);
- splines.AddRange(existingSplines);
- }
- public void GenerateObjects() {
- for (int i = 0; i < splines.Count; ++i) {
- CreateFromSpline(splines[i]);
- }
- }
- private void CreateFromSpline(BezierSpline spline) {
- for (int i = 0; i < spline.NumPoints - 1; i += 3) {
- Vector3[] points = Handles.MakeBezierPoints(spline.GetControlPoint(i),
- spline.GetControlPoint(i + 3),
- spline.GetControlPoint(i + 1),
- spline.GetControlPoint(i + 2), 20);
- for (int j = 0; j < points.Length; ++j) {
- GameObject temp = Instantiate(copyFrom);
- temp.transform.position = spline.transform.position + points[j];
- parts.Add(temp.GetComponent<LevelPart>());
- }
- }
- }
- public void ConnectAllVertices() {
- for (int i = 0; i < parts.Count; ++i) {
- if (i + 1 >= parts.Count) {
- parts[0].ConnectMeshes(parts[i].gameObject);
- break;
- }
- parts[i + 1].ConnectMeshes(parts[i].gameObject);
- }
- }
- }
- /***************
- * LevelPart.cs *
- ***************/
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- public class LevelPart : MonoBehaviour {
- private Mesh mesh;
- private Vector3[] vertices;
- //public GameObject otherObject;
- // Use this for initialization
- void Start() {
- mesh = GetComponent<MeshFilter>().mesh;
- vertices = mesh.vertices;
- //ConnectMeshes(otherObject);
- }
- // Update is called once per frame
- void Update() {
- }
- public void ConnectMeshes(GameObject p_connectTo) {
- Vector3[] otherVertices = p_connectTo.GetComponent<MeshFilter>().mesh.vertices;
- //Back: 2/8/21 (topleft), 0/15/22 (bottomleft), 4/10/23 (topright), 6/12/20 (bottomright)
- //Front: 3/9/19 (topleft), 1/13/16 (bottomleft), 5/11/17 (topright), 7/14/18 (bottomright)
- //topleft
- vertices[3] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[2]));
- vertices[9] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[8]));
- vertices[19] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[21]));
- //bottomleft
- vertices[1] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[0]));
- vertices[13] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[15]));
- vertices[16] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[22]));
- //topright
- vertices[5] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[4]));
- vertices[11] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[10]));
- vertices[17] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[23]));
- //bottomright
- vertices[7] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[6]));
- vertices[14] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[12]));
- vertices[18] = transform.InverseTransformPoint(p_connectTo.transform.TransformPoint(otherVertices[20]));
- mesh.vertices = vertices;
- mesh.RecalculateBounds();
- mesh.RecalculateNormals();
- //take the front 4 vertices, set their position to the back 4 of p_connectTo
- //should result in a pointy-ass mesh, but should work
- }
- }
- /********************
- * LevelInspector.cs *
- ********************/
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- [CustomEditor(typeof(LevelPart))]
- public class LevelInspector : Editor {
- //private LevelGenerator level;
- [DrawGizmo(GizmoType.NotSelected | GizmoType.SelectedOrChild)]
- static void RenderCustomGizmo(GameObject go, GizmoType gizmoType) {
- MeshFilter meshFilter = go.GetComponent<MeshFilter>();
- if (meshFilter != null) {
- Mesh mesh = meshFilter.sharedMesh;
- Dictionary<Vector3, float> dupes = new Dictionary<Vector3, float>();
- Vector3[] vertices = mesh.vertices;
- for (int i = 0; i < mesh.vertexCount; ++i) {
- if (dupes.ContainsKey(vertices[i])) {
- dupes[vertices[i]] += 0.05f;
- }
- else {
- dupes.Add(vertices[i], 0.0f);
- }
- Handles.Label(vertices[i] + go.transform.position + new Vector3(0f, dupes[vertices[i]], 0f), "Vertex " + i);
- }
- }
- }
- }
- [CustomEditor(typeof(LevelGenerator))]
- public class LevelGeneratorInspector : Editor {
- public override void OnInspectorGUI() {
- LevelGenerator lg = target as LevelGenerator;
- DrawDefaultInspector();
- if (GUILayout.Button("Find objects")) {
- lg.PopulateLists();
- }
- if (GUILayout.Button("Generate Objects")) {
- lg.GenerateObjects();
- }
- if (GUILayout.Button("Connect Meshes")) {
- lg.ConnectAllVertices();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment