Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static unsafe BlobAssetReference<Collider> CloneConvexCollider(Collider* colliderSource, in float3 scale, int memorySize)
- {
- var newColliderPtr = UnsafeUtility.Malloc(memorySize, 16, Allocator.Temp);
- UnsafeUtility.MemCpy(newColliderPtr, colliderSource, memorySize);
- ConvexCollider* newColliderCastPointer = (ConvexCollider*) newColliderPtr;
- int numVerts = newColliderCastPointer->ConvexHull.NumVertices;
- float3* vertPtr = newColliderCastPointer->ConvexHull.VerticesPtr;
- float3 adjustedScale = (scale - COLLIDER_SHRINK_VALUE) * COLLIDER_SIZE_MULTIPLIER;
- for (int i = 0; i < numVerts; ++i)
- {
- vertPtr[i] = vertPtr[i] * adjustedScale;
- }
- var planes = newColliderCastPointer->ConvexHull.Planes;
- float3x3 scaleMatrix = float3x3.Scale(adjustedScale);
- float3x3 normalScale = math.inverse(scaleMatrix);
- normalScale = math.transpose(normalScale);
- for (int i = 0; i < planes.Length; ++i)
- {
- float3 origin = planes[i].Normal * -planes[i].Distance;
- float3 newOrigin = math.mul(scaleMatrix, origin);
- float3 newNormal = math.normalize(math.mul(normalScale, planes[i].Normal));
- planes[i] = Math.PlaneFromDirection(newOrigin, newNormal);
- }
- BlobAssetReference<Collider> collider = BlobAssetReference<Collider>.Create(newColliderPtr, memorySize);
- UnsafeUtility.Free(newColliderPtr, Allocator.Temp);
- return collider;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement