Skip to content

Commit

Permalink
Option to disable zipping in XKT (incomplete) #125
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Jan 25, 2024
1 parent 58ded0c commit 67cb2ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
74 changes: 41 additions & 33 deletions src/XKTModel/writeXKTModelToArrayBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ const NUM_MATERIAL_ATTRIBUTES = 6;
* Writes an {@link XKTModel} to an {@link ArrayBuffer}.
*
* @param {XKTModel} xktModel The {@link XKTModel}.
* @param {String} metaModelJSON The metamodel JSON in an string.
* @param {String} metaModelJSON The metamodel JSON in a string.
* @param {Object} [stats] Collects statistics.
* @param {Object} options Options for how the XKT is written.
* @param {Boolean} [options.zip=true] ZIP the contents?
* @returns {ArrayBuffer} The {@link ArrayBuffer}.
*/
function writeXKTModelToArrayBuffer(xktModel, metaModelJSON, stats = {}) {
function writeXKTModelToArrayBuffer(xktModel, metaModelJSON, stats, options) {
const data = getModelData(xktModel, metaModelJSON, stats);
const deflatedData = deflateData(data, metaModelJSON);
const deflatedData = deflateData(data, metaModelJSON, options);
stats.texturesSize += deflatedData.textureData.byteLength;
const arrayBuffer = createArrayBuffer(deflatedData);
return arrayBuffer;
Expand Down Expand Up @@ -343,47 +345,53 @@ function getModelData(xktModel, metaModelDataStr, stats) {
return data;
}

function deflateData(data, metaModelJSON) {
function deflateData(data, metaModelJSON, options) {

function deflate(buffer) {
return (options.zip !== false) ? pako.deflate(buffer) : buffer;
}

let metaModelBytes;
if (metaModelJSON) {
const deflatedJSON = deflateJSON(metaModelJSON);
metaModelBytes = pako.deflate(deflatedJSON)
metaModelBytes = deflate(deflatedJSON)
} else {
const deflatedJSON = deflateJSON(data.metadata);
metaModelBytes = pako.deflate(deflatedJSON)
metaModelBytes = deflate(deflatedJSON)
}

return {
metadata: metaModelBytes,
textureData: pako.deflate(data.textureData.buffer),
eachTextureDataPortion: pako.deflate(data.eachTextureDataPortion.buffer),
eachTextureAttributes: pako.deflate(data.eachTextureAttributes.buffer),
positions: pako.deflate(data.positions.buffer),
normals: pako.deflate(data.normals.buffer),
colors: pako.deflate(data.colors.buffer),
uvs: pako.deflate(data.uvs.buffer),
indices: pako.deflate(data.indices.buffer),
edgeIndices: pako.deflate(data.edgeIndices.buffer),
eachTextureSetTextures: pako.deflate(data.eachTextureSetTextures.buffer),
matrices: pako.deflate(data.matrices.buffer),
reusedGeometriesDecodeMatrix: pako.deflate(data.reusedGeometriesDecodeMatrix.buffer),
eachGeometryPrimitiveType: pako.deflate(data.eachGeometryPrimitiveType.buffer),
eachGeometryPositionsPortion: pako.deflate(data.eachGeometryPositionsPortion.buffer),
eachGeometryNormalsPortion: pako.deflate(data.eachGeometryNormalsPortion.buffer),
eachGeometryColorsPortion: pako.deflate(data.eachGeometryColorsPortion.buffer),
eachGeometryUVsPortion: pako.deflate(data.eachGeometryUVsPortion.buffer),
eachGeometryIndicesPortion: pako.deflate(data.eachGeometryIndicesPortion.buffer),
eachGeometryEdgeIndicesPortion: pako.deflate(data.eachGeometryEdgeIndicesPortion.buffer),
eachMeshGeometriesPortion: pako.deflate(data.eachMeshGeometriesPortion.buffer),
eachMeshMatricesPortion: pako.deflate(data.eachMeshMatricesPortion.buffer),
eachMeshTextureSet: pako.deflate(data.eachMeshTextureSet.buffer),
eachMeshMaterialAttributes: pako.deflate(data.eachMeshMaterialAttributes.buffer),
eachEntityId: pako.deflate(JSON.stringify(data.eachEntityId)
textureData: deflate(data.textureData.buffer),
eachTextureDataPortion: deflate(data.eachTextureDataPortion.buffer),
eachTextureAttributes: deflate(data.eachTextureAttributes.buffer),
positions: deflate(data.positions.buffer),
normals: deflate(data.normals.buffer),
colors: deflate(data.colors.buffer),
uvs: deflate(data.uvs.buffer),
indices: deflate(data.indices.buffer),
edgeIndices: deflate(data.edgeIndices.buffer),
eachTextureSetTextures: deflate(data.eachTextureSetTextures.buffer),
matrices: deflate(data.matrices.buffer),
reusedGeometriesDecodeMatrix: deflate(data.reusedGeometriesDecodeMatrix.buffer),
eachGeometryPrimitiveType: deflate(data.eachGeometryPrimitiveType.buffer),
eachGeometryPositionsPortion: deflate(data.eachGeometryPositionsPortion.buffer),
eachGeometryNormalsPortion: deflate(data.eachGeometryNormalsPortion.buffer),
eachGeometryColorsPortion: deflate(data.eachGeometryColorsPortion.buffer),
eachGeometryUVsPortion: deflate(data.eachGeometryUVsPortion.buffer),
eachGeometryIndicesPortion: deflate(data.eachGeometryIndicesPortion.buffer),
eachGeometryEdgeIndicesPortion: deflate(data.eachGeometryEdgeIndicesPortion.buffer),
eachMeshGeometriesPortion: deflate(data.eachMeshGeometriesPortion.buffer),
eachMeshMatricesPortion: deflate(data.eachMeshMatricesPortion.buffer),
eachMeshTextureSet: deflate(data.eachMeshTextureSet.buffer),
eachMeshMaterialAttributes: deflate(data.eachMeshMaterialAttributes.buffer),
eachEntityId: deflate(JSON.stringify(data.eachEntityId)
.replace(/[\u007F-\uFFFF]/g, function (chr) { // Produce only ASCII-chars, so that the data can be inflated later
return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4)
})),
eachEntityMeshesPortion: pako.deflate(data.eachEntityMeshesPortion.buffer),
eachTileAABB: pako.deflate(data.eachTileAABB.buffer),
eachTileEntitiesPortion: pako.deflate(data.eachTileEntitiesPortion.buffer)
eachEntityMeshesPortion: deflate(data.eachEntityMeshesPortion.buffer),
eachTileAABB: deflate(data.eachTileAABB.buffer),
eachTileEntitiesPortion: deflate(data.eachTileEntitiesPortion.buffer)
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/convert2xkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function convert2xkt({

log("XKT document built OK. Writing to XKT file...");

const xktArrayBuffer = writeXKTModelToArrayBuffer(xktModel, metaModelJSON, stats);
const xktArrayBuffer = writeXKTModelToArrayBuffer(xktModel, metaModelJSON, stats, {zip: true});

const xktContent = Buffer.from(xktArrayBuffer);

Expand Down

0 comments on commit 67cb2ca

Please sign in to comment.