Created
February 5, 2018 23:46
-
-
Save drZool/ebb9e4dcb54492b72c3c1ce7820133b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| [ExecuteInEditMode] | |
| public class TubeTrail : MonoBehaviour | |
| { | |
| public int sections = 16; | |
| public float radius = 0.5f; | |
| public int columns = 8; | |
| public float distance = 0.01f; | |
| public List<Vector3> verts = new List<Vector3> (); | |
| public List<Vector3> normals = new List<Vector3> (); | |
| public List<int> tris = new List<int> (); | |
| List<Vector3> points; | |
| MeshFilter filter; | |
| void OnEnable () | |
| { | |
| Setup (); | |
| } | |
| void Setup () | |
| { | |
| //if (verts.Count == 0) | |
| CreateMesh (); | |
| if (points == null) { | |
| points = new List<Vector3> (); | |
| for (int i = 0; i < sections; ++i) | |
| points.Add (Vector3.zero); | |
| } | |
| filter = GetComponent<MeshFilter> (); | |
| UploadVBO (); | |
| } | |
| void CreateMesh () | |
| { | |
| if (sections < 2) | |
| sections = 2; | |
| if (columns < 4) | |
| columns = 4; | |
| verts.Clear (); | |
| normals.Clear (); | |
| for (int i = 0; i < sections; ++i) { | |
| for (float j = 0; j < columns; ++j) { | |
| verts.Add (new Vector3 (Mathf.Sin (Mathf.PI * 2 * j / columns) * radius, Mathf.Cos (Mathf.PI * 2 * j / columns) * radius, i * distance)); | |
| normals.Add (new Vector3 (Mathf.Sin (Mathf.PI * 2 * j / columns), Mathf.Cos (Mathf.PI * 2 * j / columns), 0).normalized); | |
| } | |
| } | |
| if (tris.Count != sections * columns * 12) { | |
| Debug.Log ("Tris " + tris.Count + " != " + ((sections - 1) * columns * 12)); | |
| tris.Clear (); | |
| for (int i = 0; i < sections; ++i) { | |
| int index = i * columns; | |
| int nextI = (i + 1) * columns; | |
| if ((i + 1) >= sections) | |
| nextI = index; //Dont overshoot... | |
| for (int j = 0; j < columns; ++j) { | |
| tris.Add (index + j); | |
| tris.Add (nextI + j); | |
| tris.Add (index + ((j + 1) % columns)); | |
| tris.Add (index + ((j + 1) % columns)); | |
| tris.Add (nextI + j); | |
| tris.Add (nextI + ((j + 1) % columns)); | |
| //Double sided | |
| tris.Add (index + j); | |
| tris.Add (index + ((j + 1) % columns)); | |
| tris.Add (nextI + j); | |
| tris.Add (index + ((j + 1) % columns)); | |
| tris.Add (nextI + ((j + 1) % columns)); | |
| tris.Add (nextI + j); | |
| } | |
| } | |
| } | |
| } | |
| void UploadVBO () | |
| { | |
| if (filter.sharedMesh == null) | |
| filter.sharedMesh = new Mesh (); | |
| filter.sharedMesh.SetVertices (verts); | |
| filter.sharedMesh.SetTriangles (tris, 0, true); | |
| //filter.sharedMesh.RecalculateNormals (); | |
| filter.sharedMesh.SetNormals (normals); | |
| filter.sharedMesh.UploadMeshData (false); //set to true? | |
| } | |
| void Update () | |
| { | |
| //Setup (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment