@@ -47,25 +47,13 @@ export class WebGLTextures {
4747 `Texture index ${ index } must be in [0, ${ this . maxTextureUnits_ - 1 } ]`
4848 ) ;
4949 }
50- this . gl_ . activeTexture ( this . gl_ . TEXTURE0 + index ) ;
51-
52- const textureType = this . getTextureType ( texture ) ;
53- const info = this . getDataFormatInfo ( texture . dataFormat , texture . dataType ) ;
54-
55- if ( ! this . textures_ . has ( texture ) ) {
56- this . generateTexture ( texture , info , textureType ) ;
57- }
5850
5951 const textureId = this . textures_ . get ( texture ) ;
60- if ( ! textureId ) {
61- throw new Error ( "Failed to retrieve texture ID" ) ;
62- }
63-
64- this . gl_ . bindTexture ( textureType , textureId ) ;
65- if ( texture . needsUpdate && texture . data !== null ) {
66- this . configureTextureParameters ( texture , textureType ) ;
67- this . uploadTextureData ( texture , info , textureType ) ;
68- texture . needsUpdate = false ;
52+ if ( textureId && ! texture . needsUpdate ) {
53+ this . gl_ . activeTexture ( this . gl_ . TEXTURE0 + index ) ;
54+ this . gl_ . bindTexture ( this . getTextureType ( texture ) , textureId ) ;
55+ } else {
56+ this . uploadTexture ( texture , index ) ;
6957 }
7058
7159 this . currentTexture_ = texture ;
@@ -123,7 +111,35 @@ export class WebGLTextures {
123111 }
124112 }
125113
126- public disposeTexture ( texture : Texture ) {
114+ public uploadTexture ( texture : Texture , index = 0 ) {
115+ if ( this . textures_ . has ( texture ) && ! texture . needsUpdate ) return ;
116+
117+ if ( texture . data === null ) {
118+ throw new Error ( "Cannot upload a texture that has no CPU data" ) ;
119+ }
120+
121+ const textureType = this . getTextureType ( texture ) ;
122+ const info = this . getDataFormatInfo ( texture . dataFormat , texture . dataType ) ;
123+
124+ this . gl_ . activeTexture ( this . gl_ . TEXTURE0 + index ) ;
125+
126+ if ( ! this . textures_ . has ( texture ) ) {
127+ this . generateTexture ( texture , info , textureType ) ;
128+ }
129+
130+ const textureId = this . textures_ . get ( texture ) ;
131+ if ( ! textureId ) {
132+ throw new Error ( "Failed to retrieve texture ID" ) ;
133+ }
134+
135+ this . gl_ . bindTexture ( textureType , textureId ) ;
136+ this . configureTextureParameters ( texture , textureType ) ;
137+ this . uploadTextureData ( texture , info , textureType ) ;
138+
139+ texture . needsUpdate = false ;
140+ }
141+
142+ public dispose ( texture : Texture ) {
127143 const id = this . textures_ . get ( texture ) ;
128144 if ( id ) {
129145 this . gl_ . deleteTexture ( id ) ;
@@ -142,7 +158,7 @@ export class WebGLTextures {
142158
143159 public disposeAll ( ) {
144160 for ( const texture of Array . from ( this . textures_ . keys ( ) ) ) {
145- this . disposeTexture ( texture ) ;
161+ this . dispose ( texture ) ;
146162 }
147163 this . gpuTextureBytes_ = 0 ;
148164 this . textureCount_ = 0 ;
0 commit comments