Jump to content

Octree examples?


Recommended Posts

Ok so:


1.  Does "scene.createOrUpdateSelectionOctree()" go into the loop or is that a one time call?  If it's a one time call, do I have to call Update() on the octree in the render loop?

2.  To get neighbors, do I simply call "select()" on the octree object?  if so, what do I pass for the frustrumPlanes argument?  I can see that it is a BABYLON.Plane[] array, but where do I derive this array?


Can you shed just a little more light on this?


Thanks for your time



Link to comment
Share on other sites

In Babylon.js I create octree to help me finding visible objects

To do so, you have to call scene.createOrUpdateOctree().This will take all the submeshes of every mesh and put them into the octree.


If you want to create your own octree, you can call:

var octree = new BABYLON.Octree();


then you can call octree.update(min, max, meshes) to dispatch submeshes of meshes into the octree (min and max  defining the extends of the world. There are vector3)


The octree has now a function called select(frustrumPlanes). This function go through the octree and select visible submeshes accordingly to the frustrum planes.


Basically the octree is a collection of of OctreeBlock which in turn can contain submeshes or octreeblocks


In you case, you can write a function which can go through all blocks and which try to get block around a given position:

Var select = function(position) {

        var selection = new BABYLON.Tools.SmartArray(256);


        for (var index = 0; index < octree.blocks.length; index++) {

            var block = octree.blocks[index];

            blockSelect(block, position,selection);



        return selection;



Var blockSelect = function (block, position, selection) {

        if (block.blocks) {

            for (var index = 0; index < block.blocks.length; index++) {

                var block = block.blocks[index];

                blockSelect(block, position, selection);




        // Here you can use block._boundingVectors which define the extends of the block

       // If block is near the position then selection.push(this);



The select function just below will return you the list of octreeblocks near the position. You just have to use octreeblock.meshes to get the inner meshes.


BUT (because there is always a but), this will only work if your fishes are not moving too much (Because else they may change their containing octreeblock)



Does it make sense?

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...