obj2gltf/README.md

160 lines
7.1 KiB
Markdown
Raw Normal View History

2015-10-14 17:21:45 -04:00
# OBJ2GLTF
2015-10-18 23:33:54 -04:00
Convert OBJ assets to [glTF](https://www.khronos.org/gltf) 2.0.
2015-10-18 23:33:54 -04:00
## Getting Started
2016-07-20 14:48:49 -04:00
Install [Node.js](https://nodejs.org/en/) if you don't already have it, and then:
2015-10-18 23:33:54 -04:00
```
2016-07-20 14:48:49 -04:00
npm install --save obj2gltf
2015-10-18 23:33:54 -04:00
```
### Using obj2gltf as a command-line tool:
`node bin/obj2gltf.js -i model.obj`
`node bin/obj2gltf.js -i model.obj -o model.gltf`
`node bin/obj2gltf.js -i model.obj -o model.glb`
### Using obj2gltf as a library:
#### Converting an obj model to gltf:
```javascript
var obj2gltf = require('obj2gltf');
var fs = require('fs');
obj2gltf('model.obj')
.then(function(gltf) {
var data = Buffer.from(JSON.stringify(gltf));
fs.writeFileSync('model.gltf', data);
});
```
#### Converting an obj model to glb
2016-07-20 14:48:49 -04:00
```javascript
var obj2gltf = require('obj2gltf');
var fs = require('fs');
2016-07-20 14:48:49 -04:00
var options = {
binary : true
2016-07-20 14:48:49 -04:00
}
obj2gltf('model.obj', options)
.then(function(glb) {
fs.writeFileSync('model.glb', glb);
2016-07-22 16:17:27 -04:00
});
2016-07-20 14:48:49 -04:00
```
## Material types
2016-07-20 14:48:49 -04:00
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)
* Materials common (via `KHR_materials_common` extension)
If the material type is known in advance, it should be specified with either the `metallicRoughness`, `specularGlossiness`, or `materialsCommon` flag.
In general, if a model is authored with traditional diffuse, specular, and shininess textures the `materialsCommon` flag should be passed in.
The glTF will be saved with the `KHR_materials_common` extension and the Blinn-Phong shading model will be used.
However 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 | Materials common
--- | --- | --- | ---
Ka | occlusion value | occlusion value | ambient color
Ke | emissive color | emissive color | emissive color
Kd | base color | diffuse color | diffuse color
Ks | metallic value | specular color | specular color
Ns | roughness value | glossiness value | specular shininess value
d | alpha | alpha | alpha
Tr | 1.0 - alpha | 1.0 - alpha | 1.0 - alpha
map_Ka | occlusion texture | occlusion texture | ambient texture
map_Ke | emissive texture | emissive texture | emissive texture
map_Kd | base color texture | diffuse texture | diffuse texture
map_Ks | metallic texture | specular texture | specular texture
map_Ns | roughness texture | glossiness texture | specular shininess texture
map_Bump | normal texture | normal texture | normal texture
2016-07-20 14:48:49 -04:00
2015-10-19 17:38:55 -04:00
## Usage
2017-04-12 16:55:03 -04:00
### Command line flags:
2015-10-19 17:38:55 -04:00
|Flag|Description|Required|
|----|-----------|--------|
2017-04-10 17:57:56 -04:00
|`-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`|
2017-04-10 17:57:56 -04:00
|`-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`|
2017-05-04 15:39:01 -04:00
|`--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`|
|`--materialsCommon`|The glTF will be saved with the KHR_materials_common 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|
2015-10-19 17:38:55 -04:00
2017-01-06 11:36:44 -05:00
## Build Instructions
Run the tests:
```
npm run test
```
To run ESLint on the entire codebase, run:
2017-01-06 11:36:44 -05:00
```
npm run eslint
2017-01-06 11:36:44 -05:00
```
To run ESLint automatically when a file is saved, run the following and leave it open in a console window:
2017-01-06 11:36:44 -05:00
```
npm run eslint-watch
2017-01-06 11:36:44 -05:00
```
2017-04-12 16:55:03 -04:00
## Running Test Coverage
2017-01-06 11:36:44 -05:00
2017-06-20 13:24:03 -04:00
Coverage uses [nyc](https://github.com/istanbuljs/nyc). Run:
2017-01-06 11:36:44 -05:00
```
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.
2015-10-20 09:50:37 -04:00
## Contributions
2015-10-18 23:33:54 -04:00
Pull requests are appreciated. Please use the same [Contributor License Agreement (CLA)](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/CONTRIBUTING.md) used for [Cesium](http://cesiumjs.org/).
---
Developed by the Cesium team.
<p align="center">
2017-03-13 15:28:51 -04:00
<a href="http://cesiumjs.org/"><img src="doc/cesium.png" onerror="this.src='cesium.png'"/></a>
2015-10-18 23:33:54 -04:00
</p>