Пример #1
0
    public CustomPipeline(bool useDynamicBatching, bool useGPUInstancing, bool useSRPBatcher, ShadowSettings shadowSettings,
                          CustomPostProcessingStack defaultStack,
                          int shadowMapSize, float shadowDistance, float shadowFadeRange, int shadowCascades, Vector3 shadowCascadeSplit,
                          float renderScale, int msaaSamples, bool allowHDR)
    {
        GraphicsSettings.lightsUseLinearIntensity            = true;
        GraphicsSettings.useScriptableRenderPipelineBatching = useSRPBatcher;

        this.useDynamicBatching = useDynamicBatching;
        this.useGPUInstancing   = useGPUInstancing;
        this.shadowSettings     = shadowSettings;


        this.defaultStack       = defaultStack;
        this.shadowMapSize      = shadowMapSize;
        this.shadowDistance     = shadowDistance;
        this.shadowFadeRange    = shadowFadeRange;
        this.shadowCascades     = shadowCascades;
        this.shadowCascadeSplit = shadowCascadeSplit;

        this.renderScale             = renderScale;
        QualitySettings.antiAliasing = msaaSamples;
        this.msaaSamples             = Mathf.Max(QualitySettings.antiAliasing, 1);

        this.allowHDR = allowHDR;

        globalShadowData.y = 1f / shadowFadeRange;

        if (SystemInfo.usesReversedZBuffer)
        {
            worldToShadowCascadeMatrices[4].m33 = 1f;
        }

#if UNITY_EDITOR
        Lightmapping.SetDelegate(lightmappingLightsDelegate);
#endif
    }
Пример #2
0
    void Render(ScriptableRenderContext context, Camera camera)
    {
        ScriptableCullingParameters cullingParameters;

        if (!camera.TryGetCullingParameters(out cullingParameters))
        {
            return;
        }

        // shadow distance
        cullingParameters.shadowDistance = Mathf.Min(shadowDistance, camera.farClipPlane);

        cull = context.Cull(ref cullingParameters);
        if (cull.visibleLights.Length > 0)
        {
            ConfigureLights();
            if (mainLightExists)
            {
                RenderCascadedShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
                cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
            }
            if (shadowTileCount > 0)
            {
                RenderShadows(context);
            }
        }
        else
        {
            cameraBuffer.SetGlobalVector(lightIndicesOffsetAndCountID, Vector4.zero);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
        }

        // camera setup
        context.SetupCameraProperties(camera);

        // post-processing
        var customPipelineCamera = camera.GetComponent <CustomPipelineCamera>();
        CustomPostProcessingStack activeStack = customPipelineCamera ? customPipelineCamera.PostProcessingStack : defaultStack;
        bool scaledRendering = renderScale != 1f && camera.cameraType == CameraType.Game;
        int  renderWidth     = camera.pixelWidth;
        int  renderHeight    = camera.pixelHeight;

        if (scaledRendering)
        {
            renderWidth  = (int)(renderWidth * renderScale);
            renderHeight = (int)(renderHeight * renderScale);
        }
        int  renderSamples         = camera.allowMSAA ? msaaSamples : 1;
        bool renderToTexture       = scaledRendering || renderSamples > 1 || activeStack;
        bool needsDepth            = activeStack && activeStack.NeedsDepth;
        bool needsDirectDepth      = needsDepth && renderSamples == 1; // post && no msaa
        bool needsDepthOnlyPass    = needsDepth && renderSamples > 1;  // post && msaa
        RenderTextureFormat format = allowHDR && camera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;

        if (renderToTexture)
        {
            cameraBuffer.GetTemporaryRT(cameraColorTextureId, renderWidth, renderHeight, needsDirectDepth?0:24, FilterMode.Bilinear,
                                        format, RenderTextureReadWrite.Default, renderSamples);
            if (needsDepth)
            {
                cameraBuffer.GetTemporaryRT(cameraDepthTextureId, renderWidth, renderHeight, 24, FilterMode.Point,
                                            RenderTextureFormat.Depth, RenderTextureReadWrite.Linear, 1);
            }
            if (needsDirectDepth)
            {
                cameraBuffer.SetRenderTarget(
                    cameraColorTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                    cameraDepthTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
            else
            {
                cameraBuffer.SetRenderTarget(
                    cameraColorTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
        }

        // clear
        CameraClearFlags clearFlags = camera.clearFlags;

        cameraBuffer.ClearRenderTarget((clearFlags & CameraClearFlags.Depth) != 0, (clearFlags & CameraClearFlags.Color) != 0, camera.backgroundColor);

        // global constants
        cameraBuffer.BeginSample("Render Camera");
        cameraBuffer.SetGlobalVectorArray(visibleLightColorsId, visibleLightColors);
        cameraBuffer.SetGlobalVectorArray(visibleLightDirectionsOrPositionsId, visibleLightDirectionsOrPositions);
        cameraBuffer.SetGlobalVectorArray(visibleLightAttenuationsId, visibleLightAttenuations);
        cameraBuffer.SetGlobalVectorArray(visibleLightSpotDirectionsId, visibleLightSpotDirections);
        cameraBuffer.SetGlobalVectorArray(visibleLightOcclusionMasksId, visibleLightOcclusionMasks);
        globalShadowData.z = 1f - cullingParameters.shadowDistance * globalShadowData.y;
        cameraBuffer.SetGlobalVector(globalShadowDataId, globalShadowData);
        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        // render mesh opaque
        var sortingSettings = new SortingSettings(camera);

        sortingSettings.criteria = SortingCriteria.CommonOpaque;
        var drawSettings = new DrawingSettings(new ShaderTagId("SRPDefaultUnlit"), sortingSettings);

        drawSettings.enableDynamicBatching = true;
        drawSettings.enableInstancing      = true;
        if (cull.visibleLights.Length > 0)
        {
            drawSettings.perObjectData = PerObjectData.LightIndices;
        }
        drawSettings.perObjectData |= PerObjectData.ReflectionProbes;
        drawSettings.perObjectData |= PerObjectData.Lightmaps;
        drawSettings.perObjectData |= PerObjectData.LightProbe;
        drawSettings.perObjectData |= PerObjectData.LightProbeProxyVolume;
        drawSettings.perObjectData |= PerObjectData.ShadowMask;                // shadow for static
        drawSettings.perObjectData |= PerObjectData.OcclusionProbe;            // shadow for dynamic
        drawSettings.perObjectData |= PerObjectData.OcclusionProbeProxyVolume; // shadow for dynamic
        var filterSettings = new FilteringSettings(RenderQueueRange.opaque);

        filterSettings.renderQueueRange = RenderQueueRange.opaque;
        context.DrawRenderers(cull, ref drawSettings, ref filterSettings);

        // render skybox
        context.DrawSkybox(camera);

        // post-processing
        if (activeStack)
        {
            if (needsDepthOnlyPass)
            {
                var depthOnlyDrawSettings = new DrawingSettings(new ShaderTagId("DepthOnly"), sortingSettings);
                cameraBuffer.SetRenderTarget(cameraDepthTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
                cameraBuffer.ClearRenderTarget(true, false, Color.clear);
                context.ExecuteCommandBuffer(cameraBuffer);
                cameraBuffer.Clear();
                context.DrawRenderers(cull, ref drawSettings, ref filterSettings);
            }
            activeStack.RenderAfterOpaque(postProcessingBuffer, cameraColorTextureId, cameraDepthTextureId, renderWidth, renderHeight, msaaSamples, format);
            context.ExecuteCommandBuffer(postProcessingBuffer);
            postProcessingBuffer.Clear();

            if (needsDirectDepth)
            {
                cameraBuffer.SetRenderTarget(
                    cameraColorTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                    cameraDepthTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
            else
            {
                cameraBuffer.SetRenderTarget(
                    cameraColorTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            }
            context.ExecuteCommandBuffer(cameraBuffer);
            cameraBuffer.Clear();
        }

        // render mesh transparent
        sortingSettings.criteria        = SortingCriteria.CommonTransparent;
        filterSettings.renderQueueRange = RenderQueueRange.transparent;
        context.DrawRenderers(cull, ref drawSettings, ref filterSettings);

        //DrawDefaultPipeline(context, camera);

        // post-processing
        if (renderToTexture)
        {
            if (activeStack)
            {
                activeStack.RenderAfterTransparent(postProcessingBuffer, cameraColorTextureId, cameraDepthTextureId, renderWidth, renderHeight, msaaSamples, format);
                context.ExecuteCommandBuffer(postProcessingBuffer);
                postProcessingBuffer.Clear();
            }
            else
            {
                cameraBuffer.Blit(cameraColorTextureId, BuiltinRenderTextureType.CameraTarget);
            }
            cameraBuffer.ReleaseTemporaryRT(cameraColorTextureId);
            if (needsDepth)
            {
                cameraBuffer.ReleaseTemporaryRT(cameraDepthTextureId);
            }
        }

        cameraBuffer.EndSample("Render Camera");
        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        // sumbit
        context.Submit();

        // release
        if (shadowMap)
        {
            RenderTexture.ReleaseTemporary(shadowMap);
            shadowMap = null;
        }
        if (cascadedShadowMap)
        {
            RenderTexture.ReleaseTemporary(cascadedShadowMap);
            cascadedShadowMap = null;
        }
    }