diff --git a/packages/dev/core/src/Collisions/gpuPicker.ts b/packages/dev/core/src/Collisions/gpuPicker.ts index 950573a2777..28730a21d2f 100644 --- a/packages/dev/core/src/Collisions/gpuPicker.ts +++ b/packages/dev/core/src/Collisions/gpuPicker.ts @@ -11,6 +11,7 @@ import type { IColor3Like, IVector2Like } from "core/Maths/math.like"; import type { AbstractMesh } from "core/Meshes/abstractMesh"; import { VertexBuffer } from "core/Meshes/buffer"; import type { Mesh } from "core/Meshes/mesh"; +import type { InstancedMesh } from "core/Meshes/instancedMesh"; import { Logger } from "core/Misc/logger"; import type { Scene } from "core/scene"; import type { Nullable } from "core/types"; @@ -317,13 +318,14 @@ export class GPUPicker { id++; if (mesh.hasInstances) { - const instances = (mesh as Mesh).instances; - const colorData = this._generateColorData(instances.length, id, index, GPUPicker._TempColor.r, GPUPicker._TempColor.g, GPUPicker._TempColor.b, (i, id) => { - const instance = instances[i]; + const numInstances = (mesh as Mesh).instances.filter((instance) => this._pickableMeshes.indexOf(instance) !== -1).length; + const allInstancesForPick = this._pickableMeshes.filter((m) => m.isAnInstance && (m as InstancedMesh).sourceMesh === mesh); + const colorData = this._generateColorData(numInstances, id, index, GPUPicker._TempColor.r, GPUPicker._TempColor.g, GPUPicker._TempColor.b, (i, id) => { + const instance = allInstancesForPick[i]; this._idMap[id] = this._pickableMeshes.indexOf(instance); }); - id += instances.length; + id += numInstances; const engine = mesh.getEngine(); const buffer = new VertexBuffer(engine, colorData, this._attributeName, false, false, 4, true);