NullEngine: GLB export with textures workaround

Hi

I’m using NullEngine for server-side project in Node.js and exporting a scene to GLB via GLTF2Export.GLBAsync. The textures are loaded from local GLB files via the glTF loader.

The Problem:

Exporting fails with:

Error: Failed to read pixels from texture MyTexture (Base Color).
at GetTextureDataAsync (textureTools.js)
at GLTFMaterialExporter._exportTextureImageAsync (glTFMaterialExporter.ts)

I know this is because the nullNullEngine does not have a WebGL context and can not read readPixels.

However. I found a workaround like this. I’m monkey patching the createTexture function

const _origCreateTexture = engine.createTexture.bind(engine);
engine.createTexture = function (
    urlArg: string,
    noMipmap: boolean,
    invertY: boolean,
    scene: any,
    samplingMode?: number,
    onLoad?: ((texture: InternalTexture) => void) | null,
    onError?: any,
    buffer?: any,
    ...rest: any[]
): InternalTexture {
    const texture = _origCreateTexture(urlArg, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer, ...rest);
    if (buffer) {
        texture._buffer = buffer;
    }
    return texture;
};

So it seems that there its just missing to store the buffer there.

Would it make sense to add texture._buffer = buffer to NullEngine.createTexture()? Since NullEngine can never read pixels from the GPU and this would make sense in this use case I think.

Thank you

1 Like

I have no problem with that

cc @sebavan

Love it, do you mind creating a PR ?

yes I can do that :slight_smile:

Here is the PR: NullEngine: createTexture: added buffer to internal texture by bkargerooom · Pull Request #17952 · BabylonJS/Babylon.js · GitHub

2 Likes