From 23bb8608b14739ee776a9b0b8ef172a88475f885 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 16 Oct 2017 11:52:03 -0700 Subject: [PATCH] minor optimizations in updateModelBounds() --- .../src/RenderableModelEntityItem.cpp | 66 +++++++++++++++---- .../src/RenderableModelEntityItem.h | 1 - libraries/entities/src/ModelEntityItem.cpp | 14 ++-- libraries/entities/src/ModelEntityItem.h | 5 +- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index a0c3cde28f..e2afc6eac2 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -124,10 +124,11 @@ void RenderableModelEntityItem::doInitialModelSimulation() { model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); model->setRotation(getRotation()); model->setTranslation(getPosition()); - { + + if (_needsInitialSimulation) { model->simulate(0.0f); + _needsInitialSimulation = false; } - _needsInitialSimulation = false; } void RenderableModelEntityItem::autoResizeJointArrays() { @@ -179,13 +180,59 @@ bool RenderableModelEntityItem::needsUpdateModelBounds() const { } } - return false; + return model->needsReload(); } void RenderableModelEntityItem::updateModelBounds() { PROFILE_RANGE(simulation_physics, "updateModelBounds"); - if (needsUpdateModelBounds()) { - doInitialModelSimulation(); + + if (!_dimensionsInitialized || !hasModel()) { + return; + } + + ModelPointer model = getModel(); + if (!model || !model->isLoaded()) { + return; + } + + /* adebug TODO: figure out if we need to DO anything when isAnimatingSomething() + if (isAnimatingSomething()) { + return true; + } + */ + + if (model->needsReload()) { + model->updateGeometry(); + } + + if (model->getScaleToFitDimensions() != getDimensions() || + model->getRegistrationPoint() != getRegistrationPoint()) { + // The machinery for updateModelBounds will give existing models the opportunity to fix their + // translation/rotation/scale/registration. The first two are straightforward, but the latter two + // have guards to make sure they don't happen after they've already been set. Here we reset those guards. + // This doesn't cause the entity values to change -- it just allows the model to match once it comes in. + model->setScaleToFit(false, getDimensions()); + model->setSnapModelToRegistrationPoint(false, getRegistrationPoint()); + + // now recalculate the bounds and registration + model->setScaleToFit(true, getDimensions()); + model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); + } + + bool success; + auto transform = getTransform(success); + if (success) { + if (model->getTranslation() != transform.getTranslation()) { + model->setTranslation(transform.getTranslation()); + } + if (model->getRotation() != transform.getRotation()) { + model->setRotation(transform.getRotation()); + } + } + + if (_needsInitialSimulation || _needsJointSimulation) { + model->simulate(0.0f); + _needsInitialSimulation = false; _needsJointSimulation = false; } } @@ -899,10 +946,6 @@ void RenderableModelEntityItem::copyAnimationJointDataToModel() { }); } -bool RenderableModelEntityItem::isAnimatingSomething() const { - return !getAnimationURL().isEmpty() && getAnimationIsPlaying() && getAnimationFPS() != 0.0f; -} - using namespace render; using namespace render::entities; @@ -1205,9 +1248,7 @@ void ModelEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce } } - if (entity->needsUpdateModelBounds()) { - entity->updateModelBounds(); - } + entity->updateModelBounds(); if (model->isVisible() != _visible) { // FIXME: this seems like it could be optimized if we tracked our last known visible state in @@ -1215,6 +1256,7 @@ void ModelEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce // so most of the time we don't do anything in this function. model->setVisibleInScene(_visible, scene); } + // TODO? early exit here when not visible? { PROFILE_RANGE(simulation_physics, "Fixup"); diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.h b/libraries/entities-renderer/src/RenderableModelEntityItem.h index d1424316e9..a50ca63382 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.h +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.h @@ -108,7 +108,6 @@ public: private: bool needsUpdateModelBounds() const; - bool isAnimatingSomething() const; void autoResizeJointArrays(); void copyAnimationJointDataToModel(); diff --git a/libraries/entities/src/ModelEntityItem.cpp b/libraries/entities/src/ModelEntityItem.cpp index 6af4db154a..a5d259ea87 100644 --- a/libraries/entities/src/ModelEntityItem.cpp +++ b/libraries/entities/src/ModelEntityItem.cpp @@ -557,12 +557,6 @@ void ModelEntityItem::setAnimationLoop(bool loop) { }); } -bool ModelEntityItem::getAnimationLoop() const { - return resultWithReadLock([&] { - return _animationProperties.getLoop(); - }); -} - void ModelEntityItem::setAnimationHold(bool hold) { withWriteLock([&] { _animationProperties.setHold(hold); @@ -610,8 +604,10 @@ float ModelEntityItem::getAnimationCurrentFrame() const { }); } -float ModelEntityItem::getAnimationFPS() const { +bool ModelEntityItem::isAnimatingSomething() const { return resultWithReadLock([&] { - return _animationProperties.getFPS(); - }); + return !_animationProperties.getURL().isEmpty() && + _animationProperties.getRunning() && + (_animationProperties.getFPS() != 0.0f); + }); } diff --git a/libraries/entities/src/ModelEntityItem.h b/libraries/entities/src/ModelEntityItem.h index 7efb493735..2c3ef3aa2d 100644 --- a/libraries/entities/src/ModelEntityItem.h +++ b/libraries/entities/src/ModelEntityItem.h @@ -90,7 +90,6 @@ public: bool getAnimationAllowTranslation() const { return _animationProperties.getAllowTranslation(); }; void setAnimationLoop(bool loop); - bool getAnimationLoop() const; void setAnimationHold(bool hold); bool getAnimationHold() const; @@ -101,10 +100,9 @@ public: void setAnimationLastFrame(float lastFrame); float getAnimationLastFrame() const; - bool getAnimationIsPlaying() const; float getAnimationCurrentFrame() const; - float getAnimationFPS() const; + bool isAnimatingSomething() const; static const QString DEFAULT_TEXTURES; const QString getTextures() const; @@ -123,7 +121,6 @@ public: QVector getJointRotationsSet() const; QVector getJointTranslations() const; QVector getJointTranslationsSet() const; - bool isAnimatingSomething() const; private: void setAnimationSettings(const QString& value); // only called for old bitstream format