From 0841f5b479ad4f12b7f18ad2b3efd3628985e528 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 15 Feb 2021 22:09:05 +0100 Subject: [PATCH] Refactor Pre Rotation from spline model and spline collider into Pre Transform for more control --- Source/Engine/Level/Actors/SplineModel.cpp | 26 +++++++++---------- Source/Engine/Level/Actors/SplineModel.h | 10 +++---- .../Physics/Colliders/SplineCollider.cpp | 18 ++++++------- .../Engine/Physics/Colliders/SplineCollider.h | 10 +++---- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/Source/Engine/Level/Actors/SplineModel.cpp b/Source/Engine/Level/Actors/SplineModel.cpp index 7fc623c8d..84306716b 100644 --- a/Source/Engine/Level/Actors/SplineModel.cpp +++ b/Source/Engine/Level/Actors/SplineModel.cpp @@ -28,16 +28,16 @@ SplineModel::~SplineModel() Allocator::Free(_deformationBufferData); } -Quaternion SplineModel::GetPreRotation() const +Transform SplineModel::GetPreTransform() const { - return _preRotation; + return _preTransform; } -void SplineModel::SetPreRotation(const Quaternion& value) +void SplineModel::SetPreTransform(const Transform& value) { - if (_preRotation == value) + if (_preTransform == value) return; - _preRotation = value; + _preTransform = value; OnSplineUpdated(); } @@ -135,8 +135,8 @@ void SplineModel::OnSplineUpdated() for (int32 i = 0; i < 8; i++) { // Transform mesh corner using pre-transform but use double-precision to prevent issues when rotating model - Vector3 tmp = corners[i]; - double rotation[4] = { (double)_preRotation.X, (double)_preRotation.Y, (double)_preRotation.Z, (double)_preRotation.W }; + Vector3 tmp = corners[i] * _preTransform.Scale; + double rotation[4] = { (double)_preTransform.Orientation.X, (double)_preTransform.Orientation.Y, (double)_preTransform.Orientation.Z, (double)_preTransform.Orientation.W }; const double length = sqrt(rotation[0] * rotation[0] + rotation[1] * rotation[1] + rotation[2] * rotation[2] + rotation[3] * rotation[3]); const double inv = 1.0 / length; rotation[0] *= inv; @@ -157,9 +157,9 @@ void SplineModel::OnSplineUpdated() const double yz = rotation[1] * z; const double zz = rotation[2] * z; tmp = Vector3( - (float)(pos[0] * (1.0 - yy - zz) + pos[1] * (xy - wz) + pos[2] * (xz + wy)), - (float)(pos[0] * (xy + wz) + pos[1] * (1.0 - xx - zz) + pos[2] * (yz - wx)), - (float)(pos[0] * (xz - wy) + pos[1] * (yz + wx) + pos[2] * (1.0 - xx - yy))); + (float)(pos[0] * (1.0 - yy - zz) + pos[1] * (xy - wz) + pos[2] * (xz + wy)) + _preTransform.Translation.X, + (float)(pos[0] * (xy + wz) + pos[1] * (1.0 - xx - zz) + pos[2] * (yz - wx)) + _preTransform.Translation.Y, + (float)(pos[0] * (xz - wy) + pos[1] * (yz + wx) + pos[2] * (1.0 - xx - yy)) + _preTransform.Translation.Z); localModelBounds.Minimum = Vector3::Min(localModelBounds.Minimum, tmp); localModelBounds.Maximum = Vector3::Max(localModelBounds.Maximum, tmp); @@ -359,7 +359,7 @@ void SplineModel::Draw(RenderContext& renderContext) drawCall.Deformable.MeshMaxZ = _meshMaxZ; drawCall.Deformable.GeometrySize = _box.GetSize(); drawCall.PerInstanceRandom = GetPerInstanceRandom(); - Matrix::RotationQuaternion(_preRotation, drawCall.Deformable.LocalMatrix); + _preTransform.GetWorld(drawCall.Deformable.LocalMatrix); const Transform splineTransform = GetTransform(); splineTransform.GetWorld(drawCall.World); drawCall.ObjectPosition = drawCall.World.GetTranslation() + drawCall.Deformable.LocalMatrix.GetTranslation(); @@ -445,7 +445,7 @@ void SplineModel::Serialize(SerializeStream& stream, const void* otherObj) SERIALIZE_MEMBER(BoundsScale, _boundsScale); SERIALIZE_MEMBER(LODBias, _lodBias); SERIALIZE_MEMBER(ForcedLOD, _forcedLod); - SERIALIZE_MEMBER(PreRotation, _preRotation) + SERIALIZE_MEMBER(PreTransform, _preTransform) SERIALIZE(Model); SERIALIZE(DrawModes); @@ -462,7 +462,7 @@ void SplineModel::Deserialize(DeserializeStream& stream, ISerializeModifier* mod DESERIALIZE_MEMBER(BoundsScale, _boundsScale); DESERIALIZE_MEMBER(LODBias, _lodBias); DESERIALIZE_MEMBER(ForcedLOD, _forcedLod); - DESERIALIZE_MEMBER(PreRotation, _preRotation); + DESERIALIZE_MEMBER(PreTransform, _preTransform); DESERIALIZE(Model); DESERIALIZE(DrawModes); diff --git a/Source/Engine/Level/Actors/SplineModel.h b/Source/Engine/Level/Actors/SplineModel.h index 9a2d5420b..6a0393cdc 100644 --- a/Source/Engine/Level/Actors/SplineModel.h +++ b/Source/Engine/Level/Actors/SplineModel.h @@ -26,7 +26,7 @@ private: char _forcedLod = -1; bool _deformationDirty = false; Array _instances; - Quaternion _preRotation = Quaternion::Identity; + Transform _preTransform = Transform::Identity; Spline* _spline = nullptr; GPUBuffer* _deformationBuffer = nullptr; void* _deformationBufferData = nullptr; @@ -43,15 +43,15 @@ public: AssetReference Model; /// - /// Gets the rotation applied to the model geometry before placing it over the spline. Can be used to change the way model goes over the spline. + /// Gets the transformation applied to the model geometry before placing it over the spline. Can be used to change the way model goes over the spline. /// API_PROPERTY(Attributes="EditorOrder(21), EditorDisplay(\"Model\")") - Quaternion GetPreRotation() const; + Transform GetPreTransform() const; /// - /// Sets the rotation applied to the model geometry before placing it over the spline. Can be used to change the way model goes over the spline. + /// Sets the transformation applied to the model geometry before placing it over the spline. Can be used to change the way model goes over the spline. /// - API_PROPERTY() void SetPreRotation(const Quaternion& value); + API_PROPERTY() void SetPreTransform(const Transform& value); /// /// The draw passes to use for rendering this object. diff --git a/Source/Engine/Physics/Colliders/SplineCollider.cpp b/Source/Engine/Physics/Colliders/SplineCollider.cpp index 8e7266755..f003971ae 100644 --- a/Source/Engine/Physics/Colliders/SplineCollider.cpp +++ b/Source/Engine/Physics/Colliders/SplineCollider.cpp @@ -22,16 +22,16 @@ SplineCollider::SplineCollider(const SpawnParams& params) CollisionData.Loaded.Bind(this); } -Quaternion SplineCollider::GetPreRotation() const +Transform SplineCollider::GetPreTransform() const { - return _preRotation; + return _preTransform; } -void SplineCollider::SetPreRotation(const Quaternion& value) +void SplineCollider::SetPreTransform(const Transform& value) { - if (_preRotation == value) + if (_preTransform == value) return; - _preRotation = value; + _preTransform = value; UpdateGeometry(); } @@ -130,7 +130,7 @@ void SplineCollider::Serialize(SerializeStream& stream, const void* otherObj) SERIALIZE_GET_OTHER_OBJ(SplineCollider); SERIALIZE(CollisionData); - SERIALIZE_MEMBER(PreRotation, _preRotation) + SERIALIZE_MEMBER(PreTransform, _preTransform) } void SplineCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) @@ -139,7 +139,7 @@ void SplineCollider::Deserialize(DeserializeStream& stream, ISerializeModifier* Collider::Deserialize(stream, modifier); DESERIALIZE(CollisionData); - DESERIALIZE_MEMBER(PreRotation, _preRotation); + DESERIALIZE_MEMBER(PreTransform, _preTransform); } void SplineCollider::OnParentChanged() @@ -205,10 +205,10 @@ void SplineCollider::GetGeometry(PxGeometryHolder& geometry) } // Apply local mesh transformation - if (!_preRotation.IsIdentity()) + if (!_preTransform.IsIdentity()) { for (int32 i = 0; i < collisionVertices.Count(); i++) - collisionVertices[i] = Vector3::Transform(collisionVertices[i], _preRotation); + collisionVertices[i] = _preTransform.LocalToWorld(collisionVertices[i]); } // Find collision geometry local bounds diff --git a/Source/Engine/Physics/Colliders/SplineCollider.h b/Source/Engine/Physics/Colliders/SplineCollider.h index 3bfcfacf1..42cc25e09 100644 --- a/Source/Engine/Physics/Colliders/SplineCollider.h +++ b/Source/Engine/Physics/Colliders/SplineCollider.h @@ -21,7 +21,7 @@ private: PxTriangleMesh* _triangleMesh = nullptr; Array _vertexBuffer; Array _indexBuffer; - Quaternion _preRotation = Quaternion::Identity; + Transform _preTransform = Transform::Identity; public: @@ -32,15 +32,15 @@ public: AssetReference CollisionData; /// - /// Gets the rotation applied to the collision data model geometry before placing it over the spline. Can be used to change the way model goes over the spline. + /// Gets the transformation applied to the collision data model geometry before placing it over the spline. Can be used to change the way model goes over the spline. /// API_PROPERTY(Attributes="EditorOrder(101), EditorDisplay(\"Collider\")") - Quaternion GetPreRotation() const; + Transform GetPreTransform() const; /// - /// Sets the rotation applied to the collision data model geometry before placing it over the spline. Can be used to change the way model goes over the spline. + /// Sets the transformation applied to the collision data model geometry before placing it over the spline. Can be used to change the way model goes over the spline. /// - API_PROPERTY() void SetPreRotation(const Quaternion& value); + API_PROPERTY() void SetPreTransform(const Transform& value); #if USE_EDITOR