@gltf-transform/core
Advanced tools
Comparing version 0.12.5 to 0.12.6
@@ -14,2 +14,3 @@ import { JSONDocument } from '../json-document'; | ||
buffers: Buffer[]; | ||
bufferViews: Uint8Array[]; | ||
bufferViewBuffers: Buffer[]; | ||
@@ -16,0 +17,0 @@ accessors: Accessor[]; |
{ | ||
"name": "@gltf-transform/core", | ||
"version": "0.12.5", | ||
"version": "0.12.6", | ||
"repository": "github:donmccurdy/glTF-Transform", | ||
@@ -43,3 +43,3 @@ "homepage": "https://gltf-transform.donmccurdy.com/", | ||
}, | ||
"gitHead": "5c6625390f673b95d6e8948f5c9bd0d1f6dd172c" | ||
"gitHead": "c5b4316ed2f5b1f1c3592aa73738e27e25a76e71" | ||
} |
@@ -39,2 +39,3 @@ import { Graph } from './graph'; | ||
this._disposed = true; | ||
this.graph.emit('dispose', this); | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -112,3 +112,3 @@ import { Format } from '../constants'; | ||
this._readResourcesExternal(jsonDoc, this._path.dirname(uri)); | ||
this._readResourcesInternal(jsonDoc, true); | ||
this._readResourcesInternal(jsonDoc); | ||
return jsonDoc; | ||
@@ -124,3 +124,3 @@ } | ||
this._readResourcesExternal(jsonDoc, this._path.dirname(uri)); | ||
this._readResourcesInternal(jsonDoc, false); | ||
this._readResourcesInternal(jsonDoc); | ||
return jsonDoc; | ||
@@ -127,0 +127,0 @@ } |
@@ -70,16 +70,6 @@ import { Format, GLB_BUFFER, VertexLayout } from '../constants'; | ||
/** @internal */ | ||
protected _readResourcesInternal(jsonDoc: JSONDocument, isGLB: boolean): void { | ||
const images = jsonDoc.json.images || []; | ||
const buffers = jsonDoc.json.buffers || []; | ||
[...images, ...buffers].forEach((resource: GLTF.IBuffer|GLTF.IImage, index: number) => { | ||
if (!resource.uri) { | ||
const isGLBBuffer = isGLB && index === images.length; | ||
if ((resource as GLTF.IImage)['bufferView'] === undefined && !isGLBBuffer) { | ||
throw new Error('Missing resource URI.'); | ||
} | ||
return; | ||
} | ||
protected _readResourcesInternal(jsonDoc: JSONDocument): void { | ||
function resolveResource(resource: GLTF.IBuffer | GLTF.IImage) { | ||
if (!resource.uri || (resource.uri in jsonDoc.resources)) return; | ||
if (resource.uri in jsonDoc.resources) return; | ||
if (resource.uri.match(/data:/)) { | ||
@@ -91,3 +81,17 @@ // Rewrite Data URIs to something short and unique. | ||
} | ||
} | ||
// Unpack images. | ||
const images = jsonDoc.json.images || []; | ||
images.forEach((image: GLTF.IImage) => { | ||
if (image.bufferView === undefined && image.uri === undefined) { | ||
throw new Error('Missing resource URI or buffer view.'); | ||
} | ||
resolveResource(image); | ||
}); | ||
// Unpack buffers. | ||
const buffers = jsonDoc.json.buffers || []; | ||
buffers.forEach(resolveResource); | ||
} | ||
@@ -101,3 +105,3 @@ | ||
public readJSON (jsonDoc: JSONDocument): Document { | ||
this._readResourcesInternal(jsonDoc, GLB_BUFFER in jsonDoc.resources); | ||
this._readResourcesInternal(jsonDoc); | ||
return GLTFReader.read(jsonDoc, { | ||
@@ -104,0 +108,0 @@ extensions: this._extensions, |
@@ -14,2 +14,3 @@ import { JSONDocument } from '../json-document'; | ||
public buffers: Buffer[] = []; | ||
public bufferViews: Uint8Array[] = []; | ||
public bufferViewBuffers: Buffer[] = []; | ||
@@ -16,0 +17,0 @@ public accessors: Accessor[] = []; |
@@ -73,2 +73,5 @@ import { GLB_BUFFER, PropertyType, TypedArray, mat4, vec3, vec4 } from '../constants'; | ||
const bufferDefs = json.buffers || []; | ||
doc.getRoot().listExtensionsUsed() | ||
.filter((extension) => extension.prereadTypes.includes(PropertyType.BUFFER)) | ||
.forEach((extension) => extension.preread(context, PropertyType.BUFFER)); | ||
context.buffers = bufferDefs.map((bufferDef) => { | ||
@@ -89,3 +92,13 @@ const buffer = doc.createBuffer(bufferDef.name); | ||
const bufferViewDefs = json.bufferViews || []; | ||
context.bufferViewBuffers = bufferViewDefs.map((bufferViewDef) => { | ||
context.bufferViewBuffers = bufferViewDefs.map((bufferViewDef, index) => { | ||
if (!context.bufferViews[index]) { | ||
const bufferDef = jsonDoc.json.buffers![bufferViewDef.buffer]; | ||
const resource = bufferDef.uri | ||
? jsonDoc.resources[bufferDef.uri] | ||
: jsonDoc.resources[GLB_BUFFER]; | ||
const byteOffset = bufferViewDef.byteOffset || 0; | ||
const bufferView = new Uint8Array(resource, byteOffset, bufferViewDef.byteLength); | ||
context.bufferViews[index] = bufferView; | ||
} | ||
return context.buffers[bufferViewDef.buffer]; | ||
@@ -108,3 +121,3 @@ }); | ||
// KHR_draco_mesh_compression. | ||
// KHR_draco_mesh_compression and EXT_meshopt_compression. | ||
if (accessorDef.bufferView === undefined && !accessorDef.sparse) return accessor; | ||
@@ -115,5 +128,5 @@ | ||
if (accessorDef.sparse !== undefined) { | ||
array = getSparseArray(accessorDef, jsonDoc); | ||
array = getSparseArray(accessorDef, context); | ||
} else { | ||
array = getAccessorArray(accessorDef, jsonDoc); | ||
array = getAccessorArray(accessorDef, context); | ||
} | ||
@@ -522,8 +535,6 @@ | ||
*/ | ||
function getInterleavedArray(accessorDef: GLTF.IAccessor, jsonDoc: JSONDocument): TypedArray { | ||
function getInterleavedArray(accessorDef: GLTF.IAccessor, context: ReaderContext): TypedArray { | ||
const jsonDoc = context.jsonDoc; | ||
const bufferView = context.bufferViews[accessorDef.bufferView!]; | ||
const bufferViewDef = jsonDoc.json.bufferViews![accessorDef.bufferView!]; | ||
const bufferDef = jsonDoc.json.buffers![bufferViewDef.buffer]; | ||
const resource = bufferDef.uri | ||
? jsonDoc.resources[bufferDef.uri] | ||
: jsonDoc.resources[GLB_BUFFER]; | ||
@@ -536,3 +547,3 @@ const TypedArray = ComponentTypeToTypedArray[accessorDef.componentType]; | ||
const array = new TypedArray(accessorDef.count * elementSize); | ||
const view = new DataView(resource, bufferViewDef.byteOffset, bufferViewDef.byteLength); | ||
const view = new DataView(bufferView.buffer, bufferView.byteOffset, bufferView.byteLength); | ||
const byteStride = bufferViewDef.byteStride!; | ||
@@ -577,8 +588,6 @@ | ||
*/ | ||
function getAccessorArray(accessorDef: GLTF.IAccessor, jsonDoc: JSONDocument): TypedArray { | ||
function getAccessorArray(accessorDef: GLTF.IAccessor, context: ReaderContext): TypedArray { | ||
const jsonDoc = context.jsonDoc; | ||
const bufferView = context.bufferViews[accessorDef.bufferView!]; | ||
const bufferViewDef = jsonDoc.json.bufferViews![accessorDef.bufferView!]; | ||
const bufferDef = jsonDoc.json.buffers![bufferViewDef.buffer]; | ||
const resource = bufferDef.uri | ||
? jsonDoc.resources[bufferDef.uri] | ||
: jsonDoc.resources[GLB_BUFFER]; | ||
@@ -592,6 +601,6 @@ const TypedArray = ComponentTypeToTypedArray[accessorDef.componentType]; | ||
if (bufferViewDef.byteStride !== undefined && bufferViewDef.byteStride !== elementStride) { | ||
return getInterleavedArray(accessorDef, jsonDoc); | ||
return getInterleavedArray(accessorDef, context); | ||
} | ||
const byteOffset = (bufferViewDef.byteOffset || 0) + (accessorDef.byteOffset || 0); | ||
const byteOffset = bufferView.byteOffset + (accessorDef.byteOffset || 0); | ||
const byteLength = accessorDef.count * elementSize * componentSize; | ||
@@ -601,3 +610,3 @@ | ||
// bottleneck. See https://github.com/donmccurdy/glTF-Transform/issues/256. | ||
return new TypedArray(resource.slice(byteOffset, byteOffset + byteLength)); | ||
return new TypedArray(bufferView.buffer.slice(byteOffset, byteOffset + byteLength)); | ||
} | ||
@@ -609,3 +618,3 @@ | ||
*/ | ||
function getSparseArray(accessorDef: GLTF.IAccessor, jsonDoc: JSONDocument): TypedArray { | ||
function getSparseArray(accessorDef: GLTF.IAccessor, context: ReaderContext): TypedArray { | ||
const TypedArray = ComponentTypeToTypedArray[accessorDef.componentType]; | ||
@@ -616,3 +625,3 @@ const elementSize = Accessor.getElementSize(accessorDef.type); | ||
if (accessorDef.bufferView !== undefined) { | ||
array = getAccessorArray(accessorDef, jsonDoc); | ||
array = getAccessorArray(accessorDef, context); | ||
} else { | ||
@@ -626,4 +635,4 @@ array = new TypedArray(accessorDef.count * elementSize); | ||
const valuesDef = {...accessorDef, ...sparseDef.values, count}; | ||
const indices = getAccessorArray(indicesDef as GLTF.IAccessor, jsonDoc); | ||
const values = getAccessorArray(valuesDef, jsonDoc); | ||
const indices = getAccessorArray(indicesDef as GLTF.IAccessor, context); | ||
const values = getAccessorArray(valuesDef, context); | ||
@@ -630,0 +639,0 @@ // Override indices given in the sparse data. |
@@ -93,3 +93,3 @@ import { Document } from '../document'; | ||
jsonDoc.json = json; | ||
this._readResourcesInternal(jsonDoc, false); | ||
this._readResourcesInternal(jsonDoc); | ||
await this._readResourcesExternal(jsonDoc, _dirname(uri)); | ||
@@ -106,3 +106,3 @@ return jsonDoc; | ||
const jsonDoc = this._binaryToJSON(arrayBuffer); | ||
this._readResourcesInternal(jsonDoc, true); | ||
this._readResourcesInternal(jsonDoc); | ||
await this._readResourcesExternal(jsonDoc, _dirname(uri)); | ||
@@ -109,0 +109,0 @@ return jsonDoc; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1261883
10938