Merge pull request #11362 from jherico/crash/model-race

Ensure thread-safety for model pointer access
This commit is contained in:
Sam Gateau 2017-09-16 10:45:09 -07:00 committed by GitHub
commit 84dfe53fee
2 changed files with 131 additions and 91 deletions

View file

@ -36,6 +36,29 @@
static CollisionRenderMeshCache collisionMeshCache; static CollisionRenderMeshCache collisionMeshCache;
void ModelEntityWrapper::setModel(const ModelPointer& model) {
withWriteLock([&] {
if (_model != model) {
_model = model;
if (_model) {
_needsInitialSimulation = true;
}
}
});
}
ModelPointer ModelEntityWrapper::getModel() const {
return resultWithReadLock<ModelPointer>([&] {
return _model;
});
}
bool ModelEntityWrapper::isModelLoaded() const {
return resultWithReadLock<bool>([&] {
return _model.operator bool() && _model->isLoaded();
});
}
EntityItemPointer RenderableModelEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { EntityItemPointer RenderableModelEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) {
EntityItemPointer entity{ new RenderableModelEntityItem(entityID, properties.getDimensionsInitialized()) }; EntityItemPointer entity{ new RenderableModelEntityItem(entityID, properties.getDimensionsInitialized()) };
entity->setProperties(properties); entity->setProperties(properties);
@ -43,7 +66,7 @@ EntityItemPointer RenderableModelEntityItem::factory(const EntityItemID& entityI
} }
RenderableModelEntityItem::RenderableModelEntityItem(const EntityItemID& entityItemID, bool dimensionsInitialized) : RenderableModelEntityItem::RenderableModelEntityItem(const EntityItemID& entityItemID, bool dimensionsInitialized) :
ModelEntityItem(entityItemID), ModelEntityWrapper(entityItemID),
_dimensionsInitialized(dimensionsInitialized) { _dimensionsInitialized(dimensionsInitialized) {
} }
@ -83,41 +106,47 @@ QVariantMap parseTexturesToMap(QString textures, const QVariantMap& defaultTextu
} }
void RenderableModelEntityItem::doInitialModelSimulation() { void RenderableModelEntityItem::doInitialModelSimulation() {
ModelPointer model = getModel();
if (!model) {
return;
}
// The machinery for updateModelBounds will give existing models the opportunity to fix their // 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 // 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 // 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. // entity values to change -- it just allows the model to match once it comes in.
_model->setScaleToFit(false, getDimensions()); model->setScaleToFit(false, getDimensions());
_model->setSnapModelToRegistrationPoint(false, getRegistrationPoint()); model->setSnapModelToRegistrationPoint(false, getRegistrationPoint());
// now recalculate the bounds and registration // now recalculate the bounds and registration
_model->setScaleToFit(true, getDimensions()); model->setScaleToFit(true, getDimensions());
_model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); model->setSnapModelToRegistrationPoint(true, getRegistrationPoint());
_model->setRotation(getRotation()); model->setRotation(getRotation());
_model->setTranslation(getPosition()); model->setTranslation(getPosition());
{ {
PerformanceTimer perfTimer("_model->simulate"); PerformanceTimer perfTimer("model->simulate");
_model->simulate(0.0f); model->simulate(0.0f);
} }
_needsInitialSimulation = false; _needsInitialSimulation = false;
} }
void RenderableModelEntityItem::autoResizeJointArrays() { void RenderableModelEntityItem::autoResizeJointArrays() {
if (_model && _model->isLoaded() && !_needsInitialSimulation) { ModelPointer model = getModel();
resizeJointArrays(_model->getJointStateCount()); if (model && model->isLoaded() && !_needsInitialSimulation) {
resizeJointArrays(model->getJointStateCount());
} }
} }
bool RenderableModelEntityItem::needsUpdateModelBounds() const { bool RenderableModelEntityItem::needsUpdateModelBounds() const {
if (!hasModel() || !_model) { ModelPointer model = getModel();
if (!hasModel() || !model) {
return false; return false;
} }
if (!_dimensionsInitialized || !_model->isActive()) { if (!_dimensionsInitialized || !model->isActive()) {
return false; return false;
} }
if (_model->needsReload()) { if (model->needsReload()) {
return true; return true;
} }
@ -129,21 +158,21 @@ bool RenderableModelEntityItem::needsUpdateModelBounds() const {
return true; return true;
} }
if (_model->getScaleToFitDimensions() != getDimensions()) { if (model->getScaleToFitDimensions() != getDimensions()) {
return true; return true;
} }
if (_model->getRegistrationPoint() != getRegistrationPoint()) { if (model->getRegistrationPoint() != getRegistrationPoint()) {
return true; return true;
} }
bool success; bool success;
auto transform = getTransform(success); auto transform = getTransform(success);
if (success) { if (success) {
if (_model->getTranslation() != transform.getTranslation()) { if (model->getTranslation() != transform.getTranslation()) {
return true; return true;
} }
if (_model->getRotation() != transform.getRotation()) { if (model->getRotation() != transform.getRotation()) {
return true; return true;
} }
} }
@ -158,16 +187,6 @@ void RenderableModelEntityItem::updateModelBounds() {
} }
} }
void RenderableModelEntityItem::setModel(const ModelPointer& model) {
withWriteLock([&] {
if (_model != model) {
_model = model;
if (_model) {
_needsInitialSimulation = true;
}
}
});
}
EntityItemProperties RenderableModelEntityItem::getProperties(EntityPropertyFlags desiredProperties) const { EntityItemProperties RenderableModelEntityItem::getProperties(EntityPropertyFlags desiredProperties) const {
EntityItemProperties properties = ModelEntityItem::getProperties(desiredProperties); // get the properties from our base class EntityItemProperties properties = ModelEntityItem::getProperties(desiredProperties); // get the properties from our base class
@ -175,43 +194,44 @@ EntityItemProperties RenderableModelEntityItem::getProperties(EntityPropertyFlag
properties.setTextureNames(_originalTextures); properties.setTextureNames(_originalTextures);
} }
if (_model) { ModelPointer model = getModel();
properties.setRenderInfoVertexCount(_model->getRenderInfoVertexCount()); if (model) {
properties.setRenderInfoTextureCount(_model->getRenderInfoTextureCount()); properties.setRenderInfoVertexCount(model->getRenderInfoVertexCount());
properties.setRenderInfoTextureSize(_model->getRenderInfoTextureSize()); properties.setRenderInfoTextureCount(model->getRenderInfoTextureCount());
properties.setRenderInfoDrawCalls(_model->getRenderInfoDrawCalls()); properties.setRenderInfoTextureSize(model->getRenderInfoTextureSize());
properties.setRenderInfoHasTransparent(_model->getRenderInfoHasTransparent()); properties.setRenderInfoDrawCalls(model->getRenderInfoDrawCalls());
properties.setRenderInfoHasTransparent(model->getRenderInfoHasTransparent());
if (model->isLoaded()) {
// TODO: improve naturalDimensions in the future,
// for now we've added this hack for setting natural dimensions of models
Extents meshExtents = model->getFBXGeometry().getUnscaledMeshExtents();
properties.setNaturalDimensions(meshExtents.maximum - meshExtents.minimum);
properties.calculateNaturalPosition(meshExtents.minimum, meshExtents.maximum);
}
} }
if (_model && _model->isLoaded()) {
// TODO: improve naturalDimensions in the future,
// for now we've added this hack for setting natural dimensions of models
Extents meshExtents = _model->getFBXGeometry().getUnscaledMeshExtents();
properties.setNaturalDimensions(meshExtents.maximum - meshExtents.minimum);
properties.calculateNaturalPosition(meshExtents.minimum, meshExtents.maximum);
}
return properties; return properties;
} }
bool RenderableModelEntityItem::supportsDetailedRayIntersection() const { bool RenderableModelEntityItem::supportsDetailedRayIntersection() const {
return resultWithReadLock<bool>([&] { return isModelLoaded();
return _model && _model->isLoaded();
});
} }
bool RenderableModelEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, bool RenderableModelEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElementPointer& element, float& distance, BoxFace& face, bool& keepSearching, OctreeElementPointer& element, float& distance, BoxFace& face,
glm::vec3& surfaceNormal, void** intersectedObject, bool precisionPicking) const { glm::vec3& surfaceNormal, void** intersectedObject, bool precisionPicking) const {
if (!_model) { auto model = getModel();
if (!model) {
return true; return true;
} }
// qCDebug(entitiesrenderer) << "RenderableModelEntityItem::findDetailedRayIntersection() precisionPicking:" // qCDebug(entitiesrenderer) << "RenderableModelEntityItem::findDetailedRayIntersection() precisionPicking:"
// << precisionPicking; // << precisionPicking;
QString extraInfo; QString extraInfo;
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, return model->findRayIntersectionAgainstSubMeshes(origin, direction, distance,
face, surfaceNormal, extraInfo, precisionPicking, false); face, surfaceNormal, extraInfo, precisionPicking, false);
} }
@ -242,7 +262,7 @@ void RenderableModelEntityItem::setCompoundShapeURL(const QString& url) {
// parse it twice. // parse it twice.
auto currentCompoundShapeURL = getCompoundShapeURL(); auto currentCompoundShapeURL = getCompoundShapeURL();
ModelEntityItem::setCompoundShapeURL(url); ModelEntityItem::setCompoundShapeURL(url);
if (getCompoundShapeURL() != currentCompoundShapeURL || !_model) { if (getCompoundShapeURL() != currentCompoundShapeURL || !getModel()) {
if (getShapeType() == SHAPE_TYPE_COMPOUND) { if (getShapeType() == SHAPE_TYPE_COMPOUND) {
getCollisionGeometryResource(); getCollisionGeometryResource();
} }
@ -252,17 +272,18 @@ void RenderableModelEntityItem::setCompoundShapeURL(const QString& url) {
bool RenderableModelEntityItem::isReadyToComputeShape() const { bool RenderableModelEntityItem::isReadyToComputeShape() const {
ShapeType type = getShapeType(); ShapeType type = getShapeType();
auto model = getModel();
if (type == SHAPE_TYPE_COMPOUND) { if (type == SHAPE_TYPE_COMPOUND) {
if (!_model || getCompoundShapeURL().isEmpty()) { if (!model || getCompoundShapeURL().isEmpty()) {
return false; return false;
} }
if (_model->getURL().isEmpty()) { if (model->getURL().isEmpty()) {
// we need a render geometry with a scale to proceed, so give up. // we need a render geometry with a scale to proceed, so give up.
return false; return false;
} }
if (_model->isLoaded()) { if (model->isLoaded()) {
if (!getCompoundShapeURL().isEmpty() && !_compoundShapeResource) { if (!getCompoundShapeURL().isEmpty() && !_compoundShapeResource) {
const_cast<RenderableModelEntityItem*>(this)->getCollisionGeometryResource(); const_cast<RenderableModelEntityItem*>(this)->getCollisionGeometryResource();
} }
@ -281,7 +302,7 @@ bool RenderableModelEntityItem::isReadyToComputeShape() const {
// the model is still being downloaded. // the model is still being downloaded.
return false; return false;
} else if (type >= SHAPE_TYPE_SIMPLE_HULL && type <= SHAPE_TYPE_STATIC_MESH) { } else if (type >= SHAPE_TYPE_SIMPLE_HULL && type <= SHAPE_TYPE_STATIC_MESH) {
return (_model && _model->isLoaded()); return isModelLoaded();
} }
return true; return true;
} }
@ -292,6 +313,7 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& shapeInfo) {
ShapeType type = getShapeType(); ShapeType type = getShapeType();
glm::vec3 dimensions = getDimensions(); glm::vec3 dimensions = getDimensions();
auto model = getModel();
if (type == SHAPE_TYPE_COMPOUND) { if (type == SHAPE_TYPE_COMPOUND) {
updateModelBounds(); updateModelBounds();
@ -373,14 +395,14 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& shapeInfo) {
// to the visual model and apply them to the collision model (without regard for the // to the visual model and apply them to the collision model (without regard for the
// collision model's extents). // collision model's extents).
glm::vec3 scaleToFit = dimensions / _model->getFBXGeometry().getUnscaledMeshExtents().size(); glm::vec3 scaleToFit = dimensions / model->getFBXGeometry().getUnscaledMeshExtents().size();
// multiply each point by scale before handing the point-set off to the physics engine. // multiply each point by scale before handing the point-set off to the physics engine.
// also determine the extents of the collision model. // also determine the extents of the collision model.
glm::vec3 registrationOffset = dimensions * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint()); glm::vec3 registrationOffset = dimensions * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint());
for (int32_t i = 0; i < pointCollection.size(); i++) { for (int32_t i = 0; i < pointCollection.size(); i++) {
for (int32_t j = 0; j < pointCollection[i].size(); j++) { for (int32_t j = 0; j < pointCollection[i].size(); j++) {
// back compensate for registration so we can apply that offset to the shapeInfo later // back compensate for registration so we can apply that offset to the shapeInfo later
pointCollection[i][j] = scaleToFit * (pointCollection[i][j] + _model->getOffset()) - registrationOffset; pointCollection[i][j] = scaleToFit * (pointCollection[i][j] + model->getOffset()) - registrationOffset;
} }
} }
shapeInfo.setParams(type, dimensions, getCompoundShapeURL()); shapeInfo.setParams(type, dimensions, getCompoundShapeURL());
@ -389,11 +411,11 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& shapeInfo) {
assert(_model && _model->isLoaded()); assert(_model && _model->isLoaded());
updateModelBounds(); updateModelBounds();
_model->updateGeometry(); model->updateGeometry();
// compute meshPart local transforms // compute meshPart local transforms
QVector<glm::mat4> localTransforms; QVector<glm::mat4> localTransforms;
const FBXGeometry& fbxGeometry = _model->getFBXGeometry(); const FBXGeometry& fbxGeometry = model->getFBXGeometry();
int numFbxMeshes = fbxGeometry.meshes.size(); int numFbxMeshes = fbxGeometry.meshes.size();
int totalNumVertices = 0; int totalNumVertices = 0;
glm::mat4 invRegistraionOffset = glm::translate(dimensions * (getRegistrationPoint() - ENTITY_ITEM_DEFAULT_REGISTRATION_POINT)); glm::mat4 invRegistraionOffset = glm::translate(dimensions * (getRegistrationPoint() - ENTITY_ITEM_DEFAULT_REGISTRATION_POINT));
@ -401,7 +423,7 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& shapeInfo) {
const FBXMesh& mesh = fbxGeometry.meshes.at(i); const FBXMesh& mesh = fbxGeometry.meshes.at(i);
if (mesh.clusters.size() > 0) { if (mesh.clusters.size() > 0) {
const FBXCluster& cluster = mesh.clusters.at(0); const FBXCluster& cluster = mesh.clusters.at(0);
auto jointMatrix = _model->getRig().getJointTransform(cluster.jointIndex); auto jointMatrix = model->getRig().getJointTransform(cluster.jointIndex);
// we backtranslate by the registration offset so we can apply that offset to the shapeInfo later // we backtranslate by the registration offset so we can apply that offset to the shapeInfo later
localTransforms.push_back(invRegistraionOffset * jointMatrix * cluster.inverseBindMatrix); localTransforms.push_back(invRegistraionOffset * jointMatrix * cluster.inverseBindMatrix);
} else { } else {
@ -417,7 +439,7 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& shapeInfo) {
return; return;
} }
auto& meshes = _model->getGeometry()->getMeshes(); auto& meshes = model->getGeometry()->getMeshes();
int32_t numMeshes = (int32_t)(meshes.size()); int32_t numMeshes = (int32_t)(meshes.size());
const int MAX_ALLOWED_MESH_COUNT = 1000; const int MAX_ALLOWED_MESH_COUNT = 1000;
@ -631,7 +653,8 @@ void RenderableModelEntityItem::setCollisionShape(const btCollisionShape* shape)
} }
bool RenderableModelEntityItem::contains(const glm::vec3& point) const { bool RenderableModelEntityItem::contains(const glm::vec3& point) const {
if (EntityItem::contains(point) && _model && _compoundShapeResource && _compoundShapeResource->isLoaded()) { auto model = getModel();
if (EntityItem::contains(point) && model && _compoundShapeResource && _compoundShapeResource->isLoaded()) {
return _compoundShapeResource->getFBXGeometry().convexHullContains(worldToEntity(point)); return _compoundShapeResource->getFBXGeometry().convexHullContains(worldToEntity(point));
} }
@ -639,11 +662,12 @@ bool RenderableModelEntityItem::contains(const glm::vec3& point) const {
} }
bool RenderableModelEntityItem::shouldBePhysical() const { bool RenderableModelEntityItem::shouldBePhysical() const {
auto model = getModel();
// If we have a model, make sure it hasn't failed to download. // If we have a model, make sure it hasn't failed to download.
// If it has, we'll report back that we shouldn't be physical so that physics aren't held waiting for us to be ready. // If it has, we'll report back that we shouldn't be physical so that physics aren't held waiting for us to be ready.
if (_model && getShapeType() == SHAPE_TYPE_COMPOUND && _model->didCollisionGeometryRequestFail()) { if (model && getShapeType() == SHAPE_TYPE_COMPOUND && model->didCollisionGeometryRequestFail()) {
return false; return false;
} else if (_model && getShapeType() != SHAPE_TYPE_NONE && _model->didVisualGeometryRequestFail()) { } else if (model && getShapeType() != SHAPE_TYPE_NONE && model->didVisualGeometryRequestFail()) {
return false; return false;
} else { } else {
return ModelEntityItem::shouldBePhysical(); return ModelEntityItem::shouldBePhysical();
@ -651,9 +675,10 @@ bool RenderableModelEntityItem::shouldBePhysical() const {
} }
glm::quat RenderableModelEntityItem::getAbsoluteJointRotationInObjectFrame(int index) const { glm::quat RenderableModelEntityItem::getAbsoluteJointRotationInObjectFrame(int index) const {
if (_model) { auto model = getModel();
if (model) {
glm::quat result; glm::quat result;
if (_model->getAbsoluteJointRotationInRigFrame(index, result)) { if (model->getAbsoluteJointRotationInRigFrame(index, result)) {
return result; return result;
} }
} }
@ -661,9 +686,10 @@ glm::quat RenderableModelEntityItem::getAbsoluteJointRotationInObjectFrame(int i
} }
glm::vec3 RenderableModelEntityItem::getAbsoluteJointTranslationInObjectFrame(int index) const { glm::vec3 RenderableModelEntityItem::getAbsoluteJointTranslationInObjectFrame(int index) const {
if (_model) { auto model = getModel();
if (model) {
glm::vec3 result; glm::vec3 result;
if (_model->getAbsoluteJointTranslationInRigFrame(index, result)) { if (model->getAbsoluteJointTranslationInRigFrame(index, result)) {
return result; return result;
} }
} }
@ -671,10 +697,11 @@ glm::vec3 RenderableModelEntityItem::getAbsoluteJointTranslationInObjectFrame(in
} }
bool RenderableModelEntityItem::setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) { bool RenderableModelEntityItem::setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) {
if (!_model) { auto model = getModel();
if (!model) {
return false; return false;
} }
const Rig& rig = _model->getRig(); const Rig& rig = model->getRig();
int jointParentIndex = rig.getJointParentIndex(index); int jointParentIndex = rig.getJointParentIndex(index);
if (jointParentIndex == -1) { if (jointParentIndex == -1) {
return setLocalJointRotation(index, rotation); return setLocalJointRotation(index, rotation);
@ -700,10 +727,11 @@ bool RenderableModelEntityItem::setAbsoluteJointRotationInObjectFrame(int index,
} }
bool RenderableModelEntityItem::setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) { bool RenderableModelEntityItem::setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) {
if (!_model) { auto model = getModel();
if (!model) {
return false; return false;
} }
const Rig& rig = _model->getRig(); const Rig& rig = model->getRig();
int jointParentIndex = rig.getJointParentIndex(index); int jointParentIndex = rig.getJointParentIndex(index);
if (jointParentIndex == -1) { if (jointParentIndex == -1) {
@ -730,9 +758,10 @@ bool RenderableModelEntityItem::setAbsoluteJointTranslationInObjectFrame(int ind
} }
glm::quat RenderableModelEntityItem::getLocalJointRotation(int index) const { glm::quat RenderableModelEntityItem::getLocalJointRotation(int index) const {
if (_model) { auto model = getModel();
if (model) {
glm::quat result; glm::quat result;
if (_model->getJointRotation(index, result)) { if (model->getJointRotation(index, result)) {
return result; return result;
} }
} }
@ -740,9 +769,10 @@ glm::quat RenderableModelEntityItem::getLocalJointRotation(int index) const {
} }
glm::vec3 RenderableModelEntityItem::getLocalJointTranslation(int index) const { glm::vec3 RenderableModelEntityItem::getLocalJointTranslation(int index) const {
if (_model) { auto model = getModel();
if (model) {
glm::vec3 result; glm::vec3 result;
if (_model->getJointTranslation(index, result)) { if (model->getJointTranslation(index, result)) {
return result; return result;
} }
} }
@ -810,19 +840,22 @@ void RenderableModelEntityItem::setJointTranslationsSet(const QVector<bool>& tra
void RenderableModelEntityItem::locationChanged(bool tellPhysics) { void RenderableModelEntityItem::locationChanged(bool tellPhysics) {
PerformanceTimer pertTimer("locationChanged"); PerformanceTimer pertTimer("locationChanged");
EntityItem::locationChanged(tellPhysics); EntityItem::locationChanged(tellPhysics);
if (_model && _model->isLoaded()) { auto model = getModel();
_model->updateRenderItems(); if (model && model->isLoaded()) {
model->updateRenderItems();
} }
} }
int RenderableModelEntityItem::getJointIndex(const QString& name) const { int RenderableModelEntityItem::getJointIndex(const QString& name) const {
return (_model && _model->isActive()) ? _model->getRig().indexOfJoint(name) : -1; auto model = getModel();
return (model && model->isActive()) ? model->getRig().indexOfJoint(name) : -1;
} }
QStringList RenderableModelEntityItem::getJointNames() const { QStringList RenderableModelEntityItem::getJointNames() const {
QStringList result; QStringList result;
if (_model && _model->isActive()) { auto model = getModel();
const Rig& rig = _model->getRig(); if (model && model->isActive()) {
const Rig& rig = model->getRig();
int jointCount = rig.getJointStateCount(); int jointCount = rig.getJointStateCount();
for (int jointIndex = 0; jointIndex < jointCount; jointIndex++) { for (int jointIndex = 0; jointIndex < jointCount; jointIndex++) {
result << rig.nameOfJoint(jointIndex); result << rig.nameOfJoint(jointIndex);
@ -832,19 +865,16 @@ QStringList RenderableModelEntityItem::getJointNames() const {
} }
bool RenderableModelEntityItem::getMeshes(MeshProxyList& result) { bool RenderableModelEntityItem::getMeshes(MeshProxyList& result) {
if (!_model || !_model->isLoaded()) { auto model = getModel();
if (!model || !model->isLoaded()) {
return false; return false;
} }
BLOCKING_INVOKE_METHOD(_model.get(), "getMeshes", Q_RETURN_ARG(MeshProxyList, result)); BLOCKING_INVOKE_METHOD(model.get(), "getMeshes", Q_RETURN_ARG(MeshProxyList, result));
return !result.isEmpty(); return !result.isEmpty();
} }
void RenderableModelEntityItem::copyAnimationJointDataToModel() { void RenderableModelEntityItem::copyAnimationJointDataToModel() {
ModelPointer model; auto model = getModel();
withReadLock([&] {
model = _model;
});
if (!model || !model->isLoaded()) { if (!model || !model->isLoaded()) {
return; return;
} }
@ -859,7 +889,7 @@ void RenderableModelEntityItem::copyAnimationJointDataToModel() {
jointData.rotationDirty = false; jointData.rotationDirty = false;
} }
if (jointData.translationDirty) { if (jointData.translationDirty) {
_model->setJointTranslation(index, true, jointData.joint.translation, 1.0f); model->setJointTranslation(index, true, jointData.joint.translation, 1.0f);
jointData.translationDirty = false; jointData.translationDirty = false;
} }
} }
@ -1192,7 +1222,6 @@ void ModelEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce
// When the individual mesh parts of a model finish fading, they will mark their Model as needing updating // When the individual mesh parts of a model finish fading, they will mark their Model as needing updating
// we will watch for that and ask the model to update it's render items // we will watch for that and ask the model to update it's render items
if (model->getRenderItemsNeedUpdate()) { if (model->getRenderItemsNeedUpdate()) {
qDebug() << "QQQ" << __FUNCTION__ << "Update model render items" << model->getURL();
model->updateRenderItems(); model->updateRenderItems();
} }

View file

@ -34,10 +34,24 @@ class ModelEntityRenderer;
} } } }
//#define MODEL_ENTITY_USE_FADE_EFFECT //#define MODEL_ENTITY_USE_FADE_EFFECT
class ModelEntityWrapper : public ModelEntityItem {
class RenderableModelEntityItem : public ModelEntityItem { using Parent = ModelEntityItem;
friend class render::entities::ModelEntityRenderer; friend class render::entities::ModelEntityRenderer;
protected:
ModelEntityWrapper(const EntityItemID& entityItemID) : Parent(entityItemID) {}
void setModel(const ModelPointer& model);
ModelPointer getModel() const;
bool isModelLoaded() const;
bool _needsInitialSimulation{ true };
private:
ModelPointer _model;
};
class RenderableModelEntityItem : public ModelEntityWrapper {
friend class render::entities::ModelEntityRenderer;
using Parent = ModelEntityWrapper;
public: public:
static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties); static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
@ -97,14 +111,11 @@ private:
bool isAnimatingSomething() const; bool isAnimatingSomething() const;
void autoResizeJointArrays(); void autoResizeJointArrays();
void copyAnimationJointDataToModel(); void copyAnimationJointDataToModel();
void setModel(const ModelPointer& model);
void getCollisionGeometryResource(); void getCollisionGeometryResource();
GeometryResource::Pointer _compoundShapeResource; GeometryResource::Pointer _compoundShapeResource;
bool _originalTexturesRead { false }; bool _originalTexturesRead { false };
QVariantMap _originalTextures; QVariantMap _originalTextures;
ModelPointer _model;
bool _needsInitialSimulation { true };
bool _dimensionsInitialized { true }; bool _dimensionsInitialized { true };
bool _needsJointSimulation { false }; bool _needsJointSimulation { false };
bool _showCollisionGeometry { false }; bool _showCollisionGeometry { false };