Advertisement
Guest User

trackgen

a guest
Jun 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class trackGen : MonoBehaviour
  6. {
  7.     public int sectionNum = 20;
  8.     public float minWidth = 5;
  9.     public float MaxWidth = 10;
  10.     public float sectionLenth = 5;
  11.  
  12.     public float noiseOffset;
  13.  
  14.     [Range(0,10)]
  15.     public float curveyFactor = 1;
  16.  
  17.     public MeshFilter meshFilter;
  18.  
  19.     [System.NonSerialized]
  20.     public List<Vector3> newVerts = new List<Vector3>();
  21.     [System.NonSerialized]
  22.     public List<int> newTris = new List<int>();
  23.  
  24.     MeshCollider meshCollider;
  25.  
  26.     public void GenerateTrack()
  27.     {
  28.         Mesh mesh = new Mesh();
  29.  
  30.         meshCollider = GetComponent<MeshCollider>();
  31.         meshFilter.mesh = mesh;
  32.  
  33.         mesh.Clear();
  34.  
  35.         newVerts.Clear();
  36.         newTris.Clear();
  37.  
  38.         Vector3 pos = Vector3.zero;
  39.         Vector3 lastPos = Vector3.zero;
  40.  
  41.         float initialOffset = (Mathf.PerlinNoise(0, 0 * 0.1f) * 15) + (Mathf.PerlinNoise(0, 0 * 0.05f) * 60);
  42.         float roadWidth = 10;
  43.         float lastRoadWidth = 10;
  44.  
  45.         meshCollider.sharedMesh = mesh;
  46.  
  47.         for (int i = 0; i < sectionNum; i++)
  48.         {
  49.             pos = new Vector3((Mathf.PerlinNoise( noiseOffset, i * 0.1f) * 15) + (Mathf.PerlinNoise(noiseOffset, i * 0.05f) * 60) - initialOffset, 0, sectionLenth * i);
  50.             roadWidth = Mathf.PerlinNoise(i * 0.2f, noiseOffset)  * (MaxWidth-minWidth) + minWidth;
  51.            // roadWidth = 10;
  52.             addSection((pos.x) * curveyFactor - roadWidth, (pos.x) * curveyFactor + roadWidth,( lastPos.x) * curveyFactor - lastRoadWidth,( lastPos.x) * curveyFactor + lastRoadWidth, pos.z);
  53.  
  54.             lastRoadWidth = roadWidth;
  55.             lastPos = pos;
  56.         }
  57.  
  58.  
  59.  
  60.         mesh.vertices = newVerts.ToArray();
  61.         mesh.triangles = newTris.ToArray();
  62.         mesh.RecalculateNormals();
  63.     }
  64.  
  65.     void addSection(float frontleft, float frontright, float backleft, float backright,float z) {
  66.  
  67.         AddQuad(new Vector3(frontleft, 0, 5 + z), new Vector3(frontright, 0, 5 + z), new Vector3(backleft, 0, 0 + z), new Vector3(backright, 0, 0 + z));
  68.         AddQuad(new Vector3(backleft,5, 0 + z), new Vector3(frontleft, 5, 5 + z), new Vector3(backleft, 0, 0 + z), new Vector3(frontleft, 0, 5 + z));
  69.         AddQuad(  new Vector3(backright, 0, 0 + z), new Vector3(frontright, 0, 5 + z), new Vector3(backright, 5, 0 + z), new Vector3(frontright, 5, 5 + z));
  70.  
  71.     }
  72.  
  73.     // quad orientation vv
  74.  
  75.     //------------//
  76.     // A        B //
  77.     //         /  //
  78.     //        /   //
  79.     //     /      //
  80.     // C/       D //
  81.     //------------//
  82.  
  83.     void AddQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 d) {
  84.  
  85.         AddTri(a, b, c);
  86.         AddTri(b, d, c);
  87.     }
  88.  
  89.     void AddTri(Vector3 a, Vector3 b, Vector3 c)
  90.     {
  91.         int vertsLength = newVerts.Count;
  92.  
  93.         newVerts.Add(a);
  94.         newVerts.Add(b);
  95.         newVerts.Add(c);
  96.  
  97.         newTris.Add(vertsLength + 0);
  98.         newTris.Add(vertsLength + 1);
  99.         newTris.Add(vertsLength + 2);
  100.     }
  101.  
  102.     // Update is called once per frame
  103.     void Update()
  104.     {
  105.        
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement