Forked from sketchpunk/Float32_Base64_Encoding_Decoding.js
Created
December 13, 2021 22:19
-
-
Save 8Observer8/e667b38b2b7f84b21885910e5405048b 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