DecoratedShape.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Collision/Shape/DecoratedShape.h>
  5. #include <Jolt/ObjectStream/TypeDeclarations.h>
  6. JPH_NAMESPACE_BEGIN
  7. JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(DecoratedShapeSettings)
  8. {
  9. JPH_ADD_BASE_CLASS(DecoratedShapeSettings, ShapeSettings)
  10. JPH_ADD_ATTRIBUTE(DecoratedShapeSettings, mInnerShape)
  11. }
  12. DecoratedShape::DecoratedShape(EShapeSubType inSubType, const DecoratedShapeSettings &inSettings, ShapeResult &outResult) :
  13. Shape(EShapeType::Decorated, inSubType, inSettings, outResult)
  14. {
  15. // Check that there's a shape
  16. if (inSettings.mInnerShape == nullptr && inSettings.mInnerShapePtr == nullptr)
  17. {
  18. outResult.SetError("Inner shape is null!");
  19. return;
  20. }
  21. if (inSettings.mInnerShapePtr != nullptr)
  22. {
  23. // Use provided shape
  24. mInnerShape = inSettings.mInnerShapePtr;
  25. }
  26. else
  27. {
  28. // Create child shape
  29. ShapeResult child_result = inSettings.mInnerShape->Create();
  30. if (!child_result.IsValid())
  31. {
  32. outResult = child_result;
  33. return;
  34. }
  35. mInnerShape = child_result.Get();
  36. }
  37. }
  38. const PhysicsMaterial *DecoratedShape::GetMaterial(const SubShapeID &inSubShapeID) const
  39. {
  40. return mInnerShape->GetMaterial(inSubShapeID);
  41. }
  42. void DecoratedShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  43. {
  44. mInnerShape->GetSupportingFace(inSubShapeID, inDirection, inScale, inCenterOfMassTransform, outVertices);
  45. }
  46. uint64 DecoratedShape::GetSubShapeUserData(const SubShapeID &inSubShapeID) const
  47. {
  48. return mInnerShape->GetSubShapeUserData(inSubShapeID);
  49. }
  50. void DecoratedShape::SaveSubShapeState(ShapeList &outSubShapes) const
  51. {
  52. outSubShapes.clear();
  53. outSubShapes.push_back(mInnerShape);
  54. }
  55. void DecoratedShape::RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes)
  56. {
  57. JPH_ASSERT(inNumShapes == 1);
  58. mInnerShape = inSubShapes[0];
  59. }
  60. Shape::Stats DecoratedShape::GetStatsRecursive(VisitedShapes &ioVisitedShapes) const
  61. {
  62. // Get own stats
  63. Stats stats = Shape::GetStatsRecursive(ioVisitedShapes);
  64. // Add child stats
  65. Stats child_stats = mInnerShape->GetStatsRecursive(ioVisitedShapes);
  66. stats.mSizeBytes += child_stats.mSizeBytes;
  67. stats.mNumTriangles += child_stats.mNumTriangles;
  68. return stats;
  69. }
  70. JPH_NAMESPACE_END