author Yannick Deubel (https://github.com/yandeu)
 *  copyright license https import { RepeatWrapping , Texture } from 'three' ; import { ActionSprite } from './actionSprite' ; export interface SpriteSheetFrameOptions { width : number ; height : number ; } export class SpriteSheet extends ActionSprite { private _tilesHoriz : number ; private _tilesVert : number ; private _width : number ; private _height : number ; constructor ( texture , frameOptions ) { super ( texture ) ; const { width = this . textureWidth , height = this . textureHeight } = frameOptions ; this . _tilesHoriz = this . textureWidth / width ; this . _tilesVert = this . textureHeight / height ; if ( this . _tilesHoriz !== Math . round ( this . _tilesHoriz ) ) console . warn ( 'The horizontal row does not seem to fit!' ) ; if ( this . _tilesVert !== Math . round ( this . _tilesVert ) ) console . warn ( 'The vertical row does not seem to fit!' ) ; this . _width = width ; this . _height = height ; this . _frame . width = width ; this . _frame . height = height ; this . sizeFrame ( 1 / this . _tilesHoriz , 1 / this . _tilesVert ) ; this . scaleFrame ( ) ; } setScale ( x , y ? ) { super . setScale ( x , y ) ; this . scaleFrame ( ) ; } protected scaleFrame ( ) { this . scale . set ( ( this . _width * this . _internalScale . x ) / this . _pixelRatio , ( this . _height * this . _internalScale . y ) / this . _pixelRatio , 1 ) ; } private getRow ( index ) { return Math . floor ( index / this . _tilesHoriz ) ; } private getColumn ( index ) { return index % this . _tilesHoriz ; } protected sizeFrame ( width , height ) { this . texture . wrapS = this . texture . wrapT = RepeatWrapping ; this . texture . repeat . set ( width , height ) ; this . texture . needsUpdate = true ; } protected offsetTexture ( x , y ) { if ( this . _flipX ) x += 1 / this . _tilesHoriz ; this . texture . offset . setX ( x ) ; this . texture . offset . setY ( y ) ; } flipX ( flip ) { this . _flipX = flip ; let x = 1 / this . _tilesHoriz ; const y = 1 / this . _tilesVert ; if ( flip ) x *= - 1 ; this . texture . repeat . set ( x , y ) ; this . setFrame ( this . _frame . index as number ) ; } setFrame ( index ) { this . _frame . index = index ; const x = this . getColumn ( index ) / this . _tilesHoriz ; const y = ( this . _tilesVert - this . getRow ( index ) - 1 ) / this . _tilesVert ; this . offsetTexture ( x , y ) ; } }