Thin instances intersection with static objets in the scene

Hello everyone!
Does anyone know how to calculate thin instances intersection with another static mesh on a scene?
What im trying to do is filling the scene with thin instances of Box (fill whole scene by its boundingbox) and *exclude intersecting instances. By excluding I mean set custom attribute, or just turn alpha to 0 on the instance.

How Im trying to reach that:

    const wall = scene.getMeshByName('wall');
    let count = 0;

    const wallBBOX = wall.getBoundingInfo();
        

    const voxelHalf = voxelSize / 2;
    vox.thinInstanceGetWorldMatrices().forEach((matrix, index) => {
        const x = matrix.m[12];
        const y = matrix.m[13];
        const z = matrix.m[14];
        const maximum = new BABYLON.Vector3(x + voxelHalf, y + voxelHalf, z + voxelHalf);
        const minimum = new BABYLON.Vector3(x - voxelHalf, y - voxelHalf, z - voxelHalf);


        const thinBBox = new BABYLON.BoundingInfo(minimum, maximum, matrix);
        
        
        if (wallBBOX.intersects(thinBBox, false)) {
            vox.thinInstanceSetAttributeAt("color", index, [0, 0, 0, 0]);
            console.log('intersection');
        }
    })

Ok, I gust realized that i was thinking the right way

Seems like I need to do smth with world matrix so the “instances filling” starts from “scene bounding box minimum”.
The intersected part has alpha 0 on the picute :).

PS. The hole looks weird, curious why I can see all the way through the instances.

Ok, I made it by adding scene bounding box (x, y, z) for each step of filling instances buffer:

    let matricesData = new Float32Array(16 * instanceCount);
    let colorData = new Float32Array(4 * instanceCount);
    let collisionData = new Float32Array(instanceCount);

    for (var x = 0; x < _width; x++) {
        // @ts-ignore
        identityMatrix.m[12] = (-size / 2 + ofst * x) + bbox.minimum.x;
  
        for (var y = 0; y < _height; y++) {
        // @ts-ignore
            identityMatrix.m[13] = (-size / 2 + ofst * y) + bbox.minimum.y;
            for (var z = 0; z < _depth; z++) {
            // @ts-ignore
                identityMatrix.m[14] = (-size / 2 + ofst * z) + bbox.minimum.z;

                identityMatrix.copyToArray(matricesData, index * 16);
                
                collisionData[index] = 0;
                
                const r = Math.random();
                const g = Math.random();
                const b = Math.random();
                const alpha = 1;
                new BABYLON.Color4(r, g, b, alpha).toArray(colorData, 4 * index);

                index++;
                col += 0xffffff / instanceCount;
            }
        }
    }

Still curious why my instances (when their alpha turns from 0 to 1) half-transparent?

Looks like there already was the answer.
https://forum.babylonjs.com/t/different-transparency-while-using-thin-instances/30574/10

Nice job on finding all your answers :slight_smile:

1 Like