fade on texture load

This commit is contained in:
SamGondelman 2016-08-01 14:47:20 -07:00
parent 217a102926
commit 313ba87fce
3 changed files with 14 additions and 12 deletions

View file

@ -329,8 +329,6 @@ ModelMeshPartPayload::ModelMeshPartPayload(Model* model, int _meshIndex, int par
updateTransform(transform, offsetTransform);
initCache();
_fadeStartTime = usecTimestampNow();
}
void ModelMeshPartPayload::initCache() {
@ -357,7 +355,7 @@ void ModelMeshPartPayload::initCache() {
float ModelMeshPartPayload::calcFadeRatio() const {
const float FADE_TIME = 0.5f;
float t = std::min(((float)(usecTimestampNow() - _fadeStartTime)) / ((float)(FADE_TIME * USECS_PER_SECOND)), 1.0f);
return -0.5f * (cosf(M_PI*t) - 1.0f);
return -(cosf(M_PI_2*t) - 1.0f);
}
void ModelMeshPartPayload::notifyLocationChanged() {
@ -495,9 +493,9 @@ void ModelMeshPartPayload::bindMesh(gpu::Batch& batch) const {
batch.setInputStream(2, _drawMesh->getVertexStream().makeRangedStream(2));
}
// TODO: Get rid of that extra call
if (!_hasColorAttrib) {
batch._glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
float fadeRatio = calcFadeRatio();
if (!_hasColorAttrib || fadeRatio < 1.0f) {
batch._glColor4f(1.0f, 1.0f, 1.0f, fadeRatio);
}
}
@ -528,7 +526,7 @@ void ModelMeshPartPayload::bindTransform(gpu::Batch& batch, const ShapePipeline:
void ModelMeshPartPayload::render(RenderArgs* args) const {
PerformanceTimer perfTimer("ModelMeshPartPayload::render");
if (!_model->_readyWhenAdded || !_model->_isVisible) {
if (!_model->_readyWhenAdded || !_model->_isVisible || !_hasStartedFade) {
return; // bail asap
}
@ -552,9 +550,6 @@ void ModelMeshPartPayload::render(RenderArgs* args) const {
// apply material properties
bindMaterial(batch, locations);
// model fading
batch._glColor4f(1.0f, 1.0f, 1.0f, calcFadeRatio());
if (args) {
args->_details._materialSwitches++;
}

View file

@ -83,6 +83,9 @@ public:
// Entity fade in
float calcFadeRatio() const;
void startFade() { _fadeStartTime = usecTimestampNow(); }
bool hasStartedFade() { return _hasStartedFade; }
void setHasStartedFade(bool hasStartedFade) { _hasStartedFade = hasStartedFade; }
// Render Item interface
render::ItemKey getKey() const override;
@ -104,8 +107,8 @@ public:
bool _isBlendShaped{ false };
private:
quint64 _fadeStartTime { usecTimestampNow() };
bool _hasFadeStarted { false };
quint64 _fadeStartTime { 0 };
bool _hasStartedFade { false };
};
namespace render {

View file

@ -207,6 +207,10 @@ void Model::updateRenderItems() {
render::PendingChanges pendingChanges;
foreach (auto itemID, self->_modelMeshRenderItems.keys()) {
pendingChanges.updateItem<ModelMeshPartPayload>(itemID, [modelTransform, modelMeshOffset, deleteGeometryCounter](ModelMeshPartPayload& data) {
if (!data.hasStartedFade() && data._model && data._model->isLoaded() && data._model->getGeometry()->areTexturesLoaded()) {
data.startFade();
data.setHasStartedFade(true);
}
// Ensure the model geometry was not reset between frames
if (data._model && data._model->isLoaded() && deleteGeometryCounter == data._model->_deleteGeometryCounter) {
// lazy update of cluster matrices used for rendering. We need to update them here, so we can correctly update the bounding box.