Jump to content

LinesMesh and createInstance


Recommended Posts

Hi all,

in current versiont of BabylonJs, as in the preview release, LinesMesh  does not support "createInstance". Is there a fundamental reason for that because of how LinesMesh works ? Or is it just because nobody took the time to do it ?

(I prefer ask before eventually trying to implement it by myself  )




Link to comment
Share on other sites

BABYLON.Mesh supports instances.  If you specify your positions the way Meshbiulder.CreateLinesystem() does from the lines argument, you can use a BABYLON.Mesh. 

var i = 0;
for (var l = 0; l < lines.length; l++) {
    var points = lines[l];
    for (var p = 0; p < points.length; p++) {
        positions[i] = points[p].x;
        positions[i + 1] = points[p].y;
        positions[i + 2] = points[p].z;
        i += 3;

I already mentioned that LinesMesh uses ShaderMaterial, and that does not support instances.

Link to comment
Share on other sites

Yes, an actual linesystem is more efficient than instances, since it is only one mesh.  It closely resembles a MergedMesh.  It takes more memory, but if none of your lines never move / rotate / scale, then instances are not necessary.  If you have to beat up on a resource, memory is about the safest choice.

Link to comment
Share on other sites

Hmmm @JCPalmer, it seems that I can get my lines rendered as expected using a BABYLON.Mesh and using a standard material with wireframe and emissive color. But still, the picking does not seems to work, and not only because of possible 0-side AABB if the lines are axis-aligned. If the mesh is not a LinedMesh, the mesh is supposed to be made of triangular faces, each triangle made of 3 consecutives points in the geometry positions (copied from VertexData if I understood correctly).

I might have explained what I'm trying to achieve first : I'm implementing a kind-of 2d editor which display and manipulates objects coming from CAD data and that are mainly made of lines. I'm facing memory usage issue when creating all the meshes, so I'm trying to reduce my memory footprint.

As some of thses objects are just other instance of already existing objects (but with different transform), using instance instance of cloning those meshes did sound a good avenue... But it does not seems to be that simple ?

Maybe @Deltakosh have some insight on how to reduce my memory consumption?

(Btw @Deltakosh I've been trying the preview release with the performance improvements, but it actually seems to be slower for me to create a lot of BABYLON objects between two frames than with the 3.2... If you're interested in, I can make some profiling to give you an hint of why)

Link to comment
Share on other sites

So yeah definitely interested by the profiling :)

The LinesMesh has a cool feature to help with picking (mesh.intersectionThreshold) but it does not support instancing. That is something we can work on for next release (we are about to ship 3.3) so feel free to open an issue for that


Link to comment
Share on other sites

Hi @Deltakosh

Here are the shots of the profiling I did. The exact same operation with exact same code is used. Only the babylon and babylon-material version changed. (using chrome 69.0.3497.100 on a win10 Pro).



3.2 :



3.0-rc.1 :




As you can see, it takes much more time with the 3.0-rc1 version, and it seems that the extra-time is in "SparseMove" and "SparseSlice" which are V8 functions I guess. If I dig a bit deeper, those are called from set parent setter of Node :





If I compare this function between 3.2 and 3.0, the changes are about the _scene.rootNodes:




In case of many many nodes, having just an array can be suboptimal for storing all the nodes that have no parent. Note that even if I specifiy the parent in the Mesh constructor, the node is pushed in the rootNodes array during Node base class construction, and then removed when the parent is set during Mesh construction.

One way to prevent that would be add an extra parameter to the AbstractMesh, TransformNode and Node constructor in order to prevent the node to be adding to the rootNodes array if a non-null parent is passed in the Mesh cosntructor (or if the source has a non-null parent).

Another, less impacting, option would be to pop the last element of rootNodes after the call to super in the Mesh constructor (if the parent is not null).

Not that those changes would not reduce the time spent in "indexOf", but it could save a call to rootNodes .splice. (by the way, if the order in the rootNodes array does not matter, you'd better swap the last element of the array with the one at rootNodeIndex, and then call rootNodes.pop() instead of splice, it is likely to be faster and would less stress the GC as splice creates and returns a new array containing the deleted elements).

Having a boolean member "isInRootNode" might also help to prevent the call of rootNodes.indexOf is not necessary.


I'm gonna try the LinesMesh instance now :)



Link to comment
Share on other sites

@Deltakosh , in the attached patch there is my proposing for tackling this perf regression. I tested it on my side and it works. I tried to run gulp in the bayblon repo but it crashes at some point when testing with Phantom. But the crash also happens in the master and preview branches, so it does not come from my changes.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...