void PxCloth::onRemove()
{   
   SAFE_DELETE( mMatInst );

   if ( isClientObject() )
   {
      _releaseCloth();
      _releaseMesh();

      PhysicsPlugin::getPhysicsResetSignal().remove( this, &PxCloth::onPhysicsReset );
   }

   removeFromScene();

   Parent::onRemove();
}
bool PxCloth::_createClothPatch()
{
   // Make sure we have a mesh.
   if ( !mClothMesh )
   {
      _initClothMesh();
      if ( !mClothMesh )
         return false;
   }

   // Make sure we can change the world.
   mWorld->releaseWriteLock();

   _releaseCloth();

   NxClothDesc desc;
   desc.globalPose.setRowMajor44( getTransform() );
   desc.thickness = mThickness;
   desc.density = mDensity;
   desc.bendingStiffness = mBendingStiffness;   
   desc.dampingCoefficient = mDampingCoefficient;
   desc.friction = mFriction;
   
   if ( mBendingEnabled )
      desc.flags |= NX_CLF_BENDING;   
   if ( mDampingEnabled )
      desc.flags |= NX_CLF_DAMPING;
   if ( mTriangleCollisionEnabled )
      desc.flags |= NX_CLF_TRIANGLE_COLLISION;
   if ( mSelfCollisionEnabled )
      desc.flags |= NX_CLF_SELFCOLLISION;

   desc.clothMesh = mClothMesh;    
   desc.meshData = mReceiveBuffers;

   if ( !desc.isValid() )
      return false;

   mCloth = mScene->createCloth( desc );
   mIsVBDirty = true;

   _updateStaticCloth();
   _setupAttachments();

   return true;
}
void PxCloth::unpackUpdate( NetConnection *conn, BitStream *stream )
{
   Parent::unpackUpdate( conn, stream );

   // TransformMask
   if ( stream->readFlag() )
   {
      MatrixF mat;
      mathRead( *stream, &mat );
      setTransform( mat );
   }

   // MaterialMask
   if ( stream->readFlag() )
   {
      stream->read( &mMaterialName );
      SAFE_DELETE( mMatInst );
   }

   // ClothMask
   if ( stream->readFlag() )
   {
      Point2I patchVerts;
      Point2F patchSize;
      mathRead( *stream, &patchVerts );
      mathRead( *stream, &patchSize );

      if (  patchVerts != mPatchVerts ||
            !patchSize.equal( mPatchSize ) )
      {
         mPatchVerts = patchVerts;
         mPatchSize = patchSize;
         _releaseMesh();
      }

      U32 attachMask;
      stream->read( &attachMask );
      if ( attachMask != mAttachmentMask )
      {
         mAttachmentMask = attachMask;
         _releaseCloth();
      }

      mBendingEnabled = stream->readFlag();
      mDampingEnabled = stream->readFlag();
      mTriangleCollisionEnabled = stream->readFlag();
      mSelfCollisionEnabled = stream->readFlag();
      stream->read( &mThickness );
      stream->read( &mFriction );
      stream->read( &mBendingStiffness );
      stream->read( &mDampingCoefficient );

      F32 density;
      stream->read( &density );
      if ( density != mDensity )
      {
         mDensity = density;
         _releaseCloth();
      }

      if (  isClientObject() && 
            isProperlyAdded() &&
            mWorld &&
            !mCloth )
      {
         _createClothPatch();
      }

      _updateClothProperties();
   }
}
Exemple #4
0
void PxCloth::unpackUpdate( NetConnection *conn, BitStream *stream )
{
   Parent::unpackUpdate( conn, stream );

   // TransformMask
   if ( stream->readFlag() )
   {
      MatrixF mat;
      mathRead( *stream, &mat );
   // start jc
      if(mCloth)
      {
         NxVec3 delta(mat.getPosition() - getTransform().getPosition());

         if(mCloth->isSleeping())
            mCloth->wakeUp();

         if ( mAttachmentMask & BIT( 0 ) )
            mCloth->attachVertexToGlobalPosition( 0, mCloth->getPosition( 0 ) + delta );
         if ( mAttachmentMask & BIT( 1 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x-1, mCloth->getPosition( mPatchVerts.x-1 ) + delta );   
         if ( mAttachmentMask & BIT( 2 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - mPatchVerts.x, mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - mPatchVerts.x ) + delta );
         if ( mAttachmentMask & BIT( 3 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - 1, mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - 1 ) + delta );   
         if ( mAttachmentMask & BIT( 4 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - (mPatchVerts.x/2), mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - (mPatchVerts.x/2) ) + delta );
         if ( mAttachmentMask & BIT( 5 ) )
            mCloth->attachVertexToGlobalPosition( (mPatchVerts.x/2), mCloth->getPosition( (mPatchVerts.x/2) ) + delta );
         if ( mAttachmentMask & BIT( 6 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x * (mPatchVerts.y/2), mCloth->getPosition( mPatchVerts.x * (mPatchVerts.y/2) ) + delta );
         if ( mAttachmentMask & BIT( 7 ) )
            mCloth->attachVertexToGlobalPosition( mPatchVerts.x * (mPatchVerts.y/2) + (mPatchVerts.x-1), mCloth->getPosition( mPatchVerts.x * (mPatchVerts.y/2) + (mPatchVerts.x-1) ) + delta );
         
         if ( mAttachmentMask & BIT( 8 ) )
            for ( U32 i = mPatchVerts.x * mPatchVerts.y - mPatchVerts.x; i < mPatchVerts.x * mPatchVerts.y; i++ )
               mCloth->attachVertexToGlobalPosition( i, mCloth->getPosition( i ) + delta );
         
         if ( mAttachmentMask & BIT( 9 ) )
            for ( U32 i = 0; i < mPatchVerts.x; i++ )
               mCloth->attachVertexToGlobalPosition( i, mCloth->getPosition( i ) + delta );

         if ( mAttachmentMask & BIT( 10 ) )
            for ( U32 i = 0; i < mPatchVerts.x * mPatchVerts.y; i+=mPatchVerts.x )
               mCloth->attachVertexToGlobalPosition( i, mCloth->getPosition( i ) + delta );

         if ( mAttachmentMask & BIT( 11 ) )
            for ( U32 i = mPatchVerts.x-1; i < mPatchVerts.x * mPatchVerts.y; i+=mPatchVerts.x )
               mCloth->attachVertexToGlobalPosition( i, mCloth->getPosition( i ) + delta );
      }
   // end jc


      setTransform( mat );
   }

// start jc
   // ScaleAttachmentPointsMask
   if ( stream->readFlag() )
   {
      Point3F attachmentPointScale;
      mathRead( *stream, &attachmentPointScale );

      if(mCloth)
      {
         Point3F scale(Point3F::One);
         scale.convolveInverse(mAttachmentPointScale);
         scale.convolve(attachmentPointScale);
         NxVec3 delta(scale);

         if(mCloth->isSleeping())
            mCloth->wakeUp();

         static NxVec3 delta2;
         if ( mAttachmentMask & BIT( 0 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( 0 ),delta);                                                       mCloth->attachVertexToGlobalPosition( 0, delta2); }
         if ( mAttachmentMask & BIT( 1 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x-1 ), delta);                                        mCloth->attachVertexToGlobalPosition( mPatchVerts.x-1, delta2); } 
         if ( mAttachmentMask & BIT( 2 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - mPatchVerts.x ), delta);          mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - mPatchVerts.x, delta2); }
         if ( mAttachmentMask & BIT( 3 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - 1 ), delta);                      mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - 1, delta2); }
         if ( mAttachmentMask & BIT( 4 ) )
         {   delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x * mPatchVerts.y - (mPatchVerts.x/2) ), delta);     mCloth->attachVertexToGlobalPosition( mPatchVerts.x * mPatchVerts.y - (mPatchVerts.x/2), delta2); }
         if ( mAttachmentMask & BIT( 5 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( (mPatchVerts.x/2) ), delta);                                      mCloth->attachVertexToGlobalPosition( (mPatchVerts.x/2), delta2); }
         if ( mAttachmentMask & BIT( 6 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x * (mPatchVerts.y/2) ), delta);                      mCloth->attachVertexToGlobalPosition( mPatchVerts.x * (mPatchVerts.y/2), delta2); }
         if ( mAttachmentMask & BIT( 7 ) )
         {  delta2.arrayMultiply(mCloth->getPosition( mPatchVerts.x * (mPatchVerts.y/2) + (mPatchVerts.x-1) ), delta);  mCloth->attachVertexToGlobalPosition( mPatchVerts.x * (mPatchVerts.y/2) + (mPatchVerts.x-1), delta2); }
         
         if ( mAttachmentMask & BIT( 8 ) )
            for ( U32 i = mPatchVerts.x * mPatchVerts.y - mPatchVerts.x; i < mPatchVerts.x * mPatchVerts.y; i++ )
               {   delta2.arrayMultiply(mCloth->getPosition( i ), delta);     mCloth->attachVertexToGlobalPosition( i, delta2); }
         
         if ( mAttachmentMask & BIT( 9 ) )
            for ( U32 i = 0; i < mPatchVerts.x; i++ )
               {   delta2.arrayMultiply(mCloth->getPosition( i ), delta);     mCloth->attachVertexToGlobalPosition( i, delta2); }

         if ( mAttachmentMask & BIT( 10 ) )
            for ( U32 i = 0; i < mPatchVerts.x * mPatchVerts.y; i+=mPatchVerts.x )
               {   delta2.arrayMultiply(mCloth->getPosition( i ), delta);     mCloth->attachVertexToGlobalPosition( i, delta2); }

         if ( mAttachmentMask & BIT( 11 ) )
            for ( U32 i = mPatchVerts.x-1; i < mPatchVerts.x * mPatchVerts.y; i+=mPatchVerts.x )
               {   delta2.arrayMultiply(mCloth->getPosition( i ), delta);     mCloth->attachVertexToGlobalPosition( i, delta2); }
      }

      mAttachmentPointScale = attachmentPointScale;
   }
