Skip to content

Commit a1f5874

Browse files
authored
Fix gpu picking instances when instances are disposed (#16528)
From: https://forum.babylonjs.com/t/removing-thin-instances-vs-gpupicker/57989/5 Disposing an instance fouls up the gpu picking result. This is due to changes in the order of instances in `mesh.instances` . Fixed correct mapping of color id of instances to pickable instances by checking against `_pickableMeshes` instead.
1 parent 40ec346 commit a1f5874

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/dev/core/src/Collisions/gpuPicker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { IColor3Like, IVector2Like } from "core/Maths/math.like";
1111
import type { AbstractMesh } from "core/Meshes/abstractMesh";
1212
import { VertexBuffer } from "core/Meshes/buffer";
1313
import type { Mesh } from "core/Meshes/mesh";
14+
import type { InstancedMesh } from "core/Meshes/instancedMesh";
1415
import { Logger } from "core/Misc/logger";
1516
import type { Scene } from "core/scene";
1617
import type { Nullable } from "core/types";
@@ -317,13 +318,14 @@ export class GPUPicker {
317318
id++;
318319

319320
if (mesh.hasInstances) {
320-
const instances = (mesh as Mesh).instances;
321-
const colorData = this._generateColorData(instances.length, id, index, GPUPicker._TempColor.r, GPUPicker._TempColor.g, GPUPicker._TempColor.b, (i, id) => {
322-
const instance = instances[i];
321+
const numInstances = (mesh as Mesh).instances.filter((instance) => this._pickableMeshes.indexOf(instance) !== -1).length;
322+
const allInstancesForPick = this._pickableMeshes.filter((m) => m.isAnInstance && (m as InstancedMesh).sourceMesh === mesh);
323+
const colorData = this._generateColorData(numInstances, id, index, GPUPicker._TempColor.r, GPUPicker._TempColor.g, GPUPicker._TempColor.b, (i, id) => {
324+
const instance = allInstancesForPick[i];
323325
this._idMap[id] = this._pickableMeshes.indexOf(instance);
324326
});
325327

326-
id += instances.length;
328+
id += numInstances;
327329
const engine = mesh.getEngine();
328330

329331
const buffer = new VertexBuffer(engine, colorData, this._attributeName, false, false, 4, true);

0 commit comments

Comments
 (0)