mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 17:41:12 +02:00
Support for scale property on model overlays
If "scale" property is present but "dimensions" are not, the model is NOT scale to fit. And the scale of the model's natural dimensions will be affected by the scale properties. If only the "dimensions" property is present, the model will "scale to fit" the dimensions. If both properties are present, the model still be "scale to fit" but the dimension will be scaled by the scale factor. For example: If a model is loaded that is 2cm tall, is loaded with no "dimensions" or "scale" properties. It will be displayed as 2cm tall. {"scale": 2} The above properties will result in a model that is 4cm tall. {"dimensions": 1} This will result in a model that is 1cm tall. {"scale": 2, "dimensions" 2} Will result in a model that is 2cm tall.
This commit is contained in:
parent
e0739dcc9c
commit
1d77cec125
5 changed files with 60 additions and 36 deletions
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
class Base3DOverlay : public Overlay {
|
class Base3DOverlay : public Overlay {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Base3DOverlay();
|
Base3DOverlay();
|
||||||
Base3DOverlay(const Base3DOverlay* base3DOverlay);
|
Base3DOverlay(const Base3DOverlay* base3DOverlay);
|
||||||
|
@ -27,10 +27,10 @@ public:
|
||||||
const glm::vec3& getPosition() const { return _transform.getTranslation(); }
|
const glm::vec3& getPosition() const { return _transform.getTranslation(); }
|
||||||
const glm::quat& getRotation() const { return _transform.getRotation(); }
|
const glm::quat& getRotation() const { return _transform.getRotation(); }
|
||||||
const glm::vec3& getScale() const { return _transform.getScale(); }
|
const glm::vec3& getScale() const { return _transform.getScale(); }
|
||||||
|
|
||||||
// TODO: consider implementing registration points in this class
|
// TODO: consider implementing registration points in this class
|
||||||
const glm::vec3& getCenter() const { return getPosition(); }
|
const glm::vec3& getCenter() const { return getPosition(); }
|
||||||
|
|
||||||
float getLineWidth() const { return _lineWidth; }
|
float getLineWidth() const { return _lineWidth; }
|
||||||
bool getIsSolid() const { return _isSolid; }
|
bool getIsSolid() const { return _isSolid; }
|
||||||
bool getIsDashedLine() const { return _isDashedLine; }
|
bool getIsDashedLine() const { return _isDashedLine; }
|
||||||
|
@ -43,7 +43,7 @@ public:
|
||||||
void setRotation(const glm::quat& value) { _transform.setRotation(value); }
|
void setRotation(const glm::quat& value) { _transform.setRotation(value); }
|
||||||
void setScale(float value) { _transform.setScale(value); }
|
void setScale(float value) { _transform.setScale(value); }
|
||||||
void setScale(const glm::vec3& value) { _transform.setScale(value); }
|
void setScale(const glm::vec3& value) { _transform.setScale(value); }
|
||||||
|
|
||||||
void setLineWidth(float lineWidth) { _lineWidth = lineWidth; }
|
void setLineWidth(float lineWidth) { _lineWidth = lineWidth; }
|
||||||
void setIsSolid(bool isSolid) { _isSolid = isSolid; }
|
void setIsSolid(bool isSolid) { _isSolid = isSolid; }
|
||||||
void setIsDashedLine(bool isDashedLine) { _isDashedLine = isDashedLine; }
|
void setIsDashedLine(bool isDashedLine) { _isDashedLine = isDashedLine; }
|
||||||
|
@ -55,22 +55,22 @@ public:
|
||||||
void setProperties(const QVariantMap& properties) override;
|
void setProperties(const QVariantMap& properties) override;
|
||||||
QVariant getProperty(const QString& property) override;
|
QVariant getProperty(const QString& property) override;
|
||||||
|
|
||||||
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
|
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
|
||||||
BoxFace& face, glm::vec3& surfaceNormal);
|
BoxFace& face, glm::vec3& surfaceNormal);
|
||||||
|
|
||||||
virtual bool findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
|
virtual bool findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
|
||||||
float& distance, BoxFace& face, glm::vec3& surfaceNormal, QString& extraInfo) {
|
float& distance, BoxFace& face, glm::vec3& surfaceNormal, QString& extraInfo) {
|
||||||
return findRayIntersection(origin, direction, distance, face, surfaceNormal);
|
return findRayIntersection(origin, direction, distance, face, surfaceNormal);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Transform _transform;
|
Transform _transform;
|
||||||
|
|
||||||
float _lineWidth;
|
float _lineWidth;
|
||||||
bool _isSolid;
|
bool _isSolid;
|
||||||
bool _isDashedLine;
|
bool _isDashedLine;
|
||||||
bool _ignoreRayIntersection;
|
bool _ignoreRayIntersection;
|
||||||
bool _drawInFront;
|
bool _drawInFront;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_Base3DOverlay_h
|
#endif // hifi_Base3DOverlay_h
|
||||||
|
|
|
@ -19,8 +19,7 @@ QString const ModelOverlay::TYPE = "model";
|
||||||
|
|
||||||
ModelOverlay::ModelOverlay()
|
ModelOverlay::ModelOverlay()
|
||||||
: _model(std::make_shared<Model>(std::make_shared<Rig>())),
|
: _model(std::make_shared<Model>(std::make_shared<Rig>())),
|
||||||
_modelTextures(QVariantMap()),
|
_modelTextures(QVariantMap())
|
||||||
_updateModel(false)
|
|
||||||
{
|
{
|
||||||
_model->init();
|
_model->init();
|
||||||
_isLoaded = false;
|
_isLoaded = false;
|
||||||
|
@ -44,7 +43,11 @@ void ModelOverlay::update(float deltatime) {
|
||||||
if (_updateModel) {
|
if (_updateModel) {
|
||||||
_updateModel = false;
|
_updateModel = false;
|
||||||
_model->setSnapModelToCenter(true);
|
_model->setSnapModelToCenter(true);
|
||||||
_model->setScaleToFit(true, getDimensions());
|
if (_scaleToFit) {
|
||||||
|
_model->setScaleToFit(true, getScale() * getDimensions());
|
||||||
|
} else {
|
||||||
|
_model->setScale(getScale());
|
||||||
|
}
|
||||||
_model->setRotation(getRotation());
|
_model->setRotation(getRotation());
|
||||||
_model->setTranslation(getPosition());
|
_model->setTranslation(getPosition());
|
||||||
_model->setURL(_url);
|
_model->setURL(_url);
|
||||||
|
@ -84,16 +87,33 @@ void ModelOverlay::render(RenderArgs* args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelOverlay::setProperties(const QVariantMap& properties) {
|
void ModelOverlay::setProperties(const QVariantMap& properties) {
|
||||||
auto position = getPosition();
|
auto origPosition = getPosition();
|
||||||
auto rotation = getRotation();
|
auto origRotation = getRotation();
|
||||||
|
auto origDimensions = getDimensions();
|
||||||
|
auto origScale = getScale();
|
||||||
|
|
||||||
Volume3DOverlay::setProperties(properties);
|
Base3DOverlay::setProperties(properties);
|
||||||
|
|
||||||
if (position != getPosition() || rotation != getRotation()) {
|
auto scale = properties["scale"];
|
||||||
_updateModel = true;
|
if (scale.isValid()) {
|
||||||
|
setScale(vec3FromVariant(scale));
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateModel = true;
|
auto dimensions = properties["dimensions"];
|
||||||
|
if (dimensions.isValid()) {
|
||||||
|
_scaleToFit = true;
|
||||||
|
setDimensions(vec3FromVariant(dimensions));
|
||||||
|
} else if (scale.isValid()) {
|
||||||
|
// if "scale" property is set but "dimentions" is not.
|
||||||
|
// do NOT scale to fit.
|
||||||
|
if (scale.isValid()) {
|
||||||
|
_scaleToFit = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (origPosition != getPosition() || origRotation != getRotation() || origDimensions != getDimensions() || origScale != getScale()) {
|
||||||
|
_updateModel = true;
|
||||||
|
}
|
||||||
|
|
||||||
auto urlValue = properties["url"];
|
auto urlValue = properties["url"];
|
||||||
if (urlValue.isValid() && urlValue.canConvert<QString>()) {
|
if (urlValue.isValid() && urlValue.canConvert<QString>()) {
|
||||||
|
@ -101,15 +121,15 @@ void ModelOverlay::setProperties(const QVariantMap& properties) {
|
||||||
_updateModel = true;
|
_updateModel = true;
|
||||||
_isLoaded = false;
|
_isLoaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto texturesValue = properties["textures"];
|
auto texturesValue = properties["textures"];
|
||||||
if (texturesValue.isValid() && texturesValue.canConvert(QVariant::Map)) {
|
if (texturesValue.isValid() && texturesValue.canConvert(QVariant::Map)) {
|
||||||
QVariantMap textureMap = texturesValue.toMap();
|
QVariantMap textureMap = texturesValue.toMap();
|
||||||
foreach(const QString& key, textureMap.keys()) {
|
foreach(const QString& key, textureMap.keys()) {
|
||||||
|
|
||||||
QUrl newTextureURL = textureMap[key].toUrl();
|
QUrl newTextureURL = textureMap[key].toUrl();
|
||||||
qDebug() << "Updating texture named" << key << "to texture at URL" << newTextureURL;
|
qDebug() << "Updating texture named" << key << "to texture at URL" << newTextureURL;
|
||||||
|
|
||||||
QMetaObject::invokeMethod(_model.get(), "setTextureWithNameToURL", Qt::AutoConnection,
|
QMetaObject::invokeMethod(_model.get(), "setTextureWithNameToURL", Qt::AutoConnection,
|
||||||
Q_ARG(const QString&, key),
|
Q_ARG(const QString&, key),
|
||||||
Q_ARG(const QUrl&, newTextureURL));
|
Q_ARG(const QUrl&, newTextureURL));
|
||||||
|
@ -123,8 +143,11 @@ QVariant ModelOverlay::getProperty(const QString& property) {
|
||||||
if (property == "url") {
|
if (property == "url") {
|
||||||
return _url.toString();
|
return _url.toString();
|
||||||
}
|
}
|
||||||
if (property == "dimensions" || property == "scale" || property == "size") {
|
if (property == "dimensions" || property == "size") {
|
||||||
return vec3toVariant(_model->getScaleToFitDimensions());
|
return vec3toVariant(getDimensions());
|
||||||
|
}
|
||||||
|
if (property == "scale") {
|
||||||
|
return vec3toVariant(getScale());
|
||||||
}
|
}
|
||||||
if (property == "textures") {
|
if (property == "textures") {
|
||||||
if (_modelTextures.size() > 0) {
|
if (_modelTextures.size() > 0) {
|
||||||
|
@ -143,14 +166,14 @@ QVariant ModelOverlay::getProperty(const QString& property) {
|
||||||
|
|
||||||
bool ModelOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
|
bool ModelOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
|
||||||
float& distance, BoxFace& face, glm::vec3& surfaceNormal) {
|
float& distance, BoxFace& face, glm::vec3& surfaceNormal) {
|
||||||
|
|
||||||
QString subMeshNameTemp;
|
QString subMeshNameTemp;
|
||||||
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, surfaceNormal, subMeshNameTemp);
|
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, surfaceNormal, subMeshNameTemp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModelOverlay::findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
|
bool ModelOverlay::findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
|
||||||
float& distance, BoxFace& face, glm::vec3& surfaceNormal, QString& extraInfo) {
|
float& distance, BoxFace& face, glm::vec3& surfaceNormal, QString& extraInfo) {
|
||||||
|
|
||||||
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, surfaceNormal, extraInfo);
|
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, surfaceNormal, extraInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,10 @@ private:
|
||||||
|
|
||||||
ModelPointer _model;
|
ModelPointer _model;
|
||||||
QVariantMap _modelTextures;
|
QVariantMap _modelTextures;
|
||||||
|
|
||||||
QUrl _url;
|
QUrl _url;
|
||||||
bool _updateModel;
|
bool _updateModel = { false };
|
||||||
|
bool _scaleToFit = { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_ModelOverlay_h
|
#endif // hifi_ModelOverlay_h
|
||||||
|
|
|
@ -22,7 +22,7 @@ AABox Volume3DOverlay::getBounds() const {
|
||||||
auto extents = Extents{_localBoundingBox};
|
auto extents = Extents{_localBoundingBox};
|
||||||
extents.rotate(getRotation());
|
extents.rotate(getRotation());
|
||||||
extents.shiftBy(getPosition());
|
extents.shiftBy(getPosition());
|
||||||
|
|
||||||
return AABox(extents);
|
return AABox(extents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ void Volume3DOverlay::setProperties(const QVariantMap& properties) {
|
||||||
|
|
||||||
auto dimensions = properties["dimensions"];
|
auto dimensions = properties["dimensions"];
|
||||||
|
|
||||||
// if "dimensions" property was not there, check to see if they included aliases: scale
|
// if "dimensions" property was not there, check to see if they included aliases: scale, size
|
||||||
if (!dimensions.isValid()) {
|
if (!dimensions.isValid()) {
|
||||||
dimensions = properties["scale"];
|
dimensions = properties["scale"];
|
||||||
if (!dimensions.isValid()) {
|
if (!dimensions.isValid()) {
|
||||||
|
@ -57,7 +57,7 @@ bool Volume3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::ve
|
||||||
// extents is the entity relative, scaled, centered extents of the entity
|
// extents is the entity relative, scaled, centered extents of the entity
|
||||||
glm::mat4 worldToEntityMatrix;
|
glm::mat4 worldToEntityMatrix;
|
||||||
_transform.getInverseMatrix(worldToEntityMatrix);
|
_transform.getInverseMatrix(worldToEntityMatrix);
|
||||||
|
|
||||||
glm::vec3 overlayFrameOrigin = glm::vec3(worldToEntityMatrix * glm::vec4(origin, 1.0f));
|
glm::vec3 overlayFrameOrigin = glm::vec3(worldToEntityMatrix * glm::vec4(origin, 1.0f));
|
||||||
glm::vec3 overlayFrameDirection = glm::vec3(worldToEntityMatrix * glm::vec4(direction, 0.0f));
|
glm::vec3 overlayFrameDirection = glm::vec3(worldToEntityMatrix * glm::vec4(direction, 0.0f));
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,13 @@
|
||||||
|
|
||||||
class Volume3DOverlay : public Base3DOverlay {
|
class Volume3DOverlay : public Base3DOverlay {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Volume3DOverlay() {}
|
Volume3DOverlay() {}
|
||||||
Volume3DOverlay(const Volume3DOverlay* volume3DOverlay);
|
Volume3DOverlay(const Volume3DOverlay* volume3DOverlay);
|
||||||
|
|
||||||
virtual AABox getBounds() const override;
|
virtual AABox getBounds() const override;
|
||||||
|
|
||||||
const glm::vec3& getDimensions() const { return _localBoundingBox.getDimensions(); }
|
const glm::vec3& getDimensions() const { return _localBoundingBox.getDimensions(); }
|
||||||
void setDimensions(float value) { _localBoundingBox.setBox(glm::vec3(-value / 2.0f), value); }
|
void setDimensions(float value) { _localBoundingBox.setBox(glm::vec3(-value / 2.0f), value); }
|
||||||
void setDimensions(const glm::vec3& value) { _localBoundingBox.setBox(-value / 2.0f, value); }
|
void setDimensions(const glm::vec3& value) { _localBoundingBox.setBox(-value / 2.0f, value); }
|
||||||
|
@ -29,13 +29,13 @@ public:
|
||||||
void setProperties(const QVariantMap& properties) override;
|
void setProperties(const QVariantMap& properties) override;
|
||||||
QVariant getProperty(const QString& property) override;
|
QVariant getProperty(const QString& property) override;
|
||||||
|
|
||||||
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
|
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance,
|
||||||
BoxFace& face, glm::vec3& surfaceNormal) override;
|
BoxFace& face, glm::vec3& surfaceNormal) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Centered local bounding box
|
// Centered local bounding box
|
||||||
AABox _localBoundingBox{ vec3(0.0f), 1.0f };
|
AABox _localBoundingBox{ vec3(0.0f), 1.0f };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // hifi_Volume3DOverlay_h
|
#endif // hifi_Volume3DOverlay_h
|
||||||
|
|
Loading…
Reference in a new issue