예제 #1
0
        protected override void OnUpdate()
        {
            Entities.ForEach((UnityEngine.BoxCollider2D collider) =>
            {
                // Convert the collider if it's valid.
                if (ConversionUtilities.CanConvertCollider(collider))
                {
                    try
                    {
                        var lossyScale = new float3(collider.transform.lossyScale).xy;
                        if (math.any(!math.isfinite(lossyScale)) || math.any(lossyScale <= 0.0f))
                        {
                            throw new ArgumentException("Transform XY scale cannot be zero or Infinite/NaN.", "Transform XY scale.");
                        }

                        var localToWorld = ConversionUtilities.GetColliderLocalToWorld(collider);
                        var size         = collider.size;

                        var geometry = new BoxGeometry
                        {
                            Center      = new float3(localToWorld.MultiplyPoint(collider.offset)).xy,
                            Size        = new float2(size.x * lossyScale.x, size.y * lossyScale.y),
                            Angle       = PhysicsMath.ZRotationFromQuaternion(localToWorld.rotation),
                            BevelRadius = math.max(collider.edgeRadius, PhysicsSettings.Constants.MinimumConvexRadius),
                        };

                        geometry.Validate();

                        var colliderBlob = PhysicsBoxCollider.Create(
                            geometry,
                            ConversionUtilities.GetCollisionFilterFromCollider(collider),
                            ConversionUtilities.GetPhysicsMaterialFromCollider(collider)
                            );

                        // Submit the collider for conversion.
                        m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob);
                    }
                    catch (ArgumentException exception)
                    {
                        UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider);
                    }
                }
            });
        }
예제 #2
0
        protected override void OnUpdate()
        {
            Entities.ForEach((UnityEngine.CircleCollider2D collider) =>
            {
                // Convert the collider if it's valid.
                if (ConversionUtilities.CanConvertCollider(collider))
                {
                    try
                    {
                        var lossyScale = new float3(collider.transform.lossyScale).xy;
                        if (math.any(!math.isfinite(lossyScale)) || math.any(lossyScale <= 0.0f))
                        {
                            throw new ArgumentException("Transform XY scale cannot be zero or Infinite/NaN.", "Transform XY scale.");
                        }

                        var localToWorld = ConversionUtilities.GetColliderLocalToWorld(collider);

                        var geometry = new CircleGeometry
                        {
                            Center = new float3(localToWorld.MultiplyPoint(collider.offset)).xy,
                            Radius = math.clamp(collider.radius * math.cmax(lossyScale), ConversionUtilities.MinRangeClamp, ConversionUtilities.MaxRangeClamp),
                        };

                        var colliderBlob = PhysicsCircleCollider.Create(
                            geometry,
                            ConversionUtilities.GetCollisionFilterFromCollider(collider),
                            ConversionUtilities.GetPhysicsMaterialFromCollider(collider)
                            );

                        // Submit the collider for conversion.
                        m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob);
                    }
                    catch (ArgumentException exception)
                    {
                        UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider);
                    }
                }
            });
        }
예제 #3
0
        protected override void OnUpdate()
        {
            Entities.ForEach((UnityEngine.PolygonCollider2D collider) =>
            {
                // Convert the collider if it's valid.
                if (ConversionUtilities.CanConvertCollider(collider))
                {
                    try
                    {
                        // Only single paths with no more than the maximum allowed vertex allowed.
                        // NOTE: Until we implement a convex polygon decomposition, only the convex hull of these points will be used.
                        var colliderPointCount = collider.GetTotalPointCount();
                        if (collider.pathCount != 1 || colliderPointCount > PhysicsPolygonCollider.Constants.MaxVertexCount)
                        {
                            return;
                        }

                        var lossyScale = new float3(collider.transform.lossyScale).xy;
                        if (math.any(!math.isfinite(lossyScale)) || math.any(lossyScale <= 0.0f))
                        {
                            throw new ArgumentException("Transform XY scale cannot be zero or Infinite/NaN.", "Transform XY scale.");
                        }

                        var localToWorld = ConversionUtilities.GetColliderLocalToWorld(collider);

                        UnityEngine.Vector3 offset = collider.offset;
                        collider.GetPath(0, m_PolygonVertices);

                        var vertices = new NativeArray <float2>(colliderPointCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
                        for (var i = 0; i < colliderPointCount; ++i)
                        {
                            var vertex  = localToWorld.MultiplyPoint(offset + (UnityEngine.Vector3)m_PolygonVertices[i]);
                            vertices[i] = new float2(vertex.x, vertex.y);
                        }

                        var geometry = new PolygonGeometry
                        {
                            Vertices    = vertices,
                            BevelRadius = PhysicsSettings.Constants.MinimumConvexRadius,
                        };

                        var colliderBlob = PhysicsPolygonCollider.Create(
                            geometry,
                            ConversionUtilities.GetCollisionFilterFromCollider(collider),
                            ConversionUtilities.GetPhysicsMaterialFromCollider(collider)
                            );

                        // We finished with the points.
                        vertices.Dispose();

                        // Submit the collider for conversion.
                        m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob);
                    }
                    catch (ArgumentException exception)
                    {
                        UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider);
                    }
                }
            });

            m_PolygonVertices.Clear();
        }
예제 #4
0
        protected override void OnUpdate()
        {
            Entities.ForEach((UnityEngine.CapsuleCollider2D collider) =>
            {
                // Convert the collider if it's valid.
                if (ConversionUtilities.CanConvertCollider(collider))
                {
                    try
                    {
                        UnityEngine.Vector3 vertex0;
                        UnityEngine.Vector3 vertex1;
                        float radius;

                        var halfSize = new float2(collider.size) * 0.5f;

                        if (collider.direction == UnityEngine.CapsuleDirection2D.Vertical)
                        {
                            radius  = halfSize.x;
                            vertex0 = new UnityEngine.Vector3(0.0f, halfSize.y - radius, 0.0f);
                            vertex1 = new UnityEngine.Vector3(0.0f, -halfSize.y + radius, 0.0f);
                        }
                        else
                        {
                            radius  = halfSize.y;
                            vertex0 = new UnityEngine.Vector3(halfSize.x - radius, 0.0f, 0.0f);
                            vertex1 = new UnityEngine.Vector3(-halfSize.x + radius, 0.0f, 0.0f);
                        }

                        // Add offset to capsule.
                        var colliderOffset = (UnityEngine.Vector3)collider.offset;
                        vertex0           += colliderOffset;
                        vertex1           += colliderOffset;

                        var lossyScale = new float3(collider.transform.lossyScale).xy;
                        if (math.any(!math.isfinite(lossyScale)) || math.any(lossyScale <= 0.0f))
                        {
                            throw new ArgumentException("Transform XY scale cannot be zero or Infinite/NaN.", "Transform XY scale.");
                        }

                        var localToWorld = ConversionUtilities.GetColliderLocalToWorld(collider);

                        var geometry = new CapsuleGeometry
                        {
                            Vertex0 = new float3(localToWorld.MultiplyPoint(vertex0)).xy,
                            Vertex1 = new float3(localToWorld.MultiplyPoint(vertex1)).xy,
                            Radius  = math.max(PhysicsSettings.Constants.MinimumConvexRadius, math.cmax(lossyScale) * radius),
                        };

                        var colliderBlob = PhysicsCapsuleCollider.Create(
                            geometry,
                            ConversionUtilities.GetCollisionFilterFromCollider(collider),
                            ConversionUtilities.GetPhysicsMaterialFromCollider(collider)
                            );

                        // Submit the collider for conversion.
                        m_ColliderConversionSystem.SubmitCollider(collider, ref colliderBlob);
                    }
                    catch (ArgumentException exception)
                    {
                        UnityEngine.Debug.LogWarning($"{collider.name}: {exception.Message}", collider);
                    }
                }
            });
        }