jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
936 stars 185 forks source link

Update BufferGeometry unreliable #265

Closed skoch9 closed 5 years ago

skoch9 commented 5 years ago

Hi,

I am updating the vertex positions of a BufferedGeometry object with the following code and a slider widget:

def update_vertices(self, v):
    v = v.astype("float32", copy=False)
    self.geometry.attributes["position"].array = v
    self.geometry.attributes["position"].needsUpdate = True
    self.mesh.geometry.exec_three_obj_method('computeVertexNormals')
    self.mesh.geometry.verticesNeedUpdate = True
    self.mesh.geometry.elementsNeedUpdate = True
    self.mesh.exec_three_obj_method('update')
    self.orbit.exec_three_obj_method('update')
    self.cam.exec_three_obj_method('updateProjectionMatrix')
    self.scene.exec_three_obj_method('update')

This is sometimes working and sometimes requires to rotate the view (see attached example). I couldn't figure out what is needed to make it reliably work and there also seem to be differences between different browsers.

Some of the code above is just from experimenting and it would be helpful if you could quickly tell what is actually needed to reliably update vertex positions (and the rendering), as this would be a much better approach to animations for us (any maybe others)?

myimage2

Thanks!

vidartf commented 5 years ago

Nothing obvious comes to mind (setting the .array property should make this happen by default). Are there any error messages in the browser console (and which browser are you using / which version of pythreejs etc)?

For me to look into this any close, I would need to be able to debug this, meaning I would need a way to reproduce the problem. Do you have a (preferably) simple example that reliably reproduces this issue?

skoch9 commented 5 years ago

Thanks, while condensing down my example to a MWE I stumbled upon the source of the problem. The crucial part appears to be the initialization order of scene and renderer:

renderer = Renderer(camera=cam, scene = Scene(), controls=[orbit], width=200, height=200)
scene = Scene(children=[mesh, cam, light])
renderer.scene = scene

produces the shown problems when updating the mesh in the scene whereas the following works:

scene = Scene()
renderer = Renderer(camera=cam, scene = scene, controls=[orbit], width=200, height=200)
scene.children = [mesh, cam, light]

Thanks for your help!

vidartf commented 5 years ago

Hmm. So basically you had two scene objects? I can see that this might have thrown the rendering logic off. Let me know if you find something else in addition :)

skoch9 commented 5 years ago

Yes, that has caused the update to work sometimes and sometimes not. No errors were output on the console.