Skip to content

Instantly share code, notes, and snippets.

@stonstad
Created January 19, 2026 18:50
Show Gist options
  • Select an option

  • Save stonstad/9e363ba3a5ad8e2924d63c7222c97499 to your computer and use it in GitHub Desktop.

Select an option

Save stonstad/9e363ba3a5ad8e2924d63c7222c97499 to your computer and use it in GitHub Desktop.
AddGreedyQuad
private void AddGreedyQuad(float x, float y, float z, int axisU, int axisV, int axisD, int width, int height, int faceDir,
List<Vector3> verts, List<int> tris, List<Vector2> uvs)
{
float p0x;
float p0y;
float p0z;
float p1x;
float p1y;
float p1z;
float p2x;
float p2y;
float p2z;
float p3x;
float p3y;
float p3z;
p0x = x;
p0y = y;
p0z = z;
p1x = x;
p1y = y;
p1z = z;
if (axisU == 0) p1x += width;
if (axisU == 1) p1y += width;
if (axisU == 2) p1z += width;
p2x = x;
p2y = y;
p2z = z;
if (axisV == 0) p2x += height;
if (axisV == 1) p2y += height;
if (axisV == 2) p2z += height;
p3x = x;
p3y = y;
p3z = z;
if (axisU == 0) p3x += width;
if (axisU == 1) p3y += width;
if (axisU == 2) p3z += width;
if (axisV == 0) p3x += height;
if (axisV == 1) p3y += height;
if (axisV == 2) p3z += height;
Vector3 v0;
Vector3 v1;
Vector3 v2;
Vector3 v3;
v0 = new Vector3(p0x, p0z, p0y);
v1 = new Vector3(p1x, p1z, p1y);
v2 = new Vector3(p2x, p2z, p2y);
v3 = new Vector3(p3x, p3z, p3y);
int baseIndex = verts.Count;
verts.Add(v0);
verts.Add(v1);
verts.Add(v2);
verts.Add(v3);
if (faceDir == -1)
{
// flipped U to avoid mirroring on negative faces
uvs.Add(new Vector2(1f, 0f)); // v0
uvs.Add(new Vector2(0f, 0f)); // v1
uvs.Add(new Vector2(1f, 1f)); // v2
uvs.Add(new Vector2(0f, 1f)); // v3
}
else
{
uvs.Add(new Vector2(0f, 0f)); // v0
uvs.Add(new Vector2(1f, 0f)); // v1
uvs.Add(new Vector2(0f, 1f)); // v2
uvs.Add(new Vector2(1f, 1f)); // v3
}
// Winding order depends on the face direction
if (faceDir == 1)
{
// Positive normal
tris.Add(baseIndex); // 0
tris.Add(baseIndex + 2); // 2
tris.Add(baseIndex + 1); // 1
tris.Add(baseIndex + 1); // 1
tris.Add(baseIndex + 2); // 2
tris.Add(baseIndex + 3); // 3
}
else
{
// Negative normal
tris.Add(baseIndex); // 0
tris.Add(baseIndex + 1); // 1
tris.Add(baseIndex + 2); // 2
tris.Add(baseIndex + 1); // 1
tris.Add(baseIndex + 3); // 3
tris.Add(baseIndex + 2); // 2
}
}
@stonstad
Copy link
Author

Removes array allocations per face.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment