/// <summary> /// Generates a collider using a PhysicsShapeAuthoring and PhysicsMaterialsExtensionComponent /// </summary> /// <param name="shape"></param> /// <param name="shapeExt"></param> /// <param name="offsetPosition"></param> /// <param name="offsetRotation"></param> /// <returns></returns> BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, PhysicsMaterialsExtensionComponent shapeExt, out float3 offsetPosition, out quaternion offsetRotation) { CollisionFilter filter = new CollisionFilter { CollidesWith = shape.CollidesWith.Value, BelongsTo = shape.BelongsTo.Value, GroupIndex = 0 }; Unity.Physics.Material material = new Unity.Physics.Material { CollisionResponse = shape.CollisionResponse, CustomTags = shape.CustomTags.Value, Friction = shape.Friction.Value, FrictionCombinePolicy = shape.Friction.CombineMode, Restitution = shape.Restitution.Value, RestitutionCombinePolicy = shape.Restitution.CombineMode, EnableMassFactors = shapeExt != null ? shapeExt.EnableMassFactors : false, EnableSurfaceVelocity = shapeExt != null ? shapeExt.EnableSurfaceVelocity : false }; switch (shape.ShapeType) { case ShapeType.Box: var boxProperties = shape.GetBoxProperties(); offsetPosition = boxProperties.Center; offsetRotation = boxProperties.Orientation; return(Unity.Physics.BoxCollider.Create(boxProperties, filter, material)); case ShapeType.Capsule: var capsuleProperties = shape.GetCapsuleProperties(); var capsuleGeometry = new CapsuleGeometry { Radius = capsuleProperties.Radius, Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius, Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius }; offsetPosition = capsuleProperties.Center; offsetRotation = capsuleProperties.Orientation; return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry, filter, material)); case ShapeType.Cylinder: var cylinderProperties = shape.GetCylinderProperties(); offsetPosition = cylinderProperties.Center; offsetRotation = cylinderProperties.Orientation; return(CylinderCollider.Create(cylinderProperties, filter, material)); case ShapeType.Sphere: var sphereProperties = shape.GetSphereProperties(out var orientation); var SphereGeometry = new SphereGeometry { Center = sphereProperties.Center, Radius = sphereProperties.Radius }; offsetPosition = sphereProperties.Center; offsetRotation = quaternion.identity; return(Unity.Physics.SphereCollider.Create(SphereGeometry, filter, material)); case ShapeType.ConvexHull: NativeList <float3> points = new NativeList <float3>(Allocator.Temp); shape.GetConvexHullProperties(points); var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters, filter, material); // points.Dispose(); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(ConvexCollider); case ShapeType.Mesh: NativeList <float3> verts = new NativeList <float3>(Allocator.Temp); NativeList <int3> tris = new NativeList <int3>(Allocator.Temp); shape.GetMeshProperties(verts, tris); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(Unity.Physics.MeshCollider.Create(verts, tris, filter, material)); default: UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\""); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(new BlobAssetReference <Unity.Physics.Collider>()); } }
BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, out float3 offsetPosition, out quaternion offsetRotation) { switch (shape.ShapeType) { case ShapeType.Box: var boxProperties = shape.GetBoxProperties(); offsetPosition = boxProperties.Center; offsetRotation = boxProperties.Orientation; return(Unity.Physics.BoxCollider.Create(boxProperties)); case ShapeType.Capsule: var capsuleProperties = shape.GetCapsuleProperties(); var capsuleGeometry = new CapsuleGeometry { Radius = capsuleProperties.Radius, Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius, Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius }; offsetPosition = capsuleProperties.Center; offsetRotation = capsuleProperties.Orientation; return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry)); case ShapeType.Cylinder: var cylinderProperties = shape.GetCylinderProperties(); offsetPosition = cylinderProperties.Center; offsetRotation = cylinderProperties.Orientation; return(CylinderCollider.Create(cylinderProperties)); case ShapeType.Sphere: var sphereProperties = shape.GetSphereProperties(out var orientation); var SphereGeometry = new SphereGeometry { Center = sphereProperties.Center, Radius = sphereProperties.Radius }; offsetPosition = sphereProperties.Center; offsetRotation = quaternion.identity; return(Unity.Physics.SphereCollider.Create(SphereGeometry)); case ShapeType.ConvexHull: NativeList <float3> points = new NativeList <float3>(Allocator.Temp); shape.GetConvexHullProperties(points); var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters); // points.Dispose(); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(ConvexCollider); case ShapeType.Mesh: NativeList <float3> verts = new NativeList <float3>(Allocator.Temp); NativeList <int3> tris = new NativeList <int3>(Allocator.Temp); shape.GetMeshProperties(verts, tris); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(Unity.Physics.MeshCollider.Create(verts, tris)); default: UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\""); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(new BlobAssetReference <Unity.Physics.Collider>()); } }