import { EnumClass , EnumIO , _from , _fromString } from '../misc/EnumIO' ; import { TypedArray , Byte , TypedArrayConstructor } from '../../types/CommonTypes' ; export interface ComponentTypeEnum extends EnumIO { getSizeInBytes ( ) : Byte ; isFloatingPoint ( ) : boolean ; isInteger ( ) : boolean ; } class ComponentTypeClass extends EnumClass implements ComponentTypeEnum { readonly __sizeInBytes : number ; constructor ( { index , str , sizeInBytes , } ) { super ( { index , str } ) ; this . __sizeInBytes = sizeInBytes ; } getSizeInBytes ( ) { return this . __sizeInBytes ; } isFloatingPoint ( ) { if ( this . index === 5126 || this . index === 5127 || this . index === 0x8d61 ) { return true ; } return false ; } isInteger ( ) { if ( this . index === 5120 || this . index === 5121 || this . index === 5122 || this . index === 5123 || this . index === 5124 || this . index === 5125 ) { return true ; } return false ; } } const Unknown = new ComponentTypeClass ( { index : 5119 , str : 'UNKNOWN' , sizeInBytes : 0 , } ) ; const Byte = new ComponentTypeClass ( { index : 5120 , str : 'BYTE' , sizeInBytes : 1 , } ) ; const UnsignedByte = new ComponentTypeClass ( { index : 5121 , str : 'UNSIGNED_BYTE' , sizeInBytes : 1 , } ) ; const Short = new ComponentTypeClass ( { index : 5122 , str : 'SHORT' , sizeInBytes : 2 , } ) ; const UnsignedShort = new ComponentTypeClass ( { index : 5123 , str : 'UNSIGNED_SHORT' , sizeInBytes : 2 , } ) ; const Int = new ComponentTypeClass ( { index : 5124 , str : 'INT' , sizeInBytes : 4 , } ) ; const UnsignedInt = new ComponentTypeClass ( { index : 5125 , str : 'UNSIGNED_INT' , sizeInBytes : 4 , } ) ; const Float = new ComponentTypeClass ( { index : 5126 , str : 'FLOAT' , sizeInBytes : 4 , } ) ; const Double = new ComponentTypeClass ( { index : 5127 , str : 'DOUBLE' , sizeInBytes : 8 , } ) ; const Bool = new ComponentTypeClass ( { index : 35670 , str : 'BOOL' , sizeInBytes : 1 , } ) ; const HalfFloat = new ComponentTypeClass ( { index : 0x8d61 , str : 'HALF_FLOAT_OES' , sizeInBytes : 2 , } ) ; const typeList = [ Unknown , Byte , UnsignedByte , Short , UnsignedShort , Int , UnsignedInt , Float , Double , HalfFloat , ] ; function from ( index ) { return _from ( { typeList , index } ) as ComponentTypeEnum ; } function fromString ( str ) { return _fromString ( { typeList , str } ) as ComponentTypeEnum ; } function fromTypedArray ( typedArray ) { if ( typedArray instanceof Int8Array ) { return Byte ; } else if ( typedArray instanceof Uint8Array || typedArray instanceof Uint8ClampedArray ) { return UnsignedByte ; } else if ( typedArray instanceof Int16Array ) { return Short ; } else if ( typedArray instanceof Uint16Array ) { return UnsignedShort ; } else if ( typedArray instanceof Int32Array ) { return Int ; } else if ( typedArray instanceof Uint32Array ) { return UnsignedInt ; } else if ( typedArray instanceof Float32Array ) { return Float ; } else if ( typedArray instanceof Float64Array ) { return Double ; } return Unknown ; } function toTypedArray ( componentType ) { if ( componentType === Byte ) { return Int8Array ; } else if ( componentType === UnsignedByte ) { return Uint8Array ; } else if ( componentType === Short ) { return Int16Array ; } else if ( componentType === UnsignedShort ) { return Uint16Array ; } else if ( componentType === Int ) { return Int32Array ; } else if ( componentType === UnsignedInt ) { return Uint32Array ; } else if ( componentType === Float ) { return Float32Array ; } else if ( componentType === Double ) { return Float64Array ; } else { return undefined ; } } function fromGlslString ( str_ ) { let str = str_ ; switch ( str_ ) { case 'int' : str = 'INT' ; break ; case 'float' : str = 'FLOAT' ; break ; case 'vec2' : str = 'FLOAT' ; break ; case 'vec3' : str = 'FLOAT' ; break ; case 'vec4' : str = 'FLOAT' ; break ; case 'mat2' : str = 'FLOAT' ; break ; case 'mat3' : str = 'FLOAT' ; break ; case 'mat4' : str = 'FLOAT' ; break ; case 'ivec2' : str = 'INT' ; break ; case 'ivec3' : str = 'INT' ; break ; case 'ivec4' : str = 'INT' ; break ; case 'sampler2D' : str = 'INT' ; break ; case 'sampler3D' : str = 'INT' ; break ; case 'samplerCube' : str = 'INT' ; break ; } return _fromString ( { typeList , str } ) as ComponentTypeEnum ; } export const ComponentType = Object . freeze ( { Unknown , Byte , UnsignedByte , Short , UnsignedShort , Int , UnsignedInt : UnsignedInt , Float , Double , Bool , HalfFloat , from , fromTypedArray , toTypedArray , fromString , fromGlslString , } ) ;