@@ -128,6 +128,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
128128 sharedState . webglObjectToMemory . clear ( ) ;
129129 sharedState . webglObjectToMemory . set ( sharedState . defaultVertexArray , { } ) ;
130130 sharedState . currentVertexArray = sharedState . defaultVertexArray ;
131+ sharedState . activeTextureUnit = 0x84C0 ; // TEXTURE0
131132 [ sharedState . resources , sharedState . memory ] . forEach ( function ( obj ) {
132133 for ( const prop in obj ) {
133134 obj [ prop ] = 0 ;
@@ -179,6 +180,10 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
179180 return sharedState ;
180181 }
181182
183+ function getTextureBindingKey ( target ) {
184+ return `${ sharedState . activeTextureUnit } _${ target } ` ;
185+ }
186+
182187 const sharedState = options . sharedState || createSharedState ( ctx ) ;
183188 options . sharedState = sharedState ;
184189
@@ -256,6 +261,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
256261 }
257262
258263 const ELEMENT_ARRAY_BUFFER = 0x8893 ;
264+ const TEXTURE0 = 0x84C0 ;
259265
260266 const UNSIGNED_BYTE = 0x1401 ;
261267 const TEXTURE_CUBE_MAP = 0x8513 ;
@@ -285,9 +291,10 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
285291
286292 function getTextureInfo ( target ) {
287293 target = isCubemapFace ( target ) ? TEXTURE_CUBE_MAP : target ;
288- const obj = bindings . get ( target ) ;
294+ const bindingKey = getTextureBindingKey ( target ) ;
295+ const obj = bindings . get ( bindingKey ) ;
289296 if ( ! obj ) {
290- throw new Error ( `no texture bound to ${ target } ` ) ;
297+ throw new Error ( `no texture bound to ${ target } on unit ${ sharedState . activeTextureUnit - TEXTURE0 } ` ) ;
291298 }
292299 const info = webglObjectToMemory . get ( obj ) ;
293300 if ( ! info ) {
@@ -439,12 +446,21 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
439446 bindings . set ( target , obj ) ;
440447 } ,
441448
449+ activeTexture ( gl , funcName , args ) {
450+ if ( sharedState . isContextLost ) {
451+ return ;
452+ }
453+ const [ unit ] = args ;
454+ sharedState . activeTextureUnit = unit ;
455+ } ,
456+
442457 bindTexture ( gl , funcName , args ) {
443458 if ( sharedState . isContextLost ) {
444459 return ;
445460 }
446461 const [ target , obj ] = args ;
447- bindings . set ( target , obj ) ;
462+ const bindingKey = getTextureBindingKey ( target ) ;
463+ bindings . set ( bindingKey , obj ) ;
448464 } ,
449465
450466 // void gl.copyTexImage2D(target, level, internalformat, x, y, width, height, border);
0 commit comments