Exemple #1
0
    public MyPipeline1(bool dynamicBatching, bool instancing,
                       MyPostProcessingStack1 defaultStack,
                       Texture2D ditherTexture, float ditherAnimationSpeed,
                       int shadowMapSize, float shadowDistance, float shadowFadeRange,
                       int shadowCascades, Vector3 shadowCascadeSplit,
                       float renderScale, int massSamples) //constructor with configurations
    {
        GraphicsSettings.lightsUseLinearIntensity = true;
        if (SystemInfo.usesReversedZBuffer)
        {  //set the shadow case for outside all cascades
            worldToShadowCascadeMatrices[4].m33 = 1f;
        }

        if (dynamicBatching)
        {
            drawFlags = DrawRendererFlags.EnableDynamicBatching;
        }

        if (instancing)
        {
            drawFlags |= DrawRendererFlags.EnableInstancing;
        }

        this.shadowMapSize  = shadowMapSize;
        this.shadowDistance = shadowDistance;
        globalShadowData.y  = 1f / shadowFadeRange;

        this.shadowCascades     = shadowCascades;
        this.shadowCascadeSplit = shadowCascadeSplit;

        this.ditherTexture = ditherTexture;
        if (ditherAnimationSpeed > 0f)
        {
            ConfigureDitherAnimation(ditherAnimationSpeed);
        }

        #if UNITY_EDITOR
        Lightmapping.SetDelegate(lightmappingLightsDelegate);
        #endif

        this.defaultStack = defaultStack;

        this.renderScale = renderScale;

        QualitySettings.antiAliasing = msaaSamples;
        this.msaaSamples             = Mathf.Max(QualitySettings.antiAliasing, 1);
    }
Exemple #2
0
    //------------------------------- Render MAIN -----------------------------
    void Render(ScriptableRenderContext context, Camera camera)
    {
        //Culling
        ScriptableCullingParameters cullingParameters;

        if (!CullResults.GetCullingParameters(camera, out cullingParameters))
        {
            return;
        }
        cullingParameters.shadowDistance = Mathf.Min(shadowDistance, camera.farClipPlane);

    #if UNITY_EDITOR
        //add UI to SceneWindow
        if (camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
        }
    #endif

        CullResults.Cull(ref cullingParameters, context, ref cull);

        //shadows
        if (cull.visibleLights.Count > 0)
        {
            ConfigureLights();
            if (mainLightExists)
            {
                RenderCascadedShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
                cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
            }

            if (shadowTileCount > 0)
            {
                RenderShadows(context);
            }
            else
            {  //no shadows
                cameraBuffer.DisableShaderKeyword(shadowsHardKeyword);
                cameraBuffer.DisableShaderKeyword(shadowsSoftKeyword);
            }
        }
        else  //no lights
        {
            cameraBuffer.SetGlobalVector(lightIndicesOffsetAndCountID, Vector4.zero);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
            cameraBuffer.DisableShaderKeyword(shadowsHardKeyword);
            cameraBuffer.DisableShaderKeyword(shadowsSoftKeyword);
        }

        //Setup Camera parameters
        context.SetupCameraProperties(camera);  //set MVP matrix, etc.

        //enable post processing according to camera type
        var myPipelineCamera = camera.GetComponent <MyPipelineCamera1>();
        MyPostProcessingStack1 activeStack = myPipelineCamera ?
                                             myPipelineCamera.PostProcessingStack : defaultStack;

        //enable scaled rendering for only Game camera
        bool scaledRendering = (renderScale <1f || renderScale> 1f) &&
                               camera.cameraType == CameraType.Game;

        int renderWidth  = camera.pixelWidth;
        int renderHeight = camera.pixelHeight;
        if (scaledRendering)
        {
            renderWidth  = (int)(renderWidth * renderScale);
            renderHeight = (int)(renderHeight * renderScale);
        }
        //msaa
        int renderSamples = camera.allowMSAA ? msaaSamples : 1;

        //get the camera render texture from camera buffer
        bool renderToTexture = scaledRendering || renderSamples > 1 || activeStack;

        if (renderToTexture)
        {
            cameraBuffer.GetTemporaryRT(
                cameraColorTextureId, renderWidth, renderHeight, 0,
                FilterMode.Bilinear, RenderTextureFormat.Default,
                RenderTextureReadWrite.Default, renderSamples);
            //24 is to enable depth buffer

            //create a depth buffer texture
            cameraBuffer.GetTemporaryRT(
                cameraDepthTextureId, renderWidth, renderHeight, 24,
                FilterMode.Point, RenderTextureFormat.Depth,
                RenderTextureReadWrite.Linear, renderSamples);

            cameraBuffer.SetRenderTarget(
                cameraColorTextureId,
                RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                cameraDepthTextureId,
                RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
        }

        //Setup Command Buffer and Execute
        CameraClearFlags clearFlags = camera.clearFlags;

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


        cameraBuffer.BeginSample("Render Camera");  //start a sub-level

        //transfer light properties to buffers in shader
        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();

        //Draw Opaque, Unlit Shader
        var drawSettings = new DrawRendererSettings(
            camera, new ShaderPassName("SRPDefaultUnlit"))
        {
            flags = drawFlags,
        };

        if (cull.visibleLights.Count > 0)
        {
            drawSettings.rendererConfiguration = RendererConfiguration.PerObjectLightIndices8;
        }
        drawSettings.rendererConfiguration |=
            RendererConfiguration.PerObjectReflectionProbes |
            RendererConfiguration.PerObjectLightmaps |
            RendererConfiguration.PerObjectLightProbe |
            RendererConfiguration.PerObjectLightProbeProxyVolume |
            RendererConfiguration.PerObjectShadowMask |
            RendererConfiguration.PerObjectOcclusionProbe |
            RendererConfiguration.PerObjectOcclusionProbeProxyVolume;


        drawSettings.sorting.flags = SortFlags.CommonOpaque;

        var filterSettings = new FilterRenderersSettings(true)
        {
            renderQueueRange = RenderQueueRange.opaque
        };

        context.DrawRenderers(
            cull.visibleRenderers, ref drawSettings, filterSettings);


        //Draw Skybox
        context.DrawSkybox(camera);

        //Post processing on opaque objects
        if (activeStack)
        {
            activeStack.RenderAfterOpaque(
                postProcessingBuffer, cameraColorTextureId, cameraDepthTextureId,
                renderWidth, renderHeight
                );
            context.ExecuteCommandBuffer(postProcessingBuffer);
            postProcessingBuffer.Clear();

            //reset target for the next steps
            cameraBuffer.SetRenderTarget(
                cameraColorTextureId,
                RenderBufferLoadAction.Load, RenderBufferStoreAction.Store,
                cameraDepthTextureId,
                RenderBufferLoadAction.Load, RenderBufferStoreAction.Store
                );
            context.ExecuteCommandBuffer(cameraBuffer);
            cameraBuffer.Clear();
        }


        //Draw Transparent
        drawSettings.sorting.flags      = SortFlags.CommonTransparent;
        filterSettings.renderQueueRange = RenderQueueRange.transparent;
        context.DrawRenderers(
            cull.visibleRenderers, ref drawSettings, filterSettings
            );

        //Conditions for default pipelines
        DrawDefaultPipeline(context, camera);

        //Post-processing on transparent objects
        if (renderToTexture)
        {
            activeStack.RenderAfterTransparent(postProcessingBuffer,
                                               cameraColorTextureId, cameraDepthTextureId,
                                               renderWidth, renderHeight);

            context.ExecuteCommandBuffer(postProcessingBuffer);
            postProcessingBuffer.Clear();
        }
        else
        {
            cameraBuffer.Blit(
                cameraColorTextureId, BuiltinRenderTextureType.CameraTarget
                );
        }
        cameraBuffer.ReleaseTemporaryRT(cameraColorTextureId);
        cameraBuffer.ReleaseTemporaryRT(cameraDepthTextureId);

        cameraBuffer.EndSample("Render Camera");   //end the sub-level
        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        context.Submit();

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