internal static Mesh GetMesh(float outerRadius, float innerRadius, int segments, int removedQuads = 0) { segments = Mathf.Max(segments, 3); removedQuads = Mathf.Max(removedQuads, 0); outerRadius = Mathf.Max(0, outerRadius); innerRadius = Mathf.Max(0, innerRadius); if (innerRadius > outerRadius) { float newInnerRadius = outerRadius; outerRadius = innerRadius; innerRadius = newInnerRadius; } if (removedQuads >= segments) { return(new Mesh()); } List <Vector3> vertices = new List <Vector3>(ProceduralHead.GetCircleVertices(segments)); vertices.AddRange(vertices.ToArray()); int[] triangles = new int[segments * 6]; int outerOffset = segments - 1; for (int i = 0; i < segments; i++) { vertices[i] *= innerRadius; vertices[i + segments] *= outerRadius; if (i < segments - removedQuads) { int triangleStart = i * 6; int outerStart = i + outerOffset; triangles[triangleStart++] = i == outerOffset ? 0 : i; triangles[triangleStart++] = i == outerOffset ? i : outerStart + 1; triangles[triangleStart++] = i == outerOffset ? vertices.Count - 1 : outerStart + 2; triangles[triangleStart++] = i == outerOffset ? segments : i; triangles[triangleStart++] = i == outerOffset ? 0 : outerStart + 2; triangles[triangleStart++] = i == outerOffset ? vertices.Count - 1 : i + 1; } } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); return(mesh); }
internal static Mesh GetMesh(float radius, int segments, int removedTriagles = 0) { segments = Mathf.Max(segments, 3); removedTriagles = Mathf.Max(removedTriagles, 0); if (removedTriagles >= segments) { return(new Mesh()); } List <Vector3> vertices = new List <Vector3>(ProceduralHead.GetCircleVertices(segments)); vertices.Add(Vector3.zero); int[] triangles = new int[(vertices.Count - 1) * 3]; for (int i = 0; i < vertices.Count; i++) { vertices[i] *= radius; if (i < vertices.Count - (1 + removedTriagles)) { int triangleStart = i * 3; triangles[triangleStart++] = i; triangles[triangleStart++] = i == vertices.Count - 2 ? 0 : i + 1; triangles[triangleStart++] = vertices.Count - 1; } } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); return(mesh); }
internal static Mesh GetMesh(float footRadiusOuter, float headRadiusOuter, float footRadiusInner, float headRadiusInner, float innerHeadHeight, float outerHeadHeight, float innerFootHeight, float outerFootHeight, int segments) { segments = Mathf.Max(segments, 3); headRadiusOuter = Mathf.Max(0, headRadiusOuter); footRadiusOuter = Mathf.Max(0, footRadiusOuter); innerHeadHeight = Mathf.Abs(innerHeadHeight); List <Vector3> newVerts = new List <Vector3>(ProceduralHead.GetCircleVertices(segments)); newVerts.AddRange(newVerts); // Shaft ends List <Vector3> vertices = new List <Vector3>(newVerts.ToArray()); // Outer cap ends vertices.AddRange(newVerts.ToArray()); // Inner cap ends vertices.AddRange(newVerts.ToArray()); // Inner shaft ends vertices.AddRange(newVerts.ToArray()); int[] triangles = new int[segments * 30]; Vector3 innerHeadPosition = -Vector3.up * innerHeadHeight; Vector3 outerHeadPosition = -Vector3.up * outerHeadHeight; Vector3 innerFootPosition = Vector3.up * innerFootHeight; Vector3 outerFootPosition = Vector3.up * outerFootHeight; Mesh mesh = new Mesh(); int headOffset = segments - 1; int footCapStart = segments * 2; int outerCapEnd = segments * 3; int innerCapStart = segments * 4; int innerCapEnd = segments * 5; int innerShaftStart = segments * 6; for (int i = 0; i < segments; i++) { bool finalQuad = i == segments - 1; int outerShaftHead = i; int outerShaftFoot = i + segments; int outerCapHead = i + footCapStart; int innerCapHead = outerCapHead + segments; int outerCapFoot = i + innerCapStart; int innerCapFoot = outerCapFoot + segments; int innerShaftHead = i + innerShaftStart; int innerShaftFoot = innerShaftHead + segments; // Shaft ends vertices[outerShaftHead] *= headRadiusOuter; vertices[outerShaftHead] += outerHeadPosition; vertices[outerShaftFoot] *= footRadiusOuter; vertices[outerShaftFoot] += outerFootPosition; vertices[innerCapHead] *= headRadiusOuter; vertices[innerCapHead] += outerHeadPosition; vertices[outerCapHead] *= headRadiusInner; vertices[outerCapHead] += innerHeadPosition; vertices[outerCapFoot] *= footRadiusOuter; vertices[outerCapFoot] += outerFootPosition; vertices[innerCapFoot] *= footRadiusInner; vertices[innerCapFoot] += innerFootPosition; // Inner shaft ends vertices[innerShaftHead] *= footRadiusInner; vertices[innerShaftHead] += innerFootPosition; vertices[innerShaftFoot] *= headRadiusInner; vertices[innerShaftFoot] += innerHeadPosition; int triangle = i * 30; int vert1 = outerShaftHead; int vert2 = outerShaftFoot; int vert3 = finalQuad ? segments : outerShaftFoot + 1; int vert4 = finalQuad ? 0 : outerShaftHead + 1; ProceduralHead.CreateQuad(ref triangles, ref triangle, vert1, vert2, vert3, vert4); vert1 = outerCapHead; vert2 = innerCapHead; vert3 = finalQuad ? footCapStart + segments: innerCapHead + 1; vert4 = finalQuad ? footCapStart : outerCapHead + 1; ProceduralHead.CreateQuad(ref triangles, ref triangle, vert1, vert2, vert3, vert4); vert1 = outerCapFoot; vert2 = innerCapFoot; vert3 = finalQuad ? innerCapStart + segments : innerCapFoot + 1; vert4 = finalQuad ? innerCapStart : outerCapFoot + 1; ProceduralHead.CreateQuad(ref triangles, ref triangle, vert1, vert2, vert3, vert4); vert1 = innerShaftHead; vert2 = innerShaftFoot; vert3 = finalQuad ? innerShaftStart + segments : innerShaftFoot + 1; vert4 = finalQuad ? innerShaftStart : innerShaftHead + 1; ProceduralHead.CreateQuad(ref triangles, ref triangle, vert1, vert2, vert3, vert4); } mesh.vertices = vertices.ToArray(); mesh.RecalculateBounds(); mesh.triangles = triangles; mesh.RecalculateTangents(); mesh.RecalculateNormals(); return(mesh); }
internal static Mesh GetMesh(float headRadius, float footRadius, float extent, int segments) { segments = Mathf.Max(segments, 3); footRadius = Mathf.Max(0, footRadius); headRadius = Mathf.Max(0, headRadius); extent = Mathf.Abs(extent); List <Vector3> newVerts = new List <Vector3>(ProceduralHead.GetCircleVertices(segments)); List <Vector3> vertices = new List <Vector3>(newVerts.ToArray()); vertices.AddRange(newVerts.ToArray()); vertices.AddRange(newVerts.ToArray()); vertices.AddRange(newVerts.ToArray()); int[] triangles = new int[segments * 12]; Vector3 headPosition = Vector3.up * extent; Vector3 footPosition = -Vector3.up * extent; Mesh mesh = new Mesh(); int capsStart = segments * 2; int headOffset = segments - 1; for (int i = 0; i < segments; i++) { vertices[i] *= headRadius; vertices[i] += headPosition; vertices[i + segments] *= footRadius; vertices[i + segments] += footPosition; vertices[i + capsStart] *= headRadius; vertices[i + capsStart] += headPosition; vertices[i + capsStart + segments] *= footRadius; vertices[i + capsStart + segments] += footPosition; int shaftTriangle = i * 6; int footVert = i; int headStart = footVert + headOffset; triangles[shaftTriangle++] = footVert == headOffset ? 0 : footVert; triangles[shaftTriangle++] = footVert == headOffset ? footVert : headStart + 1; triangles[shaftTriangle++] = footVert == headOffset ? capsStart - 1 : headStart + 2; triangles[shaftTriangle++] = footVert == headOffset ? segments : footVert; triangles[shaftTriangle++] = footVert == headOffset ? 0 : headStart + 2; triangles[shaftTriangle++] = footVert == headOffset ? capsStart - 1 : footVert + 1; int capTriangle = (i * 6) + (segments * 6); int capFootVert = i + capsStart; int capHeadVert = capFootVert + headOffset; triangles[capTriangle++] = capHeadVert; triangles[capTriangle++] = capsStart + segments; triangles[capTriangle++] = capFootVert != vertices.Count - 1 ? capHeadVert + 1 : capHeadVert; if (i < segments - 1) { triangles[capTriangle++] = capsStart; triangles[capTriangle++] = capFootVert; triangles[capTriangle++] = capFootVert != capHeadVert - 1 ? capFootVert + 1 : capsStart; } } mesh.vertices = vertices.ToArray(); mesh.RecalculateBounds(); mesh.triangles = triangles; mesh.RecalculateTangents(); mesh.RecalculateNormals(); return(mesh); }