Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Road generation script from "You Are The Road", which is a hacked version of:
- // http://www.unifycommunity.com/wiki/index.php?title=OptimizedTrailRenderer
- // There are a few things left in this script which were not used, such as
- // the trail width, colour fading, etc.
- // As this was hastily modified for a 48h game jam game, this may not be the
- // best learning example!
- using UnityEngine;
- using System.Collections;
- public class Road : MonoBehaviour
- {
- // Material - Must be a particle material that has the "Tint Color" property
- public Material material;
- Material instanceMaterial;
- public float uMapScale = 0.1f;
- // Emit
- public bool emit = true;
- bool emittingDone = false;
- // Lifetime of each segment
- public float lifeTime = 1;
- float lifeTimeRatio = 1;
- float fadeOutRatio;
- // Colors
- public Color[] colors;
- // Widths
- public float[] widths;
- // Segment creation data
- public float maxAngle = 2;
- public float minVertexDistance = 0.1f;
- public float maxVertexDistance = 1f;
- public int pointCount = 30;
- public bool useCollider = false;
- MeshCollider meshCollider;
- // Object
- GameObject trailObj = null;
- Mesh mesh = null;
- // Points
- Point[] points;
- void Start ()
- {
- points = new Point[pointCount];
- trailObj = new GameObject("Trail");
- trailObj.transform.parent = null;
- trailObj.transform.position = Vector3.zero;
- trailObj.transform.rotation = Quaternion.identity;
- trailObj.transform.localScale = Vector3.one;
- MeshFilter meshFilter = (MeshFilter) trailObj.AddComponent(typeof(MeshFilter));
- mesh = meshFilter.mesh;
- if (useCollider) {
- meshCollider = trailObj.AddComponent<MeshCollider>();
- meshCollider.sharedMesh = mesh;
- }
- trailObj.AddComponent(typeof(MeshRenderer));
- instanceMaterial = new Material(material);
- fadeOutRatio = 1;//f / instanceMaterial.GetColor("_TintColor").a;
- trailObj.renderer.material = instanceMaterial;
- }
- public void Update ()
- {
- // Emitting - Designed for one-time use
- if( ! emit )
- emittingDone = true;
- if(emittingDone)
- emit = false;
- // Remove expired points
- for(int i = pointCount-1; i >=0; i--)
- {
- Point point = points[i];
- if(point == null || point.timeAlive > lifeTime)
- {
- points[i] = null;
- pointCount--;
- }
- else
- break;
- }
- // Do we add any new points?
- bool add = false;
- if(emit)
- {
- if(pointCount == 0)
- {
- points[pointCount++] = new Point(transform);
- points[pointCount++] = new Point(transform);
- }
- if(pointCount == 1)
- insertPoint();
- float sqrDistance = (points[1].position - transform.position).sqrMagnitude;
- if(sqrDistance > minVertexDistance * minVertexDistance)
- {
- if(sqrDistance > maxVertexDistance * maxVertexDistance)
- {
- add = true;
- }
- else if(Quaternion.Angle(transform.rotation, points[1].rotation) > maxAngle)
- {
- add = true;
- }
- }
- if(add)
- {
- //if(pointCount == points.Length)
- //System.Array.Resize(ref points, points.Length + 50);
- insertPoint();
- }
- if( ! add )
- points[0].update(transform);
- }
- // Do we render this?
- if(pointCount < 2)
- {
- trailObj.renderer.enabled = false;
- return;
- }
- trailObj.renderer.enabled = true;
- Color[] meshColors;
- lifeTimeRatio = 1 / lifeTime;
- // Do we fade it out?
- if( ! emit )
- {
- if(pointCount == 0)
- return;
- Color color = instanceMaterial.GetColor("_TintColor");
- color.a -= fadeOutRatio * lifeTimeRatio * Time.deltaTime;
- if(color.a > 0)
- instanceMaterial.SetColor("_TintColor", color);
- else
- {
- Destroy(trailObj);
- Destroy(this);
- }
- return;
- }
- points[0].distance = points[1].distance+((points[0].position-points[1].position).magnitude);
- {
- // Rebuild it
- Vector4[] tangents = new Vector4[pointCount * 2];
- Vector3[] vertices = new Vector3[pointCount * 2];
- Vector2[] uvs = new Vector2[pointCount * 2];
- int[] triangles = new int[(pointCount-1) * 6];
- meshColors = new Color[pointCount * 2];
- float uvMultiplier = 1 / (points[pointCount-1].timeAlive - points[0].timeAlive);
- for(int i = 0; i < pointCount; i++)
- {
- Point point = points[i];
- float ratio = point.timeAlive * lifeTimeRatio;
- // Color
- Color color;
- if(colors.Length == 0)
- color = Color.Lerp(Color.white, Color.clear, ratio);
- else if(colors.Length == 1)
- color = Color.Lerp(colors[0], Color.clear, ratio);
- else if(colors.Length == 2)
- color = Color.Lerp(colors[0], colors[1], ratio);
- else
- {
- float colorRatio = ratio * (colors.Length-1);
- int min = (int) Mathf.Floor(colorRatio);
- float lerp = Mathf.InverseLerp(min, min+1, colorRatio);
- color = Color.Lerp(colors[min], colors[min+1], lerp);
- }
- meshColors[i * 2] = color;
- meshColors[(i * 2) + 1] = color;
- // Width
- float width;
- if(widths.Length == 0)
- width = 1;
- else if(widths.Length == 1)
- width = widths[0];
- else if(widths.Length == 2)
- width = Mathf.Lerp(widths[0], widths[1], ratio);
- else
- {
- float widthRatio = ratio * (widths.Length-1);
- int min = (int) Mathf.Floor(widthRatio);
- float lerp = Mathf.InverseLerp(min, min+1, widthRatio);
- width = Mathf.Lerp(widths[min], widths[min+1], lerp);
- }
- trailObj.transform.position = point.position;
- trailObj.transform.rotation = point.rotation;
- vertices[i * 2] = trailObj.transform.TransformPoint(-width*0.5f,0,0);
- vertices[(i * 2) + 1] = trailObj.transform.TransformPoint( width*0.5f,0 , 0);
- // UVs
- //float uvRatio;
- //uvRatio = (point.timeAlive - points[0].timeAlive) * uvMultiplier;
- float u = point.distance * uMapScale;
- uvs[i * 2] = new Vector2(0,u);
- uvs[(i * 2) + 1] = new Vector2(1,u);
- tangents[i * 2] = new Vector4(1,0,0,1);
- tangents[(i * 2) + 1] = new Vector4(1,0,0,1);
- if(i > 0)
- {
- // Triangles
- int triIndex = (i - 1) * 6;
- int vertIndex = i * 2;
- triangles[triIndex+0] = vertIndex - 2;
- triangles[triIndex+1] = vertIndex - 1;
- triangles[triIndex+2] = vertIndex - 0;
- triangles[triIndex+3] = vertIndex + 1;
- triangles[triIndex+4] = vertIndex + 0;
- triangles[triIndex+5] = vertIndex - 1;
- }
- }
- trailObj.transform.position = Vector3.zero;
- trailObj.transform.rotation = Quaternion.identity;
- mesh.Clear();
- mesh.vertices = vertices;
- mesh.colors = meshColors;
- mesh.uv = uvs;
- mesh.triangles = triangles;
- mesh.RecalculateNormals();
- mesh.tangents = tangents;
- if (useCollider && add) {
- meshCollider.sharedMesh = null;
- meshCollider.sharedMesh = mesh;
- }
- }
- }
- void insertPoint()
- {
- for(int i = pointCount; i > 0; i--)
- {
- points[i] = points[i-1];
- }
- points[0] = new Point(transform);
- points[0].distance = points[1].distance + (points[0].position-points[1].position).magnitude;
- if (pointCount < points.Length-1) {
- pointCount++;
- }
- Car.waypoints.Add(points[0]);
- }
- }
- public class Point
- {
- public float timeCreated = 0;
- public float distance;
- public float timeAlive
- {
- get { return Time.time - timeCreated; }
- }
- public float fadeAlpha = 0;
- public Vector3 position = Vector3.zero;
- public Vector3 forward = Vector3.zero;
- public Vector3 right = Vector3.zero;
- public Quaternion rotation = Quaternion.identity;
- public Point(Transform trans)
- {
- position = trans.position;
- rotation = trans.rotation;
- forward = trans.forward;
- right = trans.right;
- timeCreated = Time.time;
- }
- public void update(Transform trans)
- {
- position = trans.position;
- rotation = trans.rotation;
- timeCreated = Time.time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment