public Geometry Output() { Geometry geo = new Geometry(); // Vertices and normals geo.Vertices = new Vector3 [] { new Vector3( Size.x / 2, 0, -Size.y / 2), // bottom right new Vector3(-Size.x / 2, 0, -Size.y / 2), // bottom left new Vector3(-Size.x / 2, 0, Size.y / 2), // top left new Vector3( Size.x / 2, 0, Size.y / 2) // top right }; Vector3 normal = new Vector3(0f, 1f, 0f); geo.Normals = new Vector3 [] {normal, normal, normal, normal}; Vector4 tangent = new Vector4(1f, 0f, 0f, -1f); geo.Tangents = new Vector4 [] {tangent, tangent, tangent, tangent}; geo.Polygons = new int[] {0, 4}; // UV geo.UV = new Vector2 [] { new Vector2(1f, 1f), new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(1f, 1f) }; // Surface switch (Surface) { case Surface.None: geo.Triangles = new int[0]; break; case Surface.Triangulate: geo.Triangles = new int [] { 0, 1, 2, 2, 3, 0 }; break; case Surface.Converge: var conv = new Converge(geo); conv.Point = Center; conv.RecalculateNormals = false; geo = conv.Output(); break; } geo.ApplyOrientation(Orientation); geo.Offset(Center); // Orientation return geo; }
public Geometry Output() { Geometry geo = new Geometry(3); // http://upload.wikimedia.org/wikipedia/commons/9/9a/Degree-Radian_Conversion.svg float radius = 2 * Height / 3; geo.Vertices = new Vector3[3]; for (int i = 0; i < 3; i++) { float degrees = 90 + 120 * i; float radians = Mathf.PI / 180 * degrees; float cos = Mathf.Cos(radians); float sin = Mathf.Sin(radians); geo.Vertices[2 - i] = new Vector3(cos * radius, 0f, sin * radius); } // Normals Vector3 normal = new Vector3(0f, 1f, 0f); geo.Normals = new Vector3 [] {normal, normal, normal}; // UV geo.UV = new Vector2 [] { new Vector2(1.0f, 0.0f), new Vector2(0.0f, 0.0f), new Vector2(0.5f, 1.0f) }; // Triangles if (Surface) { geo.Triangles = new int [] {0, 1, 2}; } else { geo.Triangles = new int[0]; } // Polygons geo.Polygons = new int[] {0, 3}; geo.ApplyOrientation(Orientation); geo.Offset(Center); // Orientation return geo; }
public Geometry Output() { if (Segments < 3) { Debug.LogError("Sphere error: Spheres must have at least 3 segments"); } Geometry geo = new Geometry( Segments*(Segments+1), (Segments*2) * (Segments-1) * 3 -Segments*6, // (Segments*2) = # triangles in a ring, (Segments-1) = # of rings in sphere Segments*2 ); float tau = Mathf.PI * 2; int triIndex = 0; // Longitudes (meridians) for (int i = 0; i < Segments; i++) { float lonAng = Mathf.PI/2 + (Mathf.PI * i / (Segments - 1)); float lonSin = Mathf.Sin(lonAng) * Radius; float lonCos = Mathf.Cos(lonAng) * Radius; // Latitudes (parallels) int offset = i * (Segments + 1); // We iterate one additional time to create an overlapping longitude // at the UV seam for (int l = 0; l < Segments + 1; l++) { float latAng = tau * l / Segments; float latCos = Mathf.Cos(latAng) * lonCos; float latSin = Mathf.Sin(latAng) * lonCos; // Vertices, Normals, UV geo.Vertices[offset + Segments - l] = new Vector3(latCos, lonSin, latSin); geo.Normals[offset + Segments - l] = new Vector3(latCos, lonSin, latSin); geo.UV[offset + Segments - l] = new Vector2(latAng / tau, (lonSin+Radius) / (Radius*2)); // Tangents geo.Tangents[offset + Segments - l] = new Vector4(-latSin, 0f, latCos, -1).normalized; } // Polygons var polyOrigin = geo.Polygons[i*2 ] = offset; var polyLength = geo.Polygons[i*2+1] = Segments+1; if (i > 0) { for (int t = 0; t < polyLength-1; t++) { // First Triangle if (i < Segments-1) { geo.Triangles[triIndex++] = polyOrigin + t; geo.Triangles[triIndex++] = polyOrigin + t + 1; geo.Triangles[triIndex++] = polyOrigin + t - polyLength + 1; } // Second Triangle if (i > 1) { geo.Triangles[triIndex++] = polyOrigin + t; geo.Triangles[triIndex++] = polyOrigin + t - polyLength + 1; geo.Triangles[triIndex++] = polyOrigin + t - polyLength; } } } } geo.Offset(Center); geo.ApplyOrientation(Orientation); return geo; }
public Geometry Output() { bool isOpen = Mathf.Abs(EndAngle - StartAngle) < 360; bool hasMidPoint = (Opening == OpeningType.Sector && isOpen) || (Opening == OpeningType.Sector && Surface); int vertexCount = Segments; if (isOpen) vertexCount++; if (hasMidPoint) vertexCount++; Geometry geo = new Geometry(vertexCount); int arcVertices = Segments + (isOpen || hasMidPoint ? 1 : 0); // Vertices, Normals for (int i = 0; i < arcVertices; i++) { int seg = Segments + (hasMidPoint ? 0 : 0); float angle = StartAngle + ((EndAngle-StartAngle) * i / seg); float h = Mathf.Cos(angle * Mathf.Deg2Rad) * Radius; float v = Mathf.Sin(angle * Mathf.Deg2Rad) * Radius; geo.Vertices[arcVertices - i - 1] = new Vector3(h, 0f, v); geo.Normals[arcVertices - i - 1] = Vector3.up; } if (hasMidPoint) { geo.Vertices[vertexCount-1] = Vector3.zero; geo.Normals[vertexCount-1] = Vector3.up; } if (Surface) { var triangulate = new Triangulate(geo); geo = triangulate.Output(); if (!isOpen && Opening == OpeningType.Sector) { System.Array.Resize<int>(ref geo.Triangles, geo.Triangles.Length + 3); geo.Triangles[geo.Triangles.Length-3] = 0; geo.Triangles[geo.Triangles.Length-2] = Segments; geo.Triangles[geo.Triangles.Length-1] = Segments-1; } } // Polygon if (hasMidPoint && !isOpen) { geo.Polygons = new int[] {0, vertexCount-1}; } else { geo.Polygons = new int[] {0, vertexCount}; } geo.ApplyOrientation(Orientation); geo.Offset(Center); return geo; }