Convert OBJ assets to glTF 2.0.
Install Node.js if you don't already have it, and then:
npm install -g obj2gltf
obj2gltf -i model.obj
obj2gltf -i model.obj -o model.gltf
obj2gltf -i model.obj -o model.glb
const obj2gltf = require('obj2gltf');
const fs = require('fs');
obj2gltf('model.obj')
.then(function(gltf) {
const data = Buffer.from(JSON.stringify(gltf));
fs.writeFileSync('model.gltf', data);
});const obj2gltf = require('obj2gltf');
const fs = require('fs');
const options = {
binary : true
}
obj2gltf('model.obj', options)
.then(function(glb) {
fs.writeFileSync('model.glb', glb);
});Traditionally the .mtl file format describes the Blinn-Phong shading model. Meanwhile glTF 2.0 introduces physically-based materials.
There are three shading models supported by obj2gltf:
- Metallic roughness PBR
- Specular glossiness PBR (via
KHR_materials_pbrSpecularGlossinessextension) - Unlit materials (via
KHR_materials_unlitextension)
If the material type is known in advance, it should be specified with either the metallicRoughness or specularGlossiness flag.
If lighting information is already present in the model, the unlit flag should be used. This will save the glTF with the KHR_materials_unlit extension.
If the model is created with PBR textures, either the metallicRoughness or specularGlossiness flag should be passed in.
See the table below for more information about how to specify PBR values inside the .mtl file.
If none of these flags are provided, the .mtl is assumed to contain traditional Blinn-Phong materials which will be converted to metallic-roughness PBR. There may be some quality loss as traditional materials do not map perfectly to PBR materials.
Commonly in PBR workflows the the .mtl file may not exist or its values may be outdated or incorrect. As a convenience the PBR textures may be supplied directly to the command line.
Mapping of mtl slots to shading models
| Slot | Metallic roughness | Specular glossiness |
|---|---|---|
| Ka | occlusion value | occlusion value |
| Ke | emissive color | emissive color |
| Kd | base color | diffuse color |
| Ks | metallic value | specular color |
| Ns | roughness value | glossiness value |
| d | alpha | alpha |
| Tr | 1.0 - alpha | 1.0 - alpha |
| map_Ka | occlusion texture | occlusion texture |
| map_Ke | emissive texture | emissive texture |
| map_Kd | base color texture | diffuse texture |
| map_Ks | metallic texture | specular texture |
| map_Ns | roughness texture | glossiness texture |
| map_Bump | normal texture | normal texture |
Run the tests:
npm run test
To run ESLint on the entire codebase, run:
npm run eslint
To run ESLint automatically when a file is saved, run the following and leave it open in a console window:
npm run eslint-watch
Coverage uses nyc. Run:
npm run coverage
For complete coverage details, open coverage/lcov-report/index.html.
The tests and coverage covers the Node.js module; it does not cover the command-line interface, which is tiny.
To generate the documentation:
npm run jsdoc
The documentation will be placed in the doc folder.
Pull requests are appreciated. Please use the same Contributor License Agreement (CLA) used for NextechJS.
Developed by the NextechJS & CesiumJS team.

