From bb314240d31d8e4e11d8a15574187af49bd2c75f Mon Sep 17 00:00:00 2001 From: wsw0108 Date: Fri, 6 Feb 2026 11:55:04 +0800 Subject: [PATCH] support load gltf json in buffer --- packages/gltf-loader/src/GLTFLoader.js | 11 ++++++++-- packages/gltf-loader/test/test.js | 28 ++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/gltf-loader/src/GLTFLoader.js b/packages/gltf-loader/src/GLTFLoader.js index 8f429a6160..a58f960ea8 100644 --- a/packages/gltf-loader/src/GLTFLoader.js +++ b/packages/gltf-loader/src/GLTFLoader.js @@ -40,8 +40,15 @@ export default class GLTFLoader { } this._fetchOptions = this.options.fetchOptions || {}; if (gltf.buffer instanceof ArrayBuffer) { - const { json, glbBuffer } = GLBReader.read(gltf.buffer, gltf.byteOffset, gltf.byteLength); - this._init(rootPath, json, glbBuffer); + const textDecoder = new TextDecoder(); + const magic = textDecoder.decode(new Uint8Array(gltf.buffer, 0, 4)); + if (magic === 'glTF') { + const { json, glbBuffer } = GLBReader.read(gltf.buffer, gltf.byteOffset, gltf.byteLength); + this._init(rootPath, json, glbBuffer); + } else { + const json = JSON.parse(textDecoder.decode(gltf.buffer)); + this._init(rootPath, json); + } } else { this._init(rootPath, gltf); } diff --git a/packages/gltf-loader/test/test.js b/packages/gltf-loader/test/test.js index eb633c140b..c16d7bd4fe 100644 --- a/packages/gltf-loader/test/test.js +++ b/packages/gltf-loader/test/test.js @@ -26,10 +26,10 @@ function uint8ArrayEqual(a, b) { } /* eslint-disable no-undef */ const decoders= { - 'image/crn': maptalksgl.transcoders.crn && maptalksgl.transcoders.crn(), - 'image/ktx2': maptalksgl.transcoders.ktx2 && maptalksgl.transcoders.ktx2(), - 'image/cttf': maptalksgl.transcoders.ktx2 && maptalksgl.transcoders.ktx2(), - 'draco': maptalksgl.transcoders.draco && maptalksgl.transcoders.draco() + 'image/crn': maptalks.transcoders.crn && maptalks.transcoders.crn(), + 'image/ktx2': maptalks.transcoders.ktx2 && maptalks.transcoders.ktx2(), + 'image/cttf': maptalks.transcoders.ktx2 && maptalks.transcoders.ktx2(), + 'draco': maptalks.transcoders.draco && maptalks.transcoders.draco() } describe('GLTF', () => { @@ -103,6 +103,26 @@ describe('GLTF', () => { }); }); + it('load gltf as buffer', done => { + const root = 'models/v2/BoxTextured'; + gltf.Ajax.getArrayBuffer(root + '/BoxTextured.gltf', {}).then(res => { + const loader = new gltf.GLTFLoader(root, {buffer: res.data, byteOffset: 0}); + loader.load().then(gltf => { + expect(gltf.scene).to.be.eql(0); + const nodes = gltf.scenes[gltf.scene].nodes; + + expect(nodes[0]).to.be.ok(); + const textures = gltf.textures; + expect(textures.length).to.be.eql(1); + const texture = textures[0]; + expect(texture.image.array.length).to.be.eql(262144); + expect(texture.image.array instanceof Uint8Array).to.be.ok(); + expect(texture.sampler).to.be.ok(); + done(); + }); + }); + }); + it('node has many child nodes', done => { const root = 'models/v2/scene'; gltf.Ajax.getJSON(root + '/scene.gltf', {}).then(json => {