Skip to content

Instantly share code, notes, and snippets.

@korchoon
Last active May 8, 2020 11:44
Show Gist options
  • Save korchoon/e3c9181dbc3dc0b88a43d1b135497b7b to your computer and use it in GitHub Desktop.
Save korchoon/e3c9181dbc3dc0b88a43d1b135497b7b to your computer and use it in GitHub Desktop.
void BuildGizmos() {
var occupiedComb = new List<CombineInstance>();
var freeComb = new List<CombineInstance>();
int xLen = _runtimeGrid.Len0, yLen = _runtimeGrid.Len1, zLen = _runtimeGrid.Len2;
var sharedMesh = Cube();
sharedMesh.hideFlags = HideFlags.DontSave;
for (var x = 0; x < xLen; x++)
for (var y = 0; y < yLen; y++)
for (var z = 0; z < zLen; z++) {
var posLS = new Vector3(x, y, z);
var xyz = new int3(x, y, z);
if (_runtimeGrid.isWalkable.Get(xyz)) {
freeComb.Add(new CombineInstance() {
mesh = sharedMesh,
transform = Matrix4x4.TRS(posLS, Quaternion.identity, Vector3.one)
});
}
else {
occupiedComb.Add(new CombineInstance() {
mesh = sharedMesh,
transform = Matrix4x4.TRS(posLS, Quaternion.identity, Vector3.one),
});
}
}
const int maxVert = 1 << 16;
var cubeVertCount = sharedMesh.vertices.Length;
__occupiedMeshes = Combine(occupiedComb);
__freeMeshes = Combine(freeComb);
Mesh[] Combine(List<CombineInstance> arg) {
var meshCount = Mathf.CeilToInt((float) cubeVertCount * arg.Count / maxVert);
var chunk = arg.Count / meshCount;
var result = new Mesh[meshCount];
for (int i = 0; i < meshCount; i++) {
var combine = arg.Skip(i * chunk).Take(chunk).ToArray();
var mesh = new Mesh(){hideFlags = HideFlags.DontSave};
mesh.CombineMeshes(combine);
mesh.Optimize();
result[i] = mesh;
}
return result;
}
Mesh Cube() {
var mesh = new Mesh();
const float length = 1f;
const float width = 1f;
const float height = 1f;
var p0 = new Vector3(-length * .5f, -width * .5f, height * .5f);
var p1 = new Vector3(length * .5f, -width * .5f, height * .5f);
var p2 = new Vector3(length * .5f, -width * .5f, -height * .5f);
var p3 = new Vector3(-length * .5f, -width * .5f, -height * .5f);
var p4 = new Vector3(-length * .5f, width * .5f, height * .5f);
var p5 = new Vector3(length * .5f, width * .5f, height * .5f);
var p6 = new Vector3(length * .5f, width * .5f, -height * .5f);
var p7 = new Vector3(-length * .5f, width * .5f, -height * .5f);
var vertices = new[] {
p0, p1, p2, p3, // Bottom
p7, p4, p0, p3, // Left
p4, p5, p1, p0, // Front
p6, p7, p3, p2, // Back
p5, p6, p2, p1, // Right
p7, p6, p5, p4 // Top
};
var up = Vector3.up;
var down = Vector3.down;
var front = Vector3.forward;
var back = Vector3.back;
var left = Vector3.left;
var right = Vector3.right;
Vector3[] normales = {
down, down, down, down, // Bottom
left, left, left, left, // Left
front, front, front, front, // Front
back, back, back, back, // Back
right, right, right, right, // Right
up, up, up, up // Top
};
var _00 = new Vector2(0f, 0f);
var _10 = new Vector2(1f, 0f);
var _01 = new Vector2(0f, 1f);
var _11 = new Vector2(1f, 1f);
var uvs = new[] {
_11, _01, _00, _10, // Bottom
_11, _01, _00, _10, // Left
_11, _01, _00, _10, // Front
_11, _01, _00, _10, // Back
_11, _01, _00, _10, // Right
_11, _01, _00, _10, // Top
};
var triangles = new[] {
// Bottom
3, 1, 0,
3, 2, 1,
// Left
3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
// Front
3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
// Back
3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
// Right
3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
// Top
3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
};
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment