Skip to content

Commit

Permalink
Address feedback (#15176)
Browse files Browse the repository at this point in the history
* Address feedback

* Add IGPUPickingInfo to be future proof

* .
  • Loading branch information
deltakosh authored Jun 7, 2024
1 parent 052960e commit 79c3929
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
27 changes: 21 additions & 6 deletions packages/dev/core/src/Collisions/gpuPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import type { Nullable } from "core/types";
import "../Shaders/picking.fragment";
import "../Shaders/picking.vertex";

/**
* Class used to store the result of a GPU picking operation
*/
export interface IGPUPickingInfo {
/**
* Picked mesh
*/
mesh: AbstractMesh;
}

/**
* Class used to perform a picking operation using GPU
* Please note that GPUPIcker cannot pick instances, only meshes
Expand All @@ -26,9 +36,12 @@ export class GPUPicker {
private _pickableMeshes: Array<AbstractMesh>;
private _readbuffer: Uint8Array;
private _meshRenderingCount: number = 0;
private _attributeName = "instanceMeshID";
private readonly _attributeName = "instanceMeshID";

private _createRenderTarget(scene: Scene, width: number, height: number) {
if (this._pickingTexure) {
this._pickingTexure.dispose();
}
this._pickingTexure = new RenderTargetTexture(
"pickingTexure",
{ width: width, height: height },
Expand Down Expand Up @@ -88,7 +101,7 @@ export class GPUPicker {
(mesh as Mesh).removeVerticesData(this._attributeName);
}
if (this._pickingTexure) {
this._pickingTexure!.setMaterialForRendering(mesh, undefined);
this._pickingTexure.setMaterialForRendering(mesh, undefined);
}
}
this._pickableMeshes.length = 0;
Expand All @@ -114,7 +127,6 @@ export class GPUPicker {
const size = this._pickingTexure.getSize();

if (size.width !== rttSizeW || size.height !== rttSizeH || this._cachedScene !== scene) {
this._pickingTexure.dispose();
this._createRenderTarget(scene, rttSizeW, rttSizeH);
}
}
Expand Down Expand Up @@ -181,7 +193,7 @@ export class GPUPicker {
* @param disposeWhenDone defines a boolean indicating we do not want to keep resources alive (false by default)
* @returns A promise with the picking results
*/
public pickAsync(x: number, y: number, disposeWhenDone = false): Promise<Nullable<AbstractMesh>> {
public pickAsync(x: number, y: number, disposeWhenDone = false): Promise<Nullable<IGPUPickingInfo>> {
if (!this._pickableMeshes || this._pickableMeshes.length === 0) {
return Promise.resolve(null);
}
Expand All @@ -199,7 +211,6 @@ export class GPUPicker {
const size = this._pickingTexure!.getSize();

if (size.width !== rttSizeW || size.height !== rttSizeH) {
this._pickingTexure!.dispose();
this._createRenderTarget(scene, rttSizeW, rttSizeH);

this._pickingTexure!.renderList = [];
Expand Down Expand Up @@ -271,7 +282,11 @@ export class GPUPicker {
if (disposeWhenDone) {
this.dispose();
}
resolve(pickedMesh);
if (pickedMesh) {
resolve({ mesh: pickedMesh });
} else {
resolve(null);
}
}
};
});
Expand Down
6 changes: 5 additions & 1 deletion packages/dev/core/src/Shaders/picking.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ attribute vec4 instanceMeshID;

#include<bonesDeclaration>
#include<bakedVertexAnimationDeclaration>
#include<morphTargetsVertexGlobalDeclaration>
#include<morphTargetsVertexDeclaration>[0..maxSimultaneousMorphTargets]

// Uniforms

Expand All @@ -18,7 +20,9 @@ varying vec4 vMeshID;
#endif

void main(void) {


#include<morphTargetsVertexGlobal>
#include<morphTargetsVertex>[0..maxSimultaneousMorphTargets]
#include<instancesVertex>
#include<bonesVertex>
#include<bakedVertexAnimation>
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/tests/test/visualization/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"title": "GPUPicker",
"playgroundId": "#1RWFMT#1",
"renderCount": 20,
"referenceImage": "gpuPicker.png"
"referenceImage": "gpuPicker.png",
"excludedEngines": ["webgl1"]
},
{
"title": "Trailmesh - tapered and untapered",
Expand Down

0 comments on commit 79c3929

Please sign in to comment.