protected ShatterTool FindClosestPiece(ShatterTool reference, ShatterTool[] pieces, float maxDistance)
        {
            Vector3 center         = reference.Center;
            float   maxDistanceSqr = maxDistance * maxDistance;

            ShatterTool closestPiece       = null;
            float       closestDistanceSqr = 0.0f;

            for (int i = 0; i < pieces.Length; i++)
            {
                ShatterTool piece = pieces[i];

                if (piece != null)
                {
                    float distanceSqr = (center - piece.Center).sqrMagnitude;

                    if (distanceSqr < maxDistanceSqr && (distanceSqr < closestDistanceSqr || closestPiece == null))
                    {
                        closestPiece       = piece;
                        closestDistanceSqr = distanceSqr;
                    }
                }
            }

            return(closestPiece);
        }
 void Start()
 {
     shatterOnCollision         = gameObject.GetComponent <ShatterOnCollision>();
     shatterTool                = gameObject.GetComponent <ShatterTool>();
     rigidbody                  = gameObject.GetComponent <Rigidbody>();
     shatterOnCollision.enabled = false;
     shatterTool.enabled        = false;
     rigidbody.constraints      = RigidbodyConstraints.FreezeAll;
 }
예제 #3
0
    public override void OnInspectorGUI()
    {
        ShatterTool source = (ShatterTool)target;

        EditorGUILayout.BeginVertical();

        // Generation
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Generation", source.Generation.ToString());
        EditorGUILayout.EndHorizontal();

        // GenerationLimit
        EditorGUILayout.BeginHorizontal();
        source.GenerationLimit = EditorGUILayout.IntSlider(new GUIContent("Generation Limit", generationLimitTooltip), source.GenerationLimit, 1, 30);
        EditorGUILayout.EndHorizontal();

        // Cuts
        EditorGUILayout.BeginHorizontal();
        source.Cuts = EditorGUILayout.IntSlider(new GUIContent("Cuts", cutsTooltip), source.Cuts, 1, 25);
        EditorGUILayout.EndHorizontal();

        // FillCut
        EditorGUILayout.BeginHorizontal();
        source.FillCut = EditorGUILayout.Toggle(new GUIContent("Fill Cut", fillCutTooltip), source.FillCut);
        EditorGUILayout.EndHorizontal();

        // SendPreSplitMessage
        EditorGUILayout.BeginHorizontal();
        source.SendPreSplitMessage = EditorGUILayout.Toggle(new GUIContent("Pre Split msg", preSplitMsgTooltip), source.SendPreSplitMessage);
        EditorGUILayout.EndHorizontal();

        // SendPostSplitMessage
        EditorGUILayout.BeginHorizontal();
        source.SendPostSplitMessage = EditorGUILayout.Toggle(new GUIContent("Post Split msg", postSplitMsgTooltip), source.SendPostSplitMessage);
        EditorGUILayout.EndHorizontal();

        // InternalHullType
        EditorGUILayout.BeginHorizontal();
        source.InternalHullType = (HullType)EditorGUILayout.EnumPopup(new GUIContent("Internal Hull Type", internalHullTypeTooltip), source.InternalHullType);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.EndVertical();

        // Handle change
        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }
예제 #4
0
    public override void OnInspectorGUI()
    {
        ShatterTool source = (ShatterTool)target;

        EditorGUILayout.BeginVertical();

        // Generation
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Generation", source.Generation.ToString());
        EditorGUILayout.EndHorizontal();

        // GenerationLimit
        EditorGUILayout.BeginHorizontal();
        source.GenerationLimit = EditorGUILayout.IntSlider(new GUIContent("Generation Limit", generationLimitTooltip), source.GenerationLimit, 1, 30);
        EditorGUILayout.EndHorizontal();

        // Cuts
        EditorGUILayout.BeginHorizontal();
        source.Cuts = EditorGUILayout.IntSlider(new GUIContent("Cuts", cutsTooltip), source.Cuts, 1, 25);
        EditorGUILayout.EndHorizontal();

        // FillCut
        EditorGUILayout.BeginHorizontal();
        source.FillCut = EditorGUILayout.Toggle(new GUIContent("Fill Cut", fillCutTooltip), source.FillCut);
        EditorGUILayout.EndHorizontal();

        // SendPreSplitMessage
        EditorGUILayout.BeginHorizontal();
        source.SendPreSplitMessage = EditorGUILayout.Toggle(new GUIContent("Pre Split msg", preSplitMsgTooltip), source.SendPreSplitMessage);
        EditorGUILayout.EndHorizontal();

        // SendPostSplitMessage
        EditorGUILayout.BeginHorizontal();
        source.SendPostSplitMessage = EditorGUILayout.Toggle(new GUIContent("Post Split msg", postSplitMsgTooltip), source.SendPostSplitMessage);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.EndVertical();
    }
        public void PostSplit(GameObject[] newGameObjects)
        {
            // Get shatter tool scripts of the new game objects
            ShatterTool[] pieces = new ShatterTool[newGameObjects.Length];

            for (int i = 0; i < newGameObjects.Length; i++)
            {
                pieces[i] = newGameObjects[i].GetComponent <ShatterTool>();
            }

            // Attach one of the new pieces to the original parent
            if (parent != null)
            {
                ShatterTool parentShatterTool = parent.GetComponent <ShatterTool>();

                if (parentShatterTool != null)
                {
                    // Which piece should attach to the original parent?
                    ShatterTool closestPiece = null;

                    if (attachPieceToParent)
                    {
                        closestPiece = FindClosestPiece(parentShatterTool, pieces, maxPieceToParentDistance);

                        if (closestPiece != null)
                        {
                            closestPiece.transform.parent = parent;
                        }
                    }

                    // Add rigidbodies to the detached pieces
                    if (addRbToDetachedPieces)
                    {
                        foreach (ShatterTool piece in pieces)
                        {
                            if (piece != null && piece != closestPiece)
                            {
                                piece.gameObject.AddComponent <Rigidbody>();
                            }
                        }
                    }
                }
            }

            // Attach the original children to the new pieces
            foreach (Transform child in children)
            {
                ShatterTool childShatterTool = child.GetComponent <ShatterTool>();

                if (childShatterTool != null)
                {
                    // Which piece should this child attach to?
                    ShatterTool closestPiece = FindClosestPiece(childShatterTool, pieces, maxChildToPieceDistance);

                    if (attachChildrenToPieces && closestPiece != null)
                    {
                        child.parent = closestPiece.transform;
                    }
                    else
                    {
                        if (addRbToDetachedChildren)
                        {
                            child.gameObject.AddComponent <Rigidbody>();
                        }
                    }
                }
            }
        }
예제 #6
0
        private float collisionBoxTimer = 0.1f; //remove collision boxes after this delay

        public void Start()
        {
            shatterTool = GetComponent <ShatterTool>();
            renderer    = GetComponent <Renderer>();
        }
예제 #7
0
 public void Start()
 {
     shatterTool = GetComponent<ShatterTool>();
     renderer = GetComponent<Renderer>();
 }
예제 #8
0
    private void CreateNewGameObjects(IList <IHull> newHulls, out GameObject[] newGameObjects)
    {
        // Get new meshes
        Mesh[]  newMeshes  = new Mesh[newHulls.Count];
        float[] newVolumes = new float[newHulls.Count];

        float totalVolume = 0.0f;

        for (int i = 0; i < newHulls.Count; i++)
        {
            Mesh    mesh   = newHulls[i].GetMesh();
            Vector3 size   = mesh.bounds.size;
            float   volume = size.x * size.y * size.z;

            newMeshes[i]  = mesh;
            newVolumes[i] = volume;

            totalVolume += volume;
        }

        // Remove mesh references to speed up instantiation
        GetComponent <MeshFilter>().sharedMesh = null;

        MeshCollider meshCollider = GetComponent <MeshCollider>();

        if (meshCollider != null)
        {
            meshCollider.sharedMesh = null;
        }

        // Create new game objects
        newGameObjects = new GameObject[newHulls.Count];

        for (int i = 0; i < newHulls.Count; i++)
        {
            IHull newHull = newHulls[i];
            Mesh  newMesh = newMeshes[i];
            float volume  = newVolumes[i];

            GameObject newGameObject = (GameObject)Instantiate(gameObject);

            // Set shatter tool
            ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>();

            if (newShatterTool != null)
            {
                newShatterTool.hull = newHull;
            }

            // Set mesh filter
            MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>();

            if (newMeshFilter != null)
            {
                newMeshFilter.sharedMesh = newMesh;
            }

            // Set mesh collider
            MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>();

            if (newMeshCollider != null)
            {
                newMeshCollider.sharedMesh = newMesh;
            }

            // Set rigidbody
            Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>();

            if (newRigidbody != null)
            {
                newRigidbody.mass        = GetComponent <Rigidbody>().mass *(volume / totalVolume);
                newRigidbody.constraints = RigidbodyConstraints.None;

                if (!newRigidbody.isKinematic)
                {
                    newRigidbody.velocity = GetComponent <Rigidbody>().GetPointVelocity(newRigidbody.worldCenterOfMass);

                    newRigidbody.angularVelocity = GetComponent <Rigidbody>().angularVelocity;
                }
            }

            HingeJoint hinge = newGameObject.GetComponent <HingeJoint>();
            if (hinge != null)
            {
                Destroy(hinge);
            }

            // Update properties
            newShatterTool.CalculateCenter();

            newGameObjects[i] = newGameObject;
        }
    }
