Skip to content

Instantly share code, notes, and snippets.

@twobob
Created January 5, 2025 00:24
Show Gist options
  • Save twobob/6d38aacee1e37e0733015a4c21e46b79 to your computer and use it in GitHub Desktop.
Save twobob/6d38aacee1e37e0733015a4c21e46b79 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
public static class StripUtility
{
// 1) Remove specified vertices
// 2) Re-sort remaining verts by angle around the centroid
// Ensures a consistent winding for a triangle strip after removal
public static void RemoveAndReorder(ref List<Vector3> verts, List<int> indicesToRemove)
{
// Remove unwanted verts (sort indices descending so removal won't shift)
indicesToRemove.Sort((a, b) => b.CompareTo(a));
foreach(int i in indicesToRemove)
if(i >= 0 && i < verts.Count) verts.RemoveAt(i);
if(verts.Count < 3) return; // Nothing to reorder
// Compute centroid
Vector3 center = Vector3.zero;
for(int i = 0; i < verts.Count; i++)
center += verts[i];
center /= verts.Count;
// Sort by angle around centroid
verts.Sort((v1, v2) =>
{
float a1 = Mathf.Atan2(v1.y - center.y, v1.x - center.x);
float a2 = Mathf.Atan2(v2.y - center.y, v2.x - center.x);
return a1.CompareTo(a2);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment