patrickfuller / imolecule

An embeddable webGL molecule viewer and file format converter.
http://patrickfuller.github.io/imolecule/
MIT License
86 stars 18 forks source link

Failed to initialize WebGL context #23

Open jhrmnn opened 8 years ago

jhrmnn commented 8 years ago

I've noticed that in a notebook with many imolecule output cells, both Safari and Chrome fail to render all the cells, some of them throw the following error:

screen shot 2015-09-28 at 16 03 04

I've also noticed that when I develop a notebook and create a lot of imolecule output cells, the browser eventually starts throwing

WARNING: Too many active WebGL contexts. Oldest context will be lost.

I've found that this may be solved by keeping only a single renderer for different scene, but I don't know whether it would be possible to keep a single shared rendered for all imolecule outputs.

patrickfuller commented 8 years ago

The single renderer approach makes sense to me, and seems to be an easy upgrade if the renderer can change DOM elements.

Out of curiosity, how many instances do you have running when this occurs?

jhrmnn commented 8 years ago

About 30, each with hundreds of atoms (not sure whether that matters). I was a bit surprised, didn't seem like a crazy number.

patrickfuller commented 8 years ago

After some testing, it looks like the renderer can't be shared across different canvases, ie. everything rendered has to be in the <canvas> div.

I'll leave the issue open in case another solution comes up, but won't be able to fix this any time soon.

jhrmnn commented 8 years ago

Do you have an idea what refactoring would be needed? I might try doing it myself.

patrickfuller commented 8 years ago

In my testing, I made a global imoleculeRenderer variable, replaced this line with:

if (imoleculeRenderer === undefined) {
    imoleculeRenderer = new THREE.WebGLRenderer({antialias: true, alpha: true});

and then swapped out instances of this.renderer and self.renderer with imoleculeRenderer. This broke previous canvases, which makes sense.

Regarding whether or not there's a right way to do this, I honestly don't know. The one-renderer approach works if everything is on one canvas, but I don't think that's feasible in a notebook. Maybe there's a smart way to swap out active webGL canvases with static images?

sxhexe commented 6 years ago

@azag0 @patrickfuller Same problem here. Did you guys figure out any workarounds?

patrickfuller commented 6 years ago

Nothing straightforward unfortunately, at least within the constraints of the jupyter notebook.