Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/HifiExperiments/fixBillboard'
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGro committed Feb 20, 2021
2 parents 6732628 + 852edec commit 937e0a5
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 50 deletions.
17 changes: 11 additions & 6 deletions libraries/entities-renderer/src/RenderableEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ bool EntityRenderer::needsRenderUpdate() const {
return needsRenderUpdateFromEntity(_entity);
}

Transform EntityRenderer::getTransformToCenterWithMaybeOnlyLocalRotation(const EntityItemPointer& entity, bool& success) const {
return entity->getBillboardMode() == BillboardMode::NONE ? entity->getTransformToCenter(success) :
entity->getTransformToCenterWithOnlyLocalRotation(success);
}

// Returns true if the item in question needs to have updateInScene called because of changes in the entity
bool EntityRenderer::needsRenderUpdateFromEntity(const EntityItemPointer& entity) const {
if (entity->needsRenderUpdate()) {
Expand All @@ -379,12 +384,12 @@ bool EntityRenderer::needsRenderUpdateFromEntity(const EntityItemPointer& entity
}

bool success = false;
auto bound = _entity->getAABox(success);
auto bound = entity->getAABox(success);
if (success && _bound != bound) {
return true;
}

auto newModelTransform = _entity->getTransformToCenter(success);
auto newModelTransform = getTransformToCenterWithMaybeOnlyLocalRotation(entity, success);
// FIXME can we use a stale model transform here?
if (success && newModelTransform != _modelTransform) {
return true;
Expand All @@ -401,15 +406,15 @@ bool EntityRenderer::needsRenderUpdateFromEntity(const EntityItemPointer& entity
return false;
}

void EntityRenderer::updateModelTransformAndBound() {
void EntityRenderer::updateModelTransformAndBound(const EntityItemPointer& entity) {
bool success = false;
auto newModelTransform = _entity->getTransformToCenter(success);
auto newModelTransform = getTransformToCenterWithMaybeOnlyLocalRotation(entity, success);
if (success) {
_modelTransform = newModelTransform;
}

success = false;
auto bound = _entity->getAABox(success);
auto bound = entity->getAABox(success);
if (success) {
_bound = bound;
}
Expand All @@ -429,7 +434,7 @@ void EntityRenderer::doRenderUpdateSynchronous(const ScenePointer& scene, Transa

_prevIsTransparent = transparent;

updateModelTransformAndBound();
updateModelTransformAndBound(entity);

_moving = entity->isMovingRelativeToParent();
_visible = entity->getVisible();
Expand Down
11 changes: 6 additions & 5 deletions libraries/entities-renderer/src/RenderableEntityItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,22 @@ class EntityRenderer : public QObject, public std::enable_shared_from_this<Entit
virtual void doRender(RenderArgs* args) = 0;

virtual bool isFading() const { return _isFading; }
virtual void updateModelTransformAndBound();
virtual void updateModelTransformAndBound(const EntityItemPointer& entity);
virtual bool isTransparent() const { return _isFading ? Interpolate::calculateFadeRatio(_fadeStartTime) < 1.0f : false; }
inline bool isValidRenderItem() const { return _renderItemID != Item::INVALID_ITEM_ID; }

virtual void setIsVisibleInSecondaryCamera(bool value) { _isVisibleInSecondaryCamera = value; }
virtual void setRenderLayer(RenderLayer value) { _renderLayer = value; }
virtual void setCullWithParent(bool value) { _cullWithParent = value; }

signals:
void requestRenderUpdate();

protected:
template<typename T>
std::shared_ptr<T> asTypedEntity() { return std::static_pointer_cast<T>(_entity); }

static void makeStatusGetters(const EntityItemPointer& entity, Item::Status::Getters& statusGetters);
const Transform& getModelTransform() const;

Transform getTransformToCenterWithMaybeOnlyLocalRotation(const EntityItemPointer& entity, bool& success) const;

Item::Bound _bound;
SharedSoundPointer _collisionSound;
QUuid _changeHandlerId;
Expand Down Expand Up @@ -154,6 +152,9 @@ class EntityRenderer : public QObject, public std::enable_shared_from_this<Entit
const EntityItemPointer _entity;

QUuid _entityID;

signals:
void requestRenderUpdate();
};

template <typename T>
Expand Down
5 changes: 3 additions & 2 deletions libraries/entities-renderer/src/RenderableModelEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ bool RenderableModelEntityItem::needsUpdateModelBounds() const {
}

bool success;
auto transform = getTransform(success);
auto transform = getBillboardMode() == BillboardMode::NONE ? getTransform(success) : getTransformWithOnlyLocalRotation(success);
if (success) {
if (model->getTranslation() != transform.getTranslation()) {
return true;
}

if (model->getRotation() != transform.getRotation()) {
return true;
}
Expand Down Expand Up @@ -171,7 +172,7 @@ void RenderableModelEntityItem::updateModelBounds() {
}

bool success;
auto transform = getTransform(success);
auto transform = getBillboardMode() == BillboardMode::NONE ? getTransform(success) : getTransformWithOnlyLocalRotation(success);
if (success && (model->getTranslation() != transform.getTranslation() ||
model->getRotation() != transform.getRotation())) {
model->setTransformNoUpdateRenderItems(transform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ PolyLineEntityRenderer::PolyLineEntityRenderer(const EntityItemPointer& entity)
}
}

void PolyLineEntityRenderer::updateModelTransformAndBound() {
void PolyLineEntityRenderer::updateModelTransformAndBound(const EntityItemPointer& entity) {
bool success = false;
auto newModelTransform = _entity->getTransformToCenter(success);
auto newModelTransform = getTransformToCenterWithMaybeOnlyLocalRotation(entity, success);
if (success) {
_modelTransform = newModelTransform;

auto lineEntity = std::static_pointer_cast<PolyLineEntityItem>(_entity);
auto lineEntity = std::static_pointer_cast<PolyLineEntityItem>(entity);
AABox bound;
lineEntity->computeTightLocalBoundingBox(bound);
bound.transform(newModelTransform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PolyLineEntityRenderer : public TypedEntityRenderer<PolyLineEntityItem> {
public:
PolyLineEntityRenderer(const EntityItemPointer& entity);

void updateModelTransformAndBound() override;
void updateModelTransformAndBound(const EntityItemPointer& entity) override;

virtual bool isTransparent() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1785,8 +1785,9 @@ void PolyVoxEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& s

void PolyVoxEntityRenderer::doRenderUpdateAsynchronousTyped(const TypedEntityPointer& entity) {
_lastVoxelToLocalMatrix = entity->voxelToLocalMatrix();
_position = entity->getWorldPosition();
_orientation = entity->getWorldOrientation();
bool success;
_position = entity->getCenterPosition(success);
_orientation = entity->getBillboardMode() == BillboardMode::NONE ? entity->getWorldOrientation() : entity->getLocalOrientation();
_lastVoxelVolumeSize = entity->getVoxelVolumeSize();
_params->setSubData(0, vec4(_lastVoxelVolumeSize, 0.0));
graphics::MeshPointer newMesh;
Expand Down
7 changes: 2 additions & 5 deletions libraries/entities-renderer/src/RenderableShapeEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ void ShapeEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& sce
AbstractViewStateInterface::instance()->pushPostUpdateLambda(key, [this, entity] {
withWriteLock([&] {
_shape = entity->getShape();
_position = entity->getWorldPosition();
_dimensions = entity->getUnscaledDimensions(); // get unscaled to avoid scaling twice
_orientation = entity->getWorldOrientation();
_renderTransform = getModelTransform(); // contains parent scale, if this entity scales with its parent
if (_shape == entity::Sphere) {
_renderTransform.postScale(SPHERE_ENTITY_SCALE);
}

_renderTransform.postScale(_dimensions);
_renderTransform.postScale(entity->getUnscaledDimensions());
});
});
}
Expand Down Expand Up @@ -248,7 +245,7 @@ void ShapeEntityRenderer::doRender(RenderArgs* args) {
outColor = procedural->getColor(outColor);
outColor.a *= procedural->isFading() ? Interpolate::calculateFadeRatio(procedural->getFadeStartTime()) : 1.0f;
withReadLock([&] {
procedural->prepare(batch, _position, _dimensions, _orientation, _created, ProceduralProgramKey(outColor.a < 1.0f));
procedural->prepare(batch, transform.getTranslation(), transform.getScale(), transform.getRotation(), _created, ProceduralProgramKey(outColor.a < 1.0f));
});

if (render::ShapeKey(args->_globalShapeKey).isWireframe() || _primitiveMode == PrimitiveMode::LINES) {
Expand Down
4 changes: 0 additions & 4 deletions libraries/entities-renderer/src/RenderableShapeEntityItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class ShapeEntityRenderer : public TypedEntityRenderer<ShapeEntityItem> {
std::shared_ptr<graphics::ProceduralMaterial> _material { std::make_shared<graphics::ProceduralMaterial>() };
glm::vec3 _color { NAN };
float _alpha { NAN };

glm::vec3 _position;
glm::vec3 _dimensions;
glm::quat _orientation;
};

} }
Expand Down
12 changes: 6 additions & 6 deletions libraries/entities-renderer/src/RenderableTextEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ void entities::TextPayload::render(RenderArgs* args) {
}
auto textRenderable = std::static_pointer_cast<TextEntityRenderer>(renderable);

Transform modelTransform;
Transform transform;
glm::vec3 dimensions;

glm::vec4 textColor;
textRenderable->withReadLock([&] {
modelTransform = textRenderable->_renderTransform;
transform = textRenderable->_renderTransform;
dimensions = textRenderable->_dimensions;

float fadeRatio = textRenderable->_isFading ? Interpolate::calculateFadeRatio(textRenderable->_fadeStartTime) : 1.0f;
Expand All @@ -314,13 +314,13 @@ void entities::TextPayload::render(RenderArgs* args) {
return;
}

modelTransform.setRotation(BillboardModeHelpers::getBillboardRotation(modelTransform.getTranslation(), modelTransform.getRotation(), textRenderable->_billboardMode,
transform.setRotation(BillboardModeHelpers::getBillboardRotation(transform.getTranslation(), transform.getRotation(), textRenderable->_billboardMode,
args->_renderMode == RenderArgs::RenderMode::SHADOW_RENDER_MODE ? BillboardModeHelpers::getPrimaryViewFrustumPosition() : args->getViewFrustum().getPosition()));

float scale = textRenderable->_lineHeight / textRenderer->getFontSize();
modelTransform.postTranslate(glm::vec3(-0.5, 0.5, 1.0f + EPSILON / dimensions.z));
modelTransform.setScale(scale);
batch.setModelTransform(modelTransform);
transform.postTranslate(glm::vec3(-0.5, 0.5, 1.0f + EPSILON / dimensions.z));
transform.setScale(scale);
batch.setModelTransform(transform);

glm::vec2 bounds = glm::vec2(dimensions.x - (textRenderable->_leftMargin + textRenderable->_rightMargin), dimensions.y - (textRenderable->_topMargin + textRenderable->_bottomMargin));
textRenderer->draw(batch, textRenderable->_leftMargin / scale, -textRenderable->_topMargin / scale, bounds / scale, scale,
Expand Down
13 changes: 13 additions & 0 deletions libraries/entities/src/EntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,19 @@ const Transform EntityItem::getTransformToCenter(bool& success) const {
return result;
}

const Transform EntityItem::getTransformToCenterWithOnlyLocalRotation(bool& success) const {
Transform result = getTransformWithOnlyLocalRotation(success);
glm::vec3 pivot = getPivot();
if (pivot != ENTITY_ITEM_ZERO_VEC3) {
result.postTranslate(pivot);
}
glm::vec3 registrationPoint = getRegistrationPoint();
if (registrationPoint != ENTITY_ITEM_HALF_VEC3) { // If it is not already centered, translate to center
result.postTranslate((ENTITY_ITEM_HALF_VEC3 - registrationPoint) * getScaledDimensions()); // Position to center
}
return result;
}

/// The maximum bounding cube for the entity, independent of it's rotation.
/// This accounts for the registration point (upon which rotation occurs around).
///
Expand Down
1 change: 1 addition & 0 deletions libraries/entities/src/EntityItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class EntityItem : public QObject, public SpatiallyNestable, public ReadWriteLoc
void setCenterPosition(const glm::vec3& position);

const Transform getTransformToCenter(bool& success) const;
const Transform getTransformToCenterWithOnlyLocalRotation(bool& success) const;

void requiresRecalcBoxes();

Expand Down
10 changes: 6 additions & 4 deletions libraries/entities/src/EntityTreeElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ EntityItemID EntityTreeElement::evalDetailedRayIntersection(const glm::vec3& ori
// extents is the entity relative, scaled, centered extents of the entity
glm::vec3 position = entity->getWorldPosition();
glm::mat4 translation = glm::translate(position);
glm::quat orientation = entity->getWorldOrientation();
glm::mat4 rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, entity->getBillboardMode(),
BillboardMode billboardMode = entity->getBillboardMode();
glm::quat orientation = billboardMode == BillboardMode::NONE ? entity->getWorldOrientation() : entity->getLocalOrientation();
glm::mat4 rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, billboardMode,
viewFrustumPos, entity->getRotateForPicking()));
glm::mat4 entityToWorldMatrix = translation * rotation;
glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix);
Expand Down Expand Up @@ -368,8 +369,9 @@ EntityItemID EntityTreeElement::evalDetailedParabolaIntersection(const glm::vec3
// extents is the entity relative, scaled, centered extents of the entity
glm::vec3 position = entity->getWorldPosition();
glm::mat4 translation = glm::translate(position);
glm::quat orientation = entity->getWorldOrientation();
glm::mat4 rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, entity->getBillboardMode(),
BillboardMode billboardMode = entity->getBillboardMode();
glm::quat orientation = billboardMode == BillboardMode::NONE ? entity->getWorldOrientation() : entity->getLocalOrientation();
glm::mat4 rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, billboardMode,
viewFrustumPos, entity->getRotateForPicking()));
glm::mat4 entityToWorldMatrix = translation * rotation;
glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix);
Expand Down
10 changes: 6 additions & 4 deletions libraries/entities/src/GizmoEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ bool GizmoEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const
QVariantMap& extraInfo, bool precisionPicking) const {
glm::vec3 dimensions = getScaledDimensions();
glm::vec2 xyDimensions(dimensions.x, dimensions.z);
glm::quat rotation = getWorldOrientation();
BillboardMode billboardMode = getBillboardMode();
glm::quat rotation = billboardMode == BillboardMode::NONE ? getWorldOrientation() : getLocalOrientation();
rotation *= glm::angleAxis(-(float)M_PI_2, Vectors::RIGHT);
glm::vec3 position = getWorldPosition() + rotation * (dimensions * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint()));
rotation = BillboardModeHelpers::getBillboardRotation(position, rotation, getBillboardMode(), viewFrustumPos);
rotation = BillboardModeHelpers::getBillboardRotation(position, rotation, billboardMode, viewFrustumPos);

if (findRayRectangleIntersection(origin, direction, rotation, position, xyDimensions, distance)) {
glm::vec3 hitPosition = origin + (distance * direction);
Expand Down Expand Up @@ -143,10 +144,11 @@ bool GizmoEntityItem::findDetailedParabolaIntersection(const glm::vec3& origin,
//// Scale the dimensions by the diameter
glm::vec3 dimensions = getScaledDimensions();
glm::vec2 xyDimensions(dimensions.x, dimensions.z);
glm::quat rotation = getWorldOrientation();
BillboardMode billboardMode = getBillboardMode();
glm::quat rotation = billboardMode == BillboardMode::NONE ? getWorldOrientation() : getLocalOrientation();
rotation *= glm::angleAxis(-(float)M_PI_2, Vectors::RIGHT);
glm::vec3 position = getWorldPosition();
rotation = BillboardModeHelpers::getBillboardRotation(position, rotation, getBillboardMode(), viewFrustumPos);
rotation = BillboardModeHelpers::getBillboardRotation(position, rotation, billboardMode, viewFrustumPos);

glm::quat inverseRot = glm::inverse(rotation);
glm::vec3 localOrigin = inverseRot * (origin - position);
Expand Down
13 changes: 13 additions & 0 deletions libraries/entities/src/ModelEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ const Transform ModelEntityItem::getTransform(bool& success, int depth) const {

return worldTransform;
}

const Transform ModelEntityItem::getTransformWithOnlyLocalRotation(bool& success, int depth) const {
const Transform parentTransform = getParentTransform(success, depth);
Transform localTransform = getLocalTransform();
localTransform.postScale(getModelScale());

Transform worldTransform;
Transform::mult(worldTransform, parentTransform, localTransform);
worldTransform.setRotation(localTransform.getRotation());

return worldTransform;
}

void ModelEntityItem::setCompoundShapeURL(const QString& url) {
withWriteLock([&] {
if (_compoundShapeURL.get() != url) {
Expand Down
1 change: 1 addition & 0 deletions libraries/entities/src/ModelEntityItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class ModelEntityItem : public EntityItem {
virtual void setScaledDimensions(const glm::vec3& value) override;

virtual const Transform getTransform(bool& success, int depth = 0) const override;
virtual const Transform getTransformWithOnlyLocalRotation(bool& success, int depth = 0) const override;
virtual const Transform getTransform() const override;

static const QString DEFAULT_COMPOUND_SHAPE_URL;
Expand Down
7 changes: 4 additions & 3 deletions libraries/entities/src/PolyVoxEntityItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,15 @@ glm::mat4 PolyVoxEntityItem::localToVoxelMatrix() const {
}

glm::mat4 PolyVoxEntityItem::voxelToWorldMatrix(bool includeBillboard) const {
glm::quat orientation = getWorldOrientation();
glm::vec3 position = getWorldPosition();
glm::mat4 translation = glm::translate(position);
glm::mat4 rotation;
if (includeBillboard) {
rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, getBillboardMode(), BillboardModeHelpers::getPrimaryViewFrustumPosition()));
BillboardMode billboardMode = getBillboardMode();
glm::quat orientation = billboardMode == BillboardMode::NONE ? getWorldOrientation() : getLocalOrientation();
rotation = glm::mat4_cast(BillboardModeHelpers::getBillboardRotation(position, orientation, billboardMode, BillboardModeHelpers::getPrimaryViewFrustumPosition()));
} else {
rotation = glm::mat4_cast(orientation);
rotation = glm::mat4_cast(getWorldOrientation());
}
return translation * rotation * voxelToLocalMatrix();
}
Expand Down
Loading

0 comments on commit 937e0a5

Please sign in to comment.