Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cheeseonamonkey/cdbf4f88d98a58a84ebb574a05b00683 to your computer and use it in GitHub Desktop.
Save cheeseonamonkey/cdbf4f88d98a58a84ebb574a05b00683 to your computer and use it in GitHub Desktop.
Encode Float32Array to base64 , then decode it back
let verts = new Float32Array( [ 0, 2, 0, -1, 0.2, 0, 1, 0.2, 0 ] );
let v = base64_test( verts );
function base64_test( fary ){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ENCODING TEST
console.log("Origin Data", fary );
let uint = new Uint8Array( fary.buffer );
console.log( "Convert F32 to Uint8 : Byte Length Test", fary.length * 4, uint.length );
let str = btoa( String.fromCharCode.apply( null, uint ) ); //btoa( String.fromCharCode( ...uint ) );
console.log( "Base64 of Uint8 Array : ", str.length, ":", str );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// DECODE TEST
let blob = atob( str );
console.log("Blob Length", blob.length );
console.log( blob );
let ary_buf = new ArrayBuffer( blob.length );
let dv = new DataView( ary_buf );
for( let i=0; i < blob.length; i++ ) dv.setUint8( i, blob.charCodeAt(i) );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// For WebGL Buffers, can skip Float32Array, just return ArrayBuffer is all thats needed.
let f32_ary = new Float32Array( ary_buf );
console.log( f32_ary );
return f32_ary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment