public void Compute() { List <CurveSample> path = GetPath(); int vertsInShape = shapeVertices.Count; int segmentCount = path.Count - 1; var triangleIndices = new List <int>(vertsInShape * 2 * segmentCount * 3); var bentVertices = new List <MeshVertex>(vertsInShape * 2 * segmentCount * 3); foreach (var sample in path) { for (int i = 0; i < shapeVertices.Count; i++) { Vertex vertex = shapeVertices[i]; Vector3 vert = new Vector3(0, vertex.point.y, -vertex.point.x); Vector3 normal = new Vector3(0, vertex.normal.y, -vertex.normal.x); float u = (1f / shapeVertices.Count) * i; float v = textureScale * (sample.distanceInCurve + textureOffset); Vector3 uv = new Vector2(u, v); bentVertices.Add(sample.GetBent(new MeshVertex(vert, normal, uv))); } } var index = 0; for (int i = 0; i < segmentCount; i++) { for (int j = 0; j < shapeVertices.Count; j++) { int offset = j == shapeVertices.Count - 1 ? -(shapeVertices.Count - 1) : 1; int a = index + shapeVertices.Count; int b = index; int c = index + offset; int d = index + offset + shapeVertices.Count; triangleIndices.Add(c); triangleIndices.Add(b); triangleIndices.Add(a); triangleIndices.Add(a); triangleIndices.Add(d); triangleIndices.Add(c); index++; } } MeshUtility.Update(mf.sharedMesh, mf.sharedMesh, triangleIndices, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal), bentVertices.Select(b => b.uv)); var mc = GetComponent <MeshCollider>(); if (mc != null) { mc.sharedMesh = mf.sharedMesh; } }
public void Compute() { List <CurveSample> path = GetPath(); int vertsInShape = shapeVertices.Count; int segmentCount = path.Count - 1; var triangleIndices = new List <int>(vertsInShape * 2 * segmentCount * 3); var bentVertices = new List <MeshVertex>(vertsInShape * 2 * segmentCount * 3); foreach (var sample in path) { foreach (Vertex v in shapeVertices) { bentVertices.Add(sample.GetBent(new MeshVertex( new Vector3(0, v.point.y, -v.point.x), new Vector3(0, v.normal.y, -v.normal.x), new Vector2(v.uCoord, textureScale * (sample.distanceInCurve + textureOffset))))); } } var index = 0; for (int i = 0; i < segmentCount; i++) { for (int j = 0; j < shapeVertices.Count; j++) { int offset = j == shapeVertices.Count - 1 ? -(shapeVertices.Count - 1) : 1; int a = index + shapeVertices.Count; int b = index; int c = index + offset; int d = index + offset + shapeVertices.Count; triangleIndices.Add(c); triangleIndices.Add(b); triangleIndices.Add(a); triangleIndices.Add(a); triangleIndices.Add(d); triangleIndices.Add(c); index++; } } MeshUtility.Update(mf.sharedMesh, mf.sharedMesh, triangleIndices, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal), bentVertices.Select(b => b.uv)); //var mc = GetComponent<MeshCollider>(); //if (mc != null) //{ // mc.sharedMesh = mf.sharedMesh; //} }
private void FillOnce() { sampleCache.Clear(); var bentVertices = new List <MeshVertex>(source.Vertices.Count); // for each mesh vertex, we found its projection on the curve foreach (var vert in source.Vertices) { float distance = vert.position.x - source.MinX; CurveSample sample; if (!sampleCache.TryGetValue(distance, out sample)) { if (!useSpline) { if (distance > curve.Length) { distance = curve.Length; } sample = curve.GetSampleAtDistance(distance); } else { float distOnSpline = intervalStart + distance; if (distOnSpline > spline.Length) { if (spline.IsLoop) { while (distOnSpline > spline.Length) { distOnSpline -= spline.Length; } } else { distOnSpline = spline.Length; } } sample = spline.GetSampleAtDistance(distOnSpline); } sampleCache[distance] = sample; } bentVertices.Add(sample.GetBent(vert)); } MeshUtility.Update(result, source.Mesh, source.Triangles, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal)); }
private void FillStretch() { var bentVertices = new List <MeshVertex>(source.Vertices.Count); sampleCache.Clear(); // for each mesh vertex, we found its projection on the curve foreach (var vert in source.Vertices) { float distanceRate = source.Length == 0 ? 0 : Math.Abs(vert.position.x - source.MinX) / source.Length; CurveSample sample; if (!sampleCache.TryGetValue(distanceRate, out sample)) { if (!useSpline) { sample = curve.GetSampleAtDistance(curve.Length * distanceRate); } else { float intervalLength = intervalEnd == 0 ? spline.Length - intervalStart : intervalEnd - intervalStart; float distOnSpline = intervalStart + intervalLength * distanceRate; if (distOnSpline > spline.Length) { distOnSpline = spline.Length; Debug.Log("dist " + distOnSpline + " spline length " + spline.Length + " start " + intervalStart); } sample = spline.GetSampleAtDistance(distOnSpline); } sampleCache[distanceRate] = sample; } bentVertices.Add(sample.GetBent(vert)); } MeshUtility.Update(result, source.Mesh, source.Triangles, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal)); if (TryGetComponent(out MeshCollider collider)) { collider.sharedMesh = result; } }
public void FillCustomIntervals() { if (spline) { customIntervalStart = customIntervalStart > 0 ? customIntervalStart : 0; customIntervalEnd = customIntervalEnd < spline.Length ? customIntervalEnd : spline.Length; var bentVertices = new List <MeshVertex>(source.Vertices.Count); sampleCache.Clear(); // for each mesh vertex, we found its projection on the curve foreach (var vert in source.Vertices) { float distanceRate = source.Length == 0 ? 0 : Math.Abs(vert.position.x - source.MinX) / source.Length; CurveSample sample; if (!sampleCache.TryGetValue(distanceRate, out sample)) { float intervalLength = customIntervalEnd - customIntervalStart; float distOnSpline = customIntervalStart + intervalLength * distanceRate; if (distOnSpline > spline.Length) { distOnSpline = spline.Length; Debug.Log("dist " + distOnSpline + " spline length " + spline.Length + " start " + intervalStart); } sample = spline.GetSampleAtDistance(distOnSpline); sampleCache[distanceRate] = sample; } bentVertices.Add(sample.GetBent(vert)); } MeshUtility.Update(result, source.Mesh, source.Triangles, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal)); } }
private void FillRepeat() { float intervalLength = useSpline? (intervalEnd == 0 ? spline.Length : intervalEnd) - intervalStart : curve.Length; int repetitionCount = Mathf.FloorToInt(intervalLength / source.Length); // building triangles and UVs for the repeated mesh var triangles = new List <int>(); var uv = new List <Vector2>(); var uv2 = new List <Vector2>(); var uv3 = new List <Vector2>(); var uv4 = new List <Vector2>(); var uv5 = new List <Vector2>(); var uv6 = new List <Vector2>(); var uv7 = new List <Vector2>(); var uv8 = new List <Vector2>(); for (int i = 0; i < repetitionCount; i++) { foreach (var index in source.Triangles) { triangles.Add(index + source.Vertices.Count * i); } uv.AddRange(source.Mesh.uv); uv2.AddRange(source.Mesh.uv2); uv3.AddRange(source.Mesh.uv3); uv4.AddRange(source.Mesh.uv4); #if UNITY_2018_2_OR_NEWER uv5.AddRange(source.Mesh.uv5); uv6.AddRange(source.Mesh.uv6); uv7.AddRange(source.Mesh.uv7); uv8.AddRange(source.Mesh.uv8); #endif } // computing vertices and normals var bentVertices = new List <MeshVertex>(source.Vertices.Count); float offset = 0; for (int i = 0; i < repetitionCount; i++) { sampleCache.Clear(); // for each mesh vertex, we found its projection on the curve foreach (var vert in source.Vertices) { float distance = vert.position.x - source.MinX + offset; CurveSample sample; if (!sampleCache.TryGetValue(distance, out sample)) { if (!useSpline) { if (distance > curve.Length) { continue; } sample = curve.GetSampleAtDistance(distance); } else { float distOnSpline = intervalStart + distance; if (true) //spline.isLoop) { { while (distOnSpline > spline.Length) { distOnSpline -= spline.Length; } } else if (distOnSpline > spline.Length) { continue; } sample = spline.GetSampleAtDistance(distOnSpline); } sampleCache[distance] = sample; } bentVertices.Add(sample.GetBent(vert)); } offset += source.Length; } MeshUtility.Update(result, source.Mesh, triangles, bentVertices.Select(b => b.position), bentVertices.Select(b => b.normal), uv, uv2, uv3, uv4, uv5, uv6, uv7, uv8); }