Hi all,
I’m trying to find a way to detect if my BoundingInfo
is inside a percentage of my camera view (for the sake of the example, say 20% of the height and 40% of the width)… I’m trying to retro-engineering how isInFrustm
works, but I’m quite new to BJS and a total newby to camera/frustum/matrix stuff… So this is what I’ve got so far:
- I’ve started from the standard way to check if mesh is in frustum (I didn’t find any existing thread/resource about my specific need), and found that it works perfectly well with BoundingInfo too:
var frustumPlanes = BABYLON.Frustum.GetPlanes(camera.getTransformMatrix());
mesh.isInFrustum(frustumPlanes)
- Which made me look at the source code of
Camera.getTransformMatrix()
, and then thought that all I needed to do was to do something like this:
class Camera() {
// Readaptation of `getTransformMatrix` method, temporary name
public getAdaptedTransformMatrix(widthPercentage: number, heightPercentage: number): Matrix {
// Only the construction of `_projectionMatrix` seems to be influenced by FoV values, so I guessed I could keep `_computedViewMatrix` as it is
return this._computedViewMatrix.multiply(this.getAdaptedProjectionMatrix(widthPercentage, heightPercentage))
}
}
- So here is my try to get a modified projection matrix:
class Camera {
// Readaptation of `getProjectionMatrix`, temporary name
public getAdaptedProjectionMatrix(widthPercentage: number, heightPercentage: number): Matrix {
let result: Matrix = new Matrix();
// I've removed a bunch of `camera._cache` logic since I don't think I need it
// Matrix
var engine = camera.getEngine();
var scene = camera.getScene();
if (camera.mode === Camera.PERSPECTIVE_CAMERA) {
if (camera.minZ <= 0) {
camera.minZ = 0.1;
}
const reverseDepth = engine.useReverseDepthBuffer;
let getProjectionMatrix: (fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed: boolean) => void;
if (scene.useRightHandedSystem) {
getProjectionMatrix = reverseDepth ? Matrix.PerspectiveFovReverseRHToRef : Matrix.PerspectiveFovRHToRef;
} else {
getProjectionMatrix = reverseDepth ? Matrix.PerspectiveFovReverseLHToRef : Matrix.PerspectiveFovLHToRef;
}
getProjectionMatrix(camera.fov * widthPercentage, // Apply percentage here
engine.getAspectRatio(camera) * (widthPercentage / heightPercentage), // Apply percentage here
camera.minZ,
camera.maxZ,
result,
camera.fovMode === Camera.FOVMODE_VERTICAL_FIXED);
} else {
var halfWidth = engine.getRenderWidth() * widthPercentage / 2.0; // Apply percentage here
var halfHeight = engine.getRenderHeight() * heightPercentage / 2.0; // Apply percentage here
if (scene.useRightHandedSystem) {
Matrix.OrthoOffCenterRHToRef(camera.orthoLeft ?? -halfWidth,
camera.orthoRight ?? halfWidth,
camera.orthoBottom ?? -halfHeight,
camera.orthoTop ?? halfHeight,
camera.minZ,
camera.maxZ,
result);
} else {
Matrix.OrthoOffCenterLHToRef(camera.orthoLeft ?? -halfWidth,
camera.orthoRight ?? halfWidth,
camera.orthoBottom ?? -halfHeight,
camera.orthoTop ?? halfHeight,
camera.minZ,
camera.maxZ,
result);
}
}
return result;
}
}
Unluckily when I now use my custom getAdaptedTransformMatrix(0.4, 0.2)
method, the isInFrustum
check behaves as if I gave it the “classic” camera transformMatrix instead (meaning that it returns true
as soon as my boundingInfo is in my screen, and I do not need to have it near the center of my screen as I would expect)… Any idea why?
P.s: I couldn’t add more than one empty line between phrases, any idea on how I could improve the formatting of this post?