DecoratedShape.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/Shape/DecoratedShape.h>
  5. #include <ObjectStream/TypeDeclarations.h>
  6. namespace JPH {
  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. uint32 DecoratedShape::GetSubShapeUserData(const SubShapeID &inSubShapeID) const
  43. {
  44. return mInnerShape->GetSubShapeUserData(inSubShapeID);
  45. }
  46. void DecoratedShape::SaveSubShapeState(ShapeList &outSubShapes) const
  47. {
  48. outSubShapes.clear();
  49. outSubShapes.push_back(mInnerShape);
  50. }
  51. void DecoratedShape::RestoreSubShapeState(const ShapeRefC *inSubShapes, uint inNumShapes)
  52. {
  53. JPH_ASSERT(inNumShapes == 1);
  54. mInnerShape = inSubShapes[0];
  55. }
  56. Shape::Stats DecoratedShape::GetStatsRecursive(VisitedShapes &ioVisitedShapes) const
  57. {
  58. // Get own stats
  59. Stats stats = Shape::GetStatsRecursive(ioVisitedShapes);
  60. // Add child stats
  61. Stats child_stats = mInnerShape->GetStatsRecursive(ioVisitedShapes);
  62. stats.mSizeBytes += child_stats.mSizeBytes;
  63. stats.mNumTriangles += child_stats.mNumTriangles;
  64. return stats;
  65. }
  66. } // JPH