public bool StartWallEdgeAdjust(Transform controller, GameObject wall, Vector3 grabpoint)
    {
        if (TweakAction != TweakActionType.none)
        {
            return(false);
        }
        ChaperonePlaneProperties wallprops = wall.GetComponent <ChaperonePlaneProperties>();

        TweakAction            = TweakActionType.wEdgePosition;
        TweakControllerInitPos = controller.position;
        TweakControllerTrans   = controller;
        if (Vector3.Distance(wall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f)), grabpoint) < Vector3.Distance(wall.transform.TransformPoint(new Vector3(0.5f, 0f, 0f)), grabpoint))
        {
            TweakLeftWall  = wallprops.LeftWall.gameObject;
            TweakRightWall = wall;
        }
        else
        {
            TweakLeftWall  = wall;
            TweakRightWall = wallprops.RightWall.gameObject;
        }
        TweakLeftPivotPoint  = TweakLeftWall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f));
        TweakRightPivotPoint = TweakRightWall.transform.TransformPoint(new Vector3(0.5f, 0f, 0f));
        TweakTargetInitPoint = TweakLeftWall.transform.TransformPoint(new Vector3(0.5f, 0f, 0f));
        return(true);
    }
    public bool DeleteWallSegment(GameObject wall, Vector3 targetpoint)
    {
        if (TweakAction != TweakActionType.none)
        {
            return(false);
        }
        ChaperonePlaneProperties wallprops = wall.GetComponent <ChaperonePlaneProperties>();

        TweakAction = TweakActionType.wRemove;
        if (WallCount > 3)
        {
            WallCount--;
            if (Vector3.Distance(wall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f)), targetpoint) > Vector3.Distance(wall.transform.TransformPoint(new Vector3(0.5f, 0f, 0f)), targetpoint))
            {
                wallprops = wallprops.RightWall;
            }
            if (FirstWall = wallprops)
            {
                FirstWall = wallprops.LeftWall;
            }
            wallprops.LeftWall.RightWall = wallprops.RightWall;
            wallprops.RightWall.LeftWall = wallprops.LeftWall;
            Vector3 leftcorner  = wallprops.LeftWall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f));
            Vector3 rightcorner = wallprops.RightWall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f));
            wallprops.LeftWall.transform.position   = Vector3.Lerp(leftcorner, rightcorner, 0.5f);
            wallprops.LeftWall.transform.localScale = new Vector3(Vector3.Distance(leftcorner, rightcorner), wallprops.LeftWall.transform.localScale.y, 1);
            wallprops.LeftWall.transform.rotation   = Quaternion.LookRotation(leftcorner - rightcorner, Vector3.up) * Quaternion.Euler(0, 90, 0);
            Destroy(wallprops.gameObject);
        }
        return(true);
    }
    public bool SplitWallSegment(Transform controller, GameObject wall, Vector3 targetpoint)
    {
        if (TweakAction != TweakActionType.none)
        {
            return(false);
        }
        ChaperonePlaneProperties wallprops = wall.GetComponent <ChaperonePlaneProperties>();

        TweakAction = TweakActionType.wAdd;
        WallCount++;
        Vector3 leftcorner  = wall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f));
        Vector3 rightcorner = targetpoint;

        rightcorner.y             = leftcorner.y;
        wall.transform.position   = Vector3.Lerp(leftcorner, rightcorner, 0.5f);
        wall.transform.localScale = new Vector3(Vector3.Distance(leftcorner, rightcorner), wall.transform.localScale.y, 1);
        wall.transform.rotation   = Quaternion.LookRotation(leftcorner - rightcorner, Vector3.up) * Quaternion.Euler(0, 90, 0);
        ChaperonePlaneProperties newwall = CreatePlane("newwall", true, WallMaterial);

        newwall.transform.parent = transform;
        leftcorner  = rightcorner;
        rightcorner = wallprops.RightWall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f));
        newwall.transform.position   = Vector3.Lerp(leftcorner, rightcorner, 0.5f);
        newwall.transform.localScale = new Vector3(Vector3.Distance(leftcorner, rightcorner), wall.transform.localScale.y, 1);
        newwall.transform.rotation   = Quaternion.LookRotation(leftcorner - rightcorner, Vector3.up) * Quaternion.Euler(0, 90, 0);
        newwall.RightWall            = wallprops.RightWall;
        newwall.LeftWall             = wallprops;
        newwall.LeftWall.RightWall   = newwall;
        newwall.RightWall.LeftWall   = newwall;
        TweakAction = TweakActionType.none;
        StartWallEdgeAdjust(controller, newwall.gameObject, targetpoint);
        return(true);
    }
    public void MatchPlayspace()
    {
        if (PlaySpace == null)
        {
            return;
        }

        if (FirstWall != null)
        {
            ChaperonePlaneProperties wall = FirstWall;
            for (int i = 0; i < WallCount; i++)
            {
                ChaperonePlaneProperties prevwall = wall.RightWall;
                Destroy(wall.gameObject);
                wall = prevwall;
            }
            FirstWall = null;
            WallCount = 0;
        }

        Vector3 v0 = PlaySpace.transform.localRotation * new Vector3(PlaySpace.transform.localScale.x / 2, 0, PlaySpace.transform.localScale.z / 2) + PlaySpace.transform.localPosition;
        Vector3 v1 = PlaySpace.transform.localRotation * new Vector3(-PlaySpace.transform.localScale.x / 2, 0, PlaySpace.transform.localScale.z / 2) + PlaySpace.transform.localPosition;
        Vector3 v2 = PlaySpace.transform.localRotation * new Vector3(-PlaySpace.transform.localScale.x / 2, 0, -PlaySpace.transform.localScale.z / 2) + PlaySpace.transform.localPosition;
        Vector3 v3 = PlaySpace.transform.localRotation * new Vector3(PlaySpace.transform.localScale.x / 2, 0, -PlaySpace.transform.localScale.z / 2) + PlaySpace.transform.localPosition;

        FirstWall = MatchQuickCreate(v0, v1);
        MatchQuickCreate(v1, v2);
        MatchQuickCreate(v2, v3);
        MatchQuickCreate(v3, v0, FirstWall);
        WallCount = 4;
    }
    public void SaveChaperone()
    {
        TweakAction     = TweakActionType.save;
        ChaperoneSaving = true;
        ChaperoneCalibrationState state = OpenVR.Chaperone.GetCalibrationState();

        if (state != ChaperoneCalibrationState.OK)
        {
            Debug.Log("GetCalibrationState() = " + state.ToString());
            return;
        }


        SteamVR_Utils.RigidTransform rt = new SteamVR_Utils.RigidTransform();
        rt.pos = PlaySpace.transform.position;
        rt.rot = PlaySpace.transform.rotation;

        HmdMatrix34_t mat = rt.ToHmdMatrix34();

        OpenVR.ChaperoneSetup.SetWorkingStandingZeroPoseToRawTrackingPose(ref mat);

        OpenVR.ChaperoneSetup.SetWorkingPlayAreaSize(PlaySpace.transform.localScale.x, PlaySpace.transform.localScale.z);

        HmdQuad_t[] pQuadsBuffer      = new HmdQuad_t[WallCount];
        ChaperonePlaneProperties wall = FirstWall;

        for (int index = 0; index < WallCount; index++)
        {
            Vector3 wallcorner = Vector3.Scale(PlaySpace.transform.InverseTransformPoint(wall.transform.TransformPoint(new Vector3(0.5f, 0f, 0f))), PlaySpace.transform.localScale);
            pQuadsBuffer[index].vCorners0.v0 = wallcorner.x;
            pQuadsBuffer[index].vCorners0.v1 = wallcorner.y;
            pQuadsBuffer[index].vCorners0.v2 = -wallcorner.z;

            wallcorner = Vector3.Scale(PlaySpace.transform.InverseTransformPoint(wall.transform.TransformPoint(new Vector3(0.5f, 1f, 0f))), PlaySpace.transform.localScale);
            pQuadsBuffer[index].vCorners1.v0 = wallcorner.x;
            pQuadsBuffer[index].vCorners1.v1 = wallcorner.y;
            pQuadsBuffer[index].vCorners1.v2 = -wallcorner.z;

            wallcorner = Vector3.Scale(PlaySpace.transform.InverseTransformPoint(wall.transform.TransformPoint(new Vector3(-0.5f, 1f, 0f))), PlaySpace.transform.localScale);
            pQuadsBuffer[index].vCorners2.v0 = wallcorner.x;
            pQuadsBuffer[index].vCorners2.v1 = wallcorner.y;
            pQuadsBuffer[index].vCorners2.v2 = -wallcorner.z;

            wallcorner = Vector3.Scale(PlaySpace.transform.InverseTransformPoint(wall.transform.TransformPoint(new Vector3(-0.5f, 0f, 0f))), PlaySpace.transform.localScale);
            pQuadsBuffer[index].vCorners3.v0 = wallcorner.x;
            pQuadsBuffer[index].vCorners3.v1 = wallcorner.y;
            pQuadsBuffer[index].vCorners3.v2 = -wallcorner.z;

            wall = wall.LeftWall;
        }
        OpenVR.ChaperoneSetup.SetWorkingCollisionBoundsInfo(pQuadsBuffer);
        OpenVR.ChaperoneSetup.CommitWorkingCopy(EChaperoneConfigFile.Live);//
        ReloadChaperone();
        ChaperoneSaving = false;
    }
    public void SetMaterials(Material wallmat, Material playspacemat)
    {
        WallMaterial      = wallmat;
        PlaySpaceMaterial = playspacemat;
        ChaperonePlaneProperties wall = FirstWall;

        for (int i = 0; i < WallCount; i++)
        {
            wall.gameObject.GetComponent <MeshRenderer>().material = WallMaterial;
            wall = wall.RightWall;
        }
        PlaySpace.gameObject.GetComponent <MeshRenderer>().material = PlaySpaceMaterial;
    }
    public ChaperonePlaneProperties CreatePlane(string planename, bool iswall, Material planeMat)
    {
        GameObject go = new GameObject(planename);
        ChaperonePlaneProperties props = go.AddComponent <ChaperonePlaneProperties>();
        MeshFilter   mf = go.AddComponent(typeof(MeshFilter)) as MeshFilter;
        MeshRenderer mr = go.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
        MeshCollider mc = go.AddComponent(typeof(MeshCollider)) as MeshCollider;
        Mesh         m  = new Mesh();

        if (!iswall)
        {
            go.layer   = 9;
            m.vertices = new Vector3[] { new Vector3(-0.5f, 0, -0.5f),
                                         new Vector3(-0.5f, 0, 0.5f),
                                         new Vector3(0.5f, 0, 0.5f),
                                         new Vector3(0.5f, 0, -0.5f) };

            m.triangles = new int[] { 0, 1, 2, 0, 2, 3, 0, 2, 1, 0, 3, 2 };
            m.uv        = new Vector2[]
            {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(1, 1),
                new Vector2(1, 0)
            };
        }
        else
        {
            go.layer   = 8;
            m.vertices = new Vector3[] { new Vector3(0.5f, 0, 0f),
                                         new Vector3(0.5f, 1, 0f),
                                         new Vector3(-0.5f, 1, 0f),
                                         new Vector3(-0.5f, 0, 0f) };

            m.triangles = new int[] { 0, 2, 1, 0, 3, 2, 0, 1, 2, 0, 2, 3 };
            m.uv        = new Vector2[]
            {
                new Vector2(1, 0),
                new Vector2(1, 1),
                new Vector2(0, 1),
                new Vector2(0, 0)
            };
        }
        mf.mesh     = m;
        mr.material = planeMat;
        m.RecalculateNormals();
        m.RecalculateBounds();
        mc.sharedMesh = m;
        return(props);
    }
    public ChaperonePlaneProperties MatchQuickCreate(Vector3 v0, Vector3 v1)
    {
        ChaperonePlaneProperties wall = CreatePlane("ChapWall", true, WallMaterial);

        wall.transform.parent        = transform;
        wall.transform.localScale    = new Vector3(Vector3.Distance(v0, v1), 2.5f, 1);
        wall.transform.localRotation = Quaternion.LookRotation(v1 - v0, Vector3.up) * Quaternion.Euler(0, 90, 0);
        wall.transform.localPosition = Vector3.Lerp(v0, v1, 0.5f);          //+ new Vector3(0, 2.5f/2, 0);

        if (MatchLastWall != null)
        {
            MatchLastWall.LeftWall = wall;
            wall.RightWall         = MatchLastWall;
        }
        MatchLastWall = wall;

        return(wall);
    }
    public void ReloadChaperone()
    {
        TweakAction = TweakActionType.reload;
        Vector3       v0;
        Vector3       v1;
        Vector3       v3;
        float         x   = 0;
        float         y   = 0;
        HmdMatrix34_t mat = new HmdMatrix34_t();

        HmdQuad_t[] quads;
        ChaperonePlaneProperties wall, prevwall;

        ChaperoneLoading = true;

        //make sure we can get all the necessary information
        ChaperoneCalibrationState state = OpenVR.Chaperone.GetCalibrationState();

        if (state != ChaperoneCalibrationState.OK)
        {
            Debug.Log("GetCalibrationState() = " + state.ToString());
            return;
        }
        OpenVR.ChaperoneSetup.RevertWorkingCopy();
        if (!OpenVR.ChaperoneSetup.GetWorkingStandingZeroPoseToRawTrackingPose(ref mat))
        {
            Debug.Log("GetWorkingStandingZeroPoseToRawTrackingPose failed");
            return;
        }
        if (!OpenVR.ChaperoneSetup.GetWorkingPlayAreaSize(ref x, ref y))
        {
            Debug.Log("GetWorkingPlayAreaSize() failed");
            return;
        }
        if (!OpenVR.ChaperoneSetup.GetWorkingCollisionBoundsInfo(out quads))
        {
            Debug.Log("GetWorkingCollisionBoundsInfo() failed");
            return;
        }

        // Destroy walls/playspace if they already exist
        if (PlaySpace != null)
        {
            Destroy(PlaySpace.gameObject);
            PlaySpace = null;
        }
        if (FirstWall != null)
        {
            wall = FirstWall;
            for (int i = 0; i < WallCount; i++)
            {
                prevwall = wall.RightWall;
                Destroy(wall.gameObject);
                wall = prevwall;
            }
            FirstWall = null;
            WallCount = 0;
        }

        //this object will become the origin point
        SteamVR_Utils.RigidTransform rt = new SteamVR_Utils.RigidTransform(mat);
        transform.position   = rt.pos; //+ new Vector3(0, 1, 0);
        transform.rotation   = rt.rot;
        transform.localScale = Vector3.one;

        //create the rectangular playspace object
        PlaySpace = CreatePlane("ChapPlaySpace", false, PlaySpaceMaterial);
        PlaySpace.transform.parent = transform;

        //set the playspace transform
        PlaySpace.transform.localScale    = new Vector3(x, 1, y);
        PlaySpace.transform.localPosition = Vector3.zero;
        PlaySpace.transform.localRotation = Quaternion.identity;

        //Create the wall objects
        WallCount = quads.Length;
        prevwall  = null;
        foreach (HmdQuad_t quad in quads)
        {
            wall = CreatePlane("ChapWall", true, WallMaterial);
            wall.transform.parent = transform;

            // convert the necessary corners to a usable format
            v0 = new Vector3(quad.vCorners0.v0, quad.vCorners0.v1, -quad.vCorners0.v2);
            v1 = new Vector3(quad.vCorners1.v0, quad.vCorners1.v1, -quad.vCorners1.v2);
            v3 = new Vector3(quad.vCorners3.v0, quad.vCorners3.v1, -quad.vCorners3.v2);

            // set the walls transform
            wall.transform.localPosition = Vector3.Lerp(v0, v3, 0.5f);
            wall.transform.localScale    = new Vector3(Vector3.Distance(v0, v3), Vector3.Distance(v0, v1), 1);
            wall.transform.localRotation = Quaternion.LookRotation(v3 - v0, Vector3.up) * Quaternion.Euler(0, 90, 0);

            //each wall has a properties script with a link to the left and right wall. Also save a reference to the 1st wall
            if (prevwall != null)
            {
                prevwall.LeftWall = wall;
            }
            else
            {
                FirstWall = wall;
            }
            wall.RightWall = prevwall;
            prevwall       = wall;

            //Link first wall with last wall
            FirstWall.RightWall = wall;
            wall.LeftWall       = FirstWall;
        }
        ChaperoneLoading = false;
        TweakAction      = TweakActionType.none;
    }