optimize checkEnterLeaveEntities() to do distance check and stationary checks less frequently

This commit is contained in:
Brad Hefta-Gaub 2016-03-30 17:49:30 -07:00
parent d9909fb7da
commit 28978c8adb
3 changed files with 15 additions and 3 deletions

View file

@ -3347,9 +3347,10 @@ void Application::update(float deltaTime) {
}
{
PROFILE_RANGE_EX("HarvestChanges", 0xffffff00, (uint64_t)getActiveDisplayPlugin()->presentCount());
PerformanceTimer perfTimer("havestChanges");
PerformanceTimer perfTimer("harvestChanges");
if (_physicsEngine->hasOutgoingChanges()) {
getEntities()->getTree()->withWriteLock([&] {
PerformanceTimer perfTimer("handleOutgoingChanges");
const VectorOfMotionStates& outgoingChanges = _physicsEngine->getOutgoingChanges();
_entitySimulation.handleOutgoingChanges(outgoingChanges, Physics::getSessionUUID());
avatarManager->handleOutgoingChanges(outgoingChanges);
@ -3365,6 +3366,7 @@ void Application::update(float deltaTime) {
// Collision events (and their scripts) must not be handled when we're locked, above. (That would risk
// deadlock.)
_entitySimulation.handleCollisionEvents(collisionEvents);
// NOTE: the getEntities()->update() call below will wait for lock
// and will simulate entity motion (the EntityTree has been given an EntitySimulation).
getEntities()->update(); // update the models...

View file

@ -130,6 +130,7 @@ void EntityTreeRenderer::setTree(OctreePointer newTree) {
}
void EntityTreeRenderer::update() {
PerformanceTimer perfTimer("ETRupdate");
if (_tree && !_shuttingDown) {
EntityTreePointer tree = std::static_pointer_cast<EntityTree>(_tree);
tree->update();
@ -158,6 +159,7 @@ void EntityTreeRenderer::update() {
}
bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(const glm::vec3& avatarPosition, QVector<EntityItemID>* entitiesContainingAvatar) {
PerformanceTimer perfTimer("findBestZone");
bool didUpdate = false;
float radius = 1.0f; // for now, assume 1 meter radius
QVector<EntityItemPointer> foundEntities;
@ -218,12 +220,15 @@ bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(const glm::vec3&
return didUpdate;
}
bool EntityTreeRenderer::checkEnterLeaveEntities() {
PerformanceTimer perfTimer("checkEnterLeaveEntities");
auto now = usecTimestampNow();
bool didUpdate = false;
if (_tree && !_shuttingDown) {
glm::vec3 avatarPosition = _viewState->getAvatarPosition();
if (avatarPosition != _lastAvatarPosition) {
// If we've moved "enough" check to see our enter/leave state
if (glm::distance(avatarPosition, _lastAvatarPosition) > ZONE_CHECK_DISTANCE) {
QVector<EntityItemID> entitiesContainingAvatar;
didUpdate = findBestZoneAndMaybeContainingEntities(avatarPosition, &entitiesContainingAvatar);
@ -248,7 +253,8 @@ bool EntityTreeRenderer::checkEnterLeaveEntities() {
}
_currentEntitiesInside = entitiesContainingAvatar;
_lastAvatarPosition = avatarPosition;
} else {
} else if ((now - _lastZoneCheck) > ZONE_CHECK_INTERVAL) { // if it's been a while since checking zone state
_lastZoneCheck = now;
didUpdate = findBestZoneAndMaybeContainingEntities(avatarPosition, nullptr);
}
}

View file

@ -178,6 +178,10 @@ private:
std::shared_ptr<ZoneEntityItem> _bestZone;
float _bestZoneVolume;
quint64 _lastZoneCheck { 0 };
const quint64 ZONE_CHECK_INTERVAL = USECS_PER_MSEC * 100; // ~10hz
const float ZONE_CHECK_DISTANCE = 0.001f;
glm::vec3 _previousKeyLightColor;
float _previousKeyLightIntensity;
float _previousKeyLightAmbientIntensity;