Advertisement
polyfied

Create a mesh from a Playground Spline

Jul 23rd, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace PlaygroundSplines {
  5.     /// <summary>
  6.     /// The PlaygroundSplineMesh class lets you create a mesh from a Playground Spline.
  7.     /// </summary>
  8.     [ExecuteInEditMode()]
  9.     public class PlaygroundSplineMesh : MonoBehaviour {
  10.  
  11.         public PlaygroundSpline spline;
  12.         [Range(2,1000)]
  13.         public int points = 100;
  14.         [Range(.01f, 100f)]
  15.         public float width = 1f;
  16.  
  17.         int prevPoints;
  18.         float prevWidth;
  19.  
  20.         void OnEnable ()
  21.         {
  22.             if (GetComponent<Renderer>() == null)
  23.                 gameObject.AddComponent<MeshRenderer>();
  24.             if (spline == null)
  25.                 spline = GetComponent<PlaygroundSpline>();
  26.             if (spline != null)
  27.                 BuildSplineMesh(spline, points, width);
  28.             prevPoints = points;
  29.             prevWidth = width;
  30.         }
  31.  
  32.         void Update ()
  33.         {
  34.             if (prevPoints!=points || prevWidth!=width)
  35.             {
  36.                 if (spline != null)
  37.                     BuildSplineMesh(spline, points, width);
  38.                 prevPoints = points;
  39.                 prevWidth = width;
  40.             }
  41.         }
  42.        
  43.         public void BuildSplineMesh (PlaygroundSpline spline, int points, float width)
  44.         {
  45.             if (points<2)
  46.                 points = 2;
  47.             int totalVertices = points*2;
  48.             MeshFilter _mf = GetComponent<MeshFilter>()!=null? GetComponent<MeshFilter>() : gameObject.AddComponent<MeshFilter>();
  49.             Mesh _m = new Mesh();
  50.             Vector3[] verts = new Vector3[totalVertices];
  51.             Vector2[] uvs = new Vector2[totalVertices];
  52.             int[] tris = new int[(points-1)*6];
  53.  
  54.             // Construct the mesh
  55.             for (int i = 0; i<points; i++)
  56.             {
  57.                 // Create a normalized time value
  58.                 float t = (i*1f)/(points-1);
  59.                 float tNext = ((i*1f)+1)/(points-1);
  60.                 if (t>=1)
  61.                     t = .9999f;
  62.                 if (tNext>=1)
  63.                     tNext = .99999f;
  64.  
  65.                 // Get the current and next position from the spline on time
  66.                 Vector3 currentPosition = spline.GetPoint (t);
  67.                 Vector3 nextPosition = spline.GetPoint (tNext);
  68.  
  69.                 // Raycast down to determine up direction (especially practical for roads / rivers)
  70.                 RaycastHit hit;
  71.                 Vector3 up = Vector3.up;
  72.                 if (Physics.Raycast (currentPosition, Vector3.down, out hit))
  73.                     up = hit.normal;
  74.  
  75.                 // Create two width point references based on current and next position
  76.                 Vector3 dir = (Vector3.Cross(up, nextPosition - currentPosition)).normalized;
  77.                 Vector3 lPoint = currentPosition + dir * (width/2);
  78.                 Vector3 rPoint = currentPosition - dir * (width/2);
  79.  
  80.                 // Draw debug
  81.                 Debug.DrawLine(lPoint, rPoint);
  82.  
  83.                 verts[i*2] = lPoint;
  84.                 verts[(i*2)+1] = rPoint;
  85.                 uvs[i*2] = new Vector2(t,0);
  86.                 uvs[(i*2)+1] = new Vector2(t,1f);
  87.  
  88.                 if (i>0)
  89.                 {
  90.                     int triIndex = (i-1)*6;
  91.                     int vertIndex = i*2;
  92.  
  93.                     tris[triIndex] = vertIndex-2;
  94.                     tris[triIndex+1] = vertIndex-1;
  95.                     tris[triIndex+2] = vertIndex;
  96.                     tris[triIndex+3] = vertIndex;
  97.                     tris[triIndex+4] = vertIndex-1;
  98.                     tris[triIndex+5] = vertIndex+1;
  99.                 }
  100.             }
  101.  
  102.             // Assign the data to the mesh
  103.             _m.vertices = verts;
  104.             _m.uv = uvs;
  105.             _m.triangles = tris;
  106.             _m.RecalculateNormals();
  107.  
  108.             // Assign the mesh to the MeshFilter
  109.             _mf.mesh = _m;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement