protected override void BuildMeshComponents() { if (Vertices == null) { Vertices = new Vector3[3]; } if (Triangles == null) { Triangles = new int[3]; Triangles[0] = 0; Triangles[1] = 2; Triangles[2] = 1; } Vertices[0] = p1; int sign = MeshHelper.GetSide(p2, p1, p3); if (sign == -1) { Vertices[1] = p2; Vertices[2] = p3; } else { Vertices[1] = p3; Vertices[2] = p2; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vector2 center = MeshHelper.GetCenter(SplinePoints); for (int i = 0; i < SplinePoints.Length; i++) { SplinePoints[i] -= center; } var points = CatmullRomSpline.GetPoints(MeshHelper.ConvertVec2ToVec3(SplinePoints), Resolution); if (MinArea.HasValue) { points = SplineSimplification.Simplify(points, MinArea.Value, true, false); } Vertices = points.ToArray(); var connections = Triangulation.TriangulationToInt3(new List <Vector2>(MeshHelper.ConvertVec3ToVec2(Vertices))); Triangles = new int[connections.Count * 3]; for (int i = 0; i < connections.Count; i++) { Triangles[i * 3 + 0] = connections[i].A; Triangles[i * 3 + 1] = connections[i].B; Triangles[i * 3 + 2] = connections[i].C; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { var points = CatmullRomSpline.GetPoints(MeshHelper.ConvertVec2ToVec3(SplinePoints), Resolution); if (MinArea.HasValue) { points = SplineSimplification.Simplify(points, MinArea.Value, true, false); } Vertices = points.ToArray(); CenterShift = MeshHelper.GetCenter(Vertices); for (int i = 0; i < Vertices.Length; i++) { Vertices[i] -= CenterShift; } Triangles = new int[Vertices.Length * 3]; for (int i = 0; i < Vertices.Length; i++) { Triangles[i * 3 + 0] = 0; Triangles[i * 3 + 1] = i; Triangles[i * 3 + 2] = (i + 1) % Vertices.Length; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vertices = new Vector3[4]; Vector2 center = MeshHelper.GetCenter(Verts); for (int i = 0; i < 4; i++) { Vertices[i] = Verts[i] - center; } double[] angles = new double[4]; double sum = 0; for (int i = 0; i < 4; i++) { angles[i] = MeshHelper.AngleBetweenPoints(Verts[i], Verts[(i + 1) % 4], Verts[(i + 2) % 4]); sum += angles[i]; } if (System.Math.Abs(360 - sum) < 1e-3) //check for clockwise order { Triangles = new int [] { 0, 1, 3, 2, 3, 1 }; } else { int index = GetMaxIndex(angles); int a = (index + 1) % 4; int b = (index + 2) % 4; int c = (index + 3) % 4; Triangles = new int[] { a, index, c, b, a, c }; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vertices = new Vector3[4]; Vector2 center = (verts[0] + verts[1] + verts[2] + verts[3]) * 0.25f; for (int i = 0; i < 4; i++) { Vertices[i] = verts[i] - center; } if (MeshHelper.IsPointInTriangle(verts[0], verts[1], verts[2], verts[3])) { Triangles = new int[] { 0, 2, 3, 0, 1, 2 }; } else if (!MeshHelper.IsPointInTriangle(verts[3], verts[0], verts[1], verts[2])) { if (MeshHelper.IsPointInTriangle(verts[2], verts[0], verts[1], verts[3])) { Triangles = new int[] { 0, 2, 3, 1, 0, 2 }; } else { Triangles = new int[] { 0, 1, 3, 1, 2, 3 }; } } else { Triangles = new int[] { 0, 1, 3, 2, 3, 1 }; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { baseVertices = vertices; Vertices = QuickHull(new List <Vector3>(vertices)).ToArray(); Vector3 center = new Vector3(); for (int i = 0; i < vertices.Length; i++) { center += vertices[i]; } center /= vertices.Length; for (int i = 0; i < Vertices.Length; i++) { Vertices[i] -= center; } Triangles = new int[Vertices.Length * 3]; for (int i = 1; i < Vertices.Length - 1; i++) { Triangles[i * 3 + 0] = 0; Triangles[i * 3 + 1] = i; Triangles[i * 3 + 2] = i + 1; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { BaseVertices = ConvexVertices; Vertices = MeshHelper.ConvertVec2ToVec3(ConvexHull.QuickHull(MeshHelper.ConvertVec3ToVec2(ConvexVertices)).ToArray()); // oh no Triangles = new int[Vertices.Length * 3]; for (int i = 1; i < Vertices.Length - 1; i++) { Triangles[i * 3 + 0] = 0; Triangles[i * 3 + 1] = i; Triangles[i * 3 + 2] = i + 1; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vertices = new Vector3[sides + 1]; Triangles = new int[3 * sides]; float angleDelta = deg360 / sides; Vertices[0] = shift; for (int i = 1; i < sides + 1; i++) { Vector3 vertPos = new Vector3(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * radius; Vertices[i] = vertPos; Triangles[(i - 1) * 3 + 0] = (1 + i % sides); Triangles[(i - 1) * 3 + 1] = 1 + (i - 1) % sides; Triangles[(i - 1) * 3 + 2] = 0; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vector2 center = MeshHelper.GetCenter(Vertices); Vertices = new Vector3[Points.Length]; for (int i = 0; i < Vertices.Length; i++) { Vertices[i] = Points[i] - center; } Triangles = new int[Connections.Count * 3]; for (int i = 0; i < Connections.Count; i++) { Triangles[i * 3 + 0] = Connections[i].A; Triangles[i * 3 + 1] = Connections[i].B; Triangles[i * 3 + 2] = Connections[i].C; } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { bool isCircle = innerRadius == 0; Vertices = new Vector3[isCircle ? sides + 1 : 2 * sides]; Triangles = new int[isCircle ? 3 * sides : 6 * sides]; if (isCircle) { //build ordinary circle Vertices[0] = Vector3.zero; float angleDelta = deg360 / sides; for (int i = 1; i < sides + 1; i++) { Vector3 vertPos = new Vector3(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * outerRadius; Vertices[i] = vertPos; Triangles[(i - 1) * 3 + 0] = 0; Triangles[(i - 1) * 3 + 1] = 1 + (i - 1) % sides; Triangles[(i - 1) * 3 + 2] = 1 + i % sides; } } else { //build a ring! float angleDelta = deg360 / sides; int triangleIndex = 0; for (int i = 0; i < sides; i++) { Vector3 vertPosInner = new Vector3(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * innerRadius; Vector3 vertPosOuter = new Vector3(Mathf.Cos(i * angleDelta), Mathf.Sin(i * angleDelta)) * outerRadius; Vertices[i] = vertPosInner; Vertices[i + sides] = vertPosOuter; Triangles[triangleIndex++] = i; Triangles[triangleIndex++] = (i + 1) % (sides); Triangles[triangleIndex++] = (i + sides); Triangles[triangleIndex++] = (i + 1) % (sides * 2); Triangles[triangleIndex++] = (i + sides); Triangles[triangleIndex++] = (i + sides + 1) % (sides * 2); } } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { Vertices = new Vector3[1 + sides * 2]; Triangles = new int[2 * sides * 3]; UVs = new Vector2[1 + sides * 2]; Vertices[0] = new Vector3(0, 0); float angleDelta = 360 / (float)sides / 2 * Mathf.Deg2Rad; float angleShift = -360f / (sides * 4) * Mathf.Deg2Rad; for (int i = 0; i < sides * 2; i++) { Vector3 vertVec = new Vector3(Mathf.Cos(i * angleDelta + angleShift), Mathf.Sin(i * angleDelta + angleShift)); Vertices[1 + i] = vertVec * (i % 2 == 0 ? radiusA : radiusB); Triangles[(i * 3 + 2) % Triangles.Length] = 0; Triangles[(i * 3 + 1) % Triangles.Length] = 1 + i % (sides * 2); Triangles[i * 3] = 1 + (i + 1) % (sides * 2); } UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft = new List <Vector2>(); cachedVertsRight = new List <Vector2>(); } #endregion List <Vector3> verticesList = new List <Vector3>(); List <int> trianglesList = new List <int>(); Vector2 center = new Vector2(); for (int i = 0; i < lineVerts.Length; i++) { center += lineVerts[i]; } center /= lineVerts.Length; for (int i = 0; i < lineVerts.Length; i++) { lineVerts[i] -= center; } int currentVertIndex = 0; int currentTriIndex = 0; //add first two vertices float angle = Mathf.Atan2(lineVerts[1].y - lineVerts[0].y, lineVerts[1].x - lineVerts[0].x); float oldAngle, angleDiff; Vector2 p1 = new Vector2(Mathf.Cos(angle + deg90), Mathf.Sin(angle + deg90)) * lineWidth; Vector2 p2 = new Vector2(Mathf.Cos(angle - deg90), Mathf.Sin(angle - deg90)) * lineWidth; if (p1 != p2) { verticesList.Add(lineVerts[currentVertIndex] + p1); verticesList.Add(lineVerts[currentVertIndex] + p2); #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft.Add(verticesList[verticesList.Count - 2]); cachedVertsRight.Add(verticesList[verticesList.Count - 1]); } #endregion } else { verticesList.Add(lineVerts[currentVertIndex]); #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft.Add(verticesList[verticesList.Count - 1]); cachedVertsRight.Add(verticesList[verticesList.Count - 1]); } #endregion } oldAngle = angle; currentVertIndex++; // add middle vertices for (int i = 0; i < lineVerts.Length - 2; i++, currentVertIndex++) { angle = Mathf.Atan2(lineVerts[currentVertIndex + 1].y - lineVerts[currentVertIndex].y, lineVerts[currentVertIndex + 1].x - lineVerts[currentVertIndex].x); angleDiff = oldAngle + MeshHelper.AngleDifference(oldAngle, angle) * 0.5f; p1 = new Vector2(Mathf.Cos(angleDiff + deg90), Mathf.Sin(angleDiff + deg90)) * lineWidth; p2 = new Vector2(Mathf.Cos(angleDiff - deg90), Mathf.Sin(angleDiff - deg90)) * lineWidth; if (p1 != p2) { verticesList.Add(lineVerts[currentVertIndex] + p1); verticesList.Add(lineVerts[currentVertIndex] + p2); trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 1); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 2); currentTriIndex += 2; } else { verticesList.Add(lineVerts[currentTriIndex] + p1); if (verticesList[verticesList.Count - 1] != verticesList[verticesList.Count - 2]) { trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 1); currentTriIndex++; } } #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft.Add(verticesList[verticesList.Count - 2]); cachedVertsRight.Add(verticesList[verticesList.Count - 1]); } #endregion oldAngle = angle; } //add last two vertices if (lineVerts[0] != lineVerts[currentVertIndex]) { angle = Mathf.Atan2(lineVerts[currentVertIndex].y - lineVerts[currentVertIndex - 1].y, lineVerts[currentVertIndex].x - lineVerts[currentVertIndex - 1].x); p1 = new Vector2(Mathf.Cos(angle + deg90), Mathf.Sin(angle + deg90)) * lineWidth; p2 = new Vector2(Mathf.Cos(angle - deg90), Mathf.Sin(angle - deg90)) * lineWidth; if (p1 != p2) { verticesList.Add(lineVerts[currentVertIndex] + p1); verticesList.Add(lineVerts[currentVertIndex] + p2); trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 1); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 2); } else { //make LineMesh loop if (verticesList[verticesList.Count - 1] != verticesList[verticesList.Count - 2]) { verticesList.Add(lineVerts[currentTriIndex] + p1); trianglesList.Add(currentTriIndex + 0); trianglesList.Add(currentTriIndex + 3); trianglesList.Add(currentTriIndex + 1); } } #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft.Add(verticesList[verticesList.Count - 2]); cachedVertsRight.Add(verticesList[verticesList.Count - 1]); } #endregion } else { oldAngle = Mathf.Atan2( lineVerts[0].y - lineVerts[currentVertIndex].y, lineVerts[0].x - lineVerts[currentVertIndex].x); angle = Mathf.Atan2( lineVerts[1].y - lineVerts[0].y, lineVerts[1].x - lineVerts[0].x); angleDiff = oldAngle + MeshHelper.AngleDifference(oldAngle, angle) * 0.5f - deg90; p1 = new Vector2(Mathf.Cos(angleDiff - deg90), Mathf.Sin(angleDiff - deg90)) * lineWidth; p2 = new Vector2(Mathf.Cos(angleDiff + deg90), Mathf.Sin(angleDiff + deg90)) * lineWidth; verticesList[0] = lineVerts[currentVertIndex] + p1; verticesList[1] = lineVerts[currentVertIndex] + p2; #region DoubleCollider if (useDoubleCollider) { cachedVertsLeft[0] = verticesList[0]; cachedVertsRight[0] = verticesList[1]; cachedVertsLeft.Add(verticesList[verticesList.Count - 2]); cachedVertsRight.Add(verticesList[verticesList.Count - 1]); } #endregion trianglesList.Add(0); trianglesList.Add(verticesList.Count - 1); trianglesList.Add(1); trianglesList.Add(verticesList.Count - 1); trianglesList.Add(0); trianglesList.Add(verticesList.Count - 2); } Vertices = verticesList.ToArray(); Triangles = trianglesList.ToArray(); UVs = MeshHelper.UVUnwrap(Vertices); }
protected override void BuildMeshComponents() { int doubleSides = 2 * Sides; Vertices = new Vector3[6 * Sides]; Triangles = new int[6 * 3 * Sides]; float angleDelta = deg360 / doubleSides; float angleShift = angleDelta * 0.5f; float outerAngleShift = angleDelta * 0.2f; int triangleIndex = 0; for (int i = 0; i < doubleSides; i++) { Vector3 innerVertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift), Mathf.Sin(i * angleDelta + angleShift)) * InnerRadius; Vector3 rootVertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift), Mathf.Sin(i * angleDelta + angleShift)) * RootRadius; Vector3 outerVertPos; if (i % 2 == 0) { outerVertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift + outerAngleShift), Mathf.Sin(i * angleDelta + angleShift + outerAngleShift)) * OuterRadius; } else { outerVertPos = new Vector3(Mathf.Cos(i * angleDelta + angleShift - outerAngleShift), Mathf.Sin(i * angleDelta + angleShift - outerAngleShift)) * OuterRadius; } Vertices[i * 3 + 0] = innerVertPos; Vertices[i * 3 + 1] = rootVertPos; Vertices[i * 3 + 2] = outerVertPos; int a = 3 * i; int b = 3 * i + 1; int c = (3 * (i + 1)) % (3 * doubleSides); int d = (3 * (i + 1) + 1) % (3 * doubleSides); Triangles[triangleIndex++] = d; Triangles[triangleIndex++] = b; Triangles[triangleIndex++] = c; Triangles[triangleIndex++] = b; Triangles[triangleIndex++] = a; Triangles[triangleIndex++] = c; //add tooth if (i % 2 == 0) { a = 3 * i + 1; b = 3 * i + 2; c = (3 * (i + 1) + 1) % (3 * doubleSides); d = (3 * (i + 1) + 2) % (3 * doubleSides); Triangles[triangleIndex++] = d; Triangles[triangleIndex++] = b; Triangles[triangleIndex++] = c; Triangles[triangleIndex++] = b; Triangles[triangleIndex++] = a; Triangles[triangleIndex++] = c; } } UVs = MeshHelper.UVUnwrap(Vertices); }