private void drawWireSphereNow(WireSphere wireSphere,
                                       ref int curPass)
        {
            var position = wireSphere.pose.position;
            var rotation = wireSphere.pose.rotation;

            var worldPosition = _currMatrix.MultiplyPoint3x4(position);

            var dirToCamera         = (Camera.current.transform.position - worldPosition).normalized;
            var dirToCameraInMatrix = _currMatrix.inverse.MultiplyVector(dirToCamera);

            // Wire sphere outline. This is just a wire sphere that faces the camera.
            drawWireArcNow(position, dirToCameraInMatrix, dirToCameraInMatrix.Perpendicular(),
                           wireSphere.radius, 1.0f,
                           numCircleSegments: wireSphere.numSegments);


            var x = rotation * Vector3.right;
            var y = rotation * Vector3.up;
            var z = rotation * Vector3.forward;

            drawPlaneSoftenedWireArcNow(position, y, x, wireSphere.radius,
                                        inFrontOfPlaneColor: _currColor,
                                        behindPlaneColor: _currColor.WithAlpha(_currColor.a * 0.1f),
                                        planeNormal: dirToCameraInMatrix,
                                        curPass: ref curPass,
                                        fractionOfCircleToDraw: 1.0f,
                                        numCircleSegments: wireSphere.numSegments);
            drawPlaneSoftenedWireArcNow(position, z, y, wireSphere.radius,
                                        inFrontOfPlaneColor: _currColor,
                                        behindPlaneColor: _currColor.WithAlpha(_currColor.a * 0.1f),
                                        planeNormal: dirToCameraInMatrix,
                                        curPass: ref curPass,
                                        fractionOfCircleToDraw: 1.0f,
                                        numCircleSegments: wireSphere.numSegments);
            drawPlaneSoftenedWireArcNow(position, x, z, wireSphere.radius,
                                        inFrontOfPlaneColor: _currColor,
                                        behindPlaneColor: _currColor.WithAlpha(_currColor.a * 0.1f),
                                        planeNormal: dirToCameraInMatrix,
                                        curPass: ref curPass,
                                        fractionOfCircleToDraw: 1.0f,
                                        numCircleSegments: wireSphere.numSegments);
        }
        public void DrawAllGizmosToScreen()
        {
            try {
                int matrixIndex     = 0;
                int colorIndex      = 0;
                int lineIndex       = 0;
                int wireSphereIndex = 0;
                int meshIndex       = 0;

                int currPass = -1;
                _currMatrix = Matrix4x4.identity;
                _currColor  = Color.white;

                GL.wireframe = false;

                for (int i = 0; i < _operations.Count; i++)
                {
                    OperationType type = _operations[i];
                    switch (type)
                    {
                    case OperationType.SetMatrix:
                        _currMatrix = _matrices[matrixIndex++];
                        break;

                    case OperationType.SetColor:
                        _currColor = _colors[colorIndex++];
                        currPass   = -1; //force pass to be set the next time we need to draw
                        break;

                    case OperationType.ToggleWireframe:
                        GL.wireframe = !GL.wireframe;
                        break;

                    case OperationType.DrawLine:
                        setPass(ref currPass, isUnlit: true);

                        GL.Begin(GL.LINES);
                        Line line = _lines[lineIndex++];
                        GL.Vertex(_currMatrix.MultiplyPoint(line.a));
                        GL.Vertex(_currMatrix.MultiplyPoint(line.b));
                        GL.End();
                        break;

                    case OperationType.DrawWireSphere:
                        setPass(ref currPass, isUnlit: true);

                        GL.Begin(GL.LINES);
                        WireSphere wireSphere = _wireSpheres[wireSphereIndex++];
                        drawWireSphereNow(wireSphere, ref currPass);
                        GL.End();
                        break;

                    case OperationType.DrawMesh:
                        if (GL.wireframe)
                        {
                            setPass(ref currPass, isUnlit: true);
                        }
                        else
                        {
                            setPass(ref currPass, isUnlit: false);
                        }

                        Graphics.DrawMeshNow(_meshes[meshIndex++],
                                             _currMatrix * _matrices[matrixIndex++]);
                        break;

                    default:
                        throw new InvalidOperationException("Unexpected operation type " + type);
                    }
                }
            } finally {
                GL.wireframe = false;
            }
        }