IUnityPhysicsBackendSystem.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Unity Physics Backend System Native Plugin API copyright © 2025 Unity Technologies ApS
  2. //
  3. // Licensed under the Unity Companion License for Unity - dependent projects--see[Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
  4. //
  5. // Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.Please review the license for details on these and other terms and conditions.
  6. // This interface allows a native plugin to register a custom physics backend implementation.
  7. // The backend receives all physics commands issued by Unity's Physics Module through a single
  8. // entry point function, enabling full replacement of the underlying physics SDK.
  9. //
  10. // The command dispatch model uses a flat function table indexed by command category (bits 26-29)
  11. // and command ID (bits 0-25). Each command is a struct inheriting from PhysicsCommands::Command
  12. // that carries both input parameters and output fields.
  13. //
  14. // Example: Registering a physics backend entry point (based on PhysXBackend.cpp)
  15. //
  16. // #include "IUnityPhysicsBackendSystem.h"
  17. // #include "PhysicsCommands.h"
  18. // #include "PhysXPhysicsExtensionCommands.h" //include only for supporting physx specific features
  19. //
  20. // using namespace PhysicsCommands;
  21. //
  22. // // Per-category function binding tables
  23. // FunctionBinding SDKCommands[static_cast<int>(SDK::Count)];
  24. // FunctionBinding WorldCommands[static_cast<int>(World::Count)];
  25. // FunctionBinding BodyCommands[static_cast<int>(Body::Count)];
  26. // FunctionBinding ShapeCommands[static_cast<int>(Shape::Count)];
  27. // FunctionBinding QueryCommands[static_cast<int>(Query::Count)];
  28. // FunctionBinding JointCommands[static_cast<int>(Joint::Count)];
  29. // FunctionBinding ArticulationCommands[static_cast<int>(Articulation::Count)];
  30. //
  31. // //specific command bindings for PhysX specific features such as Vehicles, CharacterController and Immediate mode
  32. // FunctionBinding PhysXExtCommands[static_cast<int>(PhysXExtension::Count)];
  33. //
  34. // // The single entry point that dispatches all commands by category
  35. // static void ProcessCommand(void* sdkObject, const Context& ctx, uint32_t cmdFunc, Command& c)
  36. // {
  37. // const uint32_t commandType = cmdFunc & kFullMask;
  38. // const int cmdId = cmdFunc & kReverseFullMask;
  39. //
  40. // switch (commandType)
  41. // {
  42. // case kSDKFunctionMask: SDKCommands[cmdId](sdkObject, ctx, c); break;
  43. // case kWorldFunctionMask: WorldCommands[cmdId](sdkObject, ctx, c); break;
  44. // case kBodyFunctionMask: BodyCommands[cmdId](sdkObject, ctx, c); break;
  45. // case kShapeFunctionMask: ShapeCommands[cmdId](sdkObject, ctx, c); break;
  46. // case kQueryFunctionMask: QueryCommands[cmdId](sdkObject, ctx, c); break;
  47. // case kJointFunctionMask: JointCommands[cmdId](sdkObject, ctx, c); break;
  48. // case kArticulationFunctionMask: ArticulationCommands[cmdId](sdkObject, ctx, c); break;
  49. // case kPhysXExtensionMask: PhysXExtCommands[cmdId](sdkObject, ctx, c); break;
  50. // default: break;
  51. // }
  52. // }
  53. //
  54. // // During plugin load, register individual command handlers into the binding tables,
  55. // // then register the entry point with Unity:
  56. //
  57. // bool s_HasRegistered = false;
  58. // extern "C" void UNITY_INTERFACE_EXPORT UnityPluginLoad(IUnityInterfaces* unityInterfaces)
  59. // {
  60. // if(s_HasRegistered)
  61. // return;
  62. //
  63. // auto* physicsBackend = unityInterfaces->Get<IUnityPhysicsBackendSystem>();
  64. //
  65. // // Initialize all slots to a safe default, then register each command handler:
  66. // // SDKCommands[static_cast<int>(SDK::InitializeSdk)] = MyInitializeSdk;
  67. // // WorldCommands[static_cast<int>(World::CreateWorld)] = MyCreateWorld;
  68. // // ... etc.
  69. // // Please note that all commands should be initialized before calling Register.
  70. // // Furthermore SDK::InitializeSdk and SDK::GetIntegrationInfo need to provide valid data for registration to be successful.
  71. // s_HasRegistered = physicsBackend->Register(reinterpret_cast<BackendEntryPoint>(ProcessCommand));
  72. // }
  73. #pragma once
  74. #include "IUnityInterface.h"
  75. namespace PhysicsCommands
  76. {
  77. typedef struct Context UnityPhysicsContext;
  78. typedef struct Command UnityPhysicsCommand;
  79. }
  80. // Function signature for the physics backend entry point.
  81. // sdkObject — opaque handle to the physics SDK instance.
  82. // ctx — provides the current world and body handles for the command being dispatched.
  83. // cmdFunc — encoded command identifier: upper bits select the category, lower bits select the command index.
  84. // c — reference to the command data struct (cast to the appropriate type based on cmdFunc).
  85. typedef void (*BackendEntryPoint)(void* sdkObject, const PhysicsCommands::Context& ctx, const uint32_t cmdFunc, PhysicsCommands::Command& c);
  86. // Interface for registering a custom physics backend with Unity's Physics Module.
  87. // A plugin calls Register() with its entry point function to take over all physics command processing.
  88. UNITY_DECLARE_INTERFACE(IUnityPhysicsBackendSystem)
  89. {
  90. // Registers the backend entry point. Multiple backends can be registered, but each must have a unique ID.
  91. // Returns true if registration succeeded.
  92. bool(UNITY_INTERFACE_API * Register)(BackendEntryPoint entryPoint);
  93. };
  94. UNITY_REGISTER_INTERFACE_GUID(0xED2B16A043DE39F8ULL, 0x924E3DAB294216EAULL, IUnityPhysicsBackendSystem)