GLTF Binary format base 64

Hello, I’m trying to add a 3D BIM element to my sqlite database but i have no idea how to. I have tested a revit plugin and i had my 3D elements in the following format : data:model/gltf-binary;base64,Z2xURgIAAACkAwAA+AIAAEpTT057ImFzc2V0Ijp7ImNvcHlyaWdodCI6IiIsImdlbmVyYXRvciI6IlNoYXJwR0xURiAxLjEuMCI…
I was wondering if it was the only way to store 3D elements in sqlite database and if so how can I convert my 3D element into a text format?
Thanks.

Hi @yousra_kadcha and welcome to the community!

This doesn’t answer your question directly, but I think trying to store 3D data in a SQL database you’ll hit hard limits and experience performance issues pretty quickly. You may be better off using a cloud storage solution like AWS S3 and just store the file ID in your database, that you can retrieve in your code via the S3 API.

3 Likes

@yousra_kadcha If you want a pure text format you could also try using the .babylon file format but your textures will still be separate files, unlike GLB which has the option to package everything up into a single compressed binary file.

1 Like

Hmmm, actually looking at the .babylon file format docs again, I see there’s a "base64String" option and looking at the Babylon.js Blender exporter I see there’s a corresponding option to inline textures as encoded strings, so my advice above about separate textures was incorrect :slight_smile:

1 Like

Thanks a lot @inteja for your quick replies. Actually I want to store my 3D data in an SQL database to be able to use them later in power BI (because it supports a limited types of data sources). I’ll check your suggestions and keep you updated.

Thanks again.

I have a Question:
Can I copy base64 string to txt file or html file and read it from “data:base64,/string.txt”?
Thank very much

My 2 cents here - I will never recommend storing binary data in an SQL database. it is not meant for it. I will also not store JSON-style data (i.e. a glTF file or the .babylon) in an sql database for the same reason. There are document-based databases for that purpose and file storing services to store binary data.

2 Likes

I appreciate @RaananW position, but there are many reason why tech optimal is not real world optimal, especially in business settings. Like:

  • different people responsible for desktop or server support from developers, and they do not like one off exception.
  • Some architecture czar says no.

I had a need to convert a couple different .env files, which are binary, on the fly in a development tool /scene based on UI button clicks. When loading a texture with data that is already in memory, it needs to be in base 64. I also needed to embed one of them into source code, so I had to be able to write it out as well in base64.

You could make a little tool page, no webgl required, to make a base46 text from a .GLB. You are on your own on how your SQL insert statement, though.

  • Add an html file with a tag similar
        <input type="file" id="envFile" multiple accept=".glb"  onchange="loadEnvFile()">

For callback rest of the process is below. I actually pulled out the loading of the texture as you do not need that part.

function loadEnvFile() {
    const envFileList = document.getElementById("envFile");
    const file = envFileList.files[0];
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {
        envToBase64File(reader.result);
    };
    reader.onerror = function (error) {
      console.log('Error: ', error);
    };
}

function envToBase64File(data) {
    const blob = new Blob ( [ data ], { type : 'text/strings;' } );
    const objectUrl = window.URL.createObjectURL(blob);

    const link = window.document.createElement('a');
    link.href = objectUrl;
    link.download = `base64.txt`;
    const click = document.createEvent('MouseEvents');
    click.initEvent('click', true, false);
    link.dispatchEvent(click);
}