obj2gltf/lib/writeGltf.js

207 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-08-02 11:31:59 -04:00
"use strict";
const Cesium = require("cesium");
const mime = require("mime");
const PNG = require("pngjs").PNG;
const Promise = require("bluebird");
const getBufferPadded = require("./getBufferPadded");
const gltfToGlb = require("./gltfToGlb");
2019-02-05 20:59:09 -05:00
const defined = Cesium.defined;
const RuntimeError = Cesium.RuntimeError;
2017-04-10 17:57:56 -04:00
module.exports = writeGltf;
/**
* Write glTF resources as embedded data uris or external files.
*
* @param {Object} gltf The glTF asset.
* @param {Object} options The options object passed along from lib/obj2gltf.js
* @returns {Promise} A promise that resolves to the glTF JSON or glb buffer.
*
* @private
*/
function writeGltf(gltf, options) {
2021-08-02 11:31:59 -04:00
return encodeTextures(gltf).then(function () {
const binary = options.binary;
const separate = options.separate;
const separateTextures = options.separateTextures;
const promises = [];
if (separateTextures) {
promises.push(writeSeparateTextures(gltf, options));
} else {
writeEmbeddedTextures(gltf);
}
if (separate) {
promises.push(writeSeparateBuffers(gltf, options));
} else if (!binary) {
writeEmbeddedBuffer(gltf);
}
const binaryBuffer = gltf.buffers[0].extras._obj2gltf.source;
return Promise.all(promises).then(function () {
deleteExtras(gltf);
removeEmpty(gltf);
if (binary) {
return gltfToGlb(gltf, binaryBuffer);
}
return gltf;
});
});
}
function encodePng(texture) {
2021-08-02 11:31:59 -04:00
// Constants defined by pngjs
const rgbColorType = 2;
const rgbaColorType = 6;
const png = new PNG({
width: texture.width,
height: texture.height,
colorType: texture.transparent ? rgbaColorType : rgbColorType,
inputColorType: rgbaColorType,
inputHasAlpha: true,
});
png.data = texture.pixels;
return new Promise(function (resolve, reject) {
const chunks = [];
const stream = png.pack();
stream.on("data", function (chunk) {
chunks.push(chunk);
});
2021-08-02 11:31:59 -04:00
stream.on("end", function () {
resolve(Buffer.concat(chunks));
});
2021-08-02 11:31:59 -04:00
stream.on("error", reject);
});
}
2017-04-20 10:07:01 -04:00
function encodeTexture(texture) {
2021-08-02 11:31:59 -04:00
if (
!defined(texture.source) &&
defined(texture.pixels) &&
texture.extension === ".png"
) {
return encodePng(texture).then(function (encoded) {
texture.source = encoded;
});
}
}
function encodeTextures(gltf) {
2021-08-02 11:31:59 -04:00
// Dynamically generated PBR textures need to be encoded to png prior to being saved
const encodePromises = [];
const images = gltf.images;
const length = images.length;
for (let i = 0; i < length; ++i) {
encodePromises.push(encodeTexture(images[i].extras._obj2gltf));
}
return Promise.all(encodePromises);
}
function deleteExtras(gltf) {
2021-08-02 11:31:59 -04:00
const buffers = gltf.buffers;
const buffersLength = buffers.length;
for (let i = 0; i < buffersLength; ++i) {
delete buffers[i].extras;
}
const images = gltf.images;
const imagesLength = images.length;
for (let i = 0; i < imagesLength; ++i) {
delete images[i].extras;
}
}
2017-05-04 17:58:13 -04:00
function removeEmpty(json) {
2021-08-02 11:31:59 -04:00
Object.keys(json).forEach(function (key) {
if (
!defined(json[key]) ||
(Array.isArray(json[key]) && json[key].length === 0)
) {
delete json[key]; // Delete values that are undefined or []
} else if (typeof json[key] === "object") {
removeEmpty(json[key]);
}
});
2017-05-04 17:58:13 -04:00
}
function writeSeparateBuffers(gltf, options) {
2021-08-02 11:31:59 -04:00
const buffers = gltf.buffers;
return Promise.map(
buffers,
function (buffer) {
const source = buffer.extras._obj2gltf.source;
2022-05-25 10:18:31 -04:00
const bufferUri = `${buffer.name}.bin`;
2021-08-02 11:31:59 -04:00
buffer.uri = bufferUri;
return options.writer(bufferUri, source);
},
{ concurrency: 10 },
2021-08-02 11:31:59 -04:00
);
}
function writeSeparateTextures(gltf, options) {
2021-08-02 11:31:59 -04:00
const images = gltf.images;
return Promise.map(
images,
function (image) {
const texture = image.extras._obj2gltf;
const imageUri = image.name + texture.extension;
image.uri = imageUri;
return options.writer(imageUri, texture.source);
},
{ concurrency: 10 },
2021-08-02 11:31:59 -04:00
);
}
function writeEmbeddedBuffer(gltf) {
2021-08-02 11:31:59 -04:00
const buffer = gltf.buffers[0];
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
if (source.length > 201326580) {
throw new RuntimeError(
"Buffer is too large to embed in the glTF. Use the --separate flag instead.",
2021-08-02 11:31:59 -04:00
);
}
2022-05-25 10:18:31 -04:00
buffer.uri = `data:application/octet-stream;base64,${source.toString(
"base64",
2022-05-25 10:18:31 -04:00
)}`;
}
function writeEmbeddedTextures(gltf) {
2021-08-02 11:31:59 -04:00
const buffer = gltf.buffers[0];
const bufferExtras = buffer.extras._obj2gltf;
const bufferSource = bufferExtras.source;
const images = gltf.images;
const imagesLength = images.length;
const sources = [bufferSource];
let byteOffset = bufferSource.length;
for (let i = 0; i < imagesLength; ++i) {
const image = images[i];
const texture = image.extras._obj2gltf;
const textureSource = texture.source;
const textureByteLength = textureSource.length;
image.mimeType = mime.getType(texture.extension);
image.bufferView = gltf.bufferViews.length;
gltf.bufferViews.push({
buffer: 0,
byteOffset: byteOffset,
byteLength: textureByteLength,
});
byteOffset += textureByteLength;
sources.push(textureSource);
}
2017-07-19 17:56:24 -04:00
2021-08-02 11:31:59 -04:00
const source = getBufferPadded(Buffer.concat(sources));
bufferExtras.source = source;
buffer.byteLength = source.length;
}