Advertisement
xezrunner

Untitled

Sep 24th, 2020 (edited)
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. public Mesh CreateMesh(float startDistance = 0f, float length = 8f, float xPosition = 0f, float width = 0f)
  2.     {
  3.         if (width == 0f) width = roadWidth;
  4.  
  5.         Vector3[] verts = new Vector3[path.NumPoints * 8];
  6.         Vector2[] uvs = new Vector2[verts.Length];
  7.         Vector3[] normals = new Vector3[verts.Length];
  8.  
  9.         int numTris = 2 * (path.NumPoints - 1) + ((path.isClosedLoop) ? 2 : 0);
  10.         int[] roadTriangles = new int[numTris * 3];
  11.         int[] underRoadTriangles = new int[numTris * 3];
  12.         int[] sideOfRoadTriangles = new int[numTris * 2 * 3];
  13.  
  14.         bool usePathNormals = !(path.space == PathSpace.xyz && flattenSurface);
  15.  
  16.         /* Vertices for the top of the road are laid out like this:
  17.            0  1
  18.            8  9
  19.         and so on... So the triangle map 0,8,1 for example, defines a triangle from top left to bottom left to bottom right. */
  20.         int[] triangleMap = { 0, 8, 1, 1, 8, 9 };
  21.         int[] sidesTriangleMap = { 4, 6, 14, 12, 4, 14, 5, 15, 7, 13, 15, 5 };
  22.  
  23.         // Get start and end vertex index data
  24.         if (length == -1) // -1 means full path length
  25.             length = path.length;
  26.         var startVertex = path.GetIndexAtDistance(startDistance, EndOfPathInstruction.Stop);
  27.         var endVertex = path.GetIndexAtDistance(startDistance + length, EndOfPathInstruction.Stop);
  28.  
  29.         int vertIndex = 0;
  30.         int triIndex = 0;
  31.  
  32.         for (int i = startVertex.previousIndex; i <= endVertex.nextIndex; i++) // Go through indexes between start and end (end is not included)
  33.         {
  34.             Vector3 localUp = (usePathNormals) ? Vector3.Cross(path.GetTangent(i), path.GetNormal(i)) : path.up;
  35.             Vector3 localRight = (usePathNormals) ? path.GetNormal(i) : Vector3.Cross(localUp, path.GetTangent(i));
  36.  
  37.             // Find position to left and right of current path vertex
  38.             Vector3 vertSideA = path.GetPoint(i) - localRight * Mathf.Abs(roadWidth);
  39.             Vector3 vertSideB = path.GetPoint(i) + localRight * Mathf.Abs(roadWidth);
  40.  
  41.             // ***** OFFSET VERTEX POINTS FOR PARALLEL CURVES *****
  42.             vertSideA += localRight * xPosition;
  43.             vertSideB += localRight * xPosition;
  44.  
  45.             #region Add vertices, UVs and normals
  46.             // Add top of road vertices
  47.             verts[vertIndex + 0] = vertSideA;
  48.             verts[vertIndex + 1] = vertSideB;
  49.             // Add bottom of road vertices
  50.             verts[vertIndex + 2] = vertSideA - localUp * thickness;
  51.             verts[vertIndex + 3] = vertSideB - localUp * thickness;
  52.  
  53.             // Duplicate vertices to get flat shading for sides of road
  54.             verts[vertIndex + 4] = verts[vertIndex + 0];
  55.             verts[vertIndex + 5] = verts[vertIndex + 1];
  56.             verts[vertIndex + 6] = verts[vertIndex + 2];
  57.             verts[vertIndex + 7] = verts[vertIndex + 3];
  58.  
  59.             // Set uv on y axis to path time (0 at start of path, up to 1 at end of path)
  60.             uvs[vertIndex + 0] = new Vector2(0, path.times[i]);
  61.             uvs[vertIndex + 1] = new Vector2(1, path.times[i]);
  62.  
  63.             // Top of road normals
  64.             normals[vertIndex + 0] = localUp;
  65.             normals[vertIndex + 1] = localUp;
  66.             // Bottom of road normals
  67.             normals[vertIndex + 2] = -localUp;
  68.             normals[vertIndex + 3] = -localUp;
  69.             // Sides of road normals
  70.             normals[vertIndex + 4] = -localRight;
  71.             normals[vertIndex + 5] = localRight;
  72.             normals[vertIndex + 6] = -localRight;
  73.             normals[vertIndex + 7] = localRight;
  74.             #endregion
  75.  
  76.             // Set triangle indices
  77.             if (i < endVertex.nextIndex || path.isClosedLoop)
  78.             {
  79.                 for (int j = 0; j < triangleMap.Length; j++)
  80.                 {
  81.                     roadTriangles[triIndex + j] = (vertIndex + triangleMap[j]) % verts.Length;
  82.  
  83.                     // reverse triangle map for under road so that triangles wind the other way and are visible from underneath
  84.                     underRoadTriangles[triIndex + j] = (vertIndex + triangleMap[triangleMap.Length - 1 - j] + 2) % verts.Length;
  85.                 }
  86.                 for (int j = 0; j < sidesTriangleMap.Length; j++)
  87.                     sideOfRoadTriangles[triIndex * 2 + j] = (vertIndex + sidesTriangleMap[j]) % verts.Length;
  88.             }
  89.  
  90.             // add to vert and tri index counters
  91.             vertIndex += 8;
  92.             triIndex += 6;
  93.         }
  94.  
  95.         // Create and setup mesh
  96.         Mesh mesh = new Mesh();
  97.  
  98.         mesh.vertices = verts;
  99.         mesh.uv = uvs;
  100.         mesh.normals = normals;
  101.         mesh.subMeshCount = 3;
  102.  
  103.         mesh.SetTriangles(roadTriangles, 0);
  104.         mesh.SetTriangles(underRoadTriangles, 1);
  105.         mesh.SetTriangles(sideOfRoadTriangles, 2);
  106.  
  107.         mesh.RecalculateBounds();
  108.  
  109.         return mesh;
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement