Render Cube using Planes geometry in Threejs

If you’re reading this article, there are high chances that you are also currently looking for a solution that I was looking back then when I needed it.

Cube rendered using planes in Threejs

Without further discussion, let’s get started with the implementation.

To build a cube geometry using plane geometry as the faces of the cube, we need to understand the cube from geometrical point of view. Cube has 6 faces. These faces in our case will be plane geometries. Each face of cube has some position & rotation relative to other faces.

We will be using Threejs to render our cube geometry. I recommend some basic understanding of Threejs to be able to understand the code ahead. But even if you’re not at all aware about anything related to coding, I still recommend you to give it a read because I assure you, it will give you a very good idea about the logic behind.

First of all, we’ll setup Scene, Camera & Renderer for Threejs.

Now, we will write a function createPlane to create PlaneGeometry (which will act like face of the cube) for us of any desired size that we pass as argument to it.

Next, we will have one more function createFacesOfCube that will create faces of the cube for us using the createPlane method.

Okay, so now take some time to understand the above logic. Actually, it is very simple. You can see how we have just created the faces of the cube & re-positioned and re-rotated them in order for them to form a cube like geometry.

Let us now utilise the above written functions & make our cube come alive.

Here, we are getting the faces of the cube using the function that we’ve previously written. Thereafter, we’re grouping all the faces into a Threejs Object3D so as to actually make all the faces part of the cube.

Once this is done, we added our cube into the scene. Also, here we have set the camera positions to some pre-defined values just because these values are giving isometric-like look of our cube. So it’s easy to see like this.

Lastly, we will be rendering whatever we have been upto now. If you’re little familiar with the Threejs, above code will make sense to you. Else you can see get started section on Threejs webite here.

Note: We used OrbitControls in our code so as to make the scene interactive to mouse events. You can find the code for OrbitControls here.

Wanted to have a look on how the end result looks like? Here you go.

You can find the codesandbox link here in case the below embed does not work.

Render Cube using PlaneGeometry in Threejs

Thank you for taking out time to learn something new. I hope you learnt something from this article. I really appreciate if you give me feedback.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store