How can I `getBoundingInfo()` of particles in SPS?

Hello everyone

I have a model with more than 20,000 cubes, at first I try to use the instances to optimize it ( https://www.babylonjs-playground.com/#ZEZLAT#50 ) but the speed of rendering it was so terrible for more than 5000 cubes, then I try to use the SPS, one of my problem with SPS is the bounding box, because I need the bounding box of each cube. how can I add the bounding box to my model in SPS?

here is my SPS version of my block model: https://www.babylonjs-playground.com/#ZEZLAT#54

and here is my code :

var myAttributeFunction = function (particle, i) {
        // console.log(i, cubes[i].X);
        //console.log(particle);
     particle.position.x = parseFloat(cubes[i].X);
      particle.position.y = parseFloat(cubes[i].Y);
      particle.position.z = parseFloat(cubes[i].Z);
    //   console.log(particle.position);

      particle.scale.x = parseFloat(cubes[i].Xinc);
      particle.scale.y = parseFloat(cubes[i].Yinc);
      particle.scale.z = parseFloat(cubes[i].Zinc);

      avgX += cubes[i].X / cubes.length;
      avgY += cubes[i].Y / cubes.length;
      avgZ += cubes[i].Z / cubes.length;

      arrCubes.push(particle);


    };
    var SPS = new BABYLON.SolidParticleSystem("SPS", scene);
   const cube = new BABYLON.BoxBuilder.CreateBox(
      "cube",
      { height: 1, width: 1, depth: 1 },
      scene
    );
    SPS.addShape(cube, nb, { positionFunction: myAttributeFunction });
    const mat = new BABYLON.StandardMaterial("mat", scene);
    // mat.diffuseColor = new BABYLON.Color3.Lerp(color1, color2, cubes[i].G);
    mat.specularColor = new BABYLON.Color3(0, 0, 0);
    var mesh = SPS.buildMesh();
    mesh.material = mat;

    cube.dispose();

    var mesh = SPS.buildMesh();

    SPS.billboard = true;
    SPS.computeboundingbox = true;

    SPS.updateParticle = function (particle) {
    };
    SPS.setParticles();

You can pick individual SPS particles using Picking Solid Particles | Babylon.js Documentation

You can create your own boundingbox using BoundingBox | Babylon.js Documentation

You calculate the min and max values for the boundingbox from the picked particle.position and knowing particle.scale

1 Like

hi @JohnK , Thanks a lot.
for creating my own boundingbox I use the following code:

SPS.particles.forEach(particle => {
    particle = new BABYLON.BoundingBox({ min: BABYLON.Vector3(particle.position.x - 2.5, particle.position.y - 2.5, particle.position.z - 6), max: BABYLON.Vector3(particle.position.x + 2.5, particle.position.y + 2.5, particle.position.z + 6)} )
})

I use this but i got an error, which said that x is undefined. how can I set my own bounding box?

I use particle.setBoundingInfo(new BABYLON.BoundingInfo(newMin, newMax)); to set the my own bounding box for each cube, but again I got an error. is it possible help me know how can I set the bounding box for each cube?

Instead of creating a new boundingbox using a dummy mesh (cube) as a basis for the bounding box of a particle https://www.babylonjs-playground.com/#ZEZLAT#57

1 Like

Actually, in a pickable SPS, every solid particle is given its own boundingInfo :

particle._boundingInfo

1 Like

Thank you so much for correcting my PG :pray: :pray: :pray:

in my model I have also a Gizmo box which is used to delete the cube, I mean when my cubes are inside the Gizmo box I can push the delete button and deleted the cubes inside the box. I have built it with the aid of the instance, but the problem with instance was when the number of cubes go over 5000 there were lag on my rendering, so because of such a problem and in order to optimize the rendering speed I decided to use SPS to get high performance for my 20,000 cubes.

and now I don’t know how can I access to for example particle.getBoundingInfo().boundingBox.minimumWorld.x. because I need getBoundingInfo of each particle to use the Gizmo box to cut the Block model.

Thanks a lot for your help :pray:

I want to know is it possible to set my own bounding box for each particle. Because I need particle.getBoundingInfo().boundingBox of each particles.

Well, particle._boundingInfo.boundingBox should simply work

1 Like

but when I use particle._boundingInfo , it said particle._boundingInfo is undefined.

SPS.particles.forEach(particle => {

    console.log("a",particle._boundingInfo)
})

My bad, sorry…
The right parameter to enable particle bounding boxes is particleIntersection: true (not necesseraly a pickable SPS)

2 Likes

Thank you so much for helping me :pray: :pray: :pray: :pray:

1 Like

Hi @jerome , do you know if it’s possible to force to calculate the bounding box of a specitifc particle without setting particleIntersection to true?

This is to avoid the calculation of bounding box for each frame and particles, that has a performance cost, since my scene can have thousand of particles.

I need to get the bounding box of just one particle, to draw a selection box around it. I don’t need to calculate colisions.

Thanks!

I think that you can build your SPS with particleIntersection set to true so all the particle BBox will be instanciated.
Then you set it to true or false at any time (so for any frame you want) so computations are/aren’t done at will.

Thanks @jerome!

The particleIntersection is not a field of the SolidParticleSystem class, it’s only possible to set it in the constructor.

But there is a computeBoundingBox flag that can be set to false.

I’m turning this flag on/off when needed and its seems to be working (well, the bouding boxes are being calculated).

I didn’t measure the performance gain yet, I’ll try to create a very large SPS and see if it makes a noticeable change.

Best regards!