Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hit detection in a multi-cam scenario with billboarded meshes. #13479

Merged
merged 3 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/dev/core/src/Culling/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ Scene.prototype._internalPick = function (
): PickingInfo {
let pickingInfo = null;

const computeWorldMatrixForCamera = !!(this.activeCameras && this.activeCameras.length > 1 && this.cameraToUseForPointers !== this.activeCamera);
const currentCamera = this.cameraToUseForPointers || this.activeCamera;
sebavan marked this conversation as resolved.
Show resolved Hide resolved

for (let meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
const mesh = this.meshes[meshIndex];

Expand All @@ -798,7 +801,8 @@ Scene.prototype._internalPick = function (
continue;
}

const world = mesh.getWorldMatrix();
const forceCompute = computeWorldMatrixForCamera && mesh.isWorldMatrixCameraDependent();
const world = mesh.computeWorldMatrix(forceCompute, currentCamera);
sebavan marked this conversation as resolved.
Show resolved Hide resolved

if (mesh.hasThinInstances && (mesh as Mesh).thinInstanceEnablePicking) {
// first check if the ray intersects the whole bounding box/sphere of the mesh
Expand Down Expand Up @@ -850,6 +854,8 @@ Scene.prototype._internalMultiPick = function (
return null;
}
const pickingInfos = new Array<PickingInfo>();
const computeWorldMatrixForCamera = !!(this.activeCameras && this.activeCameras.length > 1 && this.cameraToUseForPointers !== this.activeCamera);
const currentCamera = this.cameraToUseForPointers || this.activeCamera;

for (let meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
const mesh = this.meshes[meshIndex];
Expand All @@ -862,7 +868,8 @@ Scene.prototype._internalMultiPick = function (
continue;
}

const world = mesh.getWorldMatrix();
const forceCompute = computeWorldMatrixForCamera && mesh.isWorldMatrixCameraDependent();
const world = mesh.computeWorldMatrix(forceCompute, currentCamera);

if (mesh.hasThinInstances && (mesh as Mesh).thinInstanceEnablePicking) {
const result = this._internalPickForMesh(null, rayFunction, mesh, world, true, true, trianglePredicate);
Expand Down
31 changes: 24 additions & 7 deletions packages/dev/core/src/Meshes/transformNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export class TransformNode extends Node {
return;
}
this._billboardMode = value;
this._cache.useBillboardPosition = (this._billboardMode & TransformNode.BILLBOARDMODE_USE_POSITION) !== 0;
this._computeUseBillboardPath();
}

private _preserveParentRotationForBillboard = false;
Expand All @@ -108,6 +110,11 @@ export class TransformNode extends Node {
return;
}
this._preserveParentRotationForBillboard = value;
this._computeUseBillboardPath();
}

private _computeUseBillboardPath(): void {
this._cache.useBillboardPath = this._billboardMode !== TransformNode.BILLBOARDMODE_NONE && !this.preserveParentRotationForBillboard;
}

/**
Expand Down Expand Up @@ -339,6 +346,8 @@ export class TransformNode extends Node {
cache.localMatrixUpdated = false;
cache.billboardMode = -1;
cache.infiniteDistance = false;
cache.useBillboardPosition = false;
cache.useBillboardPath = false;
}

/**
Expand Down Expand Up @@ -1008,12 +1017,22 @@ export class TransformNode extends Node {
return this.parent;
}

/**
* Returns whether the transform node world matrix computation needs the camera information to be computed.
* This is the case when the node is a billboard or has an infinite distance for instance.
* @returns true if the world matrix computation needs the camera information to be computed
*/
public isWorldMatrixCameraDependent(): boolean {
return (this._infiniteDistance && !this.parent) || (this._billboardMode !== TransformNode.BILLBOARDMODE_NONE && !this.preserveParentRotationForBillboard);
}

/**
* Computes the world matrix of the node
* @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
* @param camera defines the camera used if different from the scene active camera (This is used with modes like Billboard or infinite distance)
* @returns the world matrix
*/
public computeWorldMatrix(force?: boolean): Matrix {
public computeWorldMatrix(force: boolean = false, camera: Nullable<Camera> = null): Matrix {
if (this._isWorldMatrixFrozen && !this._isDirty) {
return this._worldMatrix;
}
Expand All @@ -1024,9 +1043,7 @@ export class TransformNode extends Node {
return this._worldMatrix;
}

const camera = <Camera>this.getScene().activeCamera;
const useBillboardPosition = (this._billboardMode & TransformNode.BILLBOARDMODE_USE_POSITION) !== 0;
const useBillboardPath = this._billboardMode !== TransformNode.BILLBOARDMODE_NONE && !this.preserveParentRotationForBillboard;
camera = camera || this.getScene().activeCamera;

this._updateCache();
const cache = this._cache;
Expand Down Expand Up @@ -1106,7 +1123,7 @@ export class TransformNode extends Node {
if (force) {
parent.computeWorldMatrix(force);
}
if (useBillboardPath) {
if (cache.useBillboardPath) {
if (this._transformToBoneReferal) {
parent.getWorldMatrix().multiplyToRef(this._transformToBoneReferal.getWorldMatrix(), TmpVectors.Matrix[7]);
} else {
Expand Down Expand Up @@ -1142,7 +1159,7 @@ export class TransformNode extends Node {
}

// Billboarding based on camera orientation (testing PG:http://www.babylonjs-playground.com/#UJEIL#13)
if (useBillboardPath && camera && this.billboardMode && !useBillboardPosition) {
if (cache.useBillboardPath && camera && this.billboardMode && !cache.useBillboardPosition) {
const storedTranslation = TmpVectors.Vector3[0];
this._worldMatrix.getTranslationToRef(storedTranslation); // Save translation

Expand Down Expand Up @@ -1177,7 +1194,7 @@ export class TransformNode extends Node {
this._worldMatrix.setTranslation(TmpVectors.Vector3[0]);
}
// Billboarding based on camera position
else if (useBillboardPath && camera && this.billboardMode && useBillboardPosition) {
else if (cache.useBillboardPath && camera && cache.useBillboardPosition) {
const storedTranslation = TmpVectors.Vector3[0];
// Save translation
this._worldMatrix.getTranslationToRef(storedTranslation);
Expand Down