Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _loadFloatAccessorAsync(context: string, accessor: IAccessor): Promise {
// TODO: support normalized and stride
if (accessor.componentType !== AccessorComponentType.FLOAT) {
throw new Error(`Invalid component type ${accessor.componentType}`);
}
if (accessor._data) {
return accessor._data as Promise;
}
const numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
const length = numComponents * accessor.count;
if (accessor.bufferView == undefined) {
accessor._data = Promise.resolve(new Float32Array(length));
}
else {
const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, accessor.bufferView);
accessor._data = this.loadBufferViewAsync(`/bufferViews/${bufferView.index}`, bufferView).then((data) => {
if (glTFMaterial && !this._glTFMaterialExporter._hasTexturesPresent(glTFMaterial)) {
continue;
}
}
let vertexData = bufferMesh.getVerticesData(attributeKind);
if (vertexData) {
const vertexBuffer = this.getVertexBufferFromMesh(attributeKind, bufferMesh);
if (vertexBuffer) {
const stride = vertexBuffer.getSize();
const bufferViewIndex = attribute.bufferViewIndex;
if (bufferViewIndex != undefined) { // check to see if bufferviewindex has a numeric value assigned.
minMax = { min: null, max: null };
if (attributeKind == VertexBuffer.PositionKind) {
minMax = _GLTFUtilities._CalculateMinMaxPositions(vertexData, 0, vertexData.length / stride, this._convertToRightHandedSystem);
}
const accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, attributeKind + " - " + babylonTransformNode.name, attribute.accessorType, AccessorComponentType.FLOAT, vertexData.length / stride, 0, minMax.min, minMax.max);
this._accessors.push(accessor);
this.setAttributeKind(meshPrimitive, attributeKind);
}
}
}
}
if (indexBufferViewIndex) {
// Create accessor
const accessor = _GLTFUtilities._CreateAccessor(indexBufferViewIndex, "indices - " + babylonTransformNode.name, AccessorType.SCALAR, AccessorComponentType.UNSIGNED_INT, submesh.indexCount, submesh.indexStart * 4, null, null);
this._accessors.push(accessor);
meshPrimitive.indices = this._accessors.length - 1;
}
if (materialIndex != null && Object.keys(meshPrimitive.attributes).length > 0) {
let sideOrientation = bufferMesh.overrideMaterialSideOrientation !== null ? bufferMesh.overrideMaterialSideOrientation : babylonMaterial.sideOrientation;
// Only reverse the winding if we have a clockwise winding
// create bufferview and accessor for keyed values.
outputLength = animationData.outputs.length;
byteLength = dataAccessorType === AccessorType.VEC3 ? animationData.outputs.length * 12 : animationData.outputs.length * 16;
// check for in and out tangents
bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name} data view`);
bufferViews.push(bufferView);
animationData.outputs.forEach(function(output) {
output.forEach(function(entry) {
binaryWriter.setFloat32(entry);
});
});
accessor = _GLTFUtilities._CreateAccessor(bufferViews.length - 1, `${name} data`, dataAccessorType, AccessorComponentType.FLOAT, outputLength, null, null, null);
accessors.push(accessor);
dataAccessorIndex = accessors.length - 1;
// create sampler
animationSampler = {
interpolation: animationData.samplerInterpolation,
input: keyframeAccessorIndex,
output: dataAccessorIndex
};
glTFAnimation.samplers.push(animationSampler);
// create channel
animationChannel = {
sampler: glTFAnimation.samplers.length - 1,
target: {
node: nodeIndex,
// create bufferview and accessor for keyed values.
outputLength = animationData.outputs.length;
byteLength = dataAccessorType === AccessorType.VEC3 ? animationData.outputs.length * 12 : animationData.outputs.length * 16;
// check for in and out tangents
bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name} data view`);
bufferViews.push(bufferView);
animationData.outputs.forEach(function(output) {
output.forEach(function(entry) {
binaryWriter.setFloat32(entry);
});
});
accessor = _GLTFUtilities._CreateAccessor(bufferViews.length - 1, `${name} data`, dataAccessorType, AccessorComponentType.FLOAT, outputLength, null, null, null);
accessors.push(accessor);
dataAccessorIndex = accessors.length - 1;
// create sampler
animationSampler = {
interpolation: animationData.samplerInterpolation,
input: keyframeAccessorIndex,
output: dataAccessorIndex
};
glTFAnimation.samplers.push(animationSampler);
// create channel
animationChannel = {
sampler: glTFAnimation.samplers.length - 1,
target: {
node: nodeIndex,
private static _GetTypedArray(context: string, componentType: AccessorComponentType, bufferView: ArrayBufferView, byteOffset: number | undefined, length: number): ArrayBufferView {
const buffer = bufferView.buffer;
byteOffset = bufferView.byteOffset + (byteOffset || 0);
try {
switch (componentType) {
case AccessorComponentType.BYTE: return new Int8Array(buffer, byteOffset, length);
case AccessorComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, length);
case AccessorComponentType.SHORT: return new Int16Array(buffer, byteOffset, length);
case AccessorComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, length);
case AccessorComponentType.UNSIGNED_INT: return new Uint32Array(buffer, byteOffset, length);
case AccessorComponentType.FLOAT: return new Float32Array(buffer, byteOffset, length);
default: throw new Error(`Invalid component type ${componentType}`);
}
}
catch (e) {
throw new Error(`${context}: ${e}`);
}
}
]).then(([indicesData, valuesData]) => {
const indices = GLTFLoader._GetTypedArray(`${context}/sparse/indices`, sparse.indices.componentType, indicesData, sparse.indices.byteOffset, sparse.count) as IndicesArray;
const sparseLength = numComponents * sparse.count;
let values: TypedArrayLike;
if (accessor.componentType === AccessorComponentType.FLOAT && !accessor.normalized) {
values = GLTFLoader._GetTypedArray(`${context}/sparse/values`, accessor.componentType, valuesData, sparse.values.byteOffset, sparseLength);
}
else {
const sparseData = GLTFLoader._GetTypedArray(`${context}/sparse/values`, accessor.componentType, valuesData, sparse.values.byteOffset, sparseLength);
values = new constructor(sparseLength);
VertexBuffer.ForEach(sparseData, 0, byteStride, numComponents, accessor.componentType, values.length, accessor.normalized || false, (value, index) => {
values[index] = value;
});
}
let valuesIndex = 0;
for (let indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
let dataIndex = indices[indicesIndex] * numComponents;
for (let componentIndex = 0; componentIndex < numComponents; componentIndex++) {
typedArray[dataIndex++] = values[valuesIndex++];
}
private static _GetTypedArrayConstructor(context: string, componentType: AccessorComponentType): TypedArrayConstructor {
switch (componentType) {
case AccessorComponentType.BYTE: return Int8Array;
case AccessorComponentType.UNSIGNED_BYTE: return Uint8Array;
case AccessorComponentType.SHORT: return Int16Array;
case AccessorComponentType.UNSIGNED_SHORT: return Uint16Array;
case AccessorComponentType.UNSIGNED_INT: return Uint32Array;
case AccessorComponentType.FLOAT: return Float32Array;
default: throw new Error(`${context}: Invalid component type ${componentType}`);
}
}