private void CreateMesh() { double height = 0.5; double radius = 0.5; Segments = SMath.Clamp(Segments, 3, 256); VertexList = new VertexNormalColor[Segments * (CreateCaps ? 2 : 1) + 1]; IndexList = new uint[Segments * 3 + (CreateCaps ? (Segments - 2) * 3 : 0)]; // Create vertices VertexList[0].Position = new Vector3(0, 0, height); for (int i = 0; i < Segments; i++) { double angle = ((double)i / (double)Segments) * Math.PI * 2.0; Vector2 planarCoordinate = Vector2.FromAngle(angle, radius); VertexList[i + 1].Position = new Vector3(planarCoordinate.X, planarCoordinate.Y, -height); if (CreateCaps) { // Top (for cap) VertexList[i + Segments + 1].Position = new Vector3(planarCoordinate.X, planarCoordinate.Y, -height); } } // Create normals Vector3 capNormal = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[1].Position, VertexList[2].Position, VertexList[3].Position) : new Vector3(0, 0, 0); for (int i = 0; i < Segments; i++) { Vector3 manifoldNormal = GeometryMath.GetTriangleNormal(VertexList[0].Position, VertexList[(i + 1) % Segments + 1].Position, VertexList[i + 1].Position); VertexList[i + 1].Normal = manifoldNormal; if (CreateCaps) { VertexList[Segments + i + 1].Normal = capNormal; } } // Create triangles int index = 0; for (int i = 0; i < Segments; i++) { // Manifold IndexList[index++] = 0; IndexList[index++] = (uint)((i + 1) % Segments + 1); IndexList[index++] = (uint)(i + 1); if (CreateCaps && i < Segments - 2) { // Cap IndexList[index++] = (uint)(Segments + 1); IndexList[index++] = (uint)(Segments + i + 2); IndexList[index++] = (uint)(Segments + i + 3); } } }
public void AddTriangle(int time, Vector3 a, Vector3 b, Vector3 c, Color32 color) { LocalBounds.Encapsulate(a); LocalBounds.Encapsulate(b); LocalBounds.Encapsulate(c); Vector3 newTriangleNormal = GeometryMath.GetTriangleNormal(a, b, c); ArrayList <VertexNormalColor> vertexList = color.A < 255 ? transparentVertexList : opaqueVertexList; List <DebugRenderTriangleIndexMarker> triangleMarkers = color.A < 255 ? transparentTriangleMarkers : opaqueTriangleMarkers; vertexList.Add(new VertexNormalColor() { Position = a, Color = color, Normal = newTriangleNormal }); vertexList.Add(new VertexNormalColor() { Position = b, Color = color, Normal = newTriangleNormal }); vertexList.Add(new VertexNormalColor() { Position = c, Color = color, Normal = newTriangleNormal }); // Find or create new index marker and add triangle DebugRenderTriangleIndexMarker searchItem = new DebugRenderTriangleIndexMarker(0, time); int index = triangleMarkers.BinarySearch(searchItem); if (index < 0) { index = ~index; } while (index > 0 && index < triangleMarkers.Count && triangleMarkers[index].time > time) { // Not exact match but the next one -> Decrease index by one to have only the range that counts up for the requested time index--; } if (index < triangleMarkers.Count && triangleMarkers[index].time == time) { // Has existing one for this time -> Update end index triangleMarkers[index].index = vertexList.Count - 1; } else { // Insert new DebugRenderTriangleIndexMarker range = new DebugRenderTriangleIndexMarker(vertexList.Count - 1, time); triangleMarkers.Insert(index, range); } if (!InfiniteLength && EndTime < time) { EndTime = time; } }
private void CreateMesh() { double height = 0.5; double TopRadius = 0.5; double BottomRadius = TopRadius * BottomRadiusScale; Segments = SMath.Clamp(Segments, 3, 256); VertexList = new VertexNormalColor[Segments * (CreateCaps ? 4 : 2)]; IndexList = new uint[Segments * 6 + (CreateCaps ? (Segments - 1) * 6 : 0)]; // Create vertices for (int i = 0; i < Segments; i++) { double angle = ((double)i / (double)Segments) * Math.PI * 2.0; Vector2 planarCoordinate = Vector2.FromAngle(angle, 1.0); // Top VertexList[i + Segments * 0].Position = new Vector3(planarCoordinate.X * TopRadius, planarCoordinate.Y * TopRadius, height); // Bottom VertexList[i + Segments * 1].Position = new Vector3(planarCoordinate.X * BottomRadius, planarCoordinate.Y * BottomRadius, -height); if (CreateCaps) { // Top (for cap) VertexList[i + Segments * 2].Position = new Vector3(planarCoordinate.X * TopRadius, planarCoordinate.Y * TopRadius, height); // Bottom (for cap) VertexList[i + Segments * 3].Position = new Vector3(planarCoordinate.X * BottomRadius, planarCoordinate.Y * BottomRadius, -height); } } // Create normals Vector3 topNormal = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[Segments * 2].Position, VertexList[Segments * 2 + 2].Position, VertexList[Segments * 2 + 1].Position) : new Vector3(0, 0, 0); Vector3 bottomNormal = CreateCaps ? GeometryMath.GetTriangleNormal(VertexList[Segments * 3].Position, VertexList[Segments * 3 + 1].Position, VertexList[Segments * 3 + 2].Position) : new Vector3(0, 0, 0); for (int i = 0; i < Segments; i++) { Vector3 manifoldNormal = GeometryMath.GetTriangleNormal(VertexList[i].Position, VertexList[(i + 1) % Segments].Position, VertexList[i + Segments].Position); VertexList[Segments * 0 + i].Normal = manifoldNormal; VertexList[Segments * 1 + i].Normal = manifoldNormal; if (CreateCaps) { VertexList[Segments * 2 + i].Normal = topNormal; VertexList[Segments * 3 + i].Normal = bottomNormal; } } // Create triangles int index = 0; for (int i = 0; i < Segments; i++) { // Manifold IndexList[index++] = (uint)i; IndexList[index++] = (uint)((i + 1) % Segments); IndexList[index++] = (uint)(i + Segments); IndexList[index++] = (uint)((i + 1) % Segments); IndexList[index++] = (uint)((i + 1) % Segments + Segments); IndexList[index++] = (uint)(i + Segments); if (CreateCaps && i < Segments - 1) { // Top cap IndexList[index++] = (uint)(Segments * 2); IndexList[index++] = (uint)(Segments * 2 + i + 1); IndexList[index++] = (uint)(Segments * 2 + i); // Bottom cap IndexList[index++] = (uint)(Segments * 3); IndexList[index++] = (uint)(Segments * 3 + i); IndexList[index++] = (uint)(Segments * 3 + i + 1); } } }