Skip to content

Instantly share code, notes, and snippets.

@ogxd
Last active March 9, 2020 21:49
Show Gist options
  • Save ogxd/361060ae591f47d1e727326975cb8631 to your computer and use it in GitHub Desktop.
Save ogxd/361060ae591f47d1e727326975cb8631 to your computer and use it in GitHub Desktop.
Create a Unity mesh from given bounds
public Mesh CreateMeshFromBounds(Vector3 center, Vector3 extends) {
var xm_ym_zm = new Vector3(center.x - extends.x, center.y - extends.y, center.z - extends.z);
var xp_ym_zm = new Vector3(center.x + extends.x, center.y - extends.y, center.z - extends.z);
var xm_yp_zm = new Vector3(center.x - extends.x, center.y + extends.y, center.z - extends.z);
var xp_yp_zm = new Vector3(center.x + extends.x, center.y + extends.y, center.z - extends.z);
var xm_ym_zp = new Vector3(center.x - extends.x, center.y - extends.y, center.z + extends.z);
var xp_ym_zp = new Vector3(center.x + extends.x, center.y - extends.y, center.z + extends.z);
var xm_yp_zp = new Vector3(center.x - extends.x, center.y + extends.y, center.z + extends.z);
var xp_yp_zp = new Vector3(center.x + extends.x, center.y + extends.y, center.z + extends.z);
Vector3[] vertices = new Vector3[] {
xm_ym_zm,
xp_ym_zm,
xm_yp_zm,
xp_yp_zm,
xm_ym_zp,
xp_ym_zp,
xm_yp_zp,
xp_yp_zp
};
int[] indices = new int[] {
0, 4, 2,
2, 4, 6,
1, 3, 5,
3, 7, 5,
0, 1, 4,
1, 5, 4,
2, 6, 3,
3, 6, 7,
0, 2, 1,
1, 2, 3,
4, 5, 6,
5, 7, 6
};
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = indices;
return mesh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment