Last active
December 23, 2023 08:51
-
-
Save sketchpunk/6c60f6b78d4b66c729dcbf460ea06b42 to your computer and use it in GitHub Desktop.
Encode Float32Array to base64 , then decode it back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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