public static MeshDraft BaselessPyramid(Vector3 baseCenter, Vector3 apex, float radius, int segments, bool inverted = false) { float segmentAngle = Mathf.PI * 2 / segments * (inverted ? -1 : 1); float currentAngle = 0f; var vertices = new Vector3[segments + 1]; vertices[0] = apex; for (var i = 1; i <= segments; i++) { vertices[i] = PTUtils.PointOnCircle3(radius, currentAngle) + baseCenter; currentAngle += segmentAngle; } var draft = new MeshDraft { name = "BaselessPyramid" }; for (var i = 1; i < segments; i++) { draft.Add(Triangle(vertices[0], vertices[i], vertices[i + 1])); } draft.Add(Triangle(vertices[0], vertices[vertices.Length - 1], vertices[1])); return(draft); }
public static MeshDraft FlatSphere(float radius, int longitudeSegments, int latitudeSegments) { float longitudeSegmentAngle = Mathf.PI * 2 / longitudeSegments; float latitudeSegmentAngle = Mathf.PI / latitudeSegments; float currentLatitude = -Mathf.PI / 2; var rings = new List <List <Vector3> >(latitudeSegments); for (var i = 0; i <= latitudeSegments; i++) { var currentLongitude = 0f; var ring = new List <Vector3>(longitudeSegments); for (int j = 0; j < longitudeSegments; j++) { ring.Add(PTUtils.PointOnSphere(radius, currentLongitude, currentLatitude)); currentLongitude -= longitudeSegmentAngle; } rings.Add(ring); currentLatitude += latitudeSegmentAngle; } var draft = new MeshDraft { name = "Flat sphere" }; for (int i = 0; i < rings.Count - 1; i++) { draft.Add(FlatBand(rings[i], rings[i + 1])); } return(draft); }
public static MeshDraft Icosahedron(float radius) { float magicAngle = Mathf.PI * 26.56505f / 180; float segmentAngle = Mathf.PI * 72 / 180; float currentAngle = 0f; var upperRing = new List <Vector3>(5); for (var i = 0; i < 5; i++) { upperRing.Add(PTUtils.PointOnSphere(radius, currentAngle, magicAngle)); currentAngle -= segmentAngle; } currentAngle = segmentAngle / 2; var lowerRing = new List <Vector3>(5); for (var i = 0; i < 5; i++) { lowerRing.Add(PTUtils.PointOnSphere(radius, currentAngle, -magicAngle)); currentAngle -= segmentAngle; } var draft = BaselessPyramid(new Vector3(0, -radius, 0), lowerRing); draft.Add(FlatBand(lowerRing, upperRing)); upperRing.Reverse(); draft.Add(BaselessPyramid(new Vector3(0, radius, 0), upperRing)); draft.name = "Icosahedron"; return(draft); }
/// <summary> /// Reverses winding order of mesh triangles /// </summary> public static void FlipTriangles(this Mesh mesh) { for (int i = 0; i < mesh.subMeshCount; i++) { var triangles = mesh.GetTriangles(i); for (int j = 0; j < triangles.Length; j += 3) { PTUtils.Swap(ref triangles[j], ref triangles[j + 1]); } mesh.SetTriangles(triangles, i); } }
public static MeshDraft Sphere(float radius, int longitudeSegments, int latitudeSegments) { var draft = new MeshDraft { name = "Sphere" }; float longitudeSegmentAngle = Mathf.PI * 2 / longitudeSegments; float latitudeSegmentAngle = Mathf.PI / latitudeSegments; float currentLatitude = -Mathf.PI / 2; for (var ring = 0; ring <= latitudeSegments; ring++) { var currentLongitude = 0f; for (int i = 0; i < longitudeSegments; i++) { var point = PTUtils.PointOnSphere(radius, currentLongitude, currentLatitude); draft.vertices.Add(point); draft.normals.Add(point.normalized); draft.uv.Add(new Vector2((float)i / longitudeSegments, (float)ring / latitudeSegments)); currentLongitude -= longitudeSegmentAngle; } currentLatitude += latitudeSegmentAngle; } int i0, i1, i2, i3; for (int ring = 0; ring < latitudeSegments; ring++) { for (int i = 0; i < longitudeSegments - 1; i++) { i0 = ring * longitudeSegments + i; i1 = (ring + 1) * longitudeSegments + i; i2 = ring * longitudeSegments + i + 1; i3 = (ring + 1) * longitudeSegments + i + 1; draft.triangles.AddRange(new[] { i0, i1, i2 }); draft.triangles.AddRange(new[] { i2, i1, i3 }); } i0 = (ring + 1) * longitudeSegments - 1; i1 = (ring + 2) * longitudeSegments - 1; i2 = ring * longitudeSegments; i3 = (ring + 1) * longitudeSegments; draft.triangles.AddRange(new[] { i0, i1, i2 }); draft.triangles.AddRange(new[] { i2, i1, i3 }); } return(draft); }
public static MeshDraft Dodecahedron(float radius) { float magicAngle1 = Mathf.PI * 52.62263590f / 180; float magicAngle2 = Mathf.PI * 10.81231754f / 180; float segmentAngle = Mathf.PI * 2 / 5; float currentAngle = 0f; var lowerCap = new List <Vector3>(); var lowerRing = new List <Vector3>(); for (var i = 0; i <= 5; i++) { lowerCap.Add(PTUtils.PointOnSphere(radius, currentAngle, -magicAngle1)); lowerRing.Add(PTUtils.PointOnSphere(radius, currentAngle, -magicAngle2)); currentAngle -= segmentAngle; } currentAngle = -segmentAngle / 2; var upperCap = new List <Vector3>(); var upperRing = new List <Vector3>(); for (var i = 0; i <= 5; i++) { upperCap.Add(PTUtils.PointOnSphere(radius, currentAngle, magicAngle1)); upperRing.Add(PTUtils.PointOnSphere(radius, currentAngle, magicAngle2)); currentAngle -= segmentAngle; } var draft = TriangleFan(lowerCap); draft.Add(FlatBand(lowerCap, lowerRing)); draft.Add(FlatBand(lowerRing, upperRing)); draft.Add(FlatBand(upperRing, upperCap)); upperCap.Reverse(); draft.Add(TriangleFan(upperCap)); draft.name = "Dodecahedron"; return(draft); }
public static MeshDraft Cylinder(float radius, int segments, float heignt) { float segmentAngle = Mathf.PI * 2 / segments; float currentAngle = 0f; var lowerRing = new List <Vector3>(segments); var upperRing = new List <Vector3>(segments); for (var i = 0; i < segments; i++) { var point = PTUtils.PointOnCircle3(radius, currentAngle); lowerRing.Add(point - Vector3.up * heignt / 2); upperRing.Add(point + Vector3.up * heignt / 2); currentAngle -= segmentAngle; } var draft = TriangleFan(lowerRing); draft.Add(Band(lowerRing, upperRing)); upperRing.Reverse(); draft.Add(TriangleFan(upperRing)); draft.name = "Cylinder"; return(draft); }
public static MeshDraft Tetrahedron(float radius) { float tetrahedralAngle = Mathf.PI * -19.471220333f / 180; float segmentAngle = Mathf.PI * 2 / 3; float currentAngle = 0f; var vertices = new List <Vector3>(4) { new Vector3(0, radius, 0) }; for (var i = 1; i < 4; i++) { vertices.Add(PTUtils.PointOnSphere(radius, currentAngle, tetrahedralAngle)); currentAngle += segmentAngle; } var draft = Triangle(vertices[0], vertices[1], vertices[2]); draft.Add(Triangle(vertices[1], vertices[3], vertices[2])); draft.Add(Triangle(vertices[0], vertices[2], vertices[3])); draft.Add(Triangle(vertices[0], vertices[3], vertices[1])); draft.name = "Tetrahedron"; return(draft); }
/// <summary> /// Draws filled circle on texture using Bresenham's algorithm /// </summary> public static void DrawFilledCircle(this Texture2D texture, int centerX, int centerY, int radius, Color color) { PTUtils.DrawFilledCircle(centerX, centerY, radius, (x, y) => texture.SetPixel(x, y, color)); }
/// <summary> /// Draws circle on texture /// </summary> public static void DrawCircle(this Texture2D texture, Vector2Int center, int radius, Color color) { PTUtils.DrawCircle(center, radius, (x, y) => texture.SetPixel(x, y, color)); }
/// <summary> /// Draws anti-aliased line on texture /// </summary> public static void DrawAALine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color) { PTUtils.DrawAALine(x0, y0, x1, y1, (x, y, t) => texture.SetPixel(x, y, Color.Lerp(texture.GetPixel(x, y), color, t))); }
/// <summary> /// Draws anti-aliased line on texture /// </summary> public static void DrawAALine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color) { PTUtils.DrawAALine(v0, v1, (x, y, t) => texture.SetPixel(x, y, Color.Lerp(texture.GetPixel(x, y), color, t))); }
/// <summary> /// Draws line on texture /// </summary> public static void DrawLine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color) { PTUtils.DrawLine(x0, y0, x1, y1, (x, y) => texture.SetPixel(x, y, color)); }
/// <summary> /// Draws line on texture /// </summary> public static void DrawLine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color) { PTUtils.DrawLine(v0, v1, (x, y) => texture.SetPixel(x, y, color)); }