Skip to content

Commit

Permalink
WebGLRenderer: Made getContext code more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Jun 13, 2020
1 parent 9772276 commit c5dc006
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,21 @@ function WebGLRenderer( parameters ) {

// initialize

let _gl;
let _gl = _context;

function getContext( contextNames, contextAttributes ) {

for ( let i = 0; i < contextNames.length; i ++ ) {

const contextName = contextNames[ i ];
const context = _canvas.getContext( contextName, contextAttributes );
if ( context !== null ) return context;

}

return null;

}

try {

Expand All @@ -204,25 +218,29 @@ function WebGLRenderer( parameters ) {
_canvas.addEventListener( 'webglcontextlost', onContextLost, false );
_canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );

if ( this.isWebGL1Renderer === true ) {
if ( _gl === null ) {

const contextNames = [ 'webgl2', 'webgl', 'experimental-webgl' ];

_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
if ( _this.isWebGL1Renderer === true ) {

} else {
contextNames.shift();

_gl = _context || _canvas.getContext( 'webgl2', contextAttributes ) || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );
}

}
_gl = getContext( contextNames, contextAttributes );

if ( _gl === null ) {
if ( _gl === null ) {

if ( _canvas.getContext( 'webgl2' ) || _canvas.getContext( 'webgl' ) || _canvas.getContext( 'experimental-webgl' ) ) {
if ( getContext( contextNames ) ) {

throw new Error( 'Error creating WebGL context with your selected attributes.' );
throw new Error( 'Error creating WebGL context with your selected attributes.' );

} else {
} else {

throw new Error( 'Error creating WebGL context.' );
throw new Error( 'Error creating WebGL context.' );

}

}

Expand Down

0 comments on commit c5dc006

Please sign in to comment.