Exemplo n.º 1
0
        protected override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            HashSet <Camera> subCameras = new HashSet <Camera>();

            foreach (Camera camera in cameras)
            {
                CameraStack stack = camera.GetComponent <CameraStack>();
                if (stack != null)
                {
                    subCameras.Add(stack.SecondaryCamera);
                    subCameras.Add(stack.TertiaryCamera);
                }
            }

            foreach (Camera camera in cameras)
            {
                if (subCameras.Contains(camera))
                {
                    continue;
                }

                PostProcessingSettings postProcessingSettings = this.postProcessingSettings;
#if UNITY_EDITOR
                if (camera.cameraType > CameraType.SceneView)
                {
                    postProcessingSettings = null;
                }
                if (camera.cameraType == CameraType.SceneView && !SceneView.currentDrawingSceneView.sceneViewState.showImageEffects)
                {
                    postProcessingSettings = null;
                }
#endif
                if (camera.cameraType == CameraType.Game && Camera.main != camera)
                {
                    postProcessingSettings = null;
                }

                CommandBuffer buffer = new CommandBuffer()
                {
                    name = camera.name
                };

                // buffer.GetTemporaryRT(cameraColorTextureId, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear, RenderTextureFormat.Default);
                buffer.GetTemporaryRT(cameraColorTexture1Id, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear, RenderTextureFormat.Default);
                buffer.GetTemporaryRT(cameraColorTexture2Id, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear, RenderTextureFormat.Default);
                buffer.GetTemporaryRT(cameraDepthTextureId, camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth);
                buffer.GetTemporaryRT(cameraDepthTexture1Id, camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth);
                buffer.GetTemporaryRT(cameraDepthTexture2Id, camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth);
                buffer.GetTemporaryRT(cameraDepthTexture3Id, camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth);
                context.ExecuteCommandBuffer(buffer);
                buffer.Clear();

                ClearFlag clearFlag = GetClearFlag(camera.clearFlags);
                if (clearFlag != ClearFlag.None)
                {
                    buffer.SetRenderTarget(
                        cameraColorTexture1Id, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                        cameraDepthTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store
                        );
                    buffer.ClearRenderTarget(
                        (clearFlag & ClearFlag.Depth) != 0,
                        (clearFlag & ClearFlag.Color) != 0,
                        (clearFlag & ClearFlag.Color) != 0 ? camera.backgroundColor : Color.clear,
                        1.0f
                        );
                }
                context.ExecuteCommandBuffer(buffer);
                buffer.Clear();

                CameraStack stack = camera.GetComponent <CameraStack>();
                if (stack != null)
                {
                    CameraRenderer.Render(context, stack.TertiaryCamera, ClearFlag.Depth, cameraColorTexture1Id, cameraDepthTexture3Id);
                    CameraRenderer.Render(context, stack.SecondaryCamera, ClearFlag.Depth, cameraColorTexture1Id, cameraDepthTexture2Id);
                }
                CameraRenderer.Render(context, camera, ClearFlag.Depth, cameraColorTexture1Id, stack != null ? cameraDepthTexture1Id : cameraDepthTextureId);

                context.SetupCameraProperties(camera);
                if (stack != null)
                {
                    PostProcessing.SetMultipleZBufferParams(buffer, camera, stack.SecondaryCamera, stack.TertiaryCamera);
                    buffer.EnableShaderKeyword(multipleDepthTexturesId);
                }
                else
                {
                    buffer.DisableShaderKeyword(multipleDepthTexturesId);
                }
                // PostProcessing.Render(buffer, cameraColorTextureId, cameraDepthTextureId, BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.CameraTarget, postProcessingSettings);
                PostProcessing.RenderOceanPass(buffer, cameraColorTexture1Id, cameraDepthTextureId, cameraColorTexture2Id, BuiltinRenderTextureType.CameraTarget, postProcessingSettings);
                PostProcessing.RenderAtmospherePass(buffer, cameraColorTexture2Id, cameraDepthTextureId, cameraColorTexture1Id, BuiltinRenderTextureType.CameraTarget, postProcessingSettings);
                PostProcessing.Blit(buffer, cameraColorTexture1Id, cameraDepthTextureId, BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.CameraTarget);
                context.ExecuteCommandBuffer(buffer);
                buffer.Clear();
                context.Submit();

                // buffer.ReleaseTemporaryRT(cameraColorTextureId);
                buffer.ReleaseTemporaryRT(cameraColorTexture1Id);
                buffer.ReleaseTemporaryRT(cameraColorTexture2Id);
                buffer.ReleaseTemporaryRT(cameraDepthTextureId);
                buffer.ReleaseTemporaryRT(cameraDepthTexture1Id);
                buffer.ReleaseTemporaryRT(cameraDepthTexture2Id);
                buffer.ReleaseTemporaryRT(cameraDepthTexture3Id);
                context.ExecuteCommandBuffer(buffer);
                buffer.Release();
            }
        }