three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Bug in r53Dev when you use deallocateObject

Open MKHenson opened this issue 14 years ago • 7 comments

Hi guys

I have been struggling for a few days to isolate this bug, but think I have successfully been able to reproduce it now in one of the examples. Basically if you add a plane to the scene, apply a MeshBasicMaterial to it and then try to remove a mesh loaded from .js file using deallocateObject you will get a whole bunch of webgl errors.

To re-create the bug simply add the below lines of code at the end of the function 'createScene' in the example file 'webgl_materials_bumpmap.html'. Believe it only happens when you try to load a .js file - because I was able to load obj files in my app and did not see any errors.

//First create plane which we add to the scene
var planeMat = new THREE.MeshBasicMaterial( { color: 0x444444, wireframe: true } )
var plane = new THREE.Mesh( new THREE.PlaneGeometry( 30, 30, 30, 30 ), planeMat );
plane.rotation.x = - Math.PI / 2;
scene.add( plane );

//Render the scene with the plane
render();

//Now try and remove + deallocate the mesh and we get loads of errors
scene.remove( mesh );
mesh.deallocate();
renderer.deallocateObject( mesh );
render();

Regards

MKHenson avatar Nov 15 '12 10:11 MKHenson

@alteredq ping.

mrdoob avatar Nov 15 '12 12:11 mrdoob

Seems like it has something to do with the material. That example uses this material:

var material = new THREE.MeshPhongMaterial( { ambient: 0x552811, color: 0x552811, specular: 0x333333, shininess: 25, bumpMap: mapHeight, bumpScale: 19, metal: false } );

If we replace it with a MeshBasicMaterial it works fine but MeshLambertMaterial and MeshPhongMaterial throw those errors.

mrdoob avatar Nov 15 '12 13:11 mrdoob

Something that may or may not be related to this... In my app, when I load in the js or obj files and apply a phong material to both at some later point in time. The material is applied, however if I then update the material with a diffuse texture and update the meshes (so basically call the

object.geometry.buffersNeedUpdate = true;
object.geometry.uvsNeedUpdate = true;

functions on each of them ) - the js model will update with the texture, but the obj model won't and will actually throw a series of errors. But as soon as I remove the texture things go back to normal.

Perhaps there is something going wrong in the loading process or the creation of the meshes and/or their materials?

Like with above, I get different behaviour when using the deallocateObject. When I remove the js mesh using deallocateObject the webgl errors are thrown, but when I remove the obj object no errors are thrown.

Again I'm not sure if its related... but it seems like these errors are happening based on which file is being loaded.

Thanks guys :)

MKHenson avatar Nov 15 '12 14:11 MKHenson

Hmmm, after some poking around, I think I know what it is, not yet sure how to best solve this.

It's something that kept me puzzled for a long time. Unlike other WebGL libs, we don't disable attributes after each drawcall. This was somehow magically working and reduced number of GL API calls.

Now after seeing these errors, I believe what could be going on in the "magic" part is that attribute buffers from other objects stayed enabled all the time and filled in missing parts if needed (because before GL objects were never deleted, so even if we removed things from scene graph and they weren't rendered, they could be still available to GL state).

If you use the same type of material for plane as for JSON object (meaning they would both use the same buffers), errors will not happen.

I'll need to investigate more.

alteredq avatar Nov 15 '12 14:11 alteredq

Uhm, this is hairy. Doing it the naive "right" way makes errors go away but performance hit is terrible. We will need some smarter buffer / attributes GL state management.

alteredq avatar Nov 15 '12 16:11 alteredq

Ok, this should fix it. Just watch out for any troubles, this is a big change, virtually everything is affected.

I went through all WebGL examples but you never know.

BTW while testing this I found out #2638 but it is unrelated to these changes, the same thing happens for live examples on GitHub.

alteredq avatar Nov 16 '12 00:11 alteredq

Awesome. I'll update the code and let you know how it goes.

MKHenson avatar Nov 16 '12 11:11 MKHenson