Exemple #1
0
    public virtual IEnumerator Cut()
    {
        // Jump to main thread for running UNITY API calls
        yield return(Ninja.JumpToUnity);

        // set the blade relative to victim
        _blade = new Plane(gameObject.transform.InverseTransformDirection(-_normalDirection),
                           gameObject.transform.InverseTransformPoint(_anchorPoint));

        // get the victims mesh
        _victim_mesh = gameObject.GetComponent <MeshFilter>().mesh;

        Vector3[] victim_vertices = _victim_mesh.vertices;
        Vector3[] victim_normals  = _victim_mesh.normals;
        Vector2[] victim_uvs      = _victim_mesh.uv;
        Vector4[] victim_tangents = _victim_mesh.tangents;

        int subMeshCount = _victim_mesh.subMeshCount;

        // Jump back to background thread to do heavy processes.
        yield return(Ninja.JumpBack);

        // reset values
        _new_vertices.Clear();

        _leftSide  = new Mesh_Maker();
        _rightSide = new Mesh_Maker();

        bool[] sides = new bool[3];
        int[]  indices;
        int    p1, p2, p3;


        // go throught the submeshes
        for (int sub = 0; sub < subMeshCount; sub++)
        {
            yield return(Ninja.JumpToUnity);

            indices = _victim_mesh.GetTriangles(sub);
            yield return(Ninja.JumpBack);

            for (int i = 0; i < indices.Length; i += 3)
            {
                p1 = indices[i];
                p2 = indices[i + 1];
                p3 = indices[i + 2];

                sides[0] = _blade.GetSide(victim_vertices[p1]);
                sides[1] = _blade.GetSide(victim_vertices[p2]);
                sides[2] = _blade.GetSide(victim_vertices[p3]);

                // whole triangle
                if (sides[0] == sides[1] && sides[0] == sides[2])
                {
                    if (sides[0])
                    { // left side
                        _leftSide.AddTriangle(
                            new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                            new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                            new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                            new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                            sub);
                    }
                    else
                    {
                        _rightSide.AddTriangle(
                            new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                            new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                            new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                            new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                            sub);
                    }
                }
                else
                { // cut the triangle
                    Cut_this_Face(
                        new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                        new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                        new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                        new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                        sub);
                }
            }
        }

        /////////////////////  Jump to main thread for running UNITY API calls
        yield return(Ninja.JumpToUnity);

        // The capping Material will be at the end
        Material[] mats = gameObject.GetComponent <MeshRenderer>().sharedMaterials;
        if (mats[mats.Length - 1].name != capMaterial.name)
        {
            Material[] newMats = new Material[mats.Length + 1];
            mats.CopyTo(newMats, 0);
            newMats[mats.Length] = capMaterial;
            mats = newMats;
        }

        _capMatSub = mats.Length - 1; // for later use
        yield return(Ninja.JumpBack);

        // cap the opennings
        Capping();

        yield return(Ninja.JumpToUnity);

        // Left Mesh
        Mesh left_HalfMesh = _leftSide.GetMesh();

        left_HalfMesh.name = "Split Mesh Left";

        // Right Mesh
        Mesh right_HalfMesh = _rightSide.GetMesh();

        right_HalfMesh.name = "Split Mesh Right";

        // assign the game objects
        gameObject.name = "left_side";
        gameObject.GetComponent <MeshFilter>().mesh = left_HalfMesh;

        GameObject leftSideObj = gameObject;

        // Clone left side's components to right side.
        GameObject rightSideObj = GameObject.Instantiate(leftSideObj);

        rightSideObj.transform.position = gameObject.transform.position;
        rightSideObj.transform.rotation = gameObject.transform.rotation;
        rightSideObj.GetComponent <MeshFilter>().mesh = right_HalfMesh;
        rightSideObj.name = "right_side";

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

        rightSideObj.transform.localScale = gameObject.transform.localScale;

        // Check for fried object material assign
        if (GetComponent <FoodStatus>().GetIsFried())
        {
            mats[0] = GetComponent <CanBeFried>().friedMaterial;
        }
        //

        // assign mats
        leftSideObj.GetComponent <MeshRenderer>().materials  = mats;
        rightSideObj.GetComponent <MeshRenderer>().materials = mats;


        // Handle new colliders of left & right pieces
        HandleCollisions(leftSideObj);
        HandleCollisions(rightSideObj);

        CanBeChopped cbc = rightSideObj.GetComponent <CanBeChopped>();

        cbc.SetRootObject(_rootObject);

        // Update maximum chopping action counts
        this.maximumChopCount -= 1;
        cbc.maximumChopCount   = this.maximumChopCount;

        foreach (CanBeChopped child in this.transform.parent.GetComponentsInChildren <CanBeChopped>())
        {
            child.maximumChopCount = this.maximumChopCount;
        }

        // Finally, mark them as chopped pieces
        leftSideObj.GetComponent <FoodStatus>().SetIsChoppedPiece(true);

        SimulationController sc = GameObject.Find("Simulation Controller").GetComponent <SimulationController>();

        rightSideObj.GetComponent <FoodStatus>().OperationDone += sc.OnOperationDone;

        rightSideObj.GetComponent <FoodStatus>().SetIsChoppedPiece(true);


        ///////////////////

        // End thread
        yield return(Ninja.JumpBack);

        yield break;
    }
