-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass of validation of EXT_mesh_features - WIP
- Loading branch information
Showing
8 changed files
with
1,286 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { BinaryBufferData } from "3d-tiles-tools"; | ||
|
||
/** | ||
* Utility methods related to glTF accessors. | ||
* | ||
* NOTE: These methods are only intended for the use in the | ||
* glTF extension validation. They make assumptions about | ||
* the validity of the glTF asset (as established by the | ||
* glTF Validator), and the structure of the glTF asset | ||
* (as established by the extension validator). | ||
* | ||
* @internal | ||
*/ | ||
export class Accessors { | ||
/** | ||
* Read the values of the given accessor into an array of numbers. | ||
* | ||
* This assumes that the accessor has the type SCALAR, and is | ||
* valid (in terms of its bufferView etc), as validated with | ||
* the glTF Validator. | ||
* | ||
* @param accessor - The glTF accessor | ||
* @param gltf - The glTF object | ||
* @param binaryBufferData - The binary buffer data of the glTF | ||
* @returns The accessor values (or undefined if the input | ||
* glTF data was invalid) | ||
*/ | ||
static readScalarAccessorValues( | ||
accessor: any, | ||
gltf: any, | ||
binaryBufferData: BinaryBufferData | ||
): number[] | undefined { | ||
const bufferViewIndex = accessor.bufferView; | ||
const bufferViews = gltf.bufferViews || []; | ||
const bufferView = bufferViews[bufferViewIndex]; | ||
const componentType = accessor.componentType; | ||
let byteStride = bufferView.byteStride; | ||
if (byteStride === undefined) { | ||
byteStride = Accessors.sizeForComponentType(componentType); | ||
if (byteStride === undefined) { | ||
// Invalid component type, should have been | ||
// detected by the glTF-Validator | ||
return undefined; | ||
} | ||
} | ||
const bufferViewData = binaryBufferData.bufferViewsData[bufferViewIndex]; | ||
const count = accessor.count; | ||
const byteOffset = accessor.byteOffset; | ||
const byteLength = count * byteStride; | ||
const accessorData = bufferViewData.subarray( | ||
byteOffset, | ||
byteOffset + byteLength | ||
); | ||
|
||
const values = []; | ||
for (let i = 0; i < count; i++) { | ||
const value = Accessors.readValue( | ||
accessorData, | ||
i, | ||
byteStride, | ||
componentType | ||
); | ||
if (value === undefined) { | ||
// Invalid component type, should have been | ||
// detected by the glTF-Validator | ||
return undefined; | ||
} | ||
values.push(value); | ||
} | ||
return values; | ||
} | ||
|
||
/** | ||
* Returns the size in bytes for the given accessor component type, | ||
* or undefined if the given component type is not valid. | ||
* | ||
* @param componentType - The component type | ||
* @returns The size in bytes | ||
*/ | ||
private static sizeForComponentType( | ||
componentType: number | ||
): number | undefined { | ||
const BYTE = 5120; | ||
const UNSIGNED_BYTE = 5121; | ||
const SHORT = 5122; | ||
const UNSIGNED_SHORT = 5123; | ||
const UNSIGNED_INT = 5125; | ||
const FLOAT = 5126; | ||
switch (componentType) { | ||
case BYTE: | ||
case UNSIGNED_BYTE: | ||
return 1; | ||
case SHORT: | ||
case UNSIGNED_SHORT: | ||
return 2; | ||
case UNSIGNED_INT: | ||
case FLOAT: | ||
return 4; | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Read a single numeric value from a buffer that contains | ||
* the accessor data | ||
* | ||
* @param buffer - The buffer | ||
* @param index - The index | ||
* @param byteStide - The byte stride | ||
* @param componentType - The component type | ||
* @returns The value | ||
*/ | ||
private static readValue( | ||
buffer: Buffer, | ||
index: number, | ||
byteStide: number, | ||
componentType: number | ||
): number | undefined { | ||
const BYTE = 5120; | ||
const UNSIGNED_BYTE = 5121; | ||
const SHORT = 5122; | ||
const UNSIGNED_SHORT = 5123; | ||
const UNSIGNED_INT = 5125; | ||
const FLOAT = 5126; | ||
const byteOffset = index * byteStide; | ||
switch (componentType) { | ||
case BYTE: | ||
return buffer.readInt8(byteOffset); | ||
case UNSIGNED_BYTE: | ||
return buffer.readUint8(byteOffset); | ||
case SHORT: | ||
return buffer.readInt16LE(byteOffset); | ||
case UNSIGNED_SHORT: | ||
return buffer.readUInt16LE(byteOffset); | ||
case UNSIGNED_INT: | ||
return buffer.readUInt32LE(byteOffset); | ||
case FLOAT: | ||
return buffer.readFloatLE(byteOffset); | ||
} | ||
return undefined; | ||
} | ||
} |
Oops, something went wrong.