How do I update mesh options which were passed when creating mesh

I am creating Cylinder using MeshBuilder.CreateCylinder. I pass some options. However, I want to be able to edit those options later such as diameter, subdivisions etc. I tried looking around for a while, but haven’t found anything.

let options = {height:5, diameter:5, tessellation:5};
var cone = BABYLON.MeshBuilder.CreateCylinder("cone", options, scene);
options.tessellation = 20; //doesn't work

How can I update the mesh properties?

1 Like

I believe the reason this doesn’t work is because you have already declared your options and passed it into the mesh builder. It’s a linear execution passed by value, not by reference. What you want is to update properties on the cone object itself. Let me get an example for you.

Update: I just tried changing things in a PG but I think you will have to edit the vertices themselves in order to easily change the tessellation. Perhaps just deleting the current mesh and then creating a new one with your updated options.

3 Likes

As @msDestiny14 says once the parameters are used to build a mesh the mesh is not editable without adding/removing/editing the vertices and their properties directly. Since the code already exists to build the mesh with the new parameters the simplest way is to dispose of the existing mesh and use the new parameters to create a new one.

If the mesh has been positioned, rotated, scaled etc clone these values before creating the new mesh and then reapply.

3 Likes

Toying with this problem myself, there are some general object properties such as position, rotation and scale, which can be updated. E.g. if you want a cylinder where you can change the length retrospectively, just update scale.y. See this example:

Note that here the pivot is set to the base of the cylinder, which makes it just change its length keeping the base position constant rather than expanding to both sides.
This does not solve all problems, but at least the height and the radius can be dealt with this way.