Exemple #2
0
    /// <summary>
    /// Cut the specified victim
    /// </summary>
    public static GameObject[] Cut(GameObject victim, Vector3 anchorPoint, Vector3 normalDirection, Material capMaterial)
    {
        // set the blade relative to victim
        _blade = new Plane(victim.transform.InverseTransformDirection(-normalDirection),
                           victim.transform.InverseTransformPoint(anchorPoint));

        // get the victims mesh
        _victim_mesh = victim.GetComponent <MeshFilter>().mesh;

        // reset values
        _new_vertices.Clear();

        _leftSide  = new Mesh_Maker();
        _rightSide = new Mesh_Maker();


        bool[] sides = new bool[3];
        int[]  indices;
        int    p1, p2, p3;

        // go throught the submeshes
        for (int sub = 0; sub < _victim_mesh.subMeshCount; sub++)
        {
            indices = _victim_mesh.GetTriangles(sub);

            for (int i = 0; i < indices.Length; i += 3)
            {
                p1 = indices[i];
                p2 = indices[i + 1];
                p3 = indices[i + 2];

                sides[0] = _blade.GetSide(_victim_mesh.vertices[p1]);
                sides[1] = _blade.GetSide(_victim_mesh.vertices[p2]);
                sides[2] = _blade.GetSide(_victim_mesh.vertices[p3]);


                // whole triangle
                if (sides[0] == sides[1] && sides[0] == sides[2])
                {
                    if (sides[0])                     // left side

                    {
                        _leftSide.AddTriangle(
                            new Vector3[] { _victim_mesh.vertices[p1], _victim_mesh.vertices[p2], _victim_mesh.vertices[p3] },
                            new Vector3[] { _victim_mesh.normals[p1], _victim_mesh.normals[p2], _victim_mesh.normals[p3] },
                            new Vector2[] { _victim_mesh.uv[p1], _victim_mesh.uv[p2], _victim_mesh.uv[p3] },
                            new Vector4[] { _victim_mesh.tangents[p1], _victim_mesh.tangents[p2], _victim_mesh.tangents[p3] },
                            sub);
                    }
                    else
                    {
                        _rightSide.AddTriangle(
                            new Vector3[] { _victim_mesh.vertices[p1], _victim_mesh.vertices[p2], _victim_mesh.vertices[p3] },
                            new Vector3[] { _victim_mesh.normals[p1], _victim_mesh.normals[p2], _victim_mesh.normals[p3] },
                            new Vector2[] { _victim_mesh.uv[p1], _victim_mesh.uv[p2], _victim_mesh.uv[p3] },
                            new Vector4[] { _victim_mesh.tangents[p1], _victim_mesh.tangents[p2], _victim_mesh.tangents[p3] },
                            sub);
                    }
                }
                else                   // cut the triangle

                {
                    Cut_this_Face(
                        new Vector3[] { _victim_mesh.vertices[p1], _victim_mesh.vertices[p2], _victim_mesh.vertices[p3] },
                        new Vector3[] { _victim_mesh.normals[p1], _victim_mesh.normals[p2], _victim_mesh.normals[p3] },
                        new Vector2[] { _victim_mesh.uv[p1], _victim_mesh.uv[p2], _victim_mesh.uv[p3] },
                        new Vector4[] { _victim_mesh.tangents[p1], _victim_mesh.tangents[p2], _victim_mesh.tangents[p3] },
                        sub);
                }
            }
        }

        // The capping Material will be at the end
        Material[] mats = victim.GetComponent <MeshRenderer>().sharedMaterials;
        if (mats[mats.Length - 1].name != capMaterial.name)
        {
            Material[] newMats = new Material[mats.Length + 1];
            mats.CopyTo(newMats, 0);
            newMats[mats.Length] = capMaterial;
            mats = newMats;
        }
        _capMatSub = mats.Length - 1;       // for later use

        // cap the opennings
        Capping();


        // Left Mesh
        Mesh left_HalfMesh = _leftSide.GetMesh();

        left_HalfMesh.name = "Split Mesh Left";

        // Right Mesh
        Mesh right_HalfMesh = _rightSide.GetMesh();

        right_HalfMesh.name = "Split Mesh Right";

        // assign the game objects

        victim.name = "left side";
        victim.GetComponent <MeshFilter>().mesh = left_HalfMesh;

        GameObject leftSideObj = victim;

        GameObject rightSideObj = new GameObject("right side", typeof(MeshFilter), typeof(MeshRenderer));

        rightSideObj.transform.position = victim.transform.position;
        rightSideObj.transform.rotation = victim.transform.rotation;
        rightSideObj.GetComponent <MeshFilter>().mesh = right_HalfMesh;


        // assign mats
        leftSideObj.GetComponent <MeshRenderer>().materials  = mats;
        rightSideObj.GetComponent <MeshRenderer>().materials = mats;

        return(new GameObject[] { leftSideObj, rightSideObj });
    }