예제 #9
0
 public void PostSplit(GameObject[] newGameObjects)
 {
     // Get shatter tool scripts of the new game objects
     ShatterTool[] pieces = new ShatterTool[newGameObjects.Length];
     
     for (int i = 0; i < newGameObjects.Length; i++)
     {
         pieces[i] = newGameObjects[i].GetComponent<ShatterTool>();
     }
     
     // Attach one of the new pieces to the original parent
     if (parent != null)
     {
         ShatterTool parentShatterTool = parent.GetComponent<ShatterTool>();
         
         if (parentShatterTool != null)
         {
             // Which piece should attach to the original parent?
             ShatterTool closestPiece = null;
             
             if (attachPieceToParent)
             {
                 closestPiece = FindClosestPiece(parentShatterTool, pieces, maxPieceToParentDistance);
                 
                 if (closestPiece != null)
                 {
                     closestPiece.transform.parent = parent;
                 }
             }
             
             // Add rigidbodies to the detached pieces
             if (addRbToDetachedPieces)
             {
                 foreach (ShatterTool piece in pieces)
                 {
                     if (piece != null && piece != closestPiece)
                     {
                         piece.gameObject.AddComponent<Rigidbody>();
                     }
                 }
             }
         }
     }
     
     // Attach the original children to the new pieces
     foreach (Transform child in children)
     {
         ShatterTool childShatterTool = child.GetComponent<ShatterTool>();
         
         if (childShatterTool != null)
         {
             // Which piece should this child attach to?
             ShatterTool closestPiece = FindClosestPiece(childShatterTool, pieces, maxChildToPieceDistance);
             
             if (attachChildrenToPieces && closestPiece != null)
             {
                 child.parent = closestPiece.transform;
             }
             else
             {
                 if (addRbToDetachedChildren)
                 {
                     child.gameObject.AddComponent<Rigidbody>();
                 }
             }
         }
     }
 }
예제 #10
0
 protected ShatterTool FindClosestPiece(ShatterTool reference, ShatterTool[] pieces, float maxDistance)
 {
     Vector3 center = reference.Center;
     float maxDistanceSqr = maxDistance * maxDistance;
     
     ShatterTool closestPiece = null;
     float closestDistanceSqr = 0.0f;
     
     for (int i = 0; i < pieces.Length; i++)
     {
         ShatterTool piece = pieces[i];
         
         if (piece != null)
         {
             float distanceSqr = (center - piece.Center).sqrMagnitude;
             
             if (distanceSqr < maxDistanceSqr && (distanceSqr < closestDistanceSqr || closestPiece == null))
             {
                 closestPiece = piece;
                 closestDistanceSqr = distanceSqr;
             }
         }
     }
     
     return closestPiece;
 }
