Refactor PhysicalMaterial usage to utilize JsonAssetReference struct

This commit is contained in:
Wojtek Figat
2024-02-15 11:45:50 +01:00
parent fe7cc62728
commit 7538296775
8 changed files with 12 additions and 25 deletions

View File

@@ -206,7 +206,7 @@ void CharacterController::CreateController()
_cachedScale = GetScale(); _cachedScale = GetScale();
const float scaling = _cachedScale.GetAbsolute().MaxValue(); const float scaling = _cachedScale.GetAbsolute().MaxValue();
const Vector3 position = _transform.LocalToWorld(_center); const Vector3 position = _transform.LocalToWorld(_center);
_controller = PhysicsBackend::CreateController(GetPhysicsScene()->GetPhysicsScene(), this, this, _contactOffset, position, _slopeLimit, (int32)_nonWalkableMode, Material.Get(), Math::Abs(_radius) * scaling, Math::Abs(_height) * scaling, _stepOffset, _shape); _controller = PhysicsBackend::CreateController(GetPhysicsScene()->GetPhysicsScene(), this, this, _contactOffset, position, _slopeLimit, (int32)_nonWalkableMode, Material, Math::Abs(_radius) * scaling, Math::Abs(_height) * scaling, _stepOffset, _shape);
// Setup // Setup
PhysicsBackend::SetControllerUpDirection(_controller, _upDirection); PhysicsBackend::SetControllerUpDirection(_controller, _upDirection);

View File

@@ -201,7 +201,7 @@ void Collider::CreateShape()
// Create shape // Create shape
const bool isTrigger = _isTrigger && CanBeTrigger(); const bool isTrigger = _isTrigger && CanBeTrigger();
_shape = PhysicsBackend::CreateShape(this, shape, Material.Get(), IsActiveInHierarchy(), isTrigger); _shape = PhysicsBackend::CreateShape(this, shape, Material, IsActiveInHierarchy(), isTrigger);
PhysicsBackend::SetShapeContactOffset(_shape, _contactOffset); PhysicsBackend::SetShapeContactOffset(_shape, _contactOffset);
UpdateLayerBits(); UpdateLayerBits();
} }
@@ -288,7 +288,7 @@ void Collider::OnMaterialChanged()
{ {
// Update the shape material // Update the shape material
if (_shape) if (_shape)
PhysicsBackend::SetShapeMaterial(_shape, Material.Get()); PhysicsBackend::SetShapeMaterial(_shape, Material);
} }
void Collider::BeginPlay(SceneBeginData* data) void Collider::BeginPlay(SceneBeginData* data)

View File

@@ -4,7 +4,7 @@
#include "Engine/Physics/Types.h" #include "Engine/Physics/Types.h"
#include "Engine/Content/JsonAsset.h" #include "Engine/Content/JsonAsset.h"
#include "Engine/Content/AssetReference.h" #include "Engine/Content/JsonAssetReference.h"
#include "Engine/Physics/Actors/PhysicsColliderActor.h" #include "Engine/Physics/Actors/PhysicsColliderActor.h"
struct RayCastHit; struct RayCastHit;
@@ -80,8 +80,8 @@ public:
/// <summary> /// <summary>
/// The physical material used to define the collider physical properties. /// The physical material used to define the collider physical properties.
/// </summary> /// </summary>
API_FIELD(Attributes="EditorOrder(2), DefaultValue(null), AssetReference(typeof(PhysicalMaterial), true), EditorDisplay(\"Collider\")") API_FIELD(Attributes="EditorOrder(2), DefaultValue(null), EditorDisplay(\"Collider\")")
AssetReference<JsonAsset> Material; JsonAssetReference<PhysicalMaterial> Material;
public: public:
/// <summary> /// <summary>

View File

@@ -15,17 +15,9 @@ class FLAXENGINE_API PhysicalMaterial final : public ISerializable
API_AUTO_SERIALIZATION(); API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE_MINIMAL(PhysicalMaterial); DECLARE_SCRIPTING_TYPE_MINIMAL(PhysicalMaterial);
private: private:
void* _material; void* _material = nullptr;
public: public:
/// <summary>
/// Initializes a new instance of the <see cref="PhysicalMaterial"/> class.
/// </summary>
PhysicalMaterial();
/// <summary>
/// Finalizes an instance of the <see cref="PhysicalMaterial"/> class.
/// </summary>
~PhysicalMaterial(); ~PhysicalMaterial();
public: public:

View File

@@ -78,11 +78,6 @@ void PhysicsSettings::Deserialize(DeserializeStream& stream, ISerializeModifier*
} }
} }
PhysicalMaterial::PhysicalMaterial()
: _material(nullptr)
{
}
PhysicalMaterial::~PhysicalMaterial() PhysicalMaterial::~PhysicalMaterial()
{ {
if (_material) if (_material)

View File

@@ -239,7 +239,7 @@ void Terrain::OnPhysicalMaterialChanged()
const auto patch = _patches[pathIndex]; const auto patch = _patches[pathIndex];
if (patch->HasCollision()) if (patch->HasCollision())
{ {
PhysicsBackend::SetShapeMaterial(patch->_physicsShape, PhysicalMaterial.Get()); PhysicsBackend::SetShapeMaterial(patch->_physicsShape, PhysicalMaterial);
} }
} }
} }

View File

@@ -2,7 +2,7 @@
#pragma once #pragma once
#include "Engine/Content/JsonAsset.h" #include "Engine/Content/JsonAssetReference.h"
#include "Engine/Content/Assets/MaterialBase.h" #include "Engine/Content/Assets/MaterialBase.h"
#include "Engine/Physics/Actors/PhysicsColliderActor.h" #include "Engine/Physics/Actors/PhysicsColliderActor.h"
@@ -79,8 +79,8 @@ public:
/// <summary> /// <summary>
/// The physical material used to define the terrain collider physical properties. /// The physical material used to define the terrain collider physical properties.
/// </summary> /// </summary>
API_FIELD(Attributes="EditorOrder(520), DefaultValue(null), Limit(-1, 100, 0.1f), EditorDisplay(\"Collision\"), AssetReference(typeof(PhysicalMaterial), true)") API_FIELD(Attributes="EditorOrder(520), DefaultValue(null), Limit(-1, 100, 0.1f), EditorDisplay(\"Collision\")")
AssetReference<JsonAsset> PhysicalMaterial; JsonAssetReference<::PhysicalMaterial> PhysicalMaterial;
/// <summary> /// <summary>
/// The draw passes to use for rendering this object. /// The draw passes to use for rendering this object.

View File

@@ -2125,7 +2125,7 @@ void TerrainPatch::CreateCollision()
shape.SetHeightField(_physicsHeightField, heightScale, rowScale, columnScale); shape.SetHeightField(_physicsHeightField, heightScale, rowScale, columnScale);
// Create shape // Create shape
_physicsShape = PhysicsBackend::CreateShape(_terrain, shape, _terrain->PhysicalMaterial.Get(), _terrain->IsActiveInHierarchy(), false); _physicsShape = PhysicsBackend::CreateShape(_terrain, shape, _terrain->PhysicalMaterial, _terrain->IsActiveInHierarchy(), false);
PhysicsBackend::SetShapeLocalPose(_physicsShape, Vector3(0, _yOffset * terrainTransform.Scale.Y, 0), Quaternion::Identity); PhysicsBackend::SetShapeLocalPose(_physicsShape, Vector3(0, _yOffset * terrainTransform.Scale.Y, 0), Quaternion::Identity);
// Create static actor // Create static actor