Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get intersection element in BabylonJS

Is there a way to get the actual intersection of to geometries with BabylonJS?

E.g. the intersection point of a line and a plane, the line intersection of two planes, the arc intersection of a sphere and a plane, etc...

Thanks!

like image 557
leeodelion Avatar asked Oct 28 '25 17:10

leeodelion


1 Answers

I believe what you are looking for is the CSG (Constructive Solid Geometry) tools in Babylon.js. To use it you can reference this tutorial here. Essentially what you want to do is the following:

CSG intersect (modified code from the link)

// a and b can be any mesh you define
var a = BABYLON.Mesh.CreateBox("box", 500, scene); 
var b = BABYLON.Mesh.CreateBox("box", 500, scene);

// Convert to CSG meshes
var aCSG = BABYLON.CSG.FromMesh(a);
var bCSG = BABYLON.CSG.FromMesh(b);
var subCSG = bCSG.intersect(aCSG);

// Disposing original meshes since we don't want to see them on the scene
a.dispose();
b.dispose();

// Convert back to regular mesh from CSG mesh
subCSG.toMesh("csg", new BABYLON.StandardMaterial("mat", scene), scene);

For more uses of CSG you should check out the documentation.

like image 89
Svit Avatar answered Oct 30 '25 15:10

Svit