Selected meshes

Hello Everyone

I have 4000 cubes on the scene and I want to select some of them interactively.
I create a box with Gizmo that can be modify in terms of locating and scaling. I want to select all the meshes that are inside the box and then deleted rest of the cubes outside the box . how can i do this, please help me to solve it.

You will know the dimensions of the box with Gizmo (box G) then place the cubes into two arrays one for in the box G one for outside box G. A cube will be inside box G when for each axis

min of box G < min of cube and max of cube < max of box G

1 Like

Thank you for helping, but i did not understand what should i do exactly, could you please explain it with more detail?

It would be easier if you created a playground with 5 or 6 cubes and a box with Gizmo that you would use to select some cubes.

2 Likes

thank you so much.

i have another question, should i use box G dimension or position? is it possible to show it to me for 5 cubes and one box G?

You need both


Yes when you have made a playground with 5 cubes and one box G

thank you so much,

https://playground.babylonjs.com/#K05LU3#6

Taking into account the scaling of box G then a cube will be inside boxG ‘inside = true’

boxG.computeWorldMatrix(true);  //force any transformations
let inX = boxG.getBoundingInfo().boundingBox.minimumWorld.x < box.getBoundingInfo().boundingBox.minimumWorld.x && box.getBoundingInfo().boundingBox.maximumWorld.x < boxG.getBoundingInfo().boundingBox.maximumWorld

//repeat for y and z and then

let inside = inX && inY && inZ

3 Likes

thank you so much

sorry for my question, in the above code boxG is refer to my arrays of cube?

other box better as cube

boxG.computeWorldMatrix(true);  //force any transformations
let inX = boxG.getBoundingInfo().boundingBox.minimumWorld.x < cube.getBoundingInfo().boundingBox.minimumWorld.x && cube.getBoundingInfo().boundingBox.maximumWorld.x < boxG.getBoundingInfo().boundingBox.maximumWorld

//repeat for y and z and then

let inside = inX && inY && inZ

sorry again.

i do it in this form, is it right?

var cube1 = BABYLON.MeshBuilder.CreateBox(“box”, { size: 1 }, scene);
cube1.position.x = 2;
var cube2 = BABYLON.MeshBuilder.CreateBox(“bo2”, { size: 1 }, scene);
cube2.position.x = 4;
var cube3 = BABYLON.MeshBuilder.CreateBox(“box3”, { size: 1 }, scene);
cube3.position.x = 6;
var cube4 = BABYLON.MeshBuilder.CreateBox(“box4”, { size: 1 }, scene);
cube4.position.x = -2;

var cube5 = BABYLON.MeshBuilder.CreateBox(“box5”, { size: 1 }, scene);
cube5.position.x = -4;

var mat = new BABYLON.StandardMaterial(“mat”, scene);
mat.diffuseColor = new BABYLON.Color3(0, 0, 1);
cube3.material = mat;
var boxG = BABYLON.MeshBuilder.CreateBox(
“box”,
{ height: 1, width: 1, depth: 1 },
scene
);
boxG.position.y = 2;

const mat2 = new BABYLON.StandardMaterial(“mat2”, scene);
mat2.diffuseColor = new BABYLON.Color3(1, 0, 0);
boxGmaterial = mat2;

// initialize GizmoManager
var gizmoManager = new BABYLON.GizmoManager(scene);
gizmoManager.boundingBoxGizmoEnable = true;
// gizmoManager.attachableMesh = box;
gizmoManager.positionGizmoEnabled = true;
gizmoManager.boundingBoxGizmoEnabled = true;
gizmoManager.usePointerToAttachGizmos = false;
gizmoManager.attachToMesh(boxG);
boxG.visibility = 0.3;

boxG.computeWorldMatrix(true); //force any transformations
let inX =
boxG.getBoundingInfo().boundingBox.minimumWorld.x <
boxG.getBoundingInfo().boundingBox.minimumWorld.x <
cube1.getBoundingInfo().boundingBox.minimumWorld.x &&
cube1.getBoundingInfo().boundingBox.maximumWorld.x <
boxG.getBoundingInfo().boundingBox.maximumWorld;

let inY =
boxG.getBoundingInfo().boundingBox.minimumWorld.y <
cube1.getBoundingInfo().boundingBox.minimumWorld.y &&
cube1.getBoundingInfo().boundingBox.maximumWorld.y <
boxG.getBoundingInfo().boundingBox.maximumWorld;

let inZ =
boxG.getBoundingInfo().boundingBox.minimumWorld.z <
cube1.getBoundingInfo().boundingBox.minimumWorld.z &&
cube1.getBoundingInfo().boundingBox.maximumWorld.z <
boxG.getBoundingInfo().boundingBox.maximumWorld;

let inside = inX && inY && inZ;

Looks OK. You need to try it in a PG and see if you get any error messages.

sorry one more question , for my project i should put these codes in a loop?

IMHO you should only call it when needed, ie when boxG is in the position you want it to be. So use some sort of event to trigger, for example from a GUI button.

1 Like

tnx a lot

sorry for taking your precious time again, i did it, and then i face to another problem,
when I want to delete cubes out of boxG , it is not very accurate.

what should i do to get exact cubes inside the boxG.

Possibly you could rotate boxG to match the orientation? You might have to consider maximum and minimums along local axes rather than just from the boundingBox. It is getting rather complicated. It is difficult to advise without knowing more detail about your usage.

the problem is when i drag boxG on screen , then it start to rotate when goes to the edge of screen, is it possible to fix it ?