OK, I found a “hack/solution”. Fell free to use it to find a more permanent solution in BabalonJS

I overrided Ray.TransformToRef to store the matrix of the ray space and it’s inverse (four last lines)

```
BABYLON.Ray.TransformToRef = (ray: BABYLON.DeepImmutable<BABYLON.Ray>, matrix:
BABYLON.DeepImmutable<BABYLON.Matrix>, result: BABYLON.Ray) => {
BABYLON.Vector3.TransformCoordinatesToRef(ray.origin, matrix, result.origin);
BABYLON.Vector3.TransformNormalToRef(ray.direction, matrix, result.direction);
result.length = ray.length;
var dir = result.direction;
var len = dir.length();
if (!(len === 0 || len === 1)) {
var num = 1.0 / len;
dir.x *= num;
dir.y *= num;
dir.z *= num;
result.length *= len;
}
if (!ray["matrix"]) { ray["matrix"] = BABYLON.Matrix.Identity(); ray["inv_matrix"] = BABYLON.Matrix.Identity(); }
result["matrix"] = ray["matrix"].multiply(matrix);
result["inv_matrix"] = BABYLON.Matrix.Identity();
result["matrix"].invertToRef(result["inv_matrix"]);
}
```

Then I used

```
let pickingRay = scene.createPickingRay( X, Y, null, camera);
let picking = scene.pickWithRay(pickingRay, null, false, (p0, p1, p2, ray) => {
let m: BABYLON.Matrix = ray["inv_matrix"];
if (m) {
let wp0 = BABYLON.Vector3.TransformCoordinates(p0, m);
let wp1 = BABYLON.Vector3.TransformCoordinates(p1, m);
let wp2 = BABYLON.Vector3.TransformCoordinates(p2, m);
let not_clipped = true;
if (scene.clipPlane) {
not_clipped = not_clipped
&& scene.clipPlane.signedDistanceTo(wp0) < 0
&& scene.clipPlane.signedDistanceTo(wp1) < 0
&& scene.clipPlane.signedDistanceTo(wp2) < 0;
}
return not_clipped;
}
else
return true;
});
```

Note : it does not work with a simple “scene.pick”, the TransformToRef is never called, I guess the ray managment is done diffently for some unknown reason.

Note 2 : I don’t manage “on the fence” triangles. I can only collide with triangles fully visible. That’s ok in my current application, but it’s a limitation.

As far as I’m concerned, it’s ok for now.

Maybe I’ll remove this “hack” if babylon fix this bug.

M.