import * as Viewer from './viewer' ; import { GfxSampler , GfxTexture , GfxDevice } from './gfx/platform/GfxPlatform' ; export interface TextureOverride { gfxTexture : GfxTexture | null ; gfxSampler ? : GfxSampler ; width : number ; height : number ; flipY : boolean ; lateBinding ? : string ; } export interface TextureBase { name : string ; width : number ; height : number ; } export class TextureMapping { public gfxTexture : GfxTexture | null = null ; public gfxSampler : GfxSampler | null = null ; public lateBinding : string | null = null ; public width : number = 0 ; public height : number = 0 ; public lodBias : number = 0 ; public flipY : boolean = false ; public reset ( ) { this . gfxTexture = null ; this . gfxSampler = null ; this . lateBinding = null ; this . width = 0 ; this . height = 0 ; this . lodBias = 0 ; this . flipY = false ; } public fillFromTextureOverride ( textureOverride ) { this . gfxTexture = textureOverride . gfxTexture ; if ( textureOverride . gfxSampler ) this . gfxSampler = textureOverride . gfxSampler ; this . width = textureOverride . width ; this . height = textureOverride . height ; this . flipY = textureOverride . flipY ; if ( textureOverride . lateBinding ) this . lateBinding = textureOverride . lateBinding ; return true ; } public copy ( other ) { this . gfxTexture = other . gfxTexture ; this . gfxSampler = other . gfxSampler ; this . lateBinding = other . lateBinding ; this . width = other . width ; this . height = other . height ; this . lodBias = other . lodBias ; this . flipY = other . flipY ; } } export interface LoadedTexture { gfxTexture : GfxTexture ; viewerTexture : Viewer . Texture ; } export abstract class TextureHolder < TextureType extends TextureBase > { public viewerTextures : Viewer . Texture [ ] = [ ] ; public gfxTextures : GfxTexture [ ] = [ ] ; public textureEntries : TextureType [ ] = [ ] ; public textureOverrides = new Map < string , TextureOverride > ( ) ; public onnewtextures : ( ( ) => void ) | null = null ; public destroy ( device ) { this . gfxTextures . forEach ( ( texture ) => device . destroyTexture ( texture ) ) ; this . viewerTextures . length = 0 ; this . gfxTextures . length = 0 ; this . textureEntries . length = 0 ; this . textureOverrides . clear ( ) ; } protected searchTextureEntryIndex ( name ) { for ( let i = 0 ; i < this . textureEntries . length ; i ++ ) { if ( this . textureEntries [ i ] . name === name ) return i ; } return - 1 ; } public findTextureEntryIndex ( name ) { return this . searchTextureEntryIndex ( name ) ; } public hasTexture ( name ) { return this . findTextureEntryIndex ( name ) >= 0 ; } protected fillTextureMappingFromEntry ( textureMapping , textureEntryIndex ) { textureMapping . gfxTexture = this . gfxTextures [ textureEntryIndex ] ; const tex0Entry = this . textureEntries [ textureEntryIndex ] ; textureMapping . width = tex0Entry . width ; textureMapping . height = tex0Entry . height ; textureMapping . flipY = false ; } public fillTextureMapping ( textureMapping , name ) { const textureOverride = this . textureOverrides . get ( name ) ; if ( textureOverride ) { textureMapping . fillFromTextureOverride ( textureOverride ) ; return true ; } const textureEntryIndex = this . findTextureEntryIndex ( name ) ; if ( textureEntryIndex >= 0 ) { this . fillTextureMappingFromEntry ( textureMapping , textureEntryIndex ) ; return true ; } return false ; } public findTexture ( name ) { const textureEntryIndex = this . findTextureEntryIndex ( name ) ; if ( textureEntryIndex >= 0 ) return this . textureEntries [ textureEntryIndex ] ; return null ; } public setTextureOverride ( name , textureOverride ) { this . textureOverrides . set ( name , textureOverride ) ; } protected abstract loadTexture ( device , textureEntry ) ; public addTextures ( device , textureEntries , overwrite = false ) { for ( let i = 0 ; i < textureEntries . length ; i ++ ) { const texture = textureEntries [ i ] ; if ( texture === null ) continue ; let index = this . textureEntries . findIndex ( ( entry ) => entry . name === texture . name ) ; if ( index >= 0 && ! overwrite ) continue ; if ( index < 0 ) index = this . textureEntries . length ; const loadedTexture = this . loadTexture ( device , texture ) ; if ( loadedTexture === null ) continue ; const { gfxTexture , viewerTexture } = loadedTexture ; this . textureEntries [ index ] = texture ; this . gfxTextures [ index ] = gfxTexture ; this . viewerTextures [ index ] = viewerTexture ; } if ( this . onnewtextures !== null ) this . onnewtextures ( ) ; } } export class FakeTextureHolder extends TextureHolder < any > { constructor ( viewerTextures ) { super ( ) ; this . viewerTextures = viewerTextures ; } public loadTexture ( device , entry ) { throw new Error ( ) ; } }