/** * Create objects, add them to collections and serialize the collections to the steams gSharedStream and gActorStream * This function doesn't setup the gPhysics global as the corresponding physics object is only used locally */ public static void serializeObjects <TSharedStream, TActorStream>(ref TSharedStream sharedStream, ref TActorStream actorStream) where TSharedStream : unmanaged, IPxOutputStream where TActorStream : unmanaged, IPxOutputStream { PxSerializationRegistry *sr = PxSerialization.createSerializationRegistry(ref *gPhysics); PxCollection *sharedCollection = null; PxCollection *actorCollection = null; createCollections(ref sharedCollection, ref actorCollection, ref *sr); // Alternatively to using PxDefaultMemoryOutputStream it would be possible to serialize to files using // PxDefaultFileOutputStream or a similar implementation of PxOutputStream. if (gUseBinarySerialization) { PxSerialization.serializeCollectionToBinary(ref sharedStream, ref *sharedCollection, ref *sr); PxSerialization.serializeCollectionToBinary(ref actorStream, ref *actorCollection, ref *sr, sharedCollection); } else { PxSerialization.serializeCollectionToXml(ref sharedStream, ref *sharedCollection, ref *sr); PxSerialization.serializeCollectionToXml(ref actorStream, ref *actorCollection, ref *sr, null, sharedCollection); } actorCollection->release(); sharedCollection->release(); sr->release(); }
/** * Deserialize shared data and use resulting collection to deserialize and instance actor collections */ public static void deserializeObjects(ref PxInputData sharedData, ref PxInputData actorData) { PxSerializationRegistry *sr = PxSerialization.createSerializationRegistry(ref *gPhysics); PxCollection *sharedCollection = null; { if (gUseBinarySerialization) { void *alignedBlock = createAlignedBlock(sharedData.getLength()); sharedData.read(alignedBlock, sharedData.getLength()); sharedCollection = PxSerialization.createCollectionFromBinary(alignedBlock, ref *sr); } else { sharedCollection = PxSerialization.createCollectionFromXml(ref sharedData, ref *gCooking, ref *sr); } } // Deserialize collection and instantiate objects twice, each time with a different transform PxTransform *transforms = stackalloc PxTransform[2] { new PxTransform(new PxVec3(-5.0f, 0.0f, 0.0f)), new PxTransform(new PxVec3(5.0f, 0.0f, 0.0f)) }; for (uint i = 0; i < 2; i++) { PxCollection *collection = null; // If the PxInputData actorData would refer to a file, it would be better to avoid reading from it twice. // This could be achieved by reading the file once to memory, and then working with copies. // This is particulary practical when using binary serialization, where the data can be directly // converted to physics objects. actorData.seek(0); if (gUseBinarySerialization) { void *alignedBlock = createAlignedBlock(actorData.getLength()); actorData.read(alignedBlock, actorData.getLength()); collection = PxSerialization.createCollectionFromBinary(alignedBlock, ref *sr, sharedCollection); } else { collection = PxSerialization.createCollectionFromXml(ref actorData, ref *gCooking, ref *sr, sharedCollection); } for (uint o = 0; o < collection->getNbObjects(); o++) { //BIOQUIRK: is<T> is not translated https://github.com/MochiLibraries/Mochi.PhysX/issues/11 //PxRigidActor* rigidActor = collection->getObject(o).is<PxRigidActor>(); PxRigidActor *rigidActor = null; { PxBase *obj = collection->getObject(o); //BIOQUIRK: Because PxRigidActor is not concrete, this ends up going down the isKindOf path, which is not publicly accessible. // This is special-cased for this snippet, ideally we should just expose is<T> in a more C#-friendly way. if ((PxConcreteType)obj->getConcreteType() is PxConcreteType.eRIGID_STATIC or PxConcreteType.eARTICULATION_LINK or PxConcreteType.eRIGID_DYNAMIC) { rigidActor = (PxRigidActor *)obj; } } if (rigidActor != null) { PxTransform globalPose = rigidActor->getGlobalPose(); globalPose = globalPose.transform(transforms[i]); rigidActor->setGlobalPose(globalPose); } } gScene->addCollection(*collection); collection->release(); } sharedCollection->release(); PxMaterial *material; gPhysics->getMaterials(&material, 1); PxRigidStatic *groundPlane = PxCreatePlane(ref *gPhysics, new PxPlane(0, 1, 0, 0), ref *material); gScene->addActor(ref *groundPlane); sr->release(); }