kevinImperial Posted October 10, 2016 Share Posted October 10, 2016 Hi everyone, A bit of a babylon philosophical question here. I want to deal with a massive amount of spheres, and want to keep it as smooth as possible. I thought I would hide spheres which are no longer in the view frustum. Now that I got that code, I wonder what is the most efficient, either to hide the meshes by selecting and applying .visibility = 0, or if it is better to dispose of the meshes ? If you're curious, in my case I only rotate the camera and don't move it, and I know what I have to show thanks to the angles within a database. Currently, Babylon still feels a bit too slow to my taste with many elements hidden. Should I change my code to dispose of the meshes and recreate them instead of hiding/showing ? Best, Kevin Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 BJS manages the frustum for you and doesn't render what isn't visible. So you don't really need to set the visibility manually. Disposing and recreating the meshes is not a good idea because this will massively trigger the garbage collector. The best approach would be imho : - to lower the number of sphere to the necessary amount for visibility, it is to say to recycle your spheres : when no longer visible, recycle it to another visible sphere. With dozens or hundreds of sphere only, you can then simulate the presence of dozen of thousands - to use instances or a solid particle system, to reduce the number of draw calls If you have a PG demo, we could help you to tweak it to achieve your goal kevinImperial 1 Quote Link to comment Share on other sites More sharing options...
fenomas Posted October 10, 2016 Share Posted October 10, 2016 Do the spheres rotate? If not, and you never move the camera, could you get away with just using sprites instead of spherical meshes? Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 fake master ! Quote Link to comment Share on other sites More sharing options...
kevinImperial Posted October 10, 2016 Author Share Posted October 10, 2016 Thanks guys, this is good feedback and can be very useful. My code is a bit massive to turn into a demo unfortunately (connection to database, etc). Currently I use clones of one sphere because I want to give different materials to different spheres, and I understood that using instances would not allow me to give different materials. I like the idea of moving the spheres position, but it still mean that at some point I need to set up a limited number of instances to draw... Fenomas, currently the spheres do not rotate, but they will in the future. (I intend to have the ones close to the camera rotate, and the ones far either low quality spheres or sprites, the issue might come from colour though). Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 Quote Currently I use clones of one sphere because I want to give different materials to different spheres, and I understood that using instances would not allow me to give different materials. If, by different materials, you mean different textures/images, you could use the SPS. The SPS uses only one material but you can assign different sub-parts from a single image (texture) to different particles like you would do with a texture atlas. http://doc.babylonjs.com/overviews/Solid_Particle_System#uvs Clones are an easy way to duplicate meshes but they lead to as many draw calls as the number of clones. Instances or SPS only makes one draw call. jellix and kevinImperial 2 Quote Link to comment Share on other sites More sharing options...
kevinImperial Posted October 10, 2016 Author Share Posted October 10, 2016 Alright, will have a read into this, thanks a lot Will keep the idea of a limited number of spheres moving. While I'm not a big fan of a limited number of elements to display, it might have to be used as a safety net. Out of the blue, how many spheres do you believe Babylon can render at once ? And how many sprites ? Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 Well, it mostly depends on your device but BJS can easily handle dozens of thousands sprites (maybe even hundred of thousands) like you can find it in many PG from this forum. If you use the SPS, the number of vertices per sphere will be an important factor (segments parameter http://doc.babylonjs.com/tutorials/Mesh_CreateXXX_Methods_With_Options_Parameter#sphere ), but you can easily animate (translation + rotation) from 8 to 10 000 boxes for instance on a decent computer. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted October 10, 2016 Share Posted October 10, 2016 Just create the sphere once, and then set the main instance of it to disabled with mesh.isEnabled(0); and it will drop it out of all draw stacks. now when ever you need to make that sphere just do a originalMesh.clone() or copy depending on how unique you need it to be, and that will make it so it's all one draw call. Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 10, 2016 Share Posted October 10, 2016 (edited) Here is an example l with about 195000 triangles each rotating. Of course with spheres you have a lot more vertices to deal with but it will give you some idea of the power of SPS. http://www.babylonjs-playground.com/#1LIVJD#3 Thank you Jerome for reminding me about link Edited October 10, 2016 by JohnK added link meteoritool 1 Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 @JohnK I think you forgot a link in your former post ;-) Quote Link to comment Share on other sites More sharing options...
kevinImperial Posted October 10, 2016 Author Share Posted October 10, 2016 4 hours ago, jerome said: If, by different materials, you mean different textures/images, you could use the SPS. The SPS uses only one material but you can assign different sub-parts from a single image (texture) to different particles like you would do with a texture atlas. http://doc.babylonjs.com/overviews/Solid_Particle_System#uvs Clones are an easy way to duplicate meshes but they lead to as many draw calls as the number of clones. Instances or SPS only makes one draw call. I'm not sure if I understood properly, but is there a way for me to use instances and give different colours to the different instances ? e.g: planets and according to their temperature change their colours. Thanks everyone for the feedback , it's really helpful to be pushed in some directions I would have not thought of quickly on my own Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 the SPS allows to have different colors per solid particles (planets...) you'll have to read the doc and look at the PG examples inside: http://doc.babylonjs.com/overviews/Solid_Particle_System example with different colors and textures per particle : http://www.babylonjs-playground.com/#WCDZS#8 kevinImperial 1 Quote Link to comment Share on other sites More sharing options...
kevinImperial Posted October 10, 2016 Author Share Posted October 10, 2016 Alright, so it is not possible with instances ? I guess SPS works fine too, I am just a bit afraid of functionalities that I would expect from a regular mesh that I would lose for some reason with the SPS. Such as clicking on a distinct mesh, etc. Quote Link to comment Share on other sites More sharing options...
jerome Posted October 10, 2016 Share Posted October 10, 2016 I'm not expert with instances, so I can't answer correctly. If you have a look at the SPS features, you'll see that there's some support for particle picking. Quote Link to comment Share on other sites More sharing options...
kevinImperial Posted October 10, 2016 Author Share Posted October 10, 2016 Alright, sounds great ! I tried something like: scene.onPointerDown, but it didn't give me anything back with the particles. Will have a deeper look into the SPS features. Thanks a lot ! Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 11, 2016 Share Posted October 11, 2016 @kevinImperial if you click on a particle in this version http://www.babylonjs-playground.com/#1LIVJD#4 and have a look in the console you will get the id and position of the particle. 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.