Create an OBJ Cube With Your Favorite Images
Ever wanted to create a CAD file with your favorite pictures? There are many ways to apply images and textures to CAD files. However, for most of these operations, expensive programs or CAD experience might be required.
This tutorial shows you how to make a simple OBJ cube, while explaining some of the basic OBJ nomenclature. It then explains the steps to map pictures on the cube's faces. The only tools you will need is a text editor like notepad and your favorite images!
Step 1: OBJ With Textures?
OBJ is a open source geometry definition file format first developed by Wavefront Technologies. It has since been adopted by many 3D graphics application vendors.
It is unitless and represents 3D geometry alone. The contents of an OBJ file can be viewed in a text editor like Notepad. It contains information about the vertices, vertex normals, UV position of texture coordinates and faces that make the polygons. OBJ files can have associated MTL files. These files contain texture definitions and use the OBJ file's information to map textures onto the polygons as desired. For more information about the OBJ and MTL files, go to https://en.wikipedia.org/wiki/Wavefront_.obj_file
This tutorial will teach you to make a simple OBJ cube from scratch and create an associated MTL file to add pictures on the faces.
Step 2: Collect Your Desired Pictures
It is important to save the desired pictures in the same folder that the OBJ file will be created. For the purpose of this tutorial I have downloaded the following pictures and saved them in a folder:
I am only using 5 pictures for 5 faces of the cube, and I am leaving the 6th bottom face of the cube pictureless.
Step 3: Download 3D Builder
3D Builder is a great free tool that works well for viewing STL and OBJ files quickly. It is built in with the Windows 10 OS.
However, if you don't have it, you can download it from the microsoft store here.
Step 4: Create a Text Document
Create a text document in the folder with the pictures. Right click in the white space, select new, and select Text document
Step 5: Add OBJ Vertices
For the purpose of this tutorial, we will create a simple 3inch X 3inch X 3inch cube. Since my import units are in mm, I will be using 76.2 mm (= 3 inches). The OBJ file format itself, however, is unitless.
Copy the following text in the text document :
v -76.2 -76.2 -76.2
v 76.2 -76.2 -76.2
v 76.2 76.2 -76.2
v -76.2 76.2 -76.2
v -76.2 -76.2 76.2
v 76.2 -76.2 76.2
v 76.2 76.2 76.2
v -76.2 76.2 76.2
These are the coordinates of the 8 vertices of the cube. I have placed the center of the cube at (0 , 0 , 0) global coordinates. To make a cube of your desired size, change the 76.2 everywhere to your dimensions.
Step 6: Add Normals and Texture Coordinates
Below the vertices, copy the following text:
vn 0 -1 0.5
vn 0 1 0.5
vn -1 0 0.5
vn 1 0 0
vn 0 0 1
vn 0 0 -1
vt 0 0
vt 1 0
vt 0 1
vt 1 1
vn and vt refer to the normals and UV texture coordinates for this cube. For the purpose of the tutorial, copy these values exactly.
Step 7: Add Face Related Information
Below the normals and texture coordinates, paste the following text:
f 1/1/1 2/2/1 6/4/1
f 1/1/1 6/4/1 5/3/1
f 3/1/2 4/2/2 8/4/2
f 3/1/2 8/4/2 7/3/2
f 4/1/3 1/2/3 5/4/3
f 4/1/3 5/4/3 8/3/3
f 2/1/4 3/2/4 7/4/4
f 2/1/4 7/4/4 6/3/4
f 5/1/5 6/2/5 7/4/5
f 5/1/5 7/4/5 8/3/5
f 4/1/6 3/2/6 2/4/6
f 4/1/6 2/4/6 1/3/6
This defines each triangle of the OBJ cube with a combination of vertices, texture coordinates, and vertex normals. There are 12 triangles for the 6 faces of the cube.
For example, the first triangle of the cube f 1/1/1 2/2/1 6/4/1 is made up of the 1st, 2nd and 6rd vertices, the 1st, 2nd and 4th texture coordinates and the 1st vertex normal. The second triangle f 1/1/1 6/4/1 5/3/1 is made up of the 1st, 6th and 5th vertices, the 1st, 4th and 3rd texture coordinates and the 1st vertex normal. The two triangles make one face of the cube which results in them having the same vertex normal.
Any change in the order of vertices, normals, and texture coordinates in the previous steps would affect the definition of the faces and corrupt the file. So for the purpose of this tutorial, copy these values into your text file exactly.
Step 8: Review Your OBJ File for Errors
Your text file should look like this :
After the review, save your text file with a .obj file extension in the same location as shown below.
Step 9: View Your OBJ File in 3D Builder
Open your OBJ file in 3D Builder. It will look like this.
If there is an error, go back to Step 8.
Step 10: Create the MTL File
Create a text document like Step 4. Once created, paste the following text:
Kd 1 1 1
Ka 0 0 0
Ks 1 1 1
The above text defines a material 'CubeTop'. Ka, Kd, Ks, Ns are the ambient color, diffuse color, specular color and specular exponent definitions. d and illum define the transparency and illumination models of the material. Refer to the link in Step 1 for a more detailed description of MTL terminology. Finally, map_Kd pic1.jpg refers to the mapping of the image pic1.jpg which will be present in the folder.
Step 11: Create the Other Materials
Similar to Step 10, copy and paste the text four times for the other 4 surfaces of the cube. Then, change the newmtl from CubeTop to CubeFront, CubeBack, CubeLeft and Cube Right. Finally change the map_Kd entries to Pic2.jpg, Pic3.jpg, Pic4.jpg and Pic5.jpg, since these are the names and extensions of all my images. MTL files also support PNG images.
The rest of the entries can be the same.
This creates 4 new materials for the 4 faces along with the associated pictures for each face. Once complete, the MTL file should look like this.
Once complete, save the text document with a .mtl file extension. I have named my MTL file as PictureCube.mtl. Your MTL file is ready. However, it is meaningless by itself.
Step 12: Call the MTL File in Uour OBJ File
The next step is to call the MTL file in the OBJ file.
In the OBJ file, add the following text in the first line of the OBJ file as shown below:
Replace PictureCube with the name of your mtl file.
Step 13: Add the Material Names in the OBJ File
The next step is to add the material names defined in the MTL file into the OBJ file. This can be tricky.
Observe the faces carefully. By following the vertex, texture coordinates and vertex normal numbering convention, you will notice that the faces are paired in following manner :
Before the first pair of faces, write the following text :
The usemtl command uses the material name from the MTL file to map the materials to the set of faces. Repeat the above for all the materials defined in the MTL file. The resultant OBJ file is shown below :
I have not added a usemtl for the last two faces since I don't want a picture for the bottom surface, and it has not been defined in the MTL file.
Step 14: Your OBJ File is Ready!
After the previous step, save your OBJ file. Then launch in 3D builder.
If there are errors in 3D builder, or the pictures are missing, go back to Step 11 and 13 and compare your files with the pasted images. Also make sure that the OBJ file, MTL file and all images are in the same folder.
Once you have successfully created it, send the model to your nearest 3D printing service bureau to print this model!