public static void ShowWindow()
        {
            killWindow = false;
            if (window == null)
            {
                pos = new Vector2(0, 0);
                if (!dim.HasValue)
                {
                    dim = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
                }

                window          = ScriptableObject.CreateInstance <PreviewWindow>();
                window.maxSize  = new Vector2(6000, 4000);
                window.position = new Rect(pos, dim.Value);
                window.ShowAsDropDown(new Rect(), dim.Value);
            }
            else
            {
                killWindow = true;
                window.Close();
            }
        }
        void OnGUI()
        {
            if (Event.current != null &&
                Event.current.isKey)
            {
                if (Event.current.keyCode == KeyCode.LeftArrow)
                {
                    if (window != null)
                    {
                        pos.x          -= fastMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.RightArrow)
                {
                    if (window != null)
                    {
                        pos.x          += fastMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.UpArrow)
                {
                    if (window != null)
                    {
                        pos.y          -= fastMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.DownArrow)
                {
                    if (window != null)
                    {
                        pos.y          += fastMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.A)
                {
                    if (window != null)
                    {
                        pos.x          -= slowMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.D)
                {
                    if (window != null)
                    {
                        pos.x          += slowMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.W)
                {
                    if (window != null)
                    {
                        pos.y          -= slowMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode == KeyCode.S)
                {
                    if (window != null)
                    {
                        pos.y          += slowMove;
                        window.position = new Rect(pos, dim.Value);
                    }
                }
                else if (Event.current.keyCode != KeyCode.None)
                {
                    killWindow = true;
                    this.Close();
                    window = null;
                }
            }

            if (Event.current != null && Event.current.type == EventType.Repaint)
            {
                float frameWidth  = position.width;
                float frameHeight = position.height;

                if (frameWidth <= frameHeight * aspect)
                {
                    frameHeight = frameWidth / aspect;
                }
                else
                {
                    frameWidth = frameHeight * aspect;
                }

                float left   = position.width - frameWidth;
                float height = position.height - frameHeight;

                if (SpectatorView.ShaderManager.Instance != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat != null &&
                    SpectatorView.ShaderManager.Instance.colorTexture != null)
                {
                    Graphics.DrawTexture(
                        new Rect(left / 2.0f, height / 2.0f, frameWidth, frameHeight),
                        SpectatorView.ShaderManager.Instance.colorTexture,
                        SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat);
                }
            }
        }
        void OnGUI()
        {
            EditorGUILayout.BeginVertical("Box");
            {
                RenderTitle("Recording");

                if (!IsRecording())
                {
                    EditorGUILayout.BeginVertical("Box");
                    {
                        if (GUILayout.Button("Start Recording"))
                        {
                            StartRecording();
                        }
                    }
                    EditorGUILayout.EndVertical();
                }
                else
                {
                    EditorGUILayout.BeginVertical("Box");
                    {
                        if (GUILayout.Button("Stop Recording"))
                        {
                            StopRecording();
                        }
                    }
                    EditorGUILayout.EndVertical();
                }
            }

            if (GUILayout.Button("Take Picture"))
            {
                TakePicture();
            }

            if (CaptureHiResHolograms() && GUILayout.Button("Take Canon Picture"))
            {
                TakeCanonPicture();
            }

            EditorGUILayout.Space();

            // Open Folder
            if (GUILayout.Button("Open Folder"))
            {
                Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "HologramCapture"));
            }

            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");
            {
                RenderTitle("Hologram Settings");

                float oldAlpha = GetAlpha();
                float newAlpha = EditorGUILayout.Slider("Alpha", oldAlpha, 0, 1);
                if (newAlpha != oldAlpha)
                {
                    SetAlpha(newAlpha);
                }

                if (SpectatorView.SpectatorViewManager.Instance != null &&
                    SpectatorView.ShaderManager.Instance != null &&
                    SpectatorView.ShaderManager.Instance.colorTexture != null &&
                    SpectatorView.ShaderManager.Instance.renderTexture != null &&
                    SpectatorView.ShaderManager.Instance.holoTexture != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendVideoMat != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendOutputMat != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat != null)
                {
                    SpectatorView.SpectatorViewManager.Instance.DefaultAlpha = newAlpha;

                    SpectatorView.ShaderManager.Instance.alphaBlendVideoMat.SetFloat("_Alpha", newAlpha);
                    SpectatorView.ShaderManager.Instance.alphaBlendOutputMat.SetFloat("_Alpha", newAlpha);
                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat.SetFloat("_Alpha", newAlpha);
                }

                EditorGUILayout.Space();

                float previousFrameOffset = GetFrameOffset();
                float newFrameOffset      = EditorGUILayout.Slider("Frame Offset", previousFrameOffset, -1 * maxFrameOffset, maxFrameOffset);

                if (SpectatorView.SpectatorViewManager.Instance)
                {
                    SpectatorView.SpectatorViewManager.Instance.DefaultFrameOffset = newFrameOffset;
                }

                if (newFrameOffset != previousFrameOffset)
                {
                    SetFrameOffset(newFrameOffset);
                }

                EditorGUILayout.Space();

                brightness = EditorGUILayout.Slider("Brightness", brightness, 0, maxBrightness);

                if (SpectatorView.SpectatorViewManager.Instance != null &&
                    SpectatorView.ShaderManager.Instance != null &&
                    SpectatorView.ShaderManager.Instance.colorTexture != null &&
                    SpectatorView.ShaderManager.Instance.renderTexture != null &&
                    SpectatorView.ShaderManager.Instance.holoTexture != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendVideoMat != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendOutputMat != null &&
                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat != null)
                {
                    SpectatorView.SpectatorViewManager.Instance.DefaultAlpha = newAlpha;

                    SpectatorView.ShaderManager.Instance.alphaBlendVideoMat.SetFloat("_Brightness", brightness);
                    SpectatorView.ShaderManager.Instance.alphaBlendOutputMat.SetFloat("_Brightness", brightness);
                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat.SetFloat("_Brightness", brightness);
                }
            }
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");
            {
                RenderTitle("Composite");

                if (GUILayout.Button("Toggle View"))
                {
                    showAllTextures = !showAllTextures;
                }

                EditorGUILayout.BeginHorizontal("Box");
                {
                    if (GUILayout.Button("Toggle Full Screen"))
                    {
                        PreviewWindow.ShowWindow();
                    }

                    GUILayout.Label("Width: ");
                    string w = GUILayout.TextField(fullScreenWidth.ToString());
                    fullScreenWidth = Convert.ToInt32(w);

                    GUILayout.Label("Height: ");
                    string h = GUILayout.TextField(fullScreenHeight.ToString());
                    fullScreenHeight = Convert.ToInt32(h);

                    PreviewWindow.SetDimensions(Convert.ToInt32(w), Convert.ToInt32(h));
                }
                EditorGUILayout.EndHorizontal();

                if (HoloToolkit.Unity.SpatialMapping.SpatialMappingManager.Instance != null &&
                    PlayerController.Instance != null)
                {
                    // Request Spatial Mapping
                    if (GUILayout.Button("Spatial Mapping"))
                    {
                        // Clear any old spatial mapping meshes.
                        if (HoloToolkit.Unity.SpatialMapping.SpatialMappingManager.Instance)
                        {
                            SpectatorView.RemoteSpatialMappingSource rsms =
                                HoloToolkit.Unity.SpatialMapping.SpatialMappingManager.Instance.GetComponent <SpectatorView.RemoteSpatialMappingSource>();
                            if (rsms != null)
                            {
                                rsms.ClearMeshes();
                            }
                            else
                            {
                                rsms = HoloToolkit.Unity.SpatialMapping.SpatialMappingManager.Instance.gameObject.AddComponent <SpectatorView.RemoteSpatialMappingSource>();
                                if (rsms != null)
                                {
                                    rsms.ClearMeshes();
                                }
                            }
                        }

                        // Set a flag on the server to send a new mesh.
                        PlayerController.Instance.RequestSpatialMapping();
                    }
                }

                // Rendering
                if (showVideo)
                {
                    Rect  spacing = GUILayoutUtility.GetRect(1, 2);
                    float left    = 0;
                    UpdateFrameDimensions(spacing.y, ref left);

                    Rect framesRect = GUILayoutUtility.GetRect(frameWidth * 2 + padding, frameHeight * 2 + padding);
                    framesRect.x += left;

                    if (Event.current != null && Event.current.type == EventType.Repaint)
                    {
                        if (SpectatorView.SpectatorViewManager.Instance != null &&
                            SpectatorView.ShaderManager.Instance != null)
                        {
                            if (
                                SpectatorView.ShaderManager.Instance.colorTexture != null &&
                                SpectatorView.ShaderManager.Instance.renderTexture != null &&
                                SpectatorView.ShaderManager.Instance.holoTexture != null &&
                                SpectatorView.ShaderManager.Instance.alphaBlendVideoMat != null &&
                                SpectatorView.ShaderManager.Instance.alphaBlendOutputMat != null &&
                                SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat != null
                                )
                            {
                                if (!SpectatorView.ShaderManager.Instance.setAlphaBlendPreviewHoloTex)
                                {
                                    SpectatorView.ShaderManager.Instance.setAlphaBlendPreviewHoloTex = true;
                                    if (QueueingHoloFrames())
                                    {
                                        SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.holoTexture);
                                        SpectatorView.ShaderManager.Instance.alphaBlendVideoMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.holoTexture);
                                        SpectatorView.ShaderManager.Instance.alphaBlendOutputMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.holoTexture);
                                    }
                                    else
                                    {
                                        SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.renderTexture);
                                        SpectatorView.ShaderManager.Instance.alphaBlendVideoMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.renderTexture);
                                        SpectatorView.ShaderManager.Instance.alphaBlendOutputMat.SetTexture("_FrontTex", SpectatorView.ShaderManager.Instance.renderTexture);
                                    }
                                }

                                Graphics.DrawTexture(
                                    new Rect(framesRect.x, framesRect.y, frameWidth, frameHeight),
                                    SpectatorView.ShaderManager.Instance.colorTexture,
                                    SpectatorView.ShaderManager.Instance.alphaBlendPreviewMat);
                            }

                            if (showAllTextures)
                            {
                                if (QueueingHoloFrames())
                                {
                                    if (SpectatorView.ShaderManager.Instance.holoTexture != null)
                                    {
                                        Graphics.DrawTexture
                                        (
                                            new Rect(framesRect.x, framesRect.y + frameHeight + padding, frameWidth, frameHeight),
                                            SpectatorView.ShaderManager.Instance.holoTexture
                                        );
                                    }
                                }
                                else
                                {
                                    if (SpectatorView.ShaderManager.Instance.renderTexture != null)
                                    {
                                        Graphics.DrawTexture
                                        (
                                            new Rect(framesRect.x, framesRect.y + frameHeight + padding, frameWidth, frameHeight),
                                            SpectatorView.ShaderManager.Instance.renderTexture
                                        );
                                    }
                                }

                                if (SpectatorView.ShaderManager.Instance.flipHorizontallyMat != null &&
                                    SpectatorView.ShaderManager.Instance.colorTexture != null)
                                {
                                    Graphics.DrawTexture
                                    (
                                        new Rect(framesRect.x + frameWidth + padding, framesRect.y + 0, frameWidth, frameHeight),
                                        SpectatorView.ShaderManager.Instance.colorTexture,
                                        SpectatorView.ShaderManager.Instance.flipHorizontallyMat
                                    );
                                }

                                if (QueueingHoloFrames())
                                {
                                    if (SpectatorView.ShaderManager.Instance.holoTexture != null && SpectatorView.ShaderManager.Instance.alphaMat != null)
                                    {
                                        Graphics.DrawTexture
                                        (
                                            new Rect(framesRect.x + frameWidth + padding, framesRect.y + frameHeight + padding, frameWidth, frameHeight),
                                            SpectatorView.ShaderManager.Instance.holoTexture,
                                            SpectatorView.ShaderManager.Instance.alphaMat
                                        );
                                    }
                                }
                                else
                                {
                                    if (SpectatorView.ShaderManager.Instance.renderTexture != null && SpectatorView.ShaderManager.Instance.alphaMat != null)
                                    {
                                        Graphics.DrawTexture
                                        (
                                            new Rect(framesRect.x + frameWidth + padding, framesRect.y + frameHeight + padding, frameWidth, frameHeight),
                                            SpectatorView.ShaderManager.Instance.renderTexture,
                                            SpectatorView.ShaderManager.Instance.alphaMat
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
            EditorGUILayout.EndVertical();
        }