# MultiMaterial Solid Particle System Breaks Picking

**URL:** <https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784>\
**Category:** Bugs\
**Tags:** particles\
**Created:** [February 25, 2020, 4:37pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784 "2020-02-25T16:37:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickhod](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nickhod/32/3824_2.png) [@nickhod](https://forum.babylonjs.com/u/nickhod)\
**Post date:** [February 25, 2020, 4:37pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/1 "2020-02-25T16:37:43Z")

</div>

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

[https://www.babylonjs-playground.com/##FV07BC](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?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 25, 2020, 4:38pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/2 "2020-02-25T16:38:20Z")

</div>

Pinging @jerome

---

<div class="post-metadata">

**Author:** ![nickhod](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nickhod/32/3824_2.png) [@nickhod](https://forum.babylonjs.com/u/nickhod)\
**Post date:** [February 25, 2020, 11:59pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/3 "2020-02-25T23:59:42Z")

</div>

@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](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.

---

<div class="post-metadata">

**Author:** ![nickhod](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nickhod/32/3824_2.png) [@nickhod](https://forum.babylonjs.com/u/nickhod)\
**Post date:** [February 26, 2020, 2:13am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/4 "2020-02-26T02:13:11Z")

</div>

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

[https://www.babylonjs-playground.com/#P1D4TZ#2](https://www.babylonjs-playground.com/#P1D4TZ#2)

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [February 26, 2020, 8:19am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/5 "2020-02-26T08:19:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [March 4, 2020, 7:43am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/6 "2020-03-04T07:43:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidworkman9](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidworkman9/32/2373_2.png) [@davidworkman9](https://forum.babylonjs.com/u/davidworkman9)\
**Post date:** [March 4, 2021, 4:10am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/7 "2021-03-04T04:10:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [March 5, 2021, 9:11am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/8 "2021-03-05T09:11:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidworkman9](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidworkman9/32/2373_2.png) [@davidworkman9](https://forum.babylonjs.com/u/davidworkman9)\
**Post date:** [March 8, 2021, 3:31pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/9 "2021-03-08T15:31:48Z")

</div>

Hi @jerome , I was able to reproduce this in the playground here:  
[https://playground.babylonjs.com/#A5JH0F#8](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.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [March 8, 2021, 3:57pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/10 "2021-03-08T15:57:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidworkman9](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidworkman9/32/2373_2.png) [@davidworkman9](https://forum.babylonjs.com/u/davidworkman9)\
**Post date:** [March 8, 2021, 4:38pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/11 "2021-03-08T16:38:45Z")

</div>

Fair enough. I’ve removed the toggling ability:

[https://playground.babylonjs.com/#A5JH0F#11](https://playground.babylonjs.com/#A5JH0F#11)

---

<div class="post-metadata">

**Author:** ![nickhod](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nickhod/32/3824_2.png) [@nickhod](https://forum.babylonjs.com/u/nickhod)\
**Post date:** [March 8, 2021, 4:49pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/12 "2021-03-08T16:49:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidworkman9](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidworkman9/32/2373_2.png) [@davidworkman9](https://forum.babylonjs.com/u/davidworkman9)\
**Post date:** [March 8, 2021, 6:04pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/13 "2021-03-08T18:04:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [March 9, 2021, 2:16pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/14 "2021-03-09T14:16:41Z")

</div>

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](https://github.com/BabylonJS/Babylon.js/pull/8059)  
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 ☹

---

<div class="post-metadata">

**Author:** ![davidworkman9](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidworkman9/32/2373_2.png) [@davidworkman9](https://forum.babylonjs.com/u/davidworkman9)\
**Post date:** [March 9, 2021, 2:38pm UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/15 "2021-03-09T14:38:50Z")

</div>

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

 ![Screen Shot 2021-03-09 at 9.36.43 AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/b/b407ee5bd2832b2eedf978e4b2c934ff95e6f654.png)

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.

---

<div class="post-metadata">

**Author:** ![jerome](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jerome/32/22_2.png) [@jerome](https://forum.babylonjs.com/u/jerome)\
**Post date:** [March 10, 2021, 8:24am UTC](https://forum.babylonjs.com/t/multimaterial-solid-particle-system-breaks-picking/8784/16 "2021-03-10T08:24:47Z")

</div>

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