/// <summary> /// Constructs a pyramid draft /// </summary> public static MeshDraft Pyramid(float radius, int segments, float height, bool generateUV = true) { float segmentAngle = 360f / segments; float currentAngle = 0; var ring = new Vector3[segments]; for (var i = 0; i < segments; i++) { ring[i] = Geometry.PointOnCircle3XZ(radius, currentAngle); currentAngle += segmentAngle; } var draft = new MeshDraft().AddBaselessPyramid(Vector3.up * height, ring, generateUV); if (generateUV) { var fanUV = new Vector2[segments]; currentAngle = 0; for (var i = 0; i < segments; i++) { Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f); uv.x = 1 - uv.x; fanUV[i] = uv; currentAngle += segmentAngle; } draft.AddTriangleFan(ring, Vector3.down, fanUV, true); } else { draft.AddTriangleFan(ring, Vector3.down, true); } draft.name = "Pyramid"; return(draft); }
/// <summary> /// Constructs a cylinder draft /// </summary> public static MeshDraft Cylinder(float radius, int segments, float height, bool generateUV = true) { float segmentAngle = 360f / segments; float currentAngle = 0; Vector3 halfHeightUp = Vector3.up * height / 2; var draft = new MeshDraft { name = "Cylinder" }; var lowerRing = new List <Vector3>(segments); var lowerDiskUV = new List <Vector2>(); var upperRing = new List <Vector3>(segments); var upperDiskUV = new List <Vector2>(); var strip = new List <Vector3>(); var stripNormals = new List <Vector3>(); var stripUV = new List <Vector2>(); for (var i = 0; i < segments; i++) { Vector3 lowerVertex; Vector3 upperVertex; AddCylinderPoints(radius, currentAngle, halfHeightUp, generateUV, ref strip, ref stripUV, ref stripNormals, out lowerVertex, out upperVertex); lowerRing.Add(lowerVertex); upperRing.Add(upperVertex); if (generateUV) { Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f); upperDiskUV.Add(uv); uv.x = 1 - uv.x; lowerDiskUV.Add(uv); } currentAngle += segmentAngle; } Vector3 lowerSeamVertex; Vector3 upperSeamVertex; AddCylinderPoints(radius, currentAngle, halfHeightUp, generateUV, ref strip, ref stripUV, ref stripNormals, out lowerSeamVertex, out upperSeamVertex); if (generateUV) { draft.AddTriangleFan(lowerRing, Vector3.down, lowerDiskUV, true); draft.AddTriangleFan(upperRing, Vector3.up, upperDiskUV); draft.AddTriangleStrip(strip, stripNormals, stripUV); } else { draft.AddTriangleFan(lowerRing, Vector3.down, true); draft.AddTriangleFan(upperRing, Vector3.up); draft.AddTriangleStrip(strip, stripNormals); } return(draft); }
public static MeshDraft Cylinder(float radius, int segments, float heignt) { float segmentAngle = 360f / segments; float currentAngle = 0; var lowerRing = new List <Vector3>(segments); var upperRing = new List <Vector3>(segments); for (var i = 0; i < segments; i++) { var point = PTUtils.PointOnCircle3XZ(radius, currentAngle); lowerRing.Add(point - Vector3.up * heignt / 2); upperRing.Add(point + Vector3.up * heignt / 2); currentAngle -= segmentAngle; } var draft = new MeshDraft { name = "Cylinder" } .AddTriangleFan(lowerRing) .Add(Band(lowerRing, upperRing)); upperRing.Reverse(); draft.AddTriangleFan(upperRing); return(draft); }
/// <summary> /// Constructs a prism draft /// </summary> public static MeshDraft Prism(float radius, int segments, float height, bool generateUV = true) { float segmentAngle = 360f / segments; float currentAngle = 0; Vector3 halfHeightUp = Vector3.up * height / 2; var lowerRing = new List <Vector3>(segments); var lowerDiskUV = new List <Vector2>(); var upperRing = new List <Vector3>(segments); var upperDiskUV = new List <Vector2>(); for (var i = 0; i < segments; i++) { var point = Geometry.PointOnCircle3XZ(radius, currentAngle); lowerRing.Add(point - halfHeightUp); upperRing.Add(point + halfHeightUp); if (generateUV) { Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f); upperDiskUV.Add(uv); uv.x = 1 - uv.x; lowerDiskUV.Add(uv); } currentAngle += segmentAngle; } var draft = new MeshDraft { name = "Prism" } .AddFlatQuadBand(lowerRing, upperRing, generateUV); if (generateUV) { draft.AddTriangleFan(upperRing, Vector3.up, upperDiskUV) .AddTriangleFan(lowerRing, Vector3.down, lowerDiskUV, true); } else { draft.AddTriangleFan(upperRing, Vector3.up) .AddTriangleFan(lowerRing, Vector3.down, true); } return(draft); }
/// <remarks> /// https://en.wikipedia.org/wiki/Triangle_fan /// </remarks> public static MeshDraft TriangleFan(List <Vector3> fan, Vector3 normal) { var draft = new MeshDraft { name = "TriangleFan", uv = new List <Vector2>(fan.Count), }; draft.AddTriangleFan(fan, normal); for (int i = 0; i < fan.Count; i++) { draft.uv.Add(new Vector2((float)i / fan.Count, (float)i / fan.Count)); } return(draft); }
public static MeshDraft Dodecahedron(float radius) { const float magicAngle1 = 52.62263590f; const float magicAngle2 = 10.81231754f; const float segmentAngle = 72; float currentAngle = 0; 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 = new MeshDraft { name = "Dodecahedron" } .AddTriangleFan(lowerCap) .Add(FlatBand(lowerCap, lowerRing)) .Add(FlatBand(lowerRing, upperRing)) .Add(FlatBand(upperRing, upperCap)); upperCap.Reverse(); draft.AddTriangleFan(upperCap); return(draft); }