Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for saving 'gltf.extras' #200

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ As a convenience the PBR textures may be supplied directly to the command line.
|`--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|
|`--extrasPath`|Path to the json file that contains an object for storing application-specific data. It will be saved to the top-level of the glTF.|No|

## Build Instructions

Expand Down
15 changes: 15 additions & 0 deletions bin/obj2gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ const argv = yargs
describe : 'The glTF will be saved with the KHR_materials_unlit extension.',
type : 'boolean',
default : defaults.unlit
},
extrasPath : {
describe : 'Path to the json file that contains an object for storing application-specific data. It will be saved to the top-level of the glTF.',
type : 'string',
normalize : true,
coerce : function (p) {
if (p.length === 0) {
throw new Error('Input path must be a file name');
}
return path.resolve(p);
}
}
}).parse(args);

Expand Down Expand Up @@ -170,6 +181,10 @@ const options = {
outputDirectory : outputDirectory
};

if (defined(argv.extrasPath)) {
options.extras = JSON.parse(fsExtra.readFileSync(argv.extrasPath));
}

console.time('Total');

obj2gltf(objPath, options)
Expand Down
4 changes: 4 additions & 0 deletions lib/createGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ function createGltf(objData, options) {
gltf.extensionsRequired.push('KHR_materials_unlit');
}

if (options.extras) {
gltf.extras = options.extras;
}

return gltf;
}

Expand Down
1 change: 1 addition & 0 deletions lib/obj2gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = obj2gltf;
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
* @param {Writer} [options.writer] A callback function that writes files that are saved as separate resources.
* @param {String} [options.outputDirectory] Output directory for writing separate resources when options.writer is not defined.
* @param {Object} [options.extras] An object for storing application-specific data. It will be saved to the top-level of the glTF.
* @return {Promise} A promise that resolves to the glTF JSON or glb buffer.
*/
function obj2gltf(objPath, options) {
Expand Down
16 changes: 16 additions & 0 deletions specs/data/extras/dummy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"cities": [
"Bangkok",
"Beijing",
"Jarkata",
"Sydney",
"London",
"Tokyo",
"San Francisco",
"New York"
],
"coordinates": {
"x": 35.12,
"y": -21.49
}
}
13 changes: 13 additions & 0 deletions specs/lib/obj2gltfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const outputDirectory = 'output';

const textureUrl = 'specs/data/box-textured/cesium.png';

const extrasPath = 'specs/data/extras/dummy.json';

describe('obj2gltf', () => {
beforeEach(() => {
spyOn(fsExtra, 'outputFile').and.returnValue(Promise.resolve());
Expand Down Expand Up @@ -55,6 +57,17 @@ describe('obj2gltf', () => {
expect(fsExtra.outputFile.calls.count()).toBe(5); // Saves out .png and four .bin for positions, normals, uvs, and indices
});

it('converts obj to gltf with extras', async () => {
const extras = JSON.parse(fsExtra.readFileSync(extrasPath));
const options = {
extras : extras
};
const gltf = await obj2gltf(texturedObjPath, options);
expect(gltf).toBeDefined();
expect(gltf.images.length).toBe(1);
expect(gltf.extras).toBe(extras);
});

it('converts obj to glb with separate resources', async () => {
const options = {
separate : true,
Expand Down