# How to create a custom cuboids at runtime?

I am creating a voxel based game (think Minecraft adventure mode) and the performance of having many box colliders and unneeded vertices/draw calls of separate cubes was really low. My first approach was combining the cubes into single mesh, which I did(for each block type) but I still had separate colliders for each block and I still had performance issues when a bigger area was loaded and a huge spike when dynamically loading world around me due to the overhead of creating colliders.

So I decided to compress blocks into bigger cuboids but I can’t work out how to create custom cuboids at runtime(specified by two Vector3s). My first approach was taking the basic cube and scaling it, but I’m not a huge fan of scaling and I had some texturing issues, so I decided to use the Mesh class and create it from scratch. I looked at the examples but it’s a bit unclear, so I am asking for people with experience using the Mesh class to help me work out how to create a custom cuboid.

Thank you for taking your time!

After looking at some examples, I managed to figure it out, it’s only missing UV coords(I just put in default X-Z planar UV coords as a placeholder). The task seemed more intimidating than it actually was. Here’s the code:

EDIT: Split up the vertices for texture mapping and did the texture mapping work. Also added adding a box collider.

``````using UnityEngine;
``````

using System.Collections;

public static class CreateArbitaryPrimitive
{

``````public static void CreateCuboid(Vector3 origin, Vector3 end)
{
Vector3 diff = end - origin;

Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3;
Vector2[] uv = new Vector2[vertices.Length];
int[] triangles = new int;
vertices = new Vector3(0, 0, 0);//side1
uv = new Vector2(1 / 6f, 0);
vertices = new Vector3(diff.x, 0, 0);//side1
uv = new Vector2(0 / 6f, 0);
vertices = new Vector3(0, diff.y, 0);//side1
uv = new Vector2(1 / 6f, 1);
vertices = new Vector3(diff.x, diff.y, 0);//side1
uv = new Vector2(0 / 6f, 1);
vertices = new Vector3(0, 0, diff.z); //side2
uv = new Vector2(1 / 6f, 0);
vertices = new Vector3(0, diff.y, 0); //side2
uv = new Vector2(2 / 6f, 1);
vertices = new Vector3(0, diff.y, diff.z);//side2
uv = new Vector2(1 / 6f, 1);
vertices = new Vector3(0, 0, 0);//side2
uv = new Vector2(2 / 6f, 0);
vertices = new Vector3(0, diff.y, diff.z);//side3
uv = new Vector2(3 / 6f, 1);
vertices = new Vector3(diff.x, 0, diff.z);//side3
uv = new Vector2(2 / 6f, 0);
vertices = new Vector3(diff.x, diff.y, diff.z);//side3
uv = new Vector2(2 / 6f, 1);
vertices = new Vector3(0, 0, diff.z);//side3
uv = new Vector2(3 / 6f, 0);
vertices = new Vector3(diff.x, diff.y, diff.z);//side4
uv = new Vector2(4 / 6f, 1);
vertices = new Vector3(diff.x, 0, diff.z);//side4
uv = new Vector2(4 / 6f, 0);
vertices = new Vector3(diff.x, diff.y, 0);//side4
uv = new Vector2(3 / 6f, 1);
vertices = new Vector3(diff.x, 0, 0);//side4
uv = new Vector2(3 / 6f, 0);
vertices = new Vector3(0, 0, 0);//bottom
uv = new Vector2(5 / 6f, 0);
vertices = new Vector3(diff.x, 0, diff.z);//bottom
uv = new Vector2(4 / 6f, 1);
vertices = new Vector3(0, 0, diff.z);//bottom
uv = new Vector2(5 / 6f, 1);
vertices = new Vector3(diff.x, 0, 0);//bottom
uv = new Vector2(4 / 6f, 0);
vertices = new Vector3(diff.x, diff.y, diff.z);//top
uv = new Vector2(1, 1);
vertices = new Vector3(0, diff.y, 0); //top
uv = new Vector2(5 / 6f, 0);
vertices = new Vector3(0, diff.y, diff.z);//top
uv = new Vector2(5 / 6f, 1);
vertices = new Vector3(diff.x, diff.y, 0);//top
uv = new Vector2(1, 0);//top

#region Triangles

//sides
//side1
triangles = 2;
triangles = 1;
triangles = 0;

triangles = 1;
triangles = 2;
triangles = 4;

//side2
triangles = 3;
triangles = 8;
triangles = 9;

triangles = 5;
triangles = 8;
triangles = 3;

//side3
triangles = 10;
triangles = 6;
triangles = 7;

triangles = 11;
triangles = 6;
triangles = 10;

//side4
triangles = 12;
triangles = 13;
triangles = 14;

triangles = 14;
triangles = 13;
triangles = 15;

//bottom

triangles = 16;
triangles = 17;
triangles = 18;

triangles = 19;
triangles = 17;
triangles = 16;

//top

triangles = 22;
triangles = 20;
triangles = 21;

triangles = 21;
triangles = 20;
triangles = 23;
#endregion

mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;

mesh.RecalculateNormals();
mesh.RecalculateBounds();

GameObject go = new GameObject("Cuboid", typeof(MeshRenderer), typeof(MeshFilter), typeof(BoxCollider));
go.transform.position = origin;
go.GetComponent<MeshFilter>().mesh = mesh;
BoxCollider collider = go.GetComponent<BoxCollider>();
collider.size = mesh.bounds.size;
collider.center = mesh.bounds.size / 2;
}
``````

}