コード例 #1
0
    public XPipeline(bool dynamicBatching, bool instancing, XPostProcessingStack defaultStack,
                     Texture2D ditherTexture, float ditherAnimationSpeed,
                     int shadowMapSize, float shadowDistance, float shadowFadeRange,
                     int shadowCascades, Vector3 shadowCascadeSplit)
    {
        GraphicsSettings.lightsUseLinearIntensity = true;
        if (SystemInfo.usesReversedZBuffer)
        {
            worldToShadowCascadeMatrices[4].m33 = 1f;
        }
        if (dynamicBatching)
        {
            drawFlags = DrawRendererFlags.EnableDynamicBatching;
        }

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

        this.defaultStack = defaultStack;

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

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

#if UNITY_EDITOR
        Lightmapping.SetDelegate(lightmappingLightsDelegate);
#endif
    }
コード例 #2
0
    void Render(ScriptableRenderContext context, Camera camera)
    {
        ScriptableCullingParameters cullingParameters;

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

#if UNITY_EDITOR
        //SceneView 处理UI
        if (camera.cameraType == CameraType.SceneView)
        {
            ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
        }
#endif
        CullResults.Cull(ref cullingParameters, context, ref cull);

        //光照信息
        if (cull.visibleLights.Count > 0)
        {
            ConfigureLights();
            if (mainLightExists)
            {
                RenderCascadedShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
                cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
            }
            if (shadowTileCount > 0)
            {
                RenderShadows(context);
            }
            else
            {
                cameraBuffer.DisableShaderKeyword(shadowsHardKeyWord);
                cameraBuffer.DisableShaderKeyword(shadowsSoftKeyWord);
            }
        }
        else
        {
            cameraBuffer.SetGlobalVector(lightIndicesOffsetAndCountID, Vector4.zero);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsHardKeyword);
            cameraBuffer.DisableShaderKeyword(cascadedShadowsSoftKeyword);
            cameraBuffer.DisableShaderKeyword(shadowsHardKeyWord);
            cameraBuffer.DisableShaderKeyword(shadowsSoftKeyWord);
        }

        //注意Scene窗口如果没有启用光源需要注意
        //  if (camera.cameraType == CameraType.Game)   //SceneView 光源个数有点问题
        {
            //阴影贴图是在常规场景之前渲染的,所以在渲染之前调用渲染阴影,但是在剔除之后
        }

        context.SetupCameraProperties(camera);  //设置视图投影矩阵

        var xPipelineCamera = camera.GetComponent <XPipelineCamera>();
        XPostProcessingStack activeStack = xPipelineCamera ?
                                           xPipelineCamera.PostProcessingStack : defaultStack;

        if (activeStack)
        {
            cameraBuffer.GetTemporaryRT(cameraColorTextureId,
                                        camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Bilinear
                                        );
            cameraBuffer.GetTemporaryRT(cameraDepthTextureId,
                                        camera.pixelWidth, camera.pixelHeight, 24, FilterMode.Point, RenderTextureFormat.Depth
                                        );
            cameraBuffer.SetRenderTarget(
                cameraColorTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                cameraDepthTextureId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store
                );
        }

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

        cameraBuffer.BeginSample("Render Camera");
        cameraBuffer.SetGlobalVectorArray(visiableLightColorsId, visiableLightColors);
        cameraBuffer.SetGlobalVectorArray(visiableLightDirectionsOrPositionsId, visiableLightDirectionsOrPositions);
        cameraBuffer.SetGlobalVectorArray(visiableLightAttenuationsId, visiableLightAttenuations);
        cameraBuffer.SetGlobalVectorArray(visiableLightSpotDirectionsId, visiableLightSpotDirections);
        cameraBuffer.SetGlobalVectorArray(visibleLightOcclusionMasksId, visibleLightOcclusionMasks);

        globalShadowData.z = 1f - cullingParameters.shadowDistance * globalShadowData.y;
        cameraBuffer.SetGlobalVector(globalShadowDataId, globalShadowData);

        context.ExecuteCommandBuffer(cameraBuffer);
        cameraBuffer.Clear();

        var drawSetting = new DrawRendererSettings(
            camera, new ShaderPassName("SRPDefaultUnlit")
            )
        {
            flags = drawFlags,
            // rendererConfiguration = RendererConfiguration.PerObjectLightIndices8
        };

        //没有光源时,防止Light Indices崩溃
        if (cull.visibleLights.Count > 0)
        {
            drawSetting.rendererConfiguration = RendererConfiguration.PerObjectLightIndices8;
        }

        drawSetting.rendererConfiguration |=
            RendererConfiguration.PerObjectReflectionProbes |
            RendererConfiguration.PerObjectLightmaps |
            RendererConfiguration.PerObjectLightProbe |
            RendererConfiguration.PerObjectLightProbeProxyVolume |
            RendererConfiguration.PerObjectShadowMask |
            RendererConfiguration.PerObjectOcclusionProbe |
            RendererConfiguration.PerObjectOcclusionProbeProxyVolume;

        //drawSetting.flags = drawFlags;   //DrawRendererFlags.EnableDynamicBatching;  //动态合批
        drawSetting.sorting.flags = SortFlags.CommonOpaque;
        var filterSettings = new FilterRenderersSettings(true)
        {
            renderQueueRange = RenderQueueRange.opaque
        };
        context.DrawRenderers(cull.visibleRenderers, ref drawSetting, filterSettings);
        context.DrawSkybox(camera);

        if (activeStack)
        {
            activeStack.RenderAfterOpaque(
                postProcessingBufffer, cameraColorTextureId, cameraDepthTextureId,
                camera.pixelWidth, camera.pixelHeight
                );
            context.ExecuteCommandBuffer(postProcessingBufffer);
            postProcessingBufffer.Clear();
            cameraBuffer.SetRenderTarget(
                cameraColorTextureId,
                RenderBufferLoadAction.Load, RenderBufferStoreAction.Store,
                cameraDepthTextureId,
                RenderBufferLoadAction.Load, RenderBufferStoreAction.Store
                );
            context.ExecuteCommandBuffer(cameraBuffer);
            cameraBuffer.Clear();
        }

        drawSetting.sorting.flags       = SortFlags.CommonTransparent;
        filterSettings.renderQueueRange = RenderQueueRange.transparent;
        context.DrawRenderers(cull.visibleRenderers, ref drawSetting, filterSettings);

        DrawDefaultPipeline(context, camera);

        //Post-Processing
        if (activeStack)
        {
            activeStack.RenderAfterTransparent(
                postProcessingBufffer, cameraColorTextureId, cameraDepthTextureId,
                camera.pixelWidth, camera.pixelHeight
                );

            context.ExecuteCommandBuffer(postProcessingBufffer);
            postProcessingBufffer.Clear();
            cameraBuffer.ReleaseTemporaryRT(cameraColorTextureId);
            cameraBuffer.ReleaseTemporaryRT(cameraDepthTextureId);
        }

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

        context.Submit();

        //提交上下文之后,释放渲染纹理
        if (shadowMap)
        {
            RenderTexture.ReleaseTemporary(shadowMap);
            shadowMap = null;
        }

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