rdeits / MeshCat.jl

WebGL-based 3D visualizer in Julia
MIT License
233 stars 43 forks source link

Is it able to set object properties? #179

Closed PikaPei closed 1 year ago

PikaPei commented 4 years ago

For example, I have a LineSegments object and want to change its color or linewidth. I know I can create a new object to cover it currently, but it's not suitable for animation. Could I use these kinds of command to accomplish it?

setprop!(vis[:linesegment], "color", [1., 0., 0., 1.])
setprop!(vis[:linesegment], "material", LineBasicMaterial(linewidth=3))

Thank you!

PikaPei commented 4 years ago

Hello, sorry for bothering you. I'm new to both JavaScript and Three.js. I just want to know whether it's possible to test MeshCat by myself? I assume that I could edit index.js in path: [@DIR, "..", "assets", "meshcat", "src"], for example, add console.log("test");. But nothing happened. Is it because the corresponding codes had been generated already?

rdeits commented 4 years ago

Hi--no, I'm afraid it's not currently possible, but I think we could do it. The setprop! commands currently can only change properties of the threejs Object itself (things like quaternion, scale, position, visible, etc.), while the color of the material is inside object.material.color.r, object.material.color.g, etc.

If you are interested in changing MeshCat yourself, I would suggest that you do:

  1. In Julia, do MeshCat.develop_meshcat_assets() which will clone https://github.com/rdeits/meshcat into MeshCat.jl/assets/meshcat for easier development.
  2. Follow the development instructions from https://github.com/rdeits/meshcat#developing-meshcat to set up all of the javascript dependencies.

The reason that just editing the source files doesn't work is that the javascript sources need to be combined with the three.js sources into a single bundle (you can find this bundle in MeshCat.jl/assets/meshcat/dist/main.js and main.min.js if you are curious). Don't edit the main.js or main.min.js file by hand: it will be overwritten when you run npm run build. Instead, edit meshcat/src/index.js and run npm run build as described in https://github.com/rdeits/meshcat#developing-meshcat

PikaPei commented 4 years ago

Oh, sorry, I did't notice that developing part. Thanks for your kind reply, I'll give it a try!

mfogelson commented 2 years ago

Was there any progress on this?

ferrolho commented 1 year ago

You can work around this issue by creating multiple instances of your object, each with its own material property. Then, you can enable the correct instance at the right frame to render smooth animations.

Here's an example:

using Colors
using MeshCat

vis = Visualizer()
open(vis)

points = [rand(3) for _ = 1:100]
pointcloud = PointCloud(points)

for i = 1:255
    material = LineBasicMaterial(color=RGB(i/255, (255-i)/255, 0.1), linewidth=2)
    setobject!(vis["line_$(i)"], Object(pointcloud, material, "Line"))
end

function hide_all_lines()
    for i = 1:255
        setvisible!(vis["line_$(i)"], false)
    end
end

anim = MeshCat.Animation()

for i = 1:255
    atframe(anim, i) do
        hide_all_lines()
        setvisible!(vis["line_$(i)"], true)
    end
end

setanimation!(vis, anim)

https://github.com/rdeits/MeshCat.jl/assets/4166093/10b48fa7-4ad5-47db-b762-db0e9249c04b