internal static CapsuleGeometry GetBakedCapsuleProperties( this PhysicsShapeAuthoring shape, out float3 center, out float height, out EulerAngles orientation ) { var capsule = shape.GetCapsuleProperties(out orientation); return(capsule.BakeToBodySpace(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), out center, out height, ref orientation)); }
public static float4x4 GetBakeToShape(PhysicsShapeAuthoring shape, float3 center, EulerAngles orientation) { var transform = shape.transform; var localToWorld = (float4x4)transform.localToWorldMatrix; var shapeToWorld = shape.GetShapeToWorldMatrix(); return(GetBakeToShape(localToWorld, shapeToWorld, ref center, ref orientation)); }
internal static void GetBakedPlaneProperties( this PhysicsShapeAuthoring shape, out float3 vertex0, out float3 vertex1, out float3 vertex2, out float3 vertex3 ) { shape.GetPlaneProperties(out var center, out var size, out EulerAngles orientation); BakeToBodySpace( center, size, orientation, shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), out vertex0, out vertex1, out vertex2, out vertex3 ); }
internal static void GetBakedConvexProperties( this PhysicsShapeAuthoring shape, NativeList <float3> pointCloud, out ConvexHullGenerationParameters generationParameters, out Hash128 hashedInputs ) { using (var inputs = new NativeList <HashableShapeInputs>(8, Allocator.TempJob)) using (var allSkinIndices = new NativeList <int>(4096, Allocator.TempJob)) using (var allBlendShapeWeights = new NativeList <float>(64, Allocator.TempJob)) { shape.GetConvexHullProperties(pointCloud, true, inputs, allSkinIndices, allBlendShapeWeights); shape.BakePoints(pointCloud); // compute convex radius var center = float3.zero; var orientation = EulerAngles.Default; var localToWorld = shape.transform.localToWorldMatrix; var shapeToWorld = shape.GetShapeToWorldMatrix(); var bakeToShape = GetPrimitiveBakeToShapeMatrix(localToWorld, shapeToWorld, ref center, ref orientation, 1f, k_DefaultAxisPriority); using (var aabb = new NativeArray <Aabb>(1, Allocator.TempJob)) { new GetAabbJob { Points = pointCloud, Aabb = aabb }.Run(); HashableShapeInputs.GetQuantizedTransformations(bakeToShape, aabb[0], out bakeToShape); } var s = new float3(math.length(bakeToShape[0]), math.length(bakeToShape[1]), math.length(bakeToShape[2])); generationParameters = shape.ConvexHullGenerationParameters; generationParameters.SimplificationTolerance = math.max( ConvexHullGenerationParametersExtensions.k_MinRecommendedSimplificationTolerance, math.cmax(s) * generationParameters.SimplificationTolerance ); generationParameters.BevelRadius *= math.cmin(s); using (var hash = new NativeArray <Hash128>(1, Allocator.TempJob)) { var job = new GetShapeInputsHashJob { Result = hash, ForceUniqueIdentifier = (uint)(shape.ForceUnique ? shape.GetInstanceID() : 0), GenerationParameters = generationParameters, Material = shape.GetMaterial(), CollisionFilter = shape.GetFilter(), BakeFromShape = shape.GetLocalToShapeMatrix(), Inputs = inputs, AllSkinIndices = allSkinIndices, AllBlendShapeWeights = allBlendShapeWeights }; job.Run(); hashedInputs = hash[0]; } } }
// matrix to transform point on a primitive from bake space into space of the shape static float4x4 GetPrimitiveBakeToShapeMatrix( PhysicsShapeAuthoring shape, ref float3 center, ref EulerAngles orientation, float3 scale, int3 basisPriority ) { if ( basisPriority.x == basisPriority.y || basisPriority.x == basisPriority.z || basisPriority.y == basisPriority.z ) { throw new ArgumentException(nameof(basisPriority)); } var localToBasis = float4x4.TRS(center, orientation, scale); // correct for imprecision in cases of no scale to prevent e.g., convex radius from being altered if (scale.Equals(new float3(1f))) { localToBasis.c0 = math.normalizesafe(localToBasis.c0); localToBasis.c1 = math.normalizesafe(localToBasis.c1); localToBasis.c2 = math.normalizesafe(localToBasis.c2); } var localToBake = math.mul(shape.transform.localToWorldMatrix, localToBasis); if (localToBake.HasNonUniformScale() || localToBake.HasShear()) { // deskew second longest axis with respect to longest axis localToBake[basisPriority[1]] = DeskewSecondaryAxis(localToBake[basisPriority[0]], localToBake[basisPriority[1]]); // recompute third axes from first two var n2 = math.normalizesafe( new float4(math.cross(localToBake[basisPriority[0]].xyz, localToBake[basisPriority[1]].xyz), 0f) ); localToBake[basisPriority[2]] = n2 * math.dot(localToBake[basisPriority[2]], n2); } var bakeToShape = math.mul(math.inverse(shape.GetShapeToWorldMatrix()), localToBake); // transform baked center/orientation (i.e. primitive basis) into shape space orientation.SetValue( quaternion.LookRotationSafe(bakeToShape[basisPriority[0]].xyz, bakeToShape[basisPriority[1]].xyz) ); center = bakeToShape.c3.xyz; return(bakeToShape); }
internal static void SetBakedSphereRadius(this PhysicsShapeAuthoring shape, float radius) { var sphere = shape.GetSphereProperties(out EulerAngles eulerAngles); var center = sphere.Center; radius = math.abs(radius); var basisToWorld = GetBasisToWorldMatrix(shape.transform.localToWorldMatrix, center, eulerAngles, 1f); var basisPriority = basisToWorld.HasShear() ? GetBasisAxisPriority(basisToWorld) : k_DefaultAxisPriority; var bakeToShape = GetPrimitiveBakeToShapeMatrix(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), ref center, ref eulerAngles, 1f, basisPriority); var scale = math.cmax(bakeToShape.DecomposeScale()); var newRadius = radius / scale; sphere.Radius = newRadius; shape.SetSphere(sphere); }
internal static SphereGeometry GetBakedSphereProperties(this PhysicsShapeAuthoring shape, out EulerAngles orientation) { var sphere = shape.GetSphereProperties(out orientation); return(sphere.BakeToBodySpace(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), ref orientation)); }
internal static CylinderGeometry GetBakedCylinderProperties(this PhysicsShapeAuthoring shape) { var cylinder = shape.GetCylinderProperties(out var orientation); return(cylinder.BakeToBodySpace(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), orientation)); }
internal static CapsuleGeometryAuthoring GetBakedCapsuleProperties(this PhysicsShapeAuthoring shape) { var capsule = shape.GetCapsuleProperties(); return(capsule.BakeToBodySpace(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix())); }
internal static BoxGeometry GetBakedBoxProperties(this PhysicsShapeAuthoring shape) { var box = shape.GetBoxProperties(out var orientation); return(box.BakeToBodySpace(shape.transform.localToWorldMatrix, shape.GetShapeToWorldMatrix(), orientation)); }