/// <summary>
        /// Renders the view of a given Renderer as if it were through another renderer
        /// </summary>
        /// <param name="camera">The origin camera</param>
        /// <param name="material">The Material to modify</param>
        /// <param name="sourceRenderer">The Source renderer</param>
        /// <param name="targetRenderer">The Target renderer</param>
        /// <param name="mesh">The Mesh of the source Renderer</param>
        /// <param name="obliquePlane">Will the projection matrix be clipped at the near plane?</param>
        /// <param name="is3d">Is the renderer not being treated as two-dimenstional?</param>
        /// <param name="isMirror">Is the renderer rendering through itself?</param>
        public void RenderIntoMaterial(Camera camera, Material material, MeshRenderer sourceRenderer, MeshRenderer targetRenderer, Mesh mesh, bool obliquePlane = true, bool is3d = false, bool isMirror = false, bool isSSR = false)
        {
            _renderData = RenderData;
            if (!Initialized)
            {
                return;
            }
#if !SKS_Portals
            //if (camera.transform.parent == transform.parent)
            //    return;
#endif

            bool   firstRender     = false;
            Camera renderingCamera = RecursionCams[CurrentDepth];
            Camera currentCamera   = camera;

            //Render Placeholder if max depth hit
            if (CurrentDepth > SKSGlobalRenderSettings.RecursionNumber)
            {
                return;
            }

            RenderTexture renderTarget = camera.targetTexture;
            CameraMarker  marker       = CameraMarker.GetMarker(camera);

            if (marker)
            {
                // marker.CurrentRenderer = sourceRenderer;
                if (marker.Owner == OtherCamera)
                {
                    return;
                }
            }

            Graphics.SetRenderTarget(renderTarget);

            //Sets up the Render Properties for this render
            RenderProperties renderProps = new RenderProperties();

            //Is this the first time that the IsMirror is being rendered this frame?
            if (camera == Camera.main)
            {
                firstRender = true;
            }
            GUIStyle style = new GUIStyle();
            style.normal.textColor = Color.red;
            renderProps           |= firstRender ? RenderProperties.FirstRender : 0;
            renderProps           |= RenderProperties.Optimize;
            renderProps           |= (CurrentDepth < 1) ? (obliquePlane ? RenderProperties.ObliquePlane : 0) : RenderProperties.ObliquePlane;
            renderProps           |= isMirror ? RenderProperties.Mirror : 0;
            renderProps           |= isSSR ? RenderProperties.IsSSR : 0;
            renderProps           |= SKSGlobalRenderSettings.CustomSkybox ? RenderProperties.RipCustomSkybox : 0;
            renderProps           |= SKSGlobalRenderSettings.AggressiveRecursionOptimization
                ? RenderProperties.AggressiveOptimization
                : 0;
            if (firstRender)
            {
                renderProps |= SKSGlobalRenderSettings.Inverted ? RenderProperties.InvertedCached : 0;
                renderProps |= SKSGlobalRenderSettings.UvFlip ? RenderProperties.UvFlipCached : 0;
            }

#if SKS_VR
            renderProps |= RenderProperties.VR;
            renderProps |= SKSGlobalRenderSettings.SinglePassStereo ? RenderProperties.SinglePass : 0;
#endif

            _recursionNumber++;


            //Renders the IsMirror itself to the rendertexture
            transform.SetParent(RenderingCameraParent);

            CurrentDepth++;

            renderingCamera.renderingPath   = camera.renderingPath;
            renderingCamera.cullingMask     = camera.cullingMask;
            renderingCamera.stereoTargetEye = StereoTargetEyeMask.None;
            renderingCamera.enabled         = false;
            RenderingCameras.Add(camera);


            //Set up the RenderData for the current frame
            RenderData.Camera          = camera;
            RenderData.RenderingCamera = renderingCamera;
            RenderData.CurrentDepth    = CurrentDepth;

#if SKS_VR
            //Stereo Rendering
            if (camera.stereoTargetEye == StereoTargetEyeMask.Both)
            {
                RenderingCameraParent.rotation = DestinationTransform.rotation *
                                                 (Quaternion.Inverse(OriginTransform.rotation) *
                                                  (camera.transform.rotation));

                RenderData tempDataLeft  = RenderData.Clone();
                RenderData tempDataRight = RenderData.Clone();

                //Left eye


                tempDataLeft.Position         = -SKVREyeTracking.EyeOffset(camera);
                tempDataLeft.ProjectionMatrix = camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left);
                tempDataLeft.TextureName      = "_LeftEyeTexture";
                tempDataLeft.RenderProperties = renderProps;
                tempDataLeft.Eye = false;
                _cameraLib.RenderCamera(tempDataLeft);

                //Right eye

                tempDataRight.Position         = SKVREyeTracking.EyeOffset(camera);
                tempDataRight.ProjectionMatrix = camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right);
                tempDataRight.TextureName      = "_RightEyeTexture";
                tempDataRight.RenderProperties = renderProps;
                tempDataRight.Eye = true;
                if (!SKSGlobalRenderSettings.SinglePassStereo)
                {
                    _cameraLib.RenderCamera(tempDataRight);
                }
                else
                {
                    renderingCamera.stereoTargetEye = StereoTargetEyeMask.Right;
                    _cameraLib.RenderCamera(tempDataRight);
                }
            }

            else
            {
                //Non-VR rendering with VR enabled

                RenderData tempData = RenderData.Clone();
                renderProps &= ~RenderProperties.SinglePass;
                renderProps &= ~RenderProperties.VR;

                tempData.RenderProperties          = renderProps;
                renderingCamera.transform.rotation = DestinationTransform.rotation *
                                                     (Quaternion.Inverse(OriginTransform.rotation) *
                                                      (camera.transform.rotation));

                tempData.ProjectionMatrix = camera.projectionMatrix;
                tempData.Position         = renderingCamera.transform.position;

                if (renderingCamera.stereoTargetEye == StereoTargetEyeMask.Left)
                {
                    tempData.TextureName = "_LeftEyeTexture";
                    tempData.Eye         = false;
                    _cameraLib.RenderCamera(tempData);
                }
                else
                {
                    tempData.TextureName = "_RightEyeTexture";
                    tempData.Eye         = true;
                    _cameraLib.RenderCamera(tempData);
                }
            }
