How to convert a 3D object to ".glTF" (Blender 2.79)¶
If you have a ".obj" file or another 3D format object and want to convert it to ".glTF", there is a way to do it with Blender.
About Blender's latest version
Due to the recent release of Blender's excellent 2.80 version, we wrote a special version of this tutorial in our User Guide, to use with Blender 2.80. You can find it on https://docs.minsar.app/create/howtos/gltf-blender-280/.
Install glTF import plugin in Blender¶
1.1. If you don't have Blender, then go and download it on https://www.blender.org/download/, then install it on your computer.
1.2. Go and download the "glTF-blender-importer" which can be found there: https://github.com/ksons/gltf-blender-importer.
1.3. Unzip the archive, open it and go to addons. There you have a folder "io_scene_glTF". Copy it into the "Scripts / addons" folder of your Blender installation. That folder should be located in your
C://ProgramFiles/Blender Foundation folder, unless you have decided to install it elsewhere.
1.4. Open up Blender.
1.5. Go into "File / User Preferences".
1.6. Go into the "Add-ons" tab.
1.7. In the search bar, type "glTF".
1.8. You should see "glTF importer", you just have to toggle the option and save the user preferences.
Install glTF export plugin in Blender¶
2.1. Download the export addon over there: https://github.com/KhronosGroup/glTF-Blender-Exporter/tree/master/scripts
2.2. Unzip the addon you just downloaded.
2.3. After unzipping the archive, copy the "io_scene_glTF2" which is located in "scripts / addons" to the "scripts / addons" folder of your Blender installation. The "scripts / addons" folder of Blender should be located in your
C://Program Files/ Blender Foundation folder, unless you've decided to install it somewhere else.
2.4. Open Blender.
2.5. Go to "File / User Preferences".
2.6. Go to the Add-ons" tab, and in the search bar write "glTF". In the list on the right side, you should see the name of the add-on "Import-Export: glTF 2.0 format". Toggle it in order to enable it, and you should be good to go. Now if you try and export your model, it will propose you to export to ".glTF".
2.7. If it doesn't work, then try to go into the "File" tab and set the "Scripts" directory to the "scripts" folder from the downloaded archive (NOT the script folder of your Blender installation).
Convert your 3D model to glTF¶
Let's say you have downloaded the 3D model of an armchair. That armchair only came in ".fbx" format. You'll have to convert it then. Because there's a lot of chance that the armchair came with textures, you'll have to make sure your ".glTF" object points correctly to these textures as well.
3.1. Let's start by importing your model. In Blender, go to "File / Import" and choose ".fbx" (or the format of your downloaded object). You should soon see your nice armchair in the viewport.
If you want to see the textures of your model you can toggle the textures/material in the corresponding tab just next to "Object Mode". See below.
3.2. Now we have to turn your object into a ".glTF", and we will start by re-linking the textures correctly. For starters, you need to change Blender render engine, and choose "Cycles Render".
3.3. Then we will have to delete the current material on the object, and create a new one. To do so, you must click on your object, and then on the right-hand side click on the "material panel".
3.4. Delete the material by clicking on "-", and create another one by clicking on "New".
3.5. Next, you have to open the "Node editor". It is indeed the place where we are going to rebuild your object material by plugging the textures to a shader. To open the Node Editor, you must first drag up the lower window, like so:
3.6. Then click on the lowest-left button and choose "Node Editor".
Here you go, you're in the Node Editor. You can see two nodes: "Diffuse" and "Material Output". As you can see, "Diffuse BSDF" is plugged into the "Surface input" of "Material Output". It means that the current shader of the object is made of a simple white color. The result is created by the "Material Output". However, we are going to need to build a specific glTF shader and plug it into the "Material Output". Fortunately we won't have to build it ourselves! The Blender glTF Exporter plugin that we have installed at the beginning of this tutorial already has one. We just have to go and fetch it!
3.7. So firstly, you have to delete the "Diffuse BSDF" node. To do that, just click on the node and type "Delete".
3.8. You now have to tell Blender where it has to go and fetch the right shader. To do that, go to "File / Link". Here, you have to navigate to the unzipped Blender glTF Exporter you manipulated earlier. For instance if it is still in your "Download" folder, then the path would be:
C:\\Users\YourName\Downloads\glTF-Blender-Exporter-master\pbr-node\gltF2.blend\NodeTree\glTF Metallic Roughness. Click on "glTF Metallic Roughness" and then "Link from Library".
3.9. Once you have linked the right shader, hover your mouse over the Node Editor and type "Shift + A". Go to "Group", and choose "glTF Metallic Roughness".
4.0. Now we have to load the textures into the Node Editor and attach them to the new node you have just created. There are two ways to do that. The first way is simply to drag and drop your texture from your computer in the node editor (one at a time).
4.1. The second way is to type "Shift + A" and choose "Search". Type "Image Texture" in the search bar and choose the adequate node.
4.2. Click on "Open" and load your first texture. Let's say it's the Diffuse, or Albedo texture.
4.3. You then have to connect your diffuse texture to the "BaseColor" input of the glTF Metallic Roughness node. Click on the "Color" output of the "Image Texture" node, and don't release your click. Now when you drag your cursor, you can see a little yellow string attached to the "Color" input. Just drag it to the yellow pin of "BaseColor", and release the mouse button. Here you are, you have successfully connected your Diffuse texture to the glTF node.
4.4. Now drag all your other textures and attach them to the right pins: Normal should go to Normal, and the OcclusionRoughnessMetallic (ORM) texture should go both in MetallicRoughness and Occlusion. Indeed, this texture is supposed to contain the Occlusion Map in its red channel, the Roughness Map in its green channel, and the Metallic Map in its blue channel. As a result, the glTF node will automatically know in which channel of the image it should search for the right texture.
Creating an ORM Map
If you don't have an ORM and that you have separated Occlusion, Metallic and Roughness textures, please turn them into an ORM by following the steps given in https://www.khronos.org/blog/art-pipeline-for-glTF. If you don't have these kind of textures, don't worry about it.
4.5. When all your maps are connected, don't forget to put your ORM and Normal in "Non-Color Data", to avoid any artefact.
4.6. When all your textures are set, don't forget to make sure the "Shader" output of the glTF Node is attached to the "Surface" input of the "Material Output" node, or else it will simply not work!
4.7. You're all set! Now you just have to export your model in ".glTF". To do that, just click on your model in the 3D view, and go to "Files / Export..." and choose ".glTF".
4.8. The export window will appear. You don't have to touch anything, just choose the folder to which you want to export your model, name your model and then click on "Export glTF 2.0" on the top right-hand side.
At the time this tutorial is written, you won't be able to export your glTF model in the same folder as your ".obj" model, and certainly not in the same folder where your textures are. If you try it, Blender will tell you that the export has failed, because it will in fact try to re-write the textures that are already processed in Blender. To avoid this error, you must export your glTF in another folder. You will see that your textures will be exported too.
You have successfully turned your object into a ".glTF"!
Please note that the process you have just gone through is much easier in Blender 2.80, as precised at the beginning of this tutorial.