public override void OnInspectorGUI()
    {
        PlaneUsageExample plane = (PlaneUsageExample)target;

        source = (GameObject)EditorGUILayout.ObjectField(source, typeof(GameObject), true);

        if (source == null)
        {
            EditorGUILayout.LabelField("Add a GameObject to Slice.");

            return;
        }

        if (!source.activeInHierarchy)
        {
            EditorGUILayout.LabelField("Object is Hidden. Cannot Slice.");

            return;
        }

        if (source.GetComponent <MeshFilter>() == null)
        {
            EditorGUILayout.LabelField("GameObject must have a MeshFilter.");

            return;
        }

        crossMat       = (Material)EditorGUILayout.ObjectField(crossMat, typeof(Material), true);
        recursiveSlice = (bool)EditorGUILayout.Toggle("Recursive Slice", recursiveSlice);

        if (GUILayout.Button("Cut Object"))
        {
            // only slice the parent object
            if (!recursiveSlice)
            {
                SlicedHull hull = plane.SliceObject(source);

                if (hull != null)
                {
                    hull.CreateLowerHull(source, crossMat);
                    hull.CreateUpperHull(source, crossMat);

                    source.SetActive(false);
                }
            }
            else
            {
                // in here we slice both the parent and all child objects
                SliceObjectRecursive(plane, source);

                source.SetActive(false);
            }
        }
    }
Esempio n. 2
0
    private void OnCollisionEnter(Collision collision)
    {
        // only cut with cloned scissor object that follows the original but isn't attached to the hand
        if (collision.gameObject.tag.Equals("Scissors") && collision.gameObject.name.Contains("Clone"))
        {
            Destroy(this.gameObject.GetComponent <ResetDroppedObject>());

            GameObject        cutPlane     = collision.gameObject.transform.Find(CUT_PLANE).gameObject;
            PlaneUsageExample planeExample = cutPlane.GetComponent <PlaneUsageExample>();
            Mesh objectMesh = this.gameObject.GetComponent <MeshFilter>().sharedMesh;

            Material originalMaterial = this.gameObject.GetComponent <SetCoralPieceOriginalMaterial>().originalMaterial;

            // only cut the object if it has enough triangles
            if (objectMesh.triangles.Length > numTriangles)
            {
                SlicedHull hull = planeExample.SliceObject(this.gameObject, null);
                if (hull != null)
                {
                    // only create the cut pieces if they have enough triangles
                    if (hull.upperHull.triangles.Length > numTriangles && hull.lowerHull.triangles.Length > numTriangles)
                    {
                        GameObject piece = hull.CreateUpperHull(this.gameObject, originalMaterial);

                        // remove 2nd and 3rd hover event from coral piece
                        this.gameObject.GetComponent <InteractableHoverEvents>().onAttachedToHand.RemoveAllListeners();
                        this.gameObject.GetComponent <InteractableHoverEvents>().onDetachedFromHand.RemoveAllListeners();

                        // create clone for the piece no longer attached to the hand
                        GameObject coralClone = Instantiate(this.gameObject);
                        coralClone.transform.position = this.gameObject.transform.position;

                        // set that clones mesh to the upper hull mesh
                        coralClone.GetComponent <MeshFilter>().sharedMesh  = piece.GetComponent <MeshFilter>().sharedMesh;
                        coralClone.GetComponent <MeshFilter>().mesh        = piece.GetComponent <MeshFilter>().mesh;
                        coralClone.GetComponent <MeshRenderer>().materials = piece.GetComponent <MeshRenderer>().materials;
                        // destroy object made by ezy slice
                        Destroy(piece);

                        Destroy(coralClone.GetComponent <BoxCollider>());
                        // add collider and rigid body to new cut piece not attached to hand
                        coralClone.AddComponent <BoxCollider>();
                        coralClone.GetComponent <Rigidbody>().isKinematic = false;
                        coralClone.GetComponent <Rigidbody>().useGravity  = true;

                        GameObject piece2 = hull.CreateLowerHull(this.gameObject, originalMaterial);

                        // set original game object mesh to the lowerhull
                        this.gameObject.GetComponent <MeshFilter>().sharedMesh  = piece2.GetComponent <MeshFilter>().sharedMesh;
                        this.gameObject.GetComponent <MeshFilter>().mesh        = piece2.GetComponent <MeshFilter>().mesh;
                        this.gameObject.GetComponent <MeshRenderer>().materials = piece2.GetComponent <MeshRenderer>().materials;
                        // destroy object made by ezy slice
                        Destroy(piece2);

                        // recreate box collider to fit new mesh
                        Destroy(this.gameObject.GetComponent <BoxCollider>());
                        this.gameObject.AddComponent <BoxCollider>();
                    }
                }
            }
            Destroy(collision.gameObject);
        }
    }
    /**
     * This function will recursively slice the provided object and all it's children.
     * Returns a list of SlicedHull objects which represents the cuts for the object
     * and all its children (if any)
     */
    public GameObject[] SliceObjectRecursive(PlaneUsageExample plane, GameObject obj)
    {
        // finally slice the requested object and return
        SlicedHull finalHull = plane.SliceObject(obj);

        if (finalHull != null)
        {
            GameObject lowerParent = finalHull.CreateLowerHull(obj, crossMat);
            GameObject upperParent = finalHull.CreateUpperHull(obj, crossMat);

            if (obj.transform.childCount > 0)
            {
                foreach (Transform child in obj.transform)
                {
                    if (child != null && child.gameObject != null)
                    {
                        // if the child has chilren, we need to recurse deeper
                        if (child.childCount > 0)
                        {
                            GameObject[] children = SliceObjectRecursive(plane, child.gameObject);

                            if (children != null)
                            {
                                // add the lower hull of the child if available
                                if (children[0] != null && lowerParent != null)
                                {
                                    children[0].transform.SetParent(lowerParent.transform, false);
                                }

                                // add the upper hull of this child if available
                                if (children[1] != null && upperParent != null)
                                {
                                    children[1].transform.SetParent(upperParent.transform, false);
                                }
                            }
                        }
                        else
                        {
                            // otherwise, just slice the child object
                            SlicedHull hull = plane.SliceObject(child.gameObject);

                            if (hull != null)
                            {
                                GameObject childLowerHull = hull.CreateLowerHull(child.gameObject, crossMat);
                                GameObject childUpperHull = hull.CreateUpperHull(child.gameObject, crossMat);

                                // add the lower hull of the child if available
                                if (childLowerHull != null && lowerParent != null)
                                {
                                    childLowerHull.transform.SetParent(lowerParent.transform, false);
                                }

                                // add the upper hull of the child if available
                                if (childUpperHull != null && upperParent != null)
                                {
                                    childUpperHull.transform.SetParent(upperParent.transform, false);
                                }
                            }
                        }
                    }
                }
            }

            return(new GameObject[] { lowerParent, upperParent });
        }

        return(null);
    }