public void GenerateMesh(ref Mesh result) { if (result == null) { result = new Mesh(); result.hideFlags = HideFlags.HideAndDontSave; result.MarkDynamic(); } if (vertices.Count < 2) { result.Clear(); return; } int additionalVertices = this.brushType == BrushType.Sphere ? 8 : 2; int actualVertexCount = vertices.Count + additionalVertices; if (actualVertexCount != meshVertices.Count) { if (actualVertexCount < meshVertices.Count) { int toRemove = meshVertices.Count - actualVertexCount; meshVertices.RemoveRange(actualVertexCount, toRemove); meshTangents.RemoveRange(actualVertexCount, toRemove); meshNormals.RemoveRange(actualVertexCount, toRemove); uvs.RemoveRange(actualVertexCount, toRemove); colors.RemoveRange(actualVertexCount, toRemove); } else { int toAdd = actualVertexCount - meshVertices.Count; meshVertices.AddRange(Enumerable.Repeat(default(Vector3), toAdd)); meshTangents.AddRange(Enumerable.Repeat(default(Vector4), toAdd)); meshNormals.AddRange(Enumerable.Repeat(default(Vector3), toAdd)); uvs.AddRange(Enumerable.Repeat(default(Vector2), toAdd)); colors.AddRange(Enumerable.Repeat(default(Color), toAdd)); } } int[] indices = null; TubeRenderer.GenerateMesh(vertices, bufferDataIndex, Flat, Web, brushType, ExtendsMultiplier, meshVertices, meshTangents, meshNormals, uvs, colors, ref indices, ref currentTotalLength); result.Clear(); result.SetVertices(meshVertices); result.SetTangents(meshTangents); result.SetNormals(meshNormals); result.SetUVs(0, uvs); result.SetColors(colors); result.SetIndices(indices, MeshTopology.Lines, 0, true); result.RecalculateBounds(); result.UploadMeshData(false); }
//static float _ConstantSize; public static void GeneratePositionData(LineData data, List <Vector3> positions, List <int> indices, List <Vector4> colors) { if (data.Points.Count < 2) { return; } List <TubeRenderer.TubeVertex> tubeVertices = new List <TubeRenderer.TubeVertex>(data.Points.Count); for (int i = 0; i < data.Points.Count; i++) { TubeRenderer.AddPoint(data.Points[i].V3, data.rotations[i].Q, data.widths[i], data.colors[i].C, data.light[i], tubeVertices); } int additionalVertices = data.brushType == BrushType.Sphere ? 8 : 2; int actualVertexCount = data.Points.Count + additionalVertices; List <Vector3> meshVertices = new List <Vector3>(Enumerable.Repeat(default(Vector3), actualVertexCount)); List <Vector4> meshTangents = new List <Vector4>(Enumerable.Repeat(default(Vector4), actualVertexCount)); List <Vector3> meshNormals = new List <Vector3>(Enumerable.Repeat(default(Vector3), actualVertexCount)); List <Vector2> meshUvs = new List <Vector2>(Enumerable.Repeat(default(Vector2), actualVertexCount)); List <Color> meshColors = new List <Color>(Enumerable.Repeat(default(Color), actualVertexCount)); int[] meshIndices = null; TubeRenderer.GenerateMesh(tubeVertices, 0, data.isFlat, data.isWeb, data.brushType, new Vector2(1, data.isFlat ? 0.3f : 1) * 0.5f, meshVertices, meshTangents, meshNormals, meshUvs, meshColors, ref meshIndices, ref _LineLength); _BrushType = (int)data.brushType; _TaperAmountShape = data.taperShape ? 1 : 0; _TaperAmountOpacity = data.taperOpacity ? 1 : 0; _LineCount = data.multiLine ? 12f : 1.0f; List <g2f> tristream = new List <g2f>(meshIndices.Length * 3); for (int l = 0; l < 1; l++) { for (int i = 0; i < meshIndices.Length / 2; i++) { int i1 = meshIndices[i * 2 + 0]; int i2 = meshIndices[i * 2 + 1]; v2g v1 = vert(new Vertex() { vertex = meshVertices[i1], color = meshColors[i1], normal = meshNormals[i1], tangent = meshTangents[i1], uv = meshUvs[i1] }); v2g v2 = vert(new Vertex() { vertex = meshVertices[i2], color = meshColors[i2], normal = meshNormals[i2], tangent = meshTangents[i2], uv = meshUvs[i2] }); v2g[] op = new v2g[2] { v1, v2 }; v2g[] IN = new v2g[2] { v1, domain(op, new Vector2(0, l / _LineCount), _LineCount) }; for (int u = 1; u <= 2; u++) { IN[0] = IN[1]; IN[1] = domain(op, new Vector2(((float)u) / 2, l / _LineCount), _LineCount); geom(IN, tristream); } } if (tristream.Count < 3) { continue; } int strip0 = positions.Count; int strip1 = positions.Count + 1; int strip2 = positions.Count + 2; var pos = tristream[0].objPos; data.transform.ApplyTo(ref pos); positions.Add(pos); colors.Add(tristream[0].color); pos = tristream[1].objPos; data.transform.ApplyTo(ref pos); positions.Add(pos); colors.Add(tristream[1].color); bool flip = _BrushType != 0; for (int i = 2; i < tristream.Count; i++) { if (flip) { indices.Add(strip2); indices.Add(strip1); indices.Add(strip0); } else { indices.Add(strip0); indices.Add(strip1); indices.Add(strip2); } flip = !flip; strip0 = strip1; strip1 = strip2; strip2++; pos = tristream[i].objPos; data.transform.ApplyTo(ref pos); positions.Add(pos); colors.Add(tristream[i].color); } } }