Can one assign Mesh.edgesWidth by object?

The problem with assigning a scalar to Mesh.edgesWidth (or any other property) is that it’s passed by value, which means you can’t globally update a group of meshes easily.

What would be awesome is if you could do something like:

let MeshSettings = {"edgesWidth": 2};
...
for (... loop to create meshes) {
   let mesh = createMesh();
   mesh.edgesWidth = MeshSettings;
}

Since objects are passed by reference, that would allow all the meshes in the group to have their properties dynamically updated without having to loop over them again.

Thoughts? Maybe this: AbstractMesh | Babylon.js Documentation ? (Although there are no examples of how to use AbstractMesh)

Hi geoidesic,

Interesting idea! I’m not completely sure I understand the motivation fully; is the goal just to avoid writing a loop? Broadly speaking, if I have a giant collection of different objects which are supposed to always have the same properties, it might have been better for me to just make them one object in the first place. Regardless, it might still be something interesting to try. It’s not clear to me how AbstractMesh fits into your objective, but if you’re looking for examples where people have used it directly (all Meshes are AstractMeshes, so just about everything uses AbstractMesh indirectly), the Playground search shows quite a lot of usages. Would one of those be helpful?

The goal is performance. My expectation is that looping is expensive. It’s also cumbersome. Maybe I have 2 properties I want to update on my group of meshes (e.g. edgesWidth and edgesColor) and I have a group containing 1000 meshes… do I loop twice over 1000 meshes? And where do I store those meshes so that I can loop over them? It all gets a bit clunky.

It would be a lot more elegant (and performant) if each mesh knew that certain properties are references and thus can be updated globally.

I don’t understand AbstractMesh at all – it’s really just a shot in the dark based on the API description Class used to store all common mesh properties, so my expectation was that one could update the property on the AbstractMesh and it would propagate to instances that extend the AbstractMesh.

I also found this: Thin Instances | Babylon.js Documentation
Which looks like it might serve my purpose, which is to be able to update one or more properties on a group of meshes. In my case the meshes are all pretty much the same (cells in a grid).

1 Like

Gotcha. If your goal is performance, I would definitely recommend doing some tests. Every generalization has its exceptions, but generally speaking in JavaScript creating more objects is very rarely the solution to a performance problem: allocation in JavaScript is slow, shockingly so. In the common case, I would not expect the tradeoff of using an additional object instead of a literal to positively impact perf. For specific cases, though, the only way to really be sure is to test.

Regarding AbstractMesh, I spoke more about this in the other thread, but regarding that specific line of the docs, I think it’s referring to, “mesh properties common to many different types of mesh,” not “mesh attributes held in common by many instance of the same mesh.” For the latter use case, I think you’re correct that what you’re looking for is mesh instancing (both the regular and thing varieties may be of interest to you). If you want to render a bunch of the same thing, like grass or trees or tiles or something, instancing is almost certainly the way to go.

Hope this is helpful, and best of luck!

1 Like

The beauty of JS is duck typing :slight_smile: You could potentially replace on the fly the edgesWidth by a property fetching data from a common place instead.

https://playground.babylonjs.com/#3P2EB5#2

1 Like