private void AddPoint(TrailPoint point, Vector3 direction, float uv) { float lifePercent = (Time.time - point.creationTime) / lifetime; float halfWidth = width.Evaluate(lifePercent); float normalStrength = strength.Evaluate(lifePercent); Color normalStrengthColor = new Color(normalStrength, normalStrength, normalStrength, normalStrength); direction.Normalize(); Vector3 pos = point.pos; Vector3 right = Vector3.Cross(upDir, direction); vertices.Add(pos - right * halfWidth); vertices.Add(pos + right * halfWidth); uvs.Add(new Vector2(0, uv)); uvs.Add(new Vector2(1, uv)); normals.Add(upDir); normals.Add(upDir); tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1)); tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1)); colors.Add(normalStrengthColor); colors.Add(normalStrengthColor); int lastVert = vertices.Count - 1; if (lastVert >= 3) { triangles.Add(lastVert - 1); triangles.Add(lastVert); triangles.Add(lastVert - 2); triangles.Add(lastVert - 2); triangles.Add(lastVert - 3); triangles.Add(lastVert - 1); } }
private void UpdateMesh(List <TrailPoint> renderPoints) { mesh.Clear(); if (renderPoints.Count < 2) { return; } //Clear lists vertices.Clear(); triangles.Clear(); uvs.Clear(); normals.Clear(); tangents.Clear(); colors.Clear(); float uvFactor = 1.0f / (renderPoints.Count - 1); //Iterate though all previous points for (int i = 0; i < renderPoints.Count; i++) { //First point TrailPoint point = renderPoints[i]; if (i == 0) { AddPoint(point, renderPoints[i + 1].pos - point.pos, 0); continue; } //Last point TrailPoint lastPoint = renderPoints[i - 1]; if (i == renderPoints.Count - 1) { AddPoint(point, point.pos - lastPoint.pos, 1); break; } //In-between points TrailPoint nextPoint = renderPoints[i + 1]; AddPoint(point, nextPoint.pos - lastPoint.pos, i * uvFactor); } mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uvs.ToArray(); mesh.normals = normals.ToArray(); mesh.tangents = tangents.ToArray(); mesh.colors = colors.ToArray(); //Those only work in Unity 5.2 and above... shouldn't be much faster anyways. //mesh.SetVertices(vertices); //mesh.SetTriangles(triangles, 0); //mesh.SetUVs(0, uvs); //mesh.SetNormals(normals); //mesh.SetTangents(tangents); //mesh.SetColors(colors); }