#else
            //Non-stereo rendering
            //RenderData.Position = camera.transform.position;
            RenderData tempData = RenderData.Clone();

            tempData.ProjectionMatrix = camera.projectionMatrix;
            tempData.TextureName      = "_RightEyeTexture";
            tempData.RenderProperties = renderProps;

            tempData.Eye = true;


            renderingCamera.transform.rotation = DestinationTransform.rotation * (Quaternion.Inverse(OriginTransform.rotation) * (camera.transform.rotation));
            CameraLib.RenderCamera(tempData);
#endif
            SKEffectCamera.CurrentDepth--;

            RenderingCameras.Remove(camera);
            if (RenderingCameras.Count == 0)
            {
                try {
                    _cameraLib.TerminateRender();
                    //SKSRenderLib.ClearUnwinder();
                }
                catch (NullReferenceException e) {
                    Debug.LogWarning("Attempted to render without proper setup");
                }
            }
        }
        private void OnWillRenderObject()
        {
#if UNITY_EDITOR
            if (!(Selection.activeGameObject == gameObject))
            {
                return;
            }

            if (!TargetController || !PortalMaterial ||
                !Mask || !SKSGlobalRenderSettings.Preview ||
                !this || Application.isPlaying)
            {
                //CleanupTemp();
                return;
            }

            MeshRenderer previewRenderer = PreviewRoot.GetComponent <MeshRenderer>();
            previewRenderer.sharedMaterial = PortalMaterial;
            //previewRenderer.enabled = true;

            SKSRenderLib lib = PreviewCamera.GetComponent <SKSRenderLib>();
            PreviewCamera.transform.localPosition = Vector3.zero;

            Camera sceneCam = SceneView.GetAllSceneCameras()[0];

            Camera cam = PreviewCamera;


            GL.Clear(true, true, Color.black);
            Graphics.SetRenderTarget(null);

            RenderProperties renderProps = new RenderProperties();

            //renderState |= RenderState.FirstRender;
            renderProps |= RenderProperties.Optimize;
            renderProps |= SKSGlobalRenderSettings.Inverted ? RenderProperties.InvertedCached : 0;
            renderProps |= !SKSGlobalRenderSettings.UvFlip ? RenderProperties.UvFlipCached : 0;
            renderProps |= RenderProperties.ObliquePlane;
            renderProps |= RenderProperties.FirstRender;
            renderProps |= RenderProperties.RipCustomSkybox;

            MeshRenderer rend  = GetComponent <MeshRenderer>();
            MeshRenderer rend2 = TargetController.GetComponent <MeshRenderer>();
            Mesh         mesh  = PreviewRoot.GetComponent <MeshFilter>().sharedMesh;
            //TargetController.PreviewRoot.GetComponent<MeshRenderer>().enabled = false;
            //TargetController.GetComponent<MeshRenderer>().enabled = false;
            cam.transform.localPosition = Vector3.zero;
            TargetController.PreviewRoot.transform.localPosition = Vector3.zero;

            cam.transform.rotation = TargetController.PreviewRoot.transform.rotation *
                                     (Quaternion.Inverse(transform.rotation) *
                                      (sceneCam.transform.rotation));

            TargetController.PreviewRoot.transform.localScale = Vector3.one;

            if (renderData == null)
            {
                renderData = new RenderData(renderProps, cam, sceneCam,
                                            sceneCam.transform.position, sceneCam.projectionMatrix, "_RightEyeTexture",
                                            PortalMaterial, new Vector2(Screen.currentResolution.width,
                                                                        Screen.currentResolution.height), previewRenderer, rend2, mesh, 1, 0, false, false);
            }
            else
            {
                renderData.Position         = sceneCam.transform.position;
                renderData.ProjectionMatrix = sceneCam.projectionMatrix;
                renderData.ScreenSize       = new Vector2(Screen.currentResolution.width,
                                                          Screen.currentResolution.height);
                renderData.RenderingCamera = PreviewCamera;
                renderData.SourceRenderer  = previewRenderer;
            }

            lib.RenderCamera(renderData);

            MaterialPropertyBlock block = new MaterialPropertyBlock();
            previewRenderer.GetPropertyBlock(block);
            RenderTexture output = (RenderTexture)block.GetTexture("_RightEyeTexture");
            //RenderTexture cachedOutput = RenderTexture.GetTemporary(output.width, output.height, output.depth, output.format);
            //cachedOutput.Create();
            //texturesToDispose.Add(cachedOutput);
            //Graphics.CopyTexture(output, cachedOutput);
            if (output)
            {
                previewRenderer.sharedMaterial.SetTexture("_RightEyeTexture", output);
            }
            if (output)
            {
                previewRenderer.sharedMaterial.SetTexture("_LeftEyeTexture", output);
            }
            if (output)
            {
                block.SetTexture("_LeftEyeTexture", output);
            }
            //PortalController.PortalMaterial.SetVector("_LeftDrawPos", PortalController.PortalMaterial.GetVector("_RightDrawPos"));

            Graphics.SetRenderTarget(null);
            lib.TerminateRender();
#endif
        }