예제 #11
0
파일: ShatterTool.cs 프로젝트: mengtest/fs
    private void CreateNewGameObjects(IList <Hull> newHulls, out GameObject[] newGameObjects)
    {
        // Get new meshes
        Mesh[]  newMeshes  = new Mesh[newHulls.Count];
        float[] newVolumes = new float[newHulls.Count];

        float totalVolume = 0.0f;

        for (int i = 0; i < newHulls.Count; i++)
        {
            Mesh    mesh   = newHulls[i].GetMesh();
            Vector3 size   = mesh.bounds.size;
            float   volume = size.x * size.y * size.z;

            newMeshes[i]  = mesh;
            newVolumes[i] = volume;

            totalVolume += volume;
        }

        // Create new game objects
        newGameObjects = new GameObject[newHulls.Count];

        for (int i = 0; i < newHulls.Count; i++)
        {
            Hull newHull = newHulls[i];
            Mesh newMesh = newMeshes[i];
            //float volume = newVolumes[i];

            GameObject newGameObject = (GameObject)Instantiate(gameObject);
            newGameObject.transform.SetParent(gameObject.transform.parent);
            newGameObject.transform.localScale    = Vector3.one;
            newGameObject.transform.localPosition = gameObject.transform.localPosition;
            // Set shatter tool
            ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>();

            if (newShatterTool != null)
            {
                newShatterTool.hull = newHull;
            }

            // Set mesh filter
            MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>();

            if (newMeshFilter != null)
            {
                newMeshFilter.mesh = newMesh;
            }

            // Set mesh collider
            MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>();

            if (newMeshCollider != null)
            {
                newMeshCollider.sharedMesh = newMesh;
            }

            // Set rigidbody
            //Rigidbody newRigidbody = newGameObject.GetComponent<Rigidbody>();

            //if (newRigidbody != null)
            //{
            //newRigidbody.mass = GetComponent<Rigidbody>().mass * (volume / totalVolume);

            //if (!newRigidbody.isKinematic)
            //{
            //    newRigidbody.velocity = GetComponent<Rigidbody>().GetPointVelocity(newRigidbody.worldCenterOfMass);

            //    newRigidbody.angularVelocity = GetComponent<Rigidbody>().angularVelocity;
            //}
            //}

            //UI3DCutFruitDown cutFruitDown = newGameObject.GetComponent<UI3DCutFruitDown>();
            //if (cutFruitDown != null)
            //{
            //    cutFruitDown.enabled = true;
            //    cutFruitDown.Down(i);
            //}

            newGameObjects[i] = newGameObject;
        }
    }
예제 #12
0
    private void CreateNewGameObjects(IList <Hull> newHulls, out GameObject[] newGameObjects)
    {
        // Get new meshes
        Mesh[]  newMeshes  = new Mesh[newHulls.Count];
        float[] newVolumes = new float[newHulls.Count];

        float totalVolume = 0.0f;

        for (int i = 0; i < newHulls.Count; i++)
        {
            Mesh    mesh   = newHulls[i].GetMesh();
            Vector3 size   = mesh.bounds.size;
            float   volume = size.x * size.y * size.z;

            newMeshes[i]  = mesh;
            newVolumes[i] = volume;

            totalVolume += volume;
        }

        // Create new game objects
        newGameObjects = new GameObject[newHulls.Count];

        for (int i = 0; i < newHulls.Count; i++)
        {
            Hull  newHull = newHulls[i];
            Mesh  newMesh = newMeshes[i];
            float volume  = newVolumes[i];

            GameObject newGameObject = (GameObject)Instantiate(gameObject);

            // Set shatter tool
            ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>();

            if (newShatterTool != null)
            {
                newShatterTool.hull = newHull;
            }

            // Set mesh filter
            MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>();

            if (newMeshFilter != null)
            {
                newMeshFilter.mesh = newMesh;
            }

            // Set mesh collider
            MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>();

            if (newMeshCollider != null)
            {
                newMeshCollider.sharedMesh = newMesh;
            }

            // Set rigidbody
            Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>();

            if (newRigidbody != null)
            {
                newRigidbody.mass = GetComponent <Rigidbody>().mass *(volume / totalVolume);

                if (!newRigidbody.isKinematic)
                {
                    newRigidbody.velocity = GetComponent <Rigidbody>().GetPointVelocity(newRigidbody.worldCenterOfMass);

                    newRigidbody.angularVelocity = GetComponent <Rigidbody>().angularVelocity;
                }
            }

            newGameObjects[i] = newGameObject;
        }
    }