MCPcopy
hub / github.com/CesiumGS/obj2gltf

github.com/CesiumGS/obj2gltf @3.2.0 sqlite

repository ↗ · DeepWiki ↗ · release 3.2.0 ↗
133 symbols 291 edges 21 files 13 documented · 10%
README

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. :white_check_mark: 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.

Core symbols most depended-on inside this repo

loadObj
called by 47
lib/loadObj.js
getPrimitives
called by 22
specs/lib/loadObjSpec.js
loadTexture
called by 20
lib/loadTexture.js
defaultValue
called by 17
lib/obj2gltf.js
loadMtl
called by 16
lib/loadMtl.js
obj2gltf
called by 15
lib/obj2gltf.js
createGltf
called by 13
lib/createGltf.js
getTextureChannel
called by 12
lib/loadMtl.js

Shape

Function 133

Languages

TypeScript100%

Modules by API surface

lib/loadObj.js38 symbols
lib/createGltf.js25 symbols
lib/loadMtl.js23 symbols
lib/writeGltf.js10 symbols
gulpfile.js8 symbols
specs/lib/loadObjSpec.js7 symbols
lib/loadTexture.js7 symbols
lib/obj2gltf.js4 symbols
lib/readLines.js2 symbols
lib/ArrayStorage.js2 symbols
specs/lib/createGltfSpec.js1 symbols
lib/outsideDirectory.js1 symbols

Dependencies from manifests, versioned

bluebird3.7.2 · 1×
cesium1.86.1 · 1×
cloc2.8.0 · 1×
eslint8.0.1 · 1×
eslint-config-cesium10.0.1 · 1×
eslint-config-prettier9.0.0 · 1×
eslint-plugin-n16.1.0 · 1×
fs-extra11.0.0 · 1×
gulp4.0.2 · 1×
husky8.0.3 · 1×
jasmine5.0.0 · 1×
jasmine-spec-reporter7.0.0 · 1×

For agents

$ claude mcp add obj2gltf \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact