meshcat-dev / meshcat-python

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

Rendering objects as semi-transparent. #35

Closed danieljfarrell closed 5 years ago

danieljfarrell commented 5 years ago

It is possible to render objects as semi-transparent so that the interior remains visible? Or another alternative could be wireframing objects. I can see the different material classes in geometry.py such as MeshPhongMaterial and MeshToonMaterial but these shaders make the surface opaque. Maybe a surface texture is the way to do this? But I'm unsure how to proceed.

matthieuvigne commented 5 years ago

Hi @danieljfarrell,

I'm also interested in such a feature. As far as I can see, meshcat-python doesn't provide such an API. However, Three.js supports transparent objects, so it can be added quite easily. This patch adds a transparency and opacity field to the MeshMaterial object for configuring these properties. So the following code

import meshcat

visualizer = meshcat.visualizer.Visualizer()
sphere = meshcat.geometry.Sphere(1.0)
material = meshcat.geometry.MeshLambertMaterial()
material.transparency = True
material.opacity = 0.5
visualizer["sphere"].set_object(sphere, material)

renders a transparent sphere: screenshot from 2019-02-24 11-06-55

@rdeits Do you confirm this isn't currently possible with meshcat? If so, I can make a PR with this patch.

danieljfarrell commented 5 years ago

Would be really interested in knowing how you solved this problem. Would you upload your fork to GitHub?

matthieuvigne commented 5 years ago

@danieljfarrell Sure! There isn't anything more than what was in the patch, it's just a few lines of code to have meshcat communicate an extra attribute to Three.js FYI, I found the information about transparency in Three.js in the doc: https://threejs.org/docs/#api/en/materials/Material.opacity

I just created a PR for this issue.

danieljfarrell commented 5 years ago

Oh sorry! Missed the link to the patch in the original comment.

rdeits commented 5 years ago

Ok, this should now be available in meshcat v0.0.15. You should be able to pip install --upgrade meshcat to get the newest version. Thank you @matthieuvigne for the PR!