// end jc

   // MaterialMask
   if ( stream->readFlag() )
   {
      stream->read( &mMaterialName );
      SAFE_DELETE( mMatInst );
   }

   // ClothMask
   if ( stream->readFlag() )
   {
      Point2I patchVerts;
      Point2F patchSize;
      mathRead( *stream, &patchVerts );
      mathRead( *stream, &patchSize );

      if (  patchVerts != mPatchVerts ||
            !patchSize.equal( mPatchSize ) )
      {
         mPatchVerts = patchVerts;
         mPatchSize = patchSize;
         _releaseMesh();
      }

      U32 attachMask;
      stream->read( &attachMask );
      if ( attachMask != mAttachmentMask )
      {
         mAttachmentMask = attachMask;
         _releaseCloth();
      }

      mBendingEnabled = stream->readFlag();
      mDampingEnabled = stream->readFlag();
      mTriangleCollisionEnabled = stream->readFlag();
      mSelfCollisionEnabled = stream->readFlag();
      stream->read( &mThickness );
      stream->read( &mFriction );
      stream->read( &mBendingStiffness );
      stream->read( &mDampingCoefficient );

      F32 density;
      stream->read( &density );
      if ( density != mDensity )
      {
         mDensity = density;
         _releaseCloth();
      }

      if (  isClientObject() && 
            isProperlyAdded() &&
            mWorld &&
            !mCloth )
      {
         _createClothPatch();
      }

      _updateClothProperties();
   }
}