Merge pull request #5139 from ZappoMan/fixRayPicking

fix FPS while still maintaining ray picking
This commit is contained in:
Seth Alves 2015-06-16 16:17:42 -07:00
commit 0ef75da5d5
3 changed files with 46 additions and 33 deletions

View file

@ -41,7 +41,11 @@ public:
ReadBitstreamToTreeParams& args,
EntityPropertyFlags& propertyFlags, bool overwriteLocalData);
virtual void somethingChangedNotification() { _needsInitialSimulation = true; }
virtual void somethingChangedNotification() {
// FIX ME: this is overly aggressive. We only really need to simulate() if something about
// the world space transform has changed and/or if some animation is occurring.
_needsInitialSimulation = true;
}
virtual bool readyToAddToScene(RenderArgs* renderArgs = nullptr);
virtual bool addToScene(EntityItemPointer self, std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges);

View file

@ -82,6 +82,7 @@ Model::Model(QObject* parent) :
_isVisible(true),
_blendNumber(0),
_appliedBlendNumber(0),
_calculatedMeshPartOffsetValid(false),
_calculatedMeshPartBoxesValid(false),
_calculatedMeshBoxesValid(false),
_calculatedMeshTrianglesValid(false),
@ -544,6 +545,10 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
// we can use the AABox's ray intersection by mapping our origin and direction into the model frame
// and testing intersection there.
if (modelFrameBox.findRayIntersection(modelFrameOrigin, modelFrameDirection, distance, face)) {
if (!_calculatedMeshBoxesValid) {
recalculateMeshBoxes(pickAgainstTriangles);
}
float bestDistance = std::numeric_limits<float>::max();
@ -660,6 +665,25 @@ bool Model::convexHullContains(glm::vec3 point) {
return false;
}
void Model::recalculateMeshPartOffsets() {
if (!_calculatedMeshPartOffsetValid) {
const FBXGeometry& geometry = _geometry->getFBXGeometry();
int numberOfMeshes = geometry.meshes.size();
_calculatedMeshPartOffset.clear();
for (int i = 0; i < numberOfMeshes; i++) {
const FBXMesh& mesh = geometry.meshes.at(i);
qint64 partOffset = 0;
for (int j = 0; j < mesh.parts.size(); j++) {
const FBXMeshPart& part = mesh.parts.at(j);
_calculatedMeshPartOffset[QPair<int,int>(i, j)] = partOffset;
partOffset += part.quadIndices.size() * sizeof(int);
partOffset += part.triangleIndices.size() * sizeof(int);
}
}
_calculatedMeshPartOffsetValid = true;
}
}
// TODO: we seem to call this too often when things haven't actually changed... look into optimizing this
// Any script might trigger findRayIntersectionAgainstSubMeshes (and maybe convexHullContains), so these
// can occur multiple times. In addition, rendering does it's own ray picking in order to decide which
@ -675,7 +699,8 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
_calculatedMeshTriangles.clear();
_calculatedMeshTriangles.resize(numberOfMeshes);
_calculatedMeshPartBoxes.clear();
_calculatedMeshPartOffet.clear();
_calculatedMeshPartOffset.clear();
_calculatedMeshPartOffsetValid = false;
for (int i = 0; i < numberOfMeshes; i++) {
const FBXMesh& mesh = geometry.meshes.at(i);
Extents scaledMeshExtents = calculateScaledOffsetExtents(mesh.meshExtents);
@ -770,7 +795,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
}
}
_calculatedMeshPartBoxes[QPair<int,int>(i, j)] = thisPartBounds;
_calculatedMeshPartOffet[QPair<int,int>(i, j)] = partOffset;
_calculatedMeshPartOffset[QPair<int,int>(i, j)] = partOffset;
partOffset += part.quadIndices.size() * sizeof(int);
partOffset += part.triangleIndices.size() * sizeof(int);
@ -778,6 +803,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
}
_calculatedMeshTriangles[i] = thisMeshTriangles;
_calculatedMeshPartBoxesValid = true;
_calculatedMeshPartOffsetValid = true;
}
}
_calculatedMeshBoxesValid = true;
@ -786,16 +812,6 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
}
void Model::renderSetup(RenderArgs* args) {
// if we don't have valid mesh boxes, calculate them now, this only matters in cases
// where our caller has passed RenderArgs which will include a view frustum we can cull
// against. We cache the results of these calculations so long as the model hasn't been
// simulated and the mesh hasn't changed.
if (args && !_calculatedMeshBoxesValid) {
_mutex.lock();
recalculateMeshBoxes();
_mutex.unlock();
}
// set up dilated textures on first render after load/simulate
const FBXGeometry& geometry = _geometry->getFBXGeometry();
if (_dilatedTextures.isEmpty()) {
@ -1342,18 +1358,15 @@ void Model::snapToRegistrationPoint() {
}
void Model::simulate(float deltaTime, bool fullUpdate) {
/*
qDebug() << "Model::simulate()";
qDebug() << " _translation:" << _translation;
qDebug() << " _rotation:" << _rotation;
*/
fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit)
|| (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint);
if (isActive() && fullUpdate) {
// NOTE: this seems problematic... need to review
_calculatedMeshBoxesValid = false; // if we have to simulate, we need to assume our mesh boxes are all invalid
// NOTE: This is overly aggressive and we are invalidating the MeshBoxes when in fact they may not be invalid
// they really only become invalid if something about the transform to world space has changed. This is
// not too bad at this point, because it doesn't impact rendering. However it does slow down ray picking
// because ray picking needs valid boxes to work
_calculatedMeshBoxesValid = false;
_calculatedMeshTrianglesValid = false;
// check for scale to fit
@ -1761,10 +1774,6 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) {
}
AABox Model::getPartBounds(int meshIndex, int partIndex) {
if (!_calculatedMeshPartBoxesValid || !_calculatedMeshBoxesValid) {
recalculateMeshBoxes(true);
}
if (meshIndex < _meshStates.size()) {
const MeshState& state = _meshStates.at(meshIndex);
bool isSkinned = state.clusterMatrices.size() > 1;
@ -1774,7 +1783,7 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) {
}
}
if (_calculatedMeshPartBoxesValid && _calculatedMeshPartBoxes.contains(QPair<int,int>(meshIndex, partIndex))) {
if (_geometry->getFBXGeometry().meshes.size() > meshIndex) {
// FIX ME! - This is currently a hack because for some mesh parts our efforts to calculate the bounding
// box of the mesh part fails. It seems to create boxes that are not consistent with where the
@ -1795,15 +1804,13 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) {
}
void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) {
if (!_readyWhenAdded) {
return; // bail asap
}
// we always need these properly calculated before we can render, this will likely already have been done
// since the engine will call our getPartBounds() before rendering us.
if (!_calculatedMeshPartBoxesValid || !_calculatedMeshBoxesValid) {
recalculateMeshBoxes(true);
// We need to make sure we have valid offsets calculated before we can render
if (!_calculatedMeshPartOffsetValid) {
recalculateMeshPartOffsets();
}
auto textureCache = DependencyManager::get<TextureCache>();
@ -2011,7 +2018,7 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran
}
}
qint64 offset = _calculatedMeshPartOffet[QPair<int,int>(meshIndex, partIndex)];
qint64 offset = _calculatedMeshPartOffset[QPair<int,int>(meshIndex, partIndex)];
if (part.quadIndices.size() > 0) {
batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset);

View file

@ -369,7 +369,8 @@ private:
};
QHash<QPair<int,int>, AABox> _calculatedMeshPartBoxes; // world coordinate AABoxes for all sub mesh part boxes
QHash<QPair<int,int>, qint64> _calculatedMeshPartOffet;
QHash<QPair<int,int>, qint64> _calculatedMeshPartOffset;
bool _calculatedMeshPartOffsetValid;
bool _calculatedMeshPartBoxesValid;
@ -381,6 +382,7 @@ private:
QMutex _mutex;
void recalculateMeshBoxes(bool pickAgainstTriangles = false);
void recalculateMeshPartOffsets();
void segregateMeshGroups(); // used to calculate our list of translucent vs opaque meshes