Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/gltf-loader/src/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 24 additions & 4 deletions packages/gltf-loader/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 => {
Expand Down