Ejemplo n.º 1
0
        /// <summary>
        ///     Handles scenveview rendering of portal previews and related editor utilities
        /// </summary>
        private void OnWillRenderObject() {
#if UNITY_EDITOR
            if (!(Selection.activeGameObject == gameObject)) return;

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

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

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

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

            var cam = PreviewCamera;


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

            var 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;

            var rend = GetComponent<MeshRenderer>();
            var rend2 = TargetController.GetComponent<MeshRenderer>();
            var 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.projectionMatrix, TextureTargetEye.Right,
                    PortalMaterial, new Vector2(Screen.currentResolution.width,
                        Screen.currentResolution.height), previewRenderer, rend2, null, null, mesh, 1, 0, false, 0);
            }
            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;
            }

            try {
                lib.RenderCamera(renderData);
            }
            catch {
                //Doesn't really matter what happened here, unity editor strangeness sometimes hucks issues
                Graphics.SetRenderTarget(null);
                lib.TerminateRender();
                return;
            }


            var block = new MaterialPropertyBlock();
            previewRenderer.GetPropertyBlock(block);

            var output = (RenderTexture) block.GetTexture(TextureTargetEye.Right.Name());
            if (output)
                previewRenderer.sharedMaterial.SetTexture(TextureTargetEye.Right.Name(), output);
            if (output)
                previewRenderer.sharedMaterial.SetTexture(TextureTargetEye.Left.Name(), output);
            if (output)
                block.SetTexture(TextureTargetEye.Left.Name(), output);

            Graphics.SetRenderTarget(null);
            lib.TerminateRender();
#endif
        }
Ejemplo n.º 2
0
        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
        }