import { Accessor , Document , Primitive , Transform } from '@gltf-transform/core' ; import { MeshQuantization } from '@gltf-transform/extensions' ; import { createTransform } from './utils' ; const NAME = 'dequantize' ; /** Options for the  dequantize function.  export interface DequantizeOptions { /**
     * Pattern (regex) used to filter vertex attribute semantics for quantization.
     * Default: `/^((?!JOINTS_).)*$/`.
     */ pattern ? : RegExp ; } const DEQUANTIZE_DEFAULTS = { pattern : /^((?!JOINTS_).)*$/ , } ; /**
 * Dequantize  Primitive , removing  MeshQuantization * if present. Dequantization will increase the size of the mesh on disk and in memory, but may be
 * necessary for compatibility with applications that don't support quantization.
  export function dequantize ( _options = DEQUANTIZE_DEFAULTS ) { const options = { ... DEQUANTIZE_DEFAULTS , ... _options } as Required < DequantizeOptions > ; return createTransform ( NAME , ( doc ) : void => { const logger = doc . getLogger ( ) ; for ( const mesh of doc . getRoot ( ) . listMeshes ( ) ) { for ( const prim of mesh . listPrimitives ( ) ) { dequantizePrimitive ( prim , options ) ; } } doc . createExtension ( MeshQuantization ) . dispose ( ) ; logger . debug ( `${ NAME }: Complete.` ) ; } ) ; } function dequantizePrimitive ( prim , options ) { for ( const semantic of prim . listSemantics ( ) ) { dequantizeAttribute ( semantic , prim . getAttribute ( semantic ) ! , options ) ; } for ( const target of prim . listTargets ( ) ) { for ( const semantic of target . listSemantics ( ) ) { dequantizeAttribute ( semantic , target . getAttribute ( semantic ) ! , options ) ; } } } function dequantizeAttribute ( semantic , attribute , options ) { if ( ! attribute . getArray ( ) ) return ; if ( ! options . pattern . test ( semantic ) ) return ; if ( attribute . getComponentSize ( ) >= 4 ) return ; const srcArray = attribute . getArray ( ) ! ; const dstArray = new Float32Array ( srcArray . length ) ; for ( let i = 0 , il = attribute . getCount ( ) , el = [ ] as number [ ] ; i < il ; i ++ ) { el = attribute . getElement ( i , el ) ; attribute . setArray ( dstArray ) . setElement ( i , el ) . setArray ( srcArray ) ; } attribute . setArray ( dstArray ) . setNormalized ( false ) ; }