meshcat-dev / meshcat-python

WebGL-based 3D visualizer for Python
MIT License
256 stars 63 forks source link

Use another camera from Three.js #99

Closed gnthibault closed 3 years ago

gnthibault commented 3 years ago

Dear all, thank you very much for this tool, it's really amazing. Really empowers python ! I am looking for a way to change the defaut orthographic projection camera, in order to use this class from three.js: https://threejs.org/docs/#api/en/cameras/PerspectiveCamera

Is there any easy way to change the default camera ?

I wanted to try something like: self.view3D["/Cameras/default"].set_object(PerspectiveCamer) But unfortunately, there does not seems to be PerspectiveCamera in https://github.com/rdeits/meshcat-python/blob/7f03547d68976004f721c320b17c7c2204b172e9/src/meshcat/geometry.py#L262

Thank you in advance for your help.

gnthibault commented 3 years ago

I've tried something like this:

class PerspectiveCamera(SceneElement): #OrthographicCamera):
    """
    Checkout https://threejs.org/docs/#api/en/cameras/PerspectiveCamera
    """
    def __init__(self, fov, aspect, near, far, zoom):
        """
        fov   : Camera frustum vertical field of view, from bottom to top of view, in degrees. Default is 50.
        aspect: Camera frustum aspect ratio, usually the canvas width / canvas height. Default is 1 (square canvas).
        near  : Camera frustum near plane. Default is 0.1. The valid range is greater than 0 and less than the current
                value of the far plane. Note that, unlike for the OrthographicCamera, 0 is not a valid value for a
                PerspectiveCamera's near plane.
        far   : Camera frustum far plane. Default is 2000.
        zoom  : Gets or sets the zoom factor of the camera. Default is 1.
        """
        #super(PerspectiveCamera, self).__init__()
        SceneElement.__init__(self)
        self.fov = fov
        self.aspect = aspect
        self.near = near
        self.far = far
        self.zoom = zoom

    def lower(self):
        data = {
            u"object": {
                u"uuid": self.uuid,
                u"type": u"PerspectiveCamera",
                u"fov": self.fov,
                u"aspect": self.aspect,
                u"near": self.near,
                u"far": self.far,
                u"zoom": self.zoom,
            }
        }
        return data
        self.camera = PerspectiveCamera(
            fov=45,
            aspect=1,
            near=1,
            far=1000,
            zoom=1)
        self.view3D['/Cameras/default/rotated'].set_object(self.camera)

Also editing commands.py to enable the setter to work properly. That yielded some interesting results. Unfortunately, I was not able to change camera from the mouse after that, any idea what is missing ?

gnthibault commented 3 years ago

I assume that somehow, my new class does not work with the following js glue: https://github.com/rdeits/meshcat/blob/bb97f651bdd36ff63c132489b70a7b091e47082f/src/index.js#L882 ? Any idea ?