コード例 #1
0
        public void EnableEditorInput(VRTK_ControllerEvents.ButtonAlias button)
        {
            _vio.isGrabbable = true;
            var grab = _gameObject.GetComponent <VRTK_InteractControllerAppearance>();

            if (grab == null)
            {
                grab = _gameObject.AddComponent <VRTK_InteractControllerAppearance>();
            }

            grab.hideControllerOnGrab = true;

            _vio.secondaryGrabActionScript = _gameObject.GetComponent <VRTK_SwapControllerGrabAction>();

            if (_vio.secondaryGrabActionScript == null)
            {
                _vio.secondaryGrabActionScript = _gameObject.AddComponent <VRTK_SwapControllerGrabAction>();
            }

            VRTK_FixedJointGrabAttach vrtkFixedJointGrab = _gameObject.GetComponent <VRTK_FixedJointGrabAttach>();

            if (vrtkFixedJointGrab == null)
            {
                vrtkFixedJointGrab            = _gameObject.AddComponent <VRTK_FixedJointGrabAttach>();
                vrtkFixedJointGrab.breakForce = Single.PositiveInfinity;
            }

            vrtkFixedJointGrab.precisionGrab = true;

            _vio.grabOverrideButton           = button;
            _vio.InteractableObjectGrabbed   += EditorGrabbed;
            _vio.InteractableObjectUngrabbed += EditorUngrabbed;
            _vio.InteractableObjectTouched   += EditorToched;
            _vio.InteractableObjectTouched   += EditorUnToched;
        }
コード例 #2
0
        public void AddGrabEndAction()
        {
            _vio.isGrabbable = true;
            var grab = _gameObject.GetComponent <VRTK_InteractControllerAppearance>();

            if (grab == null)
            {
                grab = _gameObject.AddComponent <VRTK_InteractControllerAppearance>();
            }

            grab.hideControllerOnGrab = true;

            _vio.secondaryGrabActionScript = _gameObject.GetComponent <VRTK_SwapControllerGrabAction>();

            if (_vio.secondaryGrabActionScript == null)
            {
                _vio.secondaryGrabActionScript = _gameObject.AddComponent <VRTK_SwapControllerGrabAction>();
            }

            VRTK_FixedJointGrabAttach vrtkFixedJointGrab = _gameObject.GetComponent <VRTK_FixedJointGrabAttach>();

            if (vrtkFixedJointGrab == null)
            {
                vrtkFixedJointGrab            = _gameObject.AddComponent <VRTK_FixedJointGrabAttach>();
                vrtkFixedJointGrab.breakForce = Single.PositiveInfinity;
            }

            if (_grabPoint != null)
            {
                vrtkFixedJointGrab.precisionGrab   = false;
                vrtkFixedJointGrab.leftSnapHandle  = _grabPoint.GetLeftGrabPoint();
                vrtkFixedJointGrab.rightSnapHandle = _grabPoint.GetRightGrabPoint();
            }
            else
            {
                vrtkFixedJointGrab.precisionGrab = true;
            }

            _vio.grabOverrideButton           = VRTK_ControllerEvents.ButtonAlias.GripPress;
            _vio.InteractableObjectUngrabbed += OnGrabEndVoid;
        }
コード例 #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;
    }