MultiMaterial Solid Particle System Breaks Picking

When using a SolidParticleSystem with enableMultiMaterial = true, picking doesn’t work correctly when using .pickedParticles as a lookup.

https://www.babylonjs-playground.com/##FV07BC

I guess this is due to the sub meshes involved.

(If you set enableMultiMaterial: false in the above Playground, picking will work again).

Is there any temporary workaround for this?

Pinging @jerome

@jerome

As I needed to find a quick fix for this for my work, I came up with a solution by creating my own indices to particle lookup rather than using .pickedParticles.

This playground shows it working. The relevant comments start with // ***

https://www.babylonjs-playground.com/#P1D4TZ

After reading the code for SolidParticle I had to use the private variable particle._ind to achieve that, so I don’t think it’s a good solution.

sps.multiMaterialPickedParticle(pickingInfo) or something like that would be better.

1 Like

Amended version that will work with any shape. Using a Box to test

https://www.babylonjs-playground.com/#P1D4TZ#2

1 Like

Nice fix.
Well, you’re right, this feature doesn’t work correctly for MultiMaterials. I’ll have a look how to fix this, maybe this next week.

Working on it

[EDIT] done. Now, the fixing will work even with MultiMaterials by using the new method sps.pickedParticle(pickingInfo) returning {idx: number, faceId: number}
idx being the particle index
faceId being the picked face index within the particle geometry

PR soon

2 Likes

Should this fix be in 4.2.0? I’m currently getting incorrect idx values coming back from pickedParticle with enableMultiMaterial set to true on it.

I remember this was fixed. Do you have a repro still having the bug ?

Hi @jerome , I was able to reproduce this in the playground here:
https://playground.babylonjs.com/#A5JH0F#8

If you click the center black portion you’ll get the correct index always, but if you click the grey sides you sometimes will get the correct index, but usually get a different one than then one you clicked.

Indeed your example shows a behavior different from the one you expect.
That said, it’s quite a complicated example because it uses several processes of your own, like testing the inclusion of the particle index in an array, filtering and managing this array, etc.

Don’t you have please a simpler example without all the added process (I mean with only things related to the SPS and Multimaterial and no extra custom particle management) ?
From this PG, I just can’t check if there’s a bug in the SPS or in your particle management… sorry

1 Like

Fair enough. I’ve removed the toggling ability:

https://playground.babylonjs.com/#A5JH0F#11

I was the starter of this thread. As it happens I’m moving my project to Babylon 4.2 from 4.1 this week.

Previously I was using my own custom SolidParticleSystem based on a 4.2-beta version with some of my fixes.

Having tried out the stock 4.2 one today (prompted by this thread), I’ve noticed the following bug in solidParticleSystem.ts.

public pickedParticle(pickingInfo: PickingInfo): Nullable<{idx: number, faceId: number}> {
    if (pickingInfo.hit) {
        const subMesh = pickingInfo.subMeshId;
        const faceId = pickingInfo.faceId;
        const picked = this.pickedBySubMesh;
        if (picked[subMesh] && picked[subMesh][faceId]) {
            return picked[subMesh][faceId];
        }
    }
    return null;
}

I believe

        if (picked[subMesh] && picked[subMesh][faceId]) {
            return picked[subMesh][faceId];
        }

should be something like

        if (picked[subMesh] && picked[subMesh][pickingInfo.subMeshFaceId]) {
            return picked[subMesh][pickingInfo.subMeshFaceId];
        }

If I force

pickResult.faceId = pickResult.subMeshFaceId;

before attempting to call pickedParticle() everything works. If not, I get version null reference errors.

1 Like

Your forced subMeshFaceId to faceId also fixes my problem:
https://playground.babylonjs.com/#A5JH0F#12

Actually this bug was solved here if I’m not wrong : fix bug in picking on expandable SPS by jbousquie · Pull Request #8059 · BabylonJS/Babylon.js · GitHub
But i don’t know if the PR was then merged.

If not, maybe someone could replicate the fix (only some lines of code) in the main repro, please.
I have no time to code for BJS unfortunatelly and I don’t have the dev platform on my current computer :frowning:

I’ve adjusted the main repro to include nickhod’s fix:
https://www.babylonjs-playground.com/#FV07BC#2
Here’s the diff:

Comment out line 72 to get the buggy behaviour.

Edit: wrong link. Also note that the playground has to manually be changed to 4.2.0.

1 Like

Nice fix :slight_smile:
Did you check that the picking still works the same way with simple materials (not multi) ?