• Stars
    star
    1,706
  • Rank 27,360 (Top 0.6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert OBJ assets to glTF

OBJ2GLTF

Convert OBJ assets to glTF 2.0.

Getting Started

Install Node.js if you don't already have it, and then:

npm install -g obj2gltf

Using obj2gltf as a command-line tool:

obj2gltf -i model.obj

obj2gltf -i model.obj -o model.gltf

obj2gltf -i model.obj -o model.glb

Using obj2gltf as a library:

Converting an obj model to gltf:

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);
});

Converting an obj model to glb

const obj2gltf = require("obj2gltf");
const fs = require("fs");
const options = {
  binary: true,
};
obj2gltf("model.obj", options).then(function (glb) {
  fs.writeFileSync("model.glb", glb);
});

Material types

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_pbrSpecularGlossiness extension)
  • Unlit materials (via KHR_materials_unlit extension)

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

Usage

Command line flags:

Flag Description Required
-h, --help Display help. No
-i, --input Path to the obj file. βœ… Yes
-o, --output Path of the converted glTF or glb file. No
-b, --binary Save as binary glTF (.glb). No, default false
-s, --separate Writes out separate buffers and textures instead of embedding them in the glTF file. No, default false
-t, --separateTextures Write out separate textures only. No, default false
--checkTransparency Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque. No, default false
--secure Prevent the converter from reading texture or mtl files outside of the input obj directory. No, default false
--packOcclusion Pack the occlusion texture in the red channel of metallic-roughness texture. No, default false
--metallicRoughness The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots. No, default false
--specularGlossiness The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension. No, default false
--unlit The glTF will be saved with the KHR_materials_unlit extension. No, default false
--metallicRoughnessOcclusionTexture Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material. No
--specularGlossinessTexture Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension. No
--occlusionTexture Path to the occlusion texture that should override textures in the .mtl file. No
--normalTexture Path to the normal texture that should override textures in the .mtl file. No
--baseColorTexture Path to the baseColor/diffuse texture that should override textures in the .mtl file. No
--emissiveTexture Path to the emissive texture that should override textures in the .mtl file. No
--alphaTexture Path to the alpha texture that should override textures in the .mtl file. No
--input-up-axis Up axis of the obj. No
--output-up-axis Up axis of the converted glTF. No
--triangle-winding-order-sanitization Apply triangle winding order sanitization. No

Build Instructions

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

Running Test Coverage

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.

Generating Documentation

To generate the documentation:

npm run jsdoc

The documentation will be placed in the doc folder.

Contributions

Pull requests are appreciated. Please use the same Contributor License Agreement (CLA) used for CesiumJS.


Developed by the Cesium team.

More Repositories

1

cesium

An open-source JavaScript library for world-class 3D globes and maps 🌎
JavaScript
12,828
star
2

3d-tiles

Specification for streaming massive heterogeneous 3D geospatial datasets 🌎
Batchfile
2,110
star
3

gltf-pipeline

Content pipeline tools for optimizing glTF assets. 🌐
JavaScript
1,915
star
4

cesium-unreal

Bringing the 3D geospatial ecosystem to Unreal Engine
C++
924
star
5

cesium-native

C++
423
star
6

3d-tiles-validator

Validator for 3D Tiles 🚦
TypeScript
420
star
7

webglreport

A web page that reports a browser's WebGL capabilities, including supported extensions and implementation specific capabilities, such as the maximum number of texture units.
JavaScript
401
star
8

cesium-unity

Bringing the 3D geospatial ecosystem to Unity
C#
349
star
9

3d-tiles-tools

TypeScript
295
star
10

3d-tiles-samples

Sample tilesets for learning how to use 3D Tiles πŸ“š
JavaScript
274
star
11

cesium-webpack-example

The minimal recommended setup for an application using Cesium with Webpack.
JavaScript
246
star
12

quantized-mesh

Specification for streaming massive terrain datasets for 3D visualization.
238
star
13

cesium-unity-samples

Sample project for Cesium for Unity
C#
229
star
14

cesium-threejs-experiment

A small example for using Three JS on Cesium to emulate a combined scene.
JavaScript
187
star
15

cesium-unreal-samples

Getting Started Sample Project for Cesium for Unreal
183
star
16

cesium-workshop

An example application that visualizes and annotates a 3D city using the Cesium platform.
JavaScript
164
star
17

wetzel

Generate Markdown documentation from JSON Schema
JavaScript
133
star
18

cesium-google-earth-examples

Google Earth plugin API samples ported to Cesium
JavaScript
94
star
19

cesium-materials-pack

A Cesium plugin with procedurally-shaded materials such as bricks, wood, and noise patterns
JavaScript
85
star
20

cdb-to-3dtiles

Convert CDB to 3D Tiles
C++
76
star
21

cesium-o3de

Cesium for O3DE
C++
74
star
22

cesium-omniverse

Bringing the 3D geospatial ecosystem to Omniverse
C++
56
star
23

cesium-ion-rest-api-examples

Code examples for using the Cesium ion REST API 🌎
JavaScript
35
star
24

cesium-vite-example

The minimal recommended setup for an application using Cesium with Vite.
JavaScript
34
star
25

cesium-unreal-vr-tutorial

Unreal Engine project, assets, and code used in the Cesium for Unreal VR Tutorial Series
33
star
26

cesium-ion-blender-addon

Blender add-on for uploading and tiling models with Cesium ion. https://cesium.com
Python
22
star
27

collada2gltf-web-service

Simple Node.js web service to convert 3D models from COLLADA to glTF
JavaScript
20
star
28

cesium-ion-3ds-max-plugin

Autodesk 3DS Max plugin for uploading and tiling models with Cesium ion.
MAXScript
15
star
29

cesium-omniverse-samples

Sample projects for Cesium for Omniverse
14
star
30

3d-tiles-samples-generator

TypeScript
12
star
31

webstorm-plugin

Kotlin
8
star
32

cesium-ion-sketchup-extension

SketchUp extension for uploading and tiling models with Cesium ion.
Ruby
8
star
33

OpenPhillyGlobe

"Google Earth for Philadelphia" with open source and open data.
JavaScript
7
star
34

cesium-o3de-samples

Samples project for Cesium for O3DE
CMake
6
star
35

cesium-concierge

I automate common GitHub tasks
JavaScript
6
star
36

strip-pragma-loader

JavaScript
4
star
37

eslint-config-cesium

ESLint Configuration for Cesium
JavaScript
1
star
38

cesium-ion-plugin-template

1
star