neoRiley Posted January 30, 2014 Share Posted January 30, 2014 Are there any examples of how to use the Octree feature with babylon? What I'm trying to do is have a fish tank with several fish and be able to call a method like "getNeighbors()" for avoidance essentially. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted January 30, 2014 Share Posted January 30, 2014 to create the octree, you have to call:scene.createOrUpdateSelectionOctree()The octree is then saved into scene._selectionOctree. See here for implementation details:https://github.com/BabylonJS/Babylon.js/tree/master/Babylon/Culling/Octrees Quote Link to comment Share on other sites More sharing options...
neoRiley Posted January 30, 2014 Author Share Posted January 30, 2014 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 John Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted January 31, 2014 Share Posted January 31, 2014 In Babylon.js I create octree to help me finding visible objectsTo 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); } return; } // 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? Quote Link to comment Share on other sites More sharing options...
neoRiley Posted February 1, 2014 Author Share Posted February 1, 2014 That does help! Thanks very much, I appreciate it GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.