Create a cube from planes

Hi everyone.

My situation is quite simple. I need to represent BoundingBoxes, and to do so, I simply used cubes. It made it really easy to manipulate it. But now, I need to have different colors for each of the faces of my cube. My first try consisted in simply creating six planes that I attached to a cube game object . This way, I can still use my cube GO to manipulate the BB and I was to lazy to think of a more efficient solution. Here is part of the code I used :

		boundingBox = new GameObject();
		
		// Face 1 : P1, P2, P3, P4
		GameObject plane1 = GameObject.CreatePrimitive(PrimitiveType.Plane);
		Material m1 = new Material(Shader.Find("Custom/Unlit"));
		m1.color = color1;
		plane1.renderer.material = m1;
		plane1.transform.parent = boundingBox.transform;
		plane1.transform.position = new Vector3(0.0f,0.0f,0.5f);
		plane1.transform.localRotation = Quaternion.FromToRotation(new Vector3(0.0f,1.0f,0.0f),new Vector3(0.0f,0.0f,1.0f));
		plane1.transform.localScale = new Vector3(0.1f,1.0f,0.1f);
		...

It does work (I can see my cubes), but my application is now extremely slow. I guess that having to apply all the transformations of my BB on each of the plane requires a lot of computation time.

The efficient solution would be to simply draw polygons from my vertices. I now have Unity Pro, so I was wondering if there was a simple way to Call OpenGL function DrawPolygon …

Otherwise, what solution would be the best for my problem.

Thank’s guys.
Quentin.

There’s not really a simple way to do any low-level drawing stuff, though I wouldn’t call it that challenging. (I’ve done it a lot in other languages and it’s not really that complicated.) Read through the docs on it.

This might be a hammer/nail thing since I’ve recently been working on one but I would color the cube with a tri-planar shader. If you’re not familiar with them it would allow you to texture/color each face separately. You can google it, there are several examples online.

Thanks for the suggestion. I made a few research on triplanar shaders and I was about to try, but I wanted to try another solution first : using meshes instead of planes. And it actually worked. It’s still slower than with simple cubes but it’s good enough. Thanks again for the suggestion.

If it can help someone, here is part of the code :

		vertices = new Vector3[8]{p1,p2,p3,p4,p5,p6,p7,p8};
		boundingBox = new GameObject();
		
		// Face 1 : P1, P2, P3, P4
		GameObject face1 = new GameObject();
		face1.transform.parent = boundingBox.transform;
		mesh1 = new Mesh();
		mesh1.vertices = vertices;
		mesh1.triangles = new int[6]{2,1,0,3,2,0};
		Material m1 = new Material(Shader.Find("Custom/Unlit"));
		m1.color = color1;
		MeshFilter mf = (UnityEngine.MeshFilter)(face1.gameObject.AddComponent(typeof(MeshFilter)));
		mf.mesh = mesh1;
		MeshRenderer mr = (UnityEngine.MeshRenderer)(face1.gameObject.AddComponent(typeof(MeshRenderer)));
		mr.renderer.material = m1;