private float[] ProcessImage(byte[] imgData, Leap.Image.CameraType type)
    {
        byte[] undistortedImg        = UndistortImage(imgData, type);
        byte[] croppedUndistortedImg = new byte[WIDTH_WITH_OFFSET * HEIGHT_WITH_OFFSET];
        LeapToolTracking.CropImage(
            undistortedImg,
            croppedUndistortedImg,
            TEX_WIDTH, TEX_HEIGHT,
            COL_OFFSET, //COL_OFFSET and ROW_OFFSET have to be swapped here
            ROW_OFFSET,
            WIDTH_WITH_OFFSET,
            HEIGHT_WITH_OFFSET);

        float[] markerLocations = new float[] { 1000000, 1000000, 1000000, 1000000 };
        LeapToolTracking.GetMarkerLocations(croppedUndistortedImg, markerLocations, WIDTH_WITH_OFFSET, HEIGHT_WITH_OFFSET, (int)type);

        return(markerLocations);
    }
    private void Start()
    {
        LeapToolTracking.Init(debug);
        previousLevel    = new Vector3[2];
        previousLevel[0] = new Vector3(0, 5, 0);
        previousLevel[1] = previousLevel[0];

        previousTrend    = new Vector3[2];
        previousTrend[0] = Vector3.zero;
        previousTrend[1] = Vector3.zero;

        marker0.SetActive(debug);
        marker1.SetActive(debug);

        if (debug)
        {
            marker0.GetComponent <MeshRenderer>().material.color = Color.green;
            marker1.GetComponent <MeshRenderer>().material.color = Color.blue;
        }
    }
    private void OnPreRender()
    {
        if (_currentImage == null || stopTracking)
        {
            return;
        }

        // Separate left and right image
        int imageSize = _currentImage.Width * _currentImage.Height;

        byte[] raw         = _currentImage.Data(Leap.Image.CameraType.LEFT);
        byte[] leftImgData = new byte[imageSize], rightImgData = new byte[imageSize];
        LeapToolTracking.GetLeapImages(raw, leftImgData, rightImgData, _currentImage.Width, _currentImage.Height);

        // Get location of the markers in both pictures
        float[] leftMarkerLocations  = ProcessImage(leftImgData, Leap.Image.CameraType.LEFT);
        float[] rightMarkerLocations = ProcessImage(rightImgData, Leap.Image.CameraType.RIGHT);

        // Sometimes Markers are not equally detected in both pictures. If this happens the left marker locations are swapped.
        if ((leftMarkerLocations[1] > leftMarkerLocations[3] && rightMarkerLocations[1] < rightMarkerLocations[3]) ||
            (leftMarkerLocations[1] < leftMarkerLocations[3] && rightMarkerLocations[1] > rightMarkerLocations[3]))
        {
            float tempX = leftMarkerLocations[0];
            float tempY = leftMarkerLocations[1];
            leftMarkerLocations[0] = leftMarkerLocations[2];
            leftMarkerLocations[1] = leftMarkerLocations[3];
            leftMarkerLocations[2] = tempX;
            leftMarkerLocations[3] = tempY;
        }
        //Debug.Log(System.DateTime.Now + ": RawMarkerL: (" + leftMarkerLocations[0] + "; " + leftMarkerLocations[1] + "); (" + leftMarkerLocations[2] + "; " + leftMarkerLocations[3] + ")");
        //Debug.Log(System.DateTime.Now + ": RawMarkerR: (" + rightMarkerLocations[0] + "; " + rightMarkerLocations[1] + "); (" + rightMarkerLocations[2] + "; " + rightMarkerLocations[3] + ")");

        if (ArrayIs(1000000, leftMarkerLocations) && ArrayIs(1000000, leftMarkerLocations))
        {
            toolActive = false;
            tool.SetActive(toolActive);
            return;
        }

        if (!toolActive)
        {
            toolActive = true;
            tool.SetActive(toolActive);
        }

        // Calculate marker position in 3D Space
        Vector3 marker0Pos = GetMarkerPosition(leftMarkerLocations[0], rightMarkerLocations[0], leftMarkerLocations[1], 0);
        Vector3 marker1Pos = GetMarkerPosition(leftMarkerLocations[2], rightMarkerLocations[2], leftMarkerLocations[3], 1);

        if (swapMarkers)
        {
            Vector3 temp = marker0Pos;
            marker0Pos = marker1Pos;
            marker1Pos = temp;
        }

        marker0.transform.localPosition = marker0Pos;
        marker1.transform.localPosition = marker1Pos;
        tool.transform.localPosition    = marker0Pos;
        tool.transform.LookAt(marker1.transform.position);
    }