Exemple #3
0
    public override IEnumerator Cut()
    {
        // Jump to main thread for running UNITY API calls
        yield return(Ninja.JumpToUnity);

        // set the blade relative to victim
        _blade = new Plane(gameObject.transform.InverseTransformDirection(-_normalDirection),
                           gameObject.transform.InverseTransformPoint(_anchorPoint));

        // get the victims mesh
        _victim_mesh = gameObject.GetComponent <MeshFilter>().mesh;

        Vector3[] victim_vertices = _victim_mesh.vertices;
        Vector3[] victim_normals  = _victim_mesh.normals;
        Vector2[] victim_uvs      = _victim_mesh.uv;
        Vector4[] victim_tangents = _victim_mesh.tangents;

        int subMeshCount = _victim_mesh.subMeshCount;

        // Jump back to background thread to do heavy processes.
        yield return(Ninja.JumpBack);

        // reset values
        _new_vertices.Clear();

        _leftSide  = new Mesh_Maker();
        _rightSide = new Mesh_Maker();

        bool[] sides = new bool[3];
        int[]  indices;
        int    p1, p2, p3;


        // go throught the submeshes
        for (int sub = 0; sub < subMeshCount; sub++)
        {
            yield return(Ninja.JumpToUnity);

            indices = _victim_mesh.GetTriangles(sub);
            yield return(Ninja.JumpBack);

            for (int i = 0; i < indices.Length; i += 3)
            {
                p1 = indices[i];
                p2 = indices[i + 1];
                p3 = indices[i + 2];

                sides[0] = _blade.GetSide(victim_vertices[p1]);
                sides[1] = _blade.GetSide(victim_vertices[p2]);
                sides[2] = _blade.GetSide(victim_vertices[p3]);

                // whole triangle
                if (sides[0] == sides[1] && sides[0] == sides[2])
                {
                    if (sides[0])
                    { // left side
                        _leftSide.AddTriangle(
                            new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                            new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                            new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                            new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                            sub);
                    }
                    else
                    {
                        _rightSide.AddTriangle(
                            new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                            new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                            new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                            new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                            sub);
                    }
                }
                else
                { // cut the triangle
                    Cut_this_Face(
                        new Vector3[] { victim_vertices[p1], victim_vertices[p2], victim_vertices[p3] },
                        new Vector3[] { victim_normals[p1], victim_normals[p2], victim_normals[p3] },
                        new Vector2[] { victim_uvs[p1], victim_uvs[p2], victim_uvs[p3] },
                        new Vector4[] { victim_tangents[p1], victim_tangents[p2], victim_tangents[p3] },
                        sub);
                }
            }
        }

        // Jump to main thread for running UNITY API calls
        yield return(Ninja.JumpToUnity);

        // The capping Material will be at the end
        Material[] mats = gameObject.GetComponent <MeshRenderer>().sharedMaterials;
        if (mats[mats.Length - 1].name != capMaterial.name)
        {
            Material[] newMats = new Material[mats.Length + 1];
            mats.CopyTo(newMats, 0);
            newMats[mats.Length] = capMaterial;
            mats = newMats;
        }
        _capMatSub = mats.Length - 1; // for later use

        // Left Mesh
        Mesh left_HalfMesh = _leftSide.GetMesh();

        left_HalfMesh.name = "Split Mesh Left";

        // Right Mesh
        Mesh right_HalfMesh = _rightSide.GetMesh();

        right_HalfMesh.name = "Split Mesh Right";

        // assign the game objects
        gameObject.name = "left side";
        gameObject.GetComponent <MeshFilter>().mesh = left_HalfMesh;

        GameObject leftSideObj = gameObject;

        GameObject rightSideObj = new GameObject("right side", typeof(MeshFilter), typeof(MeshRenderer));

        rightSideObj.transform.position = gameObject.transform.position;
        rightSideObj.transform.rotation = gameObject.transform.rotation;
        rightSideObj.GetComponent <MeshFilter>().mesh = right_HalfMesh;

        leftSideObj.layer  = 9;
        rightSideObj.layer = 9;

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

        rightSideObj.transform.localScale = gameObject.transform.localScale;

        // assign mats
        leftSideObj.GetComponent <MeshRenderer>().materials  = mats;
        rightSideObj.GetComponent <MeshRenderer>().materials = mats;

        // Handle new colliders of left & right pieces
        HandleCollisions(leftSideObj);
        HandleCollisions(rightSideObj);

        // Sound effects preparation
        AudioSource asrc = rightSideObj.AddComponent <AudioSource>();

        asrc.spatialBlend = 1f;
        asrc.volume       = 0.4f;
        asrc.playOnAwake  = false;
        ////
        // Other Stuff
        VRTK.VRTK_InteractableObject vrtk_io = rightSideObj.AddComponent <VRTK_InteractableObject>();
        //vrtk_io.holdButtonToGrab = false;
        VRTK_FixedJointGrabAttach vrtk_fjga = rightSideObj.AddComponent <VRTK_FixedJointGrabAttach>();

        vrtk_io.isGrabbable     = true;
        vrtk_fjga.precisionGrab = true;
        /////

        Renderer right_renderer = rightSideObj.GetComponent <Renderer>();
        Renderer left_renderer  = leftSideObj.GetComponent <Renderer>();
        float    right_bounds   = right_renderer.bounds.size.x * right_renderer.bounds.size.y * right_renderer.bounds.size.z;
        float    left_bounds    = left_renderer.bounds.size.x * left_renderer.bounds.size.y * left_renderer.bounds.size.z;

        GameObject leftController = GameObject.Find("LeftController");

        if (leftController.GetComponent <AutoGrab>() != null)
        {
            Destroy(leftController.GetComponent <AutoGrab>());
        }
        if (right_bounds > left_bounds)
        {
            Debug.Log("right side auto grab");

            objectFlesh.transform.parent   = rightSideObj.transform;
            objectFlesh.transform.position = rightSideObj.transform.position;
            objectFlesh.transform.rotation = rightSideObj.transform.rotation;

            parent = rightSideObj;

            if (right_bounds < smallerAllowedRigVolume)
            {
                AddCanBeChopped();
            }
            else
            {
                leftController.GetComponent <VRTK_InteractGrab>().ForceRelease();

                /*if (rightController.GetComponent<VRTK_InteractGrab>() != null)
                 * {
                 *  GameObject grabbedObject = rightController.GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
                 *  Debug.Log(grabbedObject);
                 *
                 *  if (grabbedObject != null && grabbedObject.GetComponent<VRTK_InteractableObject>() != null)
                 *  {
                 *      grabbedObject.GetComponent<VRTK_InteractableObject>().Ungrabbed();
                 *  }
                 * }*/
                AutoGrab autoGrabRight = leftController.AddComponent <AutoGrab>();
                autoGrabRight.interactTouch = autoGrabRight.GetComponent <VRTK_InteractTouch>();
                autoGrabRight.interactGrab  = autoGrabRight.GetComponent <VRTK_InteractGrab>();
                autoGrabRight.objectToGrab  = vrtk_io;

                // Create required components & set parameters on right piece, copy component values to new one.
                CanBePeeled cbc = rightSideObj.AddComponent <CanBePeeled>();
                cbc.capMaterial             = this.capMaterial;
                cbc.sliceTimeout            = this.sliceTimeout;
                cbc.colliderSkinWidth       = this.colliderSkinWidth;
                cbc.detachChildrenOnSlice   = this.detachChildrenOnSlice;
                cbc.newPieceMassMultiplier  = this.newPieceMassMultiplier;
                cbc.canBeChoppedWhileOnHand = this.canBeChoppedWhileOnHand;
                cbc._rootObject             = this._rootObject;
                cbc.choppingSoundBoard      = this.choppingSoundBoard;
                cbc.objectFlesh             = this.objectFlesh;
                cbc.initialRig = leftSideObj;
                ////
            }
            Destroy(leftSideObj.GetComponent <CanBePeeled>());
        }
        else
        {
            Debug.Log("left side auto grab");
            //objectFlesh.transform.localPosition = new Vector3(0,0,0);
            //objectFlesh.transform.SetParent(leftSideObj.transform);
            if (left_bounds < smallerAllowedRigVolume)
            {
                AddCanBeChopped();
            }
            else
            {
                leftController.GetComponent <VRTK_InteractGrab>().ForceRelease();

                /*if (rightController.GetComponent<VRTK_InteractGrab>() != null)
                 * {
                 *  GameObject grabbedObject = rightController.GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
                 *  Debug.Log(grabbedObject);
                 *  if (grabbedObject != null && grabbedObject.GetComponent<VRTK_InteractableObject>() != null)
                 *  {
                 *      grabbedObject.GetComponent<VRTK_InteractableObject>().Ungrabbed(rightController.GetComponent<VRTK_InteractGrab>());
                 *
                 *  }
                 * }*/
                AutoGrab autoGrabRight = leftController.AddComponent <AutoGrab>();
                autoGrabRight.interactTouch = autoGrabRight.GetComponent <VRTK_InteractTouch>();
                autoGrabRight.interactGrab  = autoGrabRight.GetComponent <VRTK_InteractGrab>();
                autoGrabRight.objectToGrab  = leftSideObj.GetComponent <VRTK_InteractableObject>();

                objectFlesh.transform.parent   = leftSideObj.transform;
                objectFlesh.transform.position = leftSideObj.transform.position;
                objectFlesh.transform.rotation = leftSideObj.transform.rotation;

                parent = leftSideObj;
            }

            Destroy(rightSideObj.GetComponent <CanBePeeled>());
        }

        // End thread
        yield return(Ninja.JumpBack);

        yield break;
    }