Skip to content

Commit

Permalink
feat(extensions): Add KHR_accessor_float64
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed May 18, 2024
1 parent 2a5ad54 commit 03325ed
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
40 changes: 39 additions & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fetch from 'node-fetch';
import mikktspace from 'mikktspace';
import { MeshoptEncoder, MeshoptSimplifier } from 'meshoptimizer';
import { ready as resampleReady, resample as resampleWASM } from 'keyframe-resample';
import { Logger, NodeIO, PropertyType, VertexLayout, vec2, Transform } from '@gltf-transform/core';
import { Logger, NodeIO, PropertyType, VertexLayout, vec2, Transform, Accessor } from '@gltf-transform/core';
import {
CenterOptions,
InstanceOptions,
Expand Down Expand Up @@ -967,6 +967,44 @@ Removes KHR_mesh_quantization, if present.`.trim(),
return Session.create(io, logger, args.input, args.output).transform(dequantize({ ...options, pattern }));
});

// F64
program
.command('f64', 'Convert all f32 accessors to f64 (EXPERIMENTAL)')
.help(`Experimental testing feature for KHR_accessor_float64`.trim())
.argument('<input>', 'Path to read glTF 2.0 (.glb, .gltf) input')
.argument('<output>', 'Path to write output')
.action(({ args, options, logger }) => {
return Session.create(io, logger, args.input, args.output).transform(
dequantize({ ...options }),
(document) => {
for (const accessor of document.getRoot().listAccessors()) {
if (accessor.getComponentType() === Accessor.ComponentType.FLOAT) {
accessor.setArray(new Float64Array(accessor.getArray()!));
}
}
}
);
});

// F32
program
.command('f32', 'Convert all f64 accessors to f32 (EXPERIMENTAL)')
.help(`Experimental testing feature for KHR_accessor_float64`.trim())
.argument('<input>', 'Path to read glTF 2.0 (.glb, .gltf) input')
.argument('<output>', 'Path to write output')
.action(({ args, options, logger }) => {
return Session.create(io, logger, args.input, args.output).transform(
dequantize({ ...options }),
(document) => {
for (const accessor of document.getRoot().listAccessors()) {
if (accessor.getComponentType() === Accessor.ComponentType.FLOAT64) {
accessor.setArray(new Float32Array(accessor.getArray()!));
}
}
}
);
});

// WELD
program
.command('weld', 'Merge equivalent vertices to optimize geometry')
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ export const GLB_BUFFER = '@glb.bin';
* Abstraction representing any one of the typed array classes supported by glTF and JavaScript.
* @hidden
*/
export type TypedArray = Float32Array | Uint32Array | Uint16Array | Uint8Array | Int16Array | Int8Array;
export type TypedArray = Float64Array | Float32Array | Uint32Array | Uint16Array | Uint8Array | Int16Array | Int8Array;

/**
* Abstraction representing the typed array constructors supported by glTF and JavaScript.
* @hidden
*/
export type TypedArrayConstructor =
| Float64ArrayConstructor
| Float32ArrayConstructor
| Uint32ArrayConstructor
| Uint16ArrayConstructor
Expand Down Expand Up @@ -159,4 +160,5 @@ export const ComponentTypeToTypedArray = {
'5123': Uint16Array,
'5125': Uint32Array,
'5126': Float32Array,
'5130': Float64Array,
};
4 changes: 4 additions & 0 deletions packages/core/src/io/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ function getInterleavedArray(accessorDef: GLTF.IAccessor, context: ReaderContext
case Accessor.ComponentType.BYTE:
value = view.getInt8(byteOffset);
break;
// KHR_accessor_float64
case Accessor.ComponentType.FLOAT64:
value = view.getFloat64(byteOffset, true);
break;
default:
throw new Error(`Unexpected componentType "${accessorDef.componentType}".`);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/io/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class GLTFWriter {
case Accessor.ComponentType.UNSIGNED_INT:
view.setUint32(viewByteOffset, value, true);
break;
// KHR_accessor_float64
case Accessor.ComponentType.FLOAT64:
view.setFloat64(viewByteOffset, value, true);
break;
default:
throw new Error('Unexpected component type: ' + componentType);
}
Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/properties/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ export class Accessor extends ExtensibleProperty<IAccessor> {
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array Float32Array}.
*/
FLOAT: 5126,
/**
* 8-byte floating point number, stored as
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array Float64Array}.
* KHR_accessor_float64
*/
FLOAT64: 5130,
};

/**********************************************************************************************
Expand Down Expand Up @@ -175,17 +181,17 @@ export class Accessor extends ExtensibleProperty<IAccessor> {
public static getComponentSize(componentType: GLTF.AccessorComponentType): number {
switch (componentType) {
case Accessor.ComponentType.BYTE:
return 1;
case Accessor.ComponentType.UNSIGNED_BYTE:
return 1;
case Accessor.ComponentType.SHORT:
return 2;
case Accessor.ComponentType.UNSIGNED_SHORT:
return 2;
case Accessor.ComponentType.UNSIGNED_INT:
return 4;
case Accessor.ComponentType.FLOAT:
return 4;
// KHR_accessor_float64
case Accessor.ComponentType.FLOAT64:
return 8;
default:
throw new Error('Unexpected component type: ' + componentType);
}
Expand Down Expand Up @@ -548,6 +554,9 @@ function arrayToComponentType(array: TypedArray): GLTF.AccessorComponentType {
return Accessor.ComponentType.SHORT;
case Int8Array:
return Accessor.ComponentType.BYTE;
// KHR_accessor_float64
case Float64Array:
return Accessor.ComponentType.FLOAT64;
default:
throw new Error('Unknown accessor componentType.');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/gltf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
export declare module GLTF {
/** Data type of the values composing each element in the accessor. */
type AccessorComponentType = 5120 | 5121 | 5122 | 5123 | 5125 | 5126;
type AccessorComponentType = 5120 | 5121 | 5122 | 5123 | 5125 | 5126 | 5130;
/** Element type contained by the accessor (SCALAR, VEC2, ...). */
type AccessorType = 'SCALAR' | 'VEC2' | 'VEC3' | 'VEC4' | 'MAT2' | 'MAT3' | 'MAT4';
/** Name of the property to be modified by an animation channel. */
Expand Down
1 change: 1 addition & 0 deletions packages/extensions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const EXT_MESH_GPU_INSTANCING = 'EXT_mesh_gpu_instancing';
export const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
export const EXT_TEXTURE_WEBP = 'EXT_texture_webp';
export const EXT_TEXTURE_AVIF = 'EXT_texture_avif';
export const KHR_ACCESSOR_FLOAT64 = 'KHR_accessor_float64';
export const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression';
export const KHR_LIGHTS_PUNCTUAL = 'KHR_lights_punctual';
export const KHR_MATERIALS_ANISOTROPY = 'KHR_materials_anisotropy';
Expand Down
3 changes: 3 additions & 0 deletions packages/extensions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EXTMeshGPUInstancing } from './ext-mesh-gpu-instancing/index.js';
import { EXTMeshoptCompression } from './ext-meshopt-compression/index.js';
import { EXTTextureAVIF } from './ext-texture-avif/index.js';
import { EXTTextureWebP } from './ext-texture-webp/index.js';
import { KHRAccessorFloat64 } from './khr-accessor-float64/index.js';
import { KHRDracoMeshCompression } from './khr-draco-mesh-compression/index.js';
import { KHRLightsPunctual } from './khr-lights-punctual/index.js';
import { KHRMaterialsAnisotropy } from './khr-materials-anisotropy/index.js';
Expand All @@ -24,6 +25,7 @@ import { KHRTextureTransform } from './khr-texture-transform/index.js';
import { KHRXMP } from './khr-xmp-json-ld/index.js';

export const KHRONOS_EXTENSIONS = [
KHRAccessorFloat64,
KHRDracoMeshCompression,
KHRLightsPunctual,
KHRMaterialsAnisotropy,
Expand Down Expand Up @@ -58,6 +60,7 @@ export * from './ext-mesh-gpu-instancing/index.js';
export * from './ext-meshopt-compression/index.js';
export * from './ext-texture-avif/index.js';
export * from './ext-texture-webp/index.js';
export * from './khr-accessor-float64/index.js';
export * from './khr-draco-mesh-compression/index.js';
export * from './khr-lights-punctual/index.js';
export * from './khr-materials-anisotropy/index.js';
Expand Down
12 changes: 6 additions & 6 deletions packages/extensions/src/khr-draco-mesh-compression/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,17 @@ function addAttribute(
): number {
switch (componentType) {
case Accessor.ComponentType.UNSIGNED_BYTE:
return builder.AddUInt8Attribute(mesh, attribute, count, itemSize, array);
return builder.AddUInt8Attribute(mesh, attribute, count, itemSize, array as Uint8Array);
case Accessor.ComponentType.BYTE:
return builder.AddInt8Attribute(mesh, attribute, count, itemSize, array);
return builder.AddInt8Attribute(mesh, attribute, count, itemSize, array as Int8Array);
case Accessor.ComponentType.UNSIGNED_SHORT:
return builder.AddUInt16Attribute(mesh, attribute, count, itemSize, array);
return builder.AddUInt16Attribute(mesh, attribute, count, itemSize, array as Uint16Array);
case Accessor.ComponentType.SHORT:
return builder.AddInt16Attribute(mesh, attribute, count, itemSize, array);
return builder.AddInt16Attribute(mesh, attribute, count, itemSize, array as Int16Array);
case Accessor.ComponentType.UNSIGNED_INT:
return builder.AddUInt32Attribute(mesh, attribute, count, itemSize, array);
return builder.AddUInt32Attribute(mesh, attribute, count, itemSize, array as Uint32Array);
case Accessor.ComponentType.FLOAT:
return builder.AddFloatAttribute(mesh, attribute, count, itemSize, array);
return builder.AddFloatAttribute(mesh, attribute, count, itemSize, array as Float32Array);
default:
throw new Error(`Unexpected component type, "${componentType}".`);
}
Expand Down

0 comments on commit 03325ed

Please sign in to comment.