Skip to content

Commit

Permalink
Merge branch 'node16' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Brakebein committed Oct 4, 2022
2 parents 93463e5 + 7533bc3 commit 875de15
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 21 deletions.
22 changes: 13 additions & 9 deletions build/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ var node_url = require('node:url');
var node_path = require('node:path');
var three = require('three');
var promises = require('node:fs/promises');
var fetch = require('node-fetch');
var sharp = require('sharp');
var node_worker_threads = require('node:worker_threads');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
var sharp__default = /*#__PURE__*/_interopDefaultLegacy(sharp);

const dom = new jsdom.JSDOM().window;
Expand Down Expand Up @@ -86,11 +88,11 @@ class FileLoader extends three.Loader {
promise = Promise.resolve(buffer.buffer);
}
else {
const req = new Request(url, {
headers: new Headers(this.requestHeader),
const req = new fetch.Request(url, {
headers: new fetch.Headers(this.requestHeader),
credentials: this.withCredentials ? 'include' : 'same-origin',
});
promise = fetch(req)
promise = fetch__default["default"](req)
.then(response => {
if (response.status === 200 || response.status === 0) {
if (response.status === 0) {
Expand Down Expand Up @@ -201,11 +203,11 @@ class ImageLoader extends three.Loader {
return sharp__default["default"](imageBuffer);
}
else if (/^https?:\/\//.test(url)) {
const req = new Request(url, {
headers: new Headers(this.requestHeader),
const req = new fetch.Request(url, {
headers: new fetch.Headers(this.requestHeader),
credentials: this.withCredentials ? 'include' : 'same-origin',
});
const response = await fetch(req);
const response = await fetch__default["default"](req);
const buffer = Buffer.from(await response.arrayBuffer());
return sharp__default["default"](buffer);
}
Expand Down Expand Up @@ -947,6 +949,7 @@ class GLTFDracoMeshCompressionExtension {
const gltfAttributeMap = primitive.extensions[this.name].attributes;
const threeAttributeMap = {};
const attributeNormalizedMap = {};
const attributeTypeMap = {};
for (const attributeName in gltfAttributeMap) {
const threeAttributeName = ATTRIBUTES[attributeName] || attributeName.toLowerCase();
threeAttributeMap[threeAttributeName] = gltfAttributeMap[attributeName];
Expand All @@ -955,7 +958,8 @@ class GLTFDracoMeshCompressionExtension {
const threeAttributeName = ATTRIBUTES[attributeName] || attributeName.toLowerCase();
if (gltfAttributeMap[attributeName] !== undefined) {
const accessorDef = json.accessors[primitive.attributes[attributeName]];
WEBGL_COMPONENT_TYPES[accessorDef.componentType];
const componentType = WEBGL_COMPONENT_TYPES[accessorDef.componentType];
attributeTypeMap[threeAttributeName] = componentType.name;
attributeNormalizedMap[threeAttributeName] = accessorDef.normalized === true;
}
}
Expand All @@ -969,7 +973,7 @@ class GLTFDracoMeshCompressionExtension {
attribute.normalized = normalized;
}
resolve(geometry);
}, threeAttributeMap);
}, threeAttributeMap, attributeTypeMap);
});
});
}
Expand Down Expand Up @@ -2172,7 +2176,7 @@ class GLTFParser {
const channel = animationDef.channels[i];
const sampler = animationDef.samplers[channel.sampler];
const target = channel.target;
const name = target.node !== undefined ? target.node : target.id;
const name = target.node;
const input = animationDef.parameters !== undefined ? animationDef.parameters[sampler.input] : sampler.input;
const output = animationDef.parameters !== undefined ? animationDef.parameters[sampler.output] : sampler.output;
pendingNodes.push(this.getDependency('node', name));
Expand Down
2 changes: 1 addition & 1 deletion build/index.cjs.map

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/index.js.map

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-three-gltf",
"version": "1.2.0",
"version": "1.2.0-node16",
"description": "Use three.js GLTFLoader in a Node.js environment",
"exports": {
"import": "./build/index.js",
Expand All @@ -9,7 +9,7 @@
"types": "types/index.d.ts",
"type": "module",
"engines": {
"node": ">=18.0.0"
"node": ">=16.0.0"
},
"scripts": {
"cleanup:build": "rimraf build",
Expand Down Expand Up @@ -41,13 +41,15 @@
"homepage": "https://github.com/Brakebein/node-three-gltf#readme",
"dependencies": {
"jsdom": "^20.0.0",
"node-fetch": "^2.6.7",
"sharp": "^0.31.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.4",
"@types/draco3dgltf": "^1.4.0",
"@types/jsdom": "^20.0.0",
"@types/node-fetch": "^2.6.2",
"@types/sharp": "^0.31.0",
"@types/three": "^0.137.0",
"ava": "^4.3.1",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nodeResolve from '@rollup/plugin-node-resolve';
import copy from 'rollup-plugin-copy';

export default {
external: ['jsdom', 'sharp', 'three'],
external: ['jsdom', 'node-fetch', 'sharp', 'three'],
input: 'src/index.ts',
plugins: [
nodeResolve(),
Expand Down
1 change: 1 addition & 0 deletions src/FileLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile } from 'node:fs/promises';
import { Loader, LoadingManager } from 'three';
import fetch, { Headers, Request } from 'node-fetch';

const loading: {
[key: string]: {
Expand Down
6 changes: 3 additions & 3 deletions src/GLTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ class GLTFDracoMeshCompressionExtension {
const accessorDef = json.accessors[ primitive.attributes[ attributeName ] ];
const componentType = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ];

attributeTypeMap[ threeAttributeName ] = componentType;
attributeTypeMap[ threeAttributeName ] = componentType.name;
attributeNormalizedMap[ threeAttributeName ] = accessorDef.normalized === true;

}
Expand All @@ -1451,7 +1451,7 @@ class GLTFDracoMeshCompressionExtension {

resolve( geometry );

}, threeAttributeMap );
}, threeAttributeMap, attributeTypeMap );

} );

Expand Down Expand Up @@ -3583,7 +3583,7 @@ class GLTFParser {
const channel = animationDef.channels[ i ];
const sampler = animationDef.samplers[ channel.sampler ];
const target = channel.target;
const name = target.node !== undefined ? target.node : target.id; // NOTE: target.id is deprecated.
const name = target.node;
const input = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.input ] : sampler.input;
const output = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.output ] : sampler.output;

Expand Down
3 changes: 2 additions & 1 deletion src/ImageLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolveObjectURL } from 'node:buffer';
import { Cache, Loader, LoadingManager } from 'three';
import sharp from 'sharp';
import fetch, { Headers, Request } from 'node-fetch';

export class ImageLoader extends Loader {

Expand Down Expand Up @@ -45,7 +46,7 @@ export class ImageLoader extends Loader {
const imageBuffer = Buffer.from(await blob.arrayBuffer());
return sharp(imageBuffer);

} else if (/^data:/.test(url)) {;
} else if (/^data:/.test(url)) {

const base64 = url.split(';base64,').pop();
const imageBuffer = Buffer.from(base64, 'base64');
Expand Down
Binary file added test/LittlestTokyo.glb
Binary file not shown.
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ test('load draco-compressed dgm.gltf', async (t) => {
});

});

test('load gltf with animations', async (t) => {

const gltf = await loadGltf('test/LittlestTokyo.glb');

gltf.scene.traverse((child) => {
if (child instanceof Mesh) {
t.truthy(child.geometry instanceof BufferGeometry, 'check BufferGeometry');
}
});

});

0 comments on commit 875de15

Please sign in to comment.