Add model version 3 serializer

This commit is contained in:
Dávid Szabó 2016-08-26 01:42:16 +02:00 committed by Julian Descottes
parent 7eab386122
commit 2962a838d3

View File

@ -1,40 +1,172 @@
(function () { (function () {
var ns = $.namespace('pskl.utils'); var ns = $.namespace('pskl.utils');
/**
*********
* META *
*********
* // Piskel
* [0] = model version
* [1] = width
* [2] = height
* [3] = fps
*
* // Descriptor
* [4] = name length
* [5] = description length
*
* // Layers
* [6] = layers count
* [layer data index start] = layer name length
* [layer data index start + 1] = opacity
* [layer data index start + 2] = frame count
* [layer data index start + 3] = base 64 png data url length (upper 16 bits)
* [layer data index start + 4] = base 64 png data url length (lower 16 bits)
*
*********
* DATA *
*********
* [7..name length-1] = name
* [name length..description length-1] = description
* [layer data index start + 4..layer name length-1] = layer name
* [layer name length..base 64 png data url length-1] = base 64 png data url
*
*
*/
ns.Serializer = { ns.Serializer = {
serializePiskel : function (piskel, expanded) { calculateRequiredBytes : function(piskel, framesData) {
var serializedLayers = piskel.getLayers().map(function (l) { var width = piskel.getWidth();
return pskl.utils.Serializer.serializeLayer(l, expanded); var height = piskel.getHeight();
}); var descriptorNameLength = piskel.getDescriptor().name.length;
return JSON.stringify({ var descriptorDescriptionLength = piskel.getDescriptor().description.length;
modelVersion : Constants.MODEL_VERSION, var layersLength = piskel.getLayers().length;
piskel : {
name : piskel.getDescriptor().name, var bytes = 0;
description : piskel.getDescriptor().description,
fps : pskl.app.piskelController.getFPS(), /********/
height : piskel.getHeight(), /* META */
width : piskel.getWidth(), /********/
layers : serializedLayers, // Piskel meta
expanded : expanded bytes += 4 * 2;
// Descriptor meta
bytes += 2 * 2;
// Layers meta
bytes += 1 * 2;
/********/
/* DATA */
/********/
// Descriptor name
bytes += descriptorNameLength * 2;
// Descriptor description
bytes += descriptorDescriptionLength * 2;
// Layers
for (var i = 0, layers = piskel.getLayers(); i < layers.length; i++) {
bytes += 3 * 2;
bytes += layers[i].name.length * 2;
bytes += framesData[i].length;
if (bytes%2 == 1) {
bytes++;
} }
}); }
return bytes;
}, },
serializeLayer : function (layer, expanded) { serializePiskel : function (piskel, expanded) {
// Render frames
var framesData = [];
for (var i = 0, layers = piskel.getLayers(); i < layers.length; i++) {
var renderer = new pskl.rendering.FramesheetRenderer(layers[i].getFrames());
var dataUri = atob(renderer.renderAsCanvas().toDataURL().split(',')[1]);
var dataUriLength = dataUri.length;
framesData.push({uri: dataUri, length: dataUriLength});
}
var bytes = pskl.utils.Serializer.calculateRequiredBytes(piskel, framesData);
var buffer = new ArrayBuffer(bytes);
var arr8 = new Uint8Array(buffer);
var arr16 = new Uint16Array(buffer);
var sub;
var width = piskel.getWidth();
var height = piskel.getHeight();
var descriptorName = piskel.getDescriptor().name;
var descriptorNameLength = descriptorName.length;
var descriptorDescription = piskel.getDescriptor().description;
var descriptorDescriptionLength = descriptorDescription.length;
/********/
/* META */
/********/
// Piskel meta
arr16[0] = Constants.MODEL_VERSION;
arr16[1] = width;
arr16[2] = height;
arr16[3] = pskl.app.piskelController.getFPS();
// Descriptor meta
arr16[4] = descriptorNameLength;
arr16[5] = descriptorDescriptionLength;
// Layers meta
arr16[6] = piskel.getLayers().length;
/********/
/* DATA */
/********/
// Descriptor name
for (var i = 0; i < descriptorNameLength; i++) {
arr16[7 + i] = descriptorName.charCodeAt(i);
}
// Descriptor description
for (var i = 0; i < descriptorDescriptionLength; i++) {
arr16[7 + descriptorNameLength + i] = descriptorDescription.charCodeAt(i);
}
// Layers
var layerStartIndex = 7 + descriptorNameLength + descriptorDescriptionLength;
for (var i = 0, layers = piskel.getLayers(); i < layers.length; i++) {
var layer = layers[i];
var frames = layer.getFrames(); var frames = layer.getFrames();
var layerToSerialize = {
name : layer.getName(), var layerName = layer.getName();
opacity : layer.getOpacity(), var layerNameLength = layerName.length;
frameCount : frames.length var opacity = layer.getOpacity();
}; var frameCount = frames.length;
if (expanded) {
layerToSerialize.grids = frames.map(function (f) {return Array.prototype.slice.call(f.pixels);}); var dataUri = framesData[i].uri
return layerToSerialize; var dataUriLength = framesData[i].length
} else {
var renderer = new pskl.rendering.FramesheetRenderer(frames); // Meta
layerToSerialize.base64PNG = renderer.renderAsCanvas().toDataURL(); arr16[layerStartIndex] = layerNameLength;
return JSON.stringify(layerToSerialize); arr16[layerStartIndex + 1] = Math.floor(opacity * 65535);
arr16[layerStartIndex + 2] = frameCount;
arr16[layerStartIndex + 3] = ((dataUriLength & 0xffff0000) >> 16) >>> 0; // Upper 16
arr16[layerStartIndex + 4] = ((dataUriLength & 0x0000ffff)) >>> 0; // Lower 16
// Name
for (var j = 0; j < layerNameLength; j++) {
arr16[layerStartIndex + 5 + j] = layerName.charCodeAt(j);
}
// Data URI
for (var j = 0; j < dataUriLength; j++) {
arr8[(layerStartIndex + 5 + layerNameLength) * 2 + j] = dataUri.charCodeAt(j);
}
layerStartIndex += Math.ceil(5 + layerNameLength + (dataUriLength / 2));
}
return buffer;
} }
} }
};
})(); })();