Example #1
0
 void DrawCircleInspector(ColliderAuthoring collider)
 {
     EditorGUI.BeginChangeCheck();
     collider.Height      = EditorGUILayout.FloatField("Height:", collider.Height);
     collider.Radius      = EditorGUILayout.FloatField("Radius:", collider.Radius);
     collider.PivotOffset = EditorGUILayout.Vector3Field("Pivot", collider.PivotOffset);
     if (EditorGUI.EndChangeCheck())
     {
         collider.Height = math.max(0, collider.Height);
         collider.Radius = math.max(0, collider.Radius);
         SceneView.RepaintAll();
     }
 }
Example #2
0
        void DrawPolygonInspector(ColliderAuthoring collider)
        {
            EditorGUI.BeginChangeCheck();
            SnapToGrid           = EditorGUILayout.Toggle("Snap to Grid?", SnapToGrid);
            collider.PivotOffset = EditorGUILayout.Vector3Field("Pivot", collider.PivotOffset);
            collider.Height      = EditorGUILayout.FloatField("Height:", collider.Height);
            if (EditorGUI.EndChangeCheck())
            {
                collider.Height = math.max(0, collider.Height);
                SceneView.RepaintAll();
            }

            var vertexCount = collider.Vertices.Count;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Add/Remove Vertex");
            if (GUILayout.Button("+"))
            {
                collider.Vertices.Add(new Vector2(0, 1));
                Array.Resize(ref ConvexPolygonBuffer, collider.Vertices.Count);
                SceneView.RepaintAll();
            }
            if (GUILayout.Button("-") && vertexCount > 0)
            {
                collider.Vertices.RemoveAt(vertexCount - 1);
                Array.Resize(ref ConvexPolygonBuffer, collider.Vertices.Count);
                SceneView.RepaintAll();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.LabelField("Vertices", EditorStyles.boldLabel);
            for (int i = 0; i < collider.Vertices.Count; i++)
            {
                var vertex = collider.Vertices[i];
                EditorGUILayout.LabelField($"Vertex {i}: ({vertex.x}, {vertex.y})", EditorStyles.miniLabel);
            }
            if (GUILayout.Button("Round to Int"))
            {
                for (int i = 0; i < collider.Vertices.Count; i++)
                {
                    collider.Vertices[i] = math.round(collider.Vertices[i]);
                }
            }
        }
        private void CreateChildrenColliders(ColliderAuthoring root,
                                             List <UnityCollider> unityColliders,
                                             NativeList <Collider> colliders,
                                             NativeList <RigidTransform> transforms,
                                             NativeList <int2> ranges)
        {
            int2 newRange = new int2(colliders.Length, 0);

            foreach (var unityCollider in unityColliders)
            {
                DeclareDependency(root, unityCollider);
                DeclareDependency(root, unityCollider.transform);
                if ((unityCollider is UnityEngine.SphereCollider || unityCollider is UnityEngine.CapsuleCollider || unityCollider is UnityEngine.BoxCollider) == false)
                {
                    throw new InvalidOperationException(
                              $"GameObject Conversion Error: Failed to convert {unityCollider}. Compound Colliders may only be composed of Sphere, Capsule, and Box Colliders. Other collider types may be supported in a future version.");
                }
                if (!unityCollider.transform.IsChildOf(root.transform))
                {
                    throw new InvalidOperationException($"GameObject Conversion Error: Failed to convert {root}. Compound Colliders may only reference children colliders.");
                }

                Entity entity = TryGetPrimaryEntity(unityCollider);
                if (entity == Entity.Null)
                {
                    //The child GameObject must be a child of the StopConversion. Skip
                    continue;
                }

                //Calculate transform

                float3 scale = (float3)unityCollider.transform.lossyScale / root.transform.lossyScale;
                if ((unityCollider is UnityEngine.SphereCollider || unityCollider is UnityEngine.CapsuleCollider) &&
                    math.cmax(scale) - math.cmin(scale) > 1.0E-5f)
                {
                    throw new InvalidOperationException(
                              $"GameObject Conversion Error: Failed to convert {unityCollider}. Only uniform scale is permitted on Sphere and Capsule colliders.");
                }

                float4x4 localToRoot = root.transform.localToWorldMatrix.inverse * unityCollider.transform.localToWorldMatrix;
                Unity.Transforms.LocalToWorld ltr = new Unity.Transforms.LocalToWorld {
                    Value = localToRoot
                };
                var rotation = quaternion.LookRotationSafe(ltr.Forward, ltr.Up);
                var position = ltr.Position;
                transforms.Add(new RigidTransform(rotation, position));

                //Calculate collider
                if (unityCollider is UnityEngine.SphereCollider unitySphere)
                {
                    colliders.Add(new SphereCollider
                    {
                        center = unitySphere.center,
                        radius = unitySphere.radius * scale.x
                    });
                }
                else if (unityCollider is UnityEngine.CapsuleCollider unityCapsule)
                {
                    float3 dir;
                    if (unityCapsule.direction == 0)
                    {
                        dir = new float3(1f, 0f, 0f);
                    }
                    else if (unityCapsule.direction == 1)
                    {
                        dir = new float3(0f, 1f, 0f);
                    }
                    else
                    {
                        dir = new float3(0f, 0f, 1f);
                    }
                    colliders.Add(new CapsuleCollider
                    {
                        pointB = (float3)unityCapsule.center + ((unityCapsule.height / 2f - unityCapsule.radius) * unityCapsule.transform.lossyScale.x * dir),
                        pointA = (float3)unityCapsule.center - ((unityCapsule.height / 2f - unityCapsule.radius) * unityCapsule.transform.lossyScale.x * dir),
                        radius = unityCapsule.radius * scale.x
                    });
                }
                else if (unityCollider is UnityEngine.BoxCollider unityBox)
                {
                    colliders.Add(new BoxCollider
                    {
                        center   = unityBox.center,
                        halfSize = unityBox.size * scale / 2f
                    });
                }
                newRange.y++;
            }
            ranges.Add(newRange);
        }