Skip to content

Instantly share code, notes, and snippets.

@pushmatrix
Last active June 10, 2020 13:25
Show Gist options
  • Save pushmatrix/5caa6bc29e800c7a57fda8f5b14d173c to your computer and use it in GitHub Desktop.
Save pushmatrix/5caa6bc29e800c7a57fda8f5b14d173c to your computer and use it in GitHub Desktop.
Create a cube.usdz of any size in the browser!
function createUSDZ(length, width, height) {
// Bytes corresponding to a 1m x 1m x 1m cube
const bytes = [80,75,3,4,10,0,0,0,0,0,121,71,202,80,241,6,140,202,131,11,0,0,131,11,0,0,10,0,24,0,99,117,98,101,50,46,117,115,100,99,134,25,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,88,82,45,85,83,68,67,0,7,0,0,0,0,0,0,187,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,4,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,1,0,0,0,0,0,10,64,1,0,0,0,0,0,0,0,2,0,0,0,8,0,0,0,0,0,0,0,3,0,0,0,0,0,10,64,2,0,0,0,0,0,0,0,17,0,0,0,18,0,0,0,3,0,0,0,0,0,0,0,20,0,0,0,21,0,0,0,22,0,0,0,3,0,0,0,0,0,0,0,24,0,0,0,25,0,0,0,26,0,0,0,10,0,0,0,0,0,0,0,28,0,0,0,29,0,0,0,30,0,0,0,31,0,0,0,32,0,0,0,33,0,0,0,34,0,0,0,35,0,0,0,36,0,0,0,25,0,0,0,1,0,0,0,0,0,0,0,37,0,0,0,1,0,0,0,0,0,0,0,38,0,0,0,1,0,0,0,0,0,0,0,39,0,0,0,4,0,0,0,0,0,0,0,41,0,0,0,39,0,0,0,42,0,0,0,43,0,0,0,12,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,3,0,0,0,36,0,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0,114,254,255,255,255,21,81,17,3,0,90,0,1,1,3,3,4,0,80,3,3,1,1,3,205,204,76,63,205,204,76,63,205,204,76,63,3,1,0,0,0,0,0,0,0,7,0,0,0,3,1,0,0,0,0,0,0,0,16,0,0,0,0,24,0,0,0,0,0,0,0,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,63,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,191,0,0,0,63,0,0,0,63,0,0,0,191,24,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,128,0,0,0,0,0,0,128,191,0,0,0,128,0,0,0,0,0,0,128,191,0,0,0,128,0,0,0,0,0,0,128,191,0,0,0,128,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,0,0,0,0,0,0,0,0,0,0,128,191,24,0,0,0,0,0,0,0,0,0,32,63,0,0,128,62,0,0,192,62,0,0,0,0,0,0,32,63,0,0,0,0,0,0,192,62,0,0,128,62,0,0,32,63,0,0,128,62,0,0,192,62,0,0,0,63,0,0,192,62,0,0,128,62,0,0,32,63,0,0,0,63,0,0,32,63,0,0,0,63,0,0,192,62,0,0,64,63,0,0,192,62,0,0,0,63,0,0,32,63,0,0,64,63,0,0,32,63,0,0,128,63,0,0,192,62,0,0,64,63,0,0,32,63,0,0,64,63,0,0,192,62,0,0,128,63,0,0,192,62,0,0,0,63,0,0,0,62,0,0,64,63,0,0,0,62,0,0,0,63,0,0,192,62,0,0,64,63,0,0,96,63,0,0,0,63,0,0,32,63,0,0,64,63,0,0,32,63,0,0,0,63,0,0,96,63,0,0,64,63,9,146,247,62,111,122,172,62,102,182,85,190,183,205,71,63,50,34,45,62,96,129,65,63,12,90,139,190,158,41,18,63,154,153,217,63,154,153,217,63,154,153,217,63,0,0,0,64,129,111,29,64,0,0,0,0,79,213,19,64,0,0,0,128,2,180,27,64,0,0,0,64,19,78,16,64,0,0,0,0,142,157,23,64,0,0,0,224,86,22,240,191,0,0,0,0,2,0,0,0,0,0,0,0,24,0,0,0,26,0,0,0,1,0,0,0,0,0,0,0,36,0,0,0,64,0,0,0,0,0,0,0,157,2,0,0,0,0,0,0,67,2,0,0,0,0,0,0,0,244,211,0,117,112,65,120,105,115,0,89,0,112,114,105,109,67,104,105,108,100,114,101,110,0,99,117,98,101,50,0,100,101,102,97,117,108,116,80,114,105,109,0,109,101,116,101,114,115,80,101,114,85,110,105,116,0,99,117,115,116,111,109,76,97,121,101,114,68,97,116,97,0,99,114,101,97,116,111,114,0,117,115,100,122,99,111,110,118,101,114,116,32,112,114,101,118,105,101,119,32,48,46,54,50,0,115,112,101,99,105,102,105,101,114,0,116,121,112,101,78,97,109,101,0,88,102,111,114,109,0,97,115,115,101,116,73,110,102,111,0,110,97,109,101,0,107,105,110,100,0,99,111,109,112,111,110,101,110,116,0,77,97,116,101,114,105,97,108,115,0,71,101,111,109,0,83,99,111,112,101,0,67,117,98,101,0,76,105,103,104,116,0,67,97,109,101,114,97,0,112,114,111,112,101,114,116,105,101,115,0,120,102,111,114,109,79,112,58,116,114,97,110,115,108,97,116,101,18,0,84,79,114,100,101,114,13,0,65,58,111,114,105,96,0,161,101,115,104,0,112,111,105,110,116,115,1,1,118,118,97,114,115,58,115,116,12,0,248,9,110,111,114,109,97,108,115,0,102,97,99,101,86,101,114,116,101,120,73,110,100,105,99,101,18,0,48,67,111,117,64,0,243,4,115,117,98,100,105,118,105,115,105,111,110,83,99,104,101,109,101,0,109,190,0,245,5,58,98,105,110,100,105,110,103,0,100,111,117,98,108,101,83,105,100,101,100,138,0,85,115,99,97,108,101,233,0,64,0,115,117,114,108,0,48,83,104,97,175,0,131,111,117,116,112,117,116,115,58,22,0,19,0,23,0,161,105,110,102,111,58,105,100,0,105,110,30,0,176,100,105,102,102,117,115,101,67,111,108,111,28,0,1,20,0,244,11,114,111,117,103,104,110,101,115,115,0,98,111,111,108,0,118,97,114,105,97,98,105,108,105,116,121,215,1,242,2,0,105,110,116,91,93,0,116,111,107,101,110,0,85,115,100,80,176,1,18,83,129,0,32,0,99,78,0,242,16,51,102,0,102,108,111,97,116,0,116,97,114,103,101,116,80,97,116,104,115,0,99,111,110,110,101,99,116,105,111,110,16,0,1,64,1,82,51,102,91,93,0,46,1,1,11,0,144,105,110,116,101,114,112,111,108,97,40,0,16,0,17,2,49,101,120,0,45,1,66,111,114,100,50,45,0,115,110,101,0,113,117,97,116,100,0,19,51,31,1,160,51,0,116,111,107,101,110,91,93,0,4,0,0,0,0,0,0,0,8,0,0,0,9,0,0,0,14,0,0,0,4,0,0,0,54,0,0,0,0,0,0,0,62,0,0,0,0,0,0,0,0,81,221,255,255,255,85,1,0,241,9,20,17,85,81,20,69,20,5,1,2,2,1,1,3,1,2,2,244,8,248,20,244,12,236,6,0,240,8,244,34,1,35,0,35,35,35,6,1,214,35,45,246,35,0,35,0,35,35,0,35,0,36,1,0,0,0,0,0,0,0,32,2,0,1,0,48,11,64,88,7,0,65,0,41,0,4,8,0,177,11,64,0,0,128,63,0,0,9,64,100,16,0,17,31,6,0,80,0,0,42,64,12,7,0,65,0,11,64,128,8,0,49,31,0,16,8,0,49,11,64,156,8,0,49,41,0,19,8,0,49,11,64,172,8,0,51,41,0,192,8,0,17,27,8,0,49,11,64,212,8,0,64,41,0,4,1,9,0,48,41,0,37,7,0,67,0,11,64,16,16,0,19,28,8,0,19,40,24,0,19,40,16,0,19,44,16,0,1,15,0,35,0,44,8,0,51,1,64,47,24,0,2,17,0,49,3,128,120,25,0,51,3,160,48,24,0,19,49,8,0,19,50,8,0,17,161,32,0,51,24,0,51,16,0,145,205,204,204,62,0,0,8,64,173,24,0,51,34,0,186,8,0,19,54,32,0,17,200,16,0,51,24,128,55,16,0,19,57,8,0,17,240,57,1,51,24,128,58,16,0,17,24,57,1,51,20,128,59,16,0,19,60,8,0,17,224,24,0,51,17,0,240,8,0,19,61,24,0,32,0,5,9,0,48,24,0,62,7,0,65,0,11,64,12,16,0,51,23,0,36,8,0,19,63,24,0,17,64,16,0,160,11,128,80,5,0,0,0,0,11,128,103,0,0,0,0,0,0,0,111,0,0,0,0,0,0,0,0,240,93,1,0,0,0,1,20,64,69,81,69,85,5,21,5,69,85,85,20,85,85,69,65,85,85,81,21,69,85,85,21,0,251,6,246,6,5,244,6,6,243,6,8,241,6,5,5,240,6,11,237,6,14,235,22,232,25,230,25,2,229,28,251,6,227,30,225,32,223,23,11,222,28,228,28,7,221,36,219,38,216,41,254,3,214,28,251,20,213,44,211,44,2,210,47,208,49,206,49,2,205,52,227,30,203,52,227,31,202,30,0,0,0,0,0,0,0,30,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,240,14,1,0,0,0,1,85,65,1,85,21,4,1,0,19,3,2,233,5,4,3,3,4,233,18,3,3,233,4,2,41,0,0,0,0,0,0,0,0,240,23,255,255,255,255,85,69,85,85,85,81,5,5,0,4,14,4,208,2,45,201,3,1,253,6,254,1,252,253,11,46,209,2,42,20,1,177,4,0,30,0,0,0,0,0,0,0,0,240,12,0,0,0,0,81,81,1,0,84,20,5,1,255,21,240,252,254,13,245,254,1,1,254,1,6,251,254,30,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,208,1,0,0,0,1,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0,0,0,240,15,3,0,0,0,85,85,21,4,4,21,0,5,0,6,6,4,4,252,8,4,5,4,4,4,2,4,4,4,4,252,20,0,0,0,0,0,0,0,0,240,2,0,0,0,0,5,0,4,64,1,0,0,0,7,255,251,7,249,6,0,0,0,0,0,0,0,84,79,75,69,78,83,0,0,0,0,0,0,0,0,0,0,92,5,0,0,0,0,0,0,91,2,0,0,0,0,0,0,83,84,82,73,78,71,83,0,0,0,0,0,0,0,0,0,183,7,0,0,0,0,0,0,24,0,0,0,0,0,0,0,70,73,69,76,68,83,0,0,0,0,0,0,0,0,0,0,207,7,0,0,0,0,0,0,122,1,0,0,0,0,0,0,70,73,69,76,68,83,69,84,83,0,0,0,0,0,0,0,73,9,0,0,0,0,0,0,127,0,0,0,0,0,0,0,80,65,84,72,83,0,0,0,0,0,0,0,0,0,0,0,200,9,0,0,0,0,0,0,143,0,0,0,0,0,0,0,83,80,69,67,83,0,0,0,0,0,0,0,0,0,0,0,87,10,0,0,0,0,0,0,100,0,0,0,0,0,0,0,80,75,1,2,0,0,10,0,0,0,0,0,121,71,202,80,241,6,140,202,131,11,0,0,131,11,0,0,10,0,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,99,117,98,101,50,46,117,115,100,99,134,25,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,75,5,6,0,0,0,0,1,0,1,0,80,0,0,0,195,11,0,0,0,0];
// Offset where the x, y, z scale properties are set
const byteOffset = 1344;
// Create a buffer & data view to read / write to it
let buffer = new ArrayBuffer(bytes.length);
let view = new DataView(buffer);
// Populate the buffer with the usdz bytes
bytes.forEach((b,i) => {
view.setUint8(i, b);
});
// Set the scale for the x, y, z axis
view.setFloat32(byteOffset, length, true);
view.setFloat32(byteOffset + 4, width, true);
view.setFloat32(byteOffset + 8, height, true);
return arrayBufferToBase64(buffer);
}
function arrayBufferToBase64(buffer) {
let binary = '';
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
function launchQuickLook(length, width, height) {
let usdz = createUSDZ(length, width, height);
let link = document.createElement("a");
link.appendChild(document.createElement('img'));
link.rel = "ar";
link.download = "cube.usdz";
link.href = "data:model/usd;base64," + usdz;
link.click();
}
// Example usage
launchQuickLook(1,2,1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment