From 20e0c241634832cb159f49edeab0d4a707466d53 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Fri, 1 Jan 2021 23:42:06 -0800 Subject: [PATCH] cleanup + create --- .../src/RenderableModelEntityItem.cpp | 6 +++--- libraries/entities/src/EntityItem.cpp | 8 ++++---- libraries/hfm/src/hfm/HFM.cpp | 2 +- libraries/render-utils/src/Model.cpp | 13 +++---------- libraries/render-utils/src/Model.h | 1 - .../create/assets/data/createAppTooltips.json | 3 +++ .../entityProperties/html/js/entityProperties.js | 5 +++++ 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 7efc988bb4..f7baa4e894 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -227,12 +227,12 @@ EntityItemProperties RenderableModelEntityItem::getProperties(const EntityProper glm::vec3 RenderableModelEntityItem::getPivot() const { auto model = getModel(); - auto raycastOffset = EntityItem::getPivot(); + auto pivot = EntityItem::getPivot(); if (!model || !model->isLoaded() || !_useOriginalPivot) { - return raycastOffset; + return pivot; } - return raycastOffset + model->getOriginalOffset(); + return pivot + model->getOriginalOffset(); } bool RenderableModelEntityItem::supportsDetailedIntersection() const { diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 48490aeb51..3996920572 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1628,16 +1628,16 @@ AACube EntityItem::getMaximumAACube(bool& success) const { _recalcMaxAACube = false; // we want to compute the furthestExtent that an entity can extend out from its "position" // to do this we compute the max of these two vec3s: registration and 1-registration - // and then scale by dimensions - glm::vec3 offset = getScaledDimensions() * getRegistrationPoint() + getPivot(); - glm::vec3 maxExtents = glm::max(offset, glm::vec3(1.0f) - offset); + // and then scale by dimensions and add the absolute value of the pivot + glm::vec3 registrationPoint = getRegistrationPoint(); + glm::vec3 maxExtents = getScaledDimensions() * glm::max(registrationPoint, glm::vec3(1.0f) - registrationPoint); // there exists a sphere that contains maxExtents for all rotations float radius = glm::length(maxExtents); // put a cube around the sphere // TODO? replace _maxAACube with _boundingSphereRadius - glm::vec3 minimumCorner = centerOfRotation - glm::vec3(radius, radius, radius); + glm::vec3 minimumCorner = (centerOfRotation + getPivot()) - glm::vec3(radius, radius, radius); _maxAACube = AACube(minimumCorner, radius * 2.0f); } } else { diff --git a/libraries/hfm/src/hfm/HFM.cpp b/libraries/hfm/src/hfm/HFM.cpp index 8fb0720c0d..dd13d6d4f3 100644 --- a/libraries/hfm/src/hfm/HFM.cpp +++ b/libraries/hfm/src/hfm/HFM.cpp @@ -104,7 +104,7 @@ bool HFMModel::convexHullContains(const glm::vec3& point) const { auto checkEachPrimitive = [=](HFMMesh& mesh, QVector indices, int primitiveSize) -> bool { // Check whether the point is "behind" all the primitives. // But first must transform from model-frame into mesh-frame - glm::vec3 transformedPoint = glm::vec3(glm::inverse(mesh.modelTransform) * glm::vec4(point, 1.0f)); + glm::vec3 transformedPoint = glm::vec3(glm::inverse(offset * mesh.modelTransform) * glm::vec4(point, 1.0f)); int verticesSize = mesh.vertices.size(); for (int j = 0; j < indices.size() - 2; // -2 in case the vertices aren't the right size -- we access j + 2 below diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index a58bfd6f5b..44dbc00f70 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -151,7 +151,6 @@ void Model::setOffset(const glm::vec3& offset) { // if someone manually sets our offset, then we are no longer snapped to center _snapModelToRegistrationPoint = false; _snappedToRegistrationPoint = false; - _needsTransformUpdate = true; } void Model::calculateTextureInfo() { @@ -384,9 +383,7 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g if (_snapModelToRegistrationPoint || _forceOffset) { meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(_offset)); } else { - Extents unscaledExtents = getUnscaledMeshExtents(); - glm::vec3 unscaledDimensions = unscaledExtents.maximum - unscaledExtents.minimum; - meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(unscaledDimensions * (0.5f - _registrationPoint))); + meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(getNaturalDimensions() * (0.5f - _registrationPoint))); } glm::mat4 worldToMeshMatrix = glm::inverse(meshToWorldMatrix); @@ -550,9 +547,7 @@ bool Model::findParabolaIntersectionAgainstSubMeshes(const glm::vec3& origin, co if (_snapModelToRegistrationPoint || _forceOffset) { meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(_offset)); } else { - Extents unscaledExtents = getUnscaledMeshExtents(); - glm::vec3 unscaledDimensions = unscaledExtents.maximum - unscaledExtents.minimum; - meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(unscaledDimensions * (0.5f - _registrationPoint))); + meshToWorldMatrix = meshToWorldMatrix * (glm::scale(_scale) * glm::translate(getNaturalDimensions() * (0.5f - _registrationPoint))); } glm::mat4 worldToMeshMatrix = glm::inverse(meshToWorldMatrix); @@ -1418,7 +1413,6 @@ void Model::setSnapModelToRegistrationPoint(bool snapModelToRegistrationPoint, c _snapModelToRegistrationPoint = snapModelToRegistrationPoint; _registrationPoint = clampedRegistrationPoint; _snappedToRegistrationPoint = false; // force re-centering - _needsTransformUpdate = true; } } @@ -1446,7 +1440,7 @@ void Model::setUseDualQuaternionSkinning(bool value) { void Model::simulate(float deltaTime, bool fullUpdate) { DETAILED_PROFILE_RANGE(simulation_detail, __FUNCTION__); fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit) - || (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint) || _needsTransformUpdate; + || (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint); if (isLoaded() && fullUpdate) { onInvalidate(); @@ -1462,7 +1456,6 @@ void Model::simulate(float deltaTime, bool fullUpdate) { glm::mat4 parentTransform = glm::scale(_scale) * ((_snapModelToRegistrationPoint || _forceOffset) ? glm::translate(_offset) : glm::translate(getNaturalDimensions() * (0.5f - _registrationPoint))); updateRig(deltaTime, parentTransform); - _needsTransformUpdate = false; } } diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index aa5df1a3dd..9bf828f9fb 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -413,7 +413,6 @@ protected: bool _snapModelToRegistrationPoint; /// is the model's offset automatically adjusted to a registration point in model space bool _snappedToRegistrationPoint; /// are we currently snapped to a registration point glm::vec3 _registrationPoint { glm::vec3(0.5f) }; /// the point in model space our center is snapped to - bool _needsTransformUpdate { false }; bool _forceOffset { false }; std::vector _meshStates; diff --git a/scripts/system/create/assets/data/createAppTooltips.json b/scripts/system/create/assets/data/createAppTooltips.json index 478793ab78..7c70a8dea8 100644 --- a/scripts/system/create/assets/data/createAppTooltips.json +++ b/scripts/system/create/assets/data/createAppTooltips.json @@ -601,6 +601,9 @@ "groupCulled": { "tooltip": "If false, individual pieces of the entity may be culled by the render engine. If true, either the entire entity will be culled, or it won't at all." }, + "useOriginalPivot": { + "tooltip": "If false, the model will be centered based on its content, ignoring any offset in the model itself. If true, the model will respect its original offset." + }, "webColor": { "tooltip": "The tint of the web entity." }, diff --git a/scripts/system/create/entityProperties/html/js/entityProperties.js b/scripts/system/create/entityProperties/html/js/entityProperties.js index f3f92a887e..592808bd15 100644 --- a/scripts/system/create/entityProperties/html/js/entityProperties.js +++ b/scripts/system/create/entityProperties/html/js/entityProperties.js @@ -614,6 +614,11 @@ const GROUPS = [ propertyID: "compoundShapeURL", hideIfCertified: true, }, + { + label: "Use Original Pivot", + type: "bool", + propertyID: "useOriginalPivot", + }, { label: "Animation", type: "string",