Option for not combining buffers

The purpose of this option is to support simpler downstream
processing.
This commit is contained in:
DESKTOP-CHD43CN\ajb 2022-11-13 13:34:00 +11:00
parent 9b018ff696
commit a28e482437
4 changed files with 34 additions and 17 deletions

View File

@ -67,6 +67,11 @@ const argv = yargs
type: "boolean", type: "boolean",
default: defaults.separateTextures, default: defaults.separateTextures,
}, },
noCombineBuffers: {
describe: "Do not combine data buffers.",
type: "boolean",
default: defaults.noCombineBuffers,
},
checkTransparency: { checkTransparency: {
describe: describe:
"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.", "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.",
@ -205,6 +210,7 @@ const options = {
binary: binary, binary: binary,
separate: argv.separate, separate: argv.separate,
separateTextures: argv.separateTextures, separateTextures: argv.separateTextures,
noCombineBuffers: argv.noCombineBuffers,
checkTransparency: argv.checkTransparency, checkTransparency: argv.checkTransparency,
secure: argv.secure, secure: argv.secure,
packOcclusion: argv.packOcclusion, packOcclusion: argv.packOcclusion,

View File

@ -108,7 +108,7 @@ function createGltf(objData, options) {
}); });
} }
addBuffers(gltf, bufferState, name, options.separate); addBuffers(gltf, bufferState, name, options.separate, options.noCombineBuffers);
if (options.specularGlossiness) { if (options.specularGlossiness) {
gltf.extensionsUsed.push("KHR_materials_pbrSpecularGlossiness"); gltf.extensionsUsed.push("KHR_materials_pbrSpecularGlossiness");
@ -289,7 +289,7 @@ function addSeparateBuffers(gltf, bufferState, name) {
); );
} }
function addBuffers(gltf, bufferState, name, separate) { function addBuffers(gltf, bufferState, name, separate, noCombineBuffers) {
const buffers = bufferState.positionBuffers.concat( const buffers = bufferState.positionBuffers.concat(
bufferState.normalBuffers, bufferState.normalBuffers,
bufferState.uvBuffers, bufferState.uvBuffers,
@ -301,8 +301,8 @@ function addBuffers(gltf, bufferState, name, separate) {
buffersByteLength += buffers[i].length; buffersByteLength += buffers[i].length;
} }
if (separate && buffersByteLength > createGltf._getBufferMaxByteLength()) { if ((separate && buffersByteLength > createGltf._getBufferMaxByteLength()) || noCombineBuffers) {
// Don't combine buffers if the combined buffer will exceed the Node limit. // Don't combine buffers if the combined buffer will exceed the Node limit, or the user asked for it.
addSeparateBuffers(gltf, bufferState, name); addSeparateBuffers(gltf, bufferState, name);
} else { } else {
addCombinedBuffers(gltf, bufferState, name); addCombinedBuffers(gltf, bufferState, name);

View File

@ -20,6 +20,7 @@ module.exports = obj2gltf;
* @param {Boolean} [options.binary=false] Convert to binary glTF. * @param {Boolean} [options.binary=false] Convert to binary glTF.
* @param {Boolean} [options.separate=false] Write out separate buffer files and textures instead of embedding them in the glTF. * @param {Boolean} [options.separate=false] Write out separate buffer files and textures instead of embedding them in the glTF.
* @param {Boolean} [options.separateTextures=false] Write out separate textures only. * @param {Boolean} [options.separateTextures=false] Write out separate textures only.
* @param {Boolean} [options.noCombineBuffers=false] Do not combine buffers,
* @param {Boolean} [options.checkTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. * @param {Boolean} [options.checkTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
* @param {Boolean} [options.secure=false] Prevent the converter from reading textures or mtl files outside of the input obj directory. * @param {Boolean} [options.secure=false] Prevent the converter from reading textures or mtl files outside of the input obj directory.
* @param {Boolean} [options.packOcclusion=false] Pack the occlusion texture in the red channel of the metallic-roughness texture. * @param {Boolean} [options.packOcclusion=false] Pack the occlusion texture in the red channel of the metallic-roughness texture.
@ -50,6 +51,7 @@ function obj2gltf(objPath, options) {
options.separateTextures = options.separateTextures =
defaultValue(options.separateTextures, defaults.separateTextures) || defaultValue(options.separateTextures, defaults.separateTextures) ||
options.separate; options.separate;
options.noCombineBuffers = defaultValue(options.noCombineBuffers, defaults.noCombineBuffers);
options.checkTransparency = defaultValue( options.checkTransparency = defaultValue(
options.checkTransparency, options.checkTransparency,
defaults.checkTransparency defaults.checkTransparency
@ -173,6 +175,12 @@ obj2gltf.defaults = {
* @default false * @default false
*/ */
separateTextures: false, separateTextures: false,
/**
* Gets or sets whether to use different buffers for different meshes.
* @type Boolean
* @default false
*/
noCombineBuffers: false,
/** /**
* Gets or sets whether the converter will do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. * Gets or sets whether the converter will do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
* @type Boolean * @type Boolean

View File

@ -36,7 +36,7 @@ function writeGltf(gltf, options) {
if (separate) { if (separate) {
promises.push(writeSeparateBuffers(gltf, options)); promises.push(writeSeparateBuffers(gltf, options));
} else if (!binary) { } else if (!binary) {
writeEmbeddedBuffer(gltf); writeEmbeddedBuffers(gltf);
} }
const binaryBuffer = gltf.buffers[0].extras._obj2gltf.source; const binaryBuffer = gltf.buffers[0].extras._obj2gltf.source;
@ -158,20 +158,23 @@ function writeSeparateTextures(gltf, options) {
); );
} }
function writeEmbeddedBuffer(gltf) { function writeEmbeddedBuffers(gltf) {
const buffer = gltf.buffers[0]; const buffersLength = gltf.buffers.length;
const source = buffer.extras._obj2gltf.source; for (let i = 0; i < buffersLength; ++i) {
const buffer = gltf.buffers[i];
const source = buffer.extras._obj2gltf.source;
// Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266 // Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266
if (source.length > 201326580) { if (source.length > 201326580) {
throw new RuntimeError( throw new RuntimeError(
"Buffer is too large to embed in the glTF. Use the --separate flag instead." "Buffer is too large to embed in the glTF. Use the --separate flag instead."
); );
}
buffer.uri = `data:application/octet-stream;base64,${source.toString(
"base64"
)}`;
} }
buffer.uri = `data:application/octet-stream;base64,${source.toString(
"base64"
)}`;
} }
function writeEmbeddedTextures(gltf) { function writeEmbeddedTextures(gltf) {