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

Update the AABB during rendering only if it hasn't already been updated in the current frame #7296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/framework/lightmapper/lightmapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ class Lightmapper {

const meshInstances = bakeNode.meshInstances;
for (let i = 0; i < meshInstances.length; i++) {
if (meshInstances[i]._isVisible(shadowCam)) {
if (meshInstances[i]._isVisible(shadowCam, this.renderer._aabbUpdateIndex)) {
nodeVisible = true;
break;
}
Expand Down
13 changes: 10 additions & 3 deletions src/scene/mesh-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ class MeshInstance {
*/
_calculateSortDistance = null;

/** @private */
_aabbUpdateIndex = 0;

/**
* Create a new MeshInstance instance.
*
Expand Down Expand Up @@ -942,10 +945,11 @@ class MeshInstance {
* date, which forward-renderer takes care of. This function should not be called elsewhere.
*
* @param {Camera} camera - The camera to test visibility against.
* @param {number} aabbUpdateIndex - An index used to detect if AABB has already been updated this frame.
* @returns {boolean} - True if the mesh instance is visible by the camera, false otherwise.
* @ignore
*/
_isVisible(camera) {
_isVisible(camera, aabbUpdateIndex) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use _isVisible(camera, aabbUpdateIndex=NaN) { here?


if (this.visible) {

Expand All @@ -954,8 +958,11 @@ class MeshInstance {
return this.isVisibleFunc(camera);
}

_tempSphere.center = this.aabb.center; // this line evaluates aabb
_tempSphere.radius = this._aabb.halfExtents.length();
const aabb = this._aabbUpdateIndex === aabbUpdateIndex ? this._aabb : this.aabb; // this line evaluates aabb
this._aabbUpdateIndex = aabbUpdateIndex;

_tempSphere.center = aabb.center;
_tempSphere.radius = aabb.halfExtents.length();

return camera.frustum.containsSphere(_tempSphere) > 0;
}
Expand Down
7 changes: 6 additions & 1 deletion src/scene/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class Renderer {

blueNoise = new BlueNoise(123);

/** @private */
_aabbUpdateIndex = 0;

/**
* Create a new instance.
*
Expand Down Expand Up @@ -916,7 +919,7 @@ class Renderer {
const drawCall = drawCalls[i];
if (drawCall.visible) {

const visible = !doCull || !drawCall.cull || drawCall._isVisible(camera);
const visible = !doCull || !drawCall.cull || drawCall._isVisible(camera, this._aabbUpdateIndex);
if (visible) {
drawCall.visibleThisFrame = true;

Expand Down Expand Up @@ -1131,6 +1134,8 @@ class Renderer {
const cullTime = now();
// #endif

this._aabbUpdateIndex++;

const { scene } = this;

this.processingMeshInstances.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/scene/renderer/shadow-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ShadowRenderer {
const meshInstance = meshInstances[i];

if (meshInstance.castShadow) {
if (!meshInstance.cull || meshInstance._isVisible(camera)) {
if (!meshInstance.cull || meshInstance._isVisible(camera, this.renderer._aabbUpdateIndex)) {
meshInstance.visibleThisFrame = true;
visible.push(meshInstance);
}
Expand Down
Loading