vpython / vpython-jupyter

3D visualization made easy
MIT License
141 stars 64 forks source link

Missing scene/camera sync with browser view after web page-mouse interaction. #85

Open nastasi opened 4 years ago

nastasi commented 4 years ago

If you change the point of view dragging with mouse in the browser window the camera and scene data are not updated. It could be CPU intensive/inefficient so could be possible to add at least an explicit sync method to be able to retrieve correct parameters just before perform some action like camera movement after key pressure detection or just before an action is made on scene or camera ?

BruceSherwood commented 4 years ago

I don't understand. Run the following code and you will see scene.forward and scene.camera.axis changing as you use the mouse to rotate:

box()
forward = label(pos=vec(0,.2,0), text='')
axis = label(pos=vec(0,-.2,0), text='')
while True:
    rate(50)
    forward.text = scene.forward
    axis.text = scene.camera.axis
nastasi commented 4 years ago

Correct, instead scene.up remains to <0,1,0>, is it correct ?

box()
forward = label(pos=vec(0,.2,0), text='')
axis = label(pos=vec(0,-.2,0), text='')
up = label(pos=vec(0,-.4,0), text='')
while True:
    rate(50)
    forward.text = scene.forward
    axis.text = scene.camera.axis
    up.text = scene.up
BruceSherwood commented 4 years ago

That is how it currently works, though it could use some critical thought. I'm not entirely comfortable with this corner of the vpython world.

nastasi commented 4 years ago

Using the test below things became a little more clear. I didn't yet understand how it works exactly but seems that up is a sort of gamma in the polar coordinates (alpha, beta , gamma). If you try to do a 360° using "right" button it return to <0,1,0> BUT if you move the scene with the mouse and try again to do 360° up components change differently than before. @BruceSherwood ideas ? (BTW: when you change the scene with the mouse just canvas and forward items are in the event list)

from math import pi as PI
from vpython import (box, label, vec, rate, scene)

box()
forward = label(pos=vec(0, .2, 0), text='')
axis = label(pos=vec(0, -.2, 0), text='')
up = label(pos=vec(0, -.4, 0), text='')

def keydown(ev):
    if ev.key == 'right':
        print('rotate')
        scene.camera.rotate(PI / 8, axis=scene.camera.axis)
    if ev.key == 'left':
        print('rotate')
        scene.camera.rotate(-PI / 8, axis=scene.camera.axis)

scene.bind('keydown', keydown)

while True:
    rate(50)
    forward.text = "F: %.3f, %.3f, %.3f" % (scene.forward.x, scene.forward.y, scene.forward.z)
    axis.text = "A: %.3f, %.3f, %.3f" % (scene.camera.axis.x, scene.camera.axis.y, scene.camera.axis.z)
    up.text = "U: %.3f, %.3f, %.3f" % (scene.up.x, scene.up.y, scene.up.z)