Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.   static unsafe BlobAssetReference<Collider> CloneConvexCollider(Collider* colliderSource, in float3 scale, int memorySize)
  2.         {
  3.             var newColliderPtr = UnsafeUtility.Malloc(memorySize, 16, Allocator.Temp);
  4.  
  5.             UnsafeUtility.MemCpy(newColliderPtr, colliderSource, memorySize);
  6.  
  7.             ConvexCollider* newColliderCastPointer = (ConvexCollider*) newColliderPtr;
  8.  
  9.             int numVerts = newColliderCastPointer->ConvexHull.NumVertices;
  10.             float3* vertPtr = newColliderCastPointer->ConvexHull.VerticesPtr;
  11.  
  12.             float3 adjustedScale = (scale - COLLIDER_SHRINK_VALUE) * COLLIDER_SIZE_MULTIPLIER;
  13.  
  14.             for (int i = 0; i < numVerts; ++i)
  15.             {
  16.                 vertPtr[i] = vertPtr[i] * adjustedScale;
  17.             }
  18.  
  19.             var planes = newColliderCastPointer->ConvexHull.Planes;
  20.  
  21.             float3x3 scaleMatrix = float3x3.Scale(adjustedScale);
  22.             float3x3 normalScale = math.inverse(scaleMatrix);
  23.             normalScale = math.transpose(normalScale);
  24.  
  25.             for (int i = 0; i < planes.Length; ++i)
  26.             {
  27.                 float3 origin = planes[i].Normal * -planes[i].Distance;
  28.  
  29.                 float3 newOrigin = math.mul(scaleMatrix, origin);
  30.  
  31.                 float3 newNormal = math.normalize(math.mul(normalScale, planes[i].Normal));
  32.  
  33.                 planes[i] = Math.PlaneFromDirection(newOrigin, newNormal);
  34.             }
  35.  
  36.             BlobAssetReference<Collider> collider = BlobAssetReference<Collider>.Create(newColliderPtr, memorySize);
  37.  
  38.             UnsafeUtility.Free(newColliderPtr, Allocator.Temp);
  39.  
  40.             return collider;
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement