void RenderDirectionalShadow(ref ScriptableRenderContext context, ref CullingResults cullingResults, int shadowMapSize, int index, ref VisibleLight visibleLight)
    {
        if (shadowData.lights[index] == null)
        {
            return;
        }

        CoreUtils.SetRenderTarget(buffer, shadowData.shadowMaps, ClearFlag.Depth, 0, CubemapFace.Unknown, index);
        SubmitBuffer(ref context, buffer);

        for (int j = 0; j < 4; j++)
        {
            if (shadowData.lights[index].cascades[j] == null)
            {
                continue;
            }

            Rect tileViewport = new Rect(shadowData.lights[index].cascades[j].tileOffset.x * shadowData.lights[index].tileSize, shadowData.lights[index].cascades[j].tileOffset.y * shadowData.lights[index].tileSize, shadowData.lights[index].tileSize, shadowData.lights[index].tileSize);

            buffer.SetViewport(new Rect(tileViewport));
            buffer.EnableScissorRect(new Rect(tileViewport.x + 4f, tileViewport.y + 4f, shadowData.lights[index].tileSize - 8f, shadowData.lights[index].tileSize - 8f));
            buffer.SetViewProjectionMatrices(shadowData.lights[index].cascades[j].viewMatrix, shadowData.lights[index].cascades[j].projectionMatrix);
            ShaderInput.SetShadowBias(buffer, visibleLight.light.shadowBias);
            SubmitBuffer(ref context, buffer);

            ShadowDrawingSettings shadowSettings = new ShadowDrawingSettings(cullingResults, index);
            shadowSettings.splitData = shadowData.lights[index].cascades[j].splitData;
            context.DrawShadows(ref shadowSettings);
        }

        buffer.DisableScissorRect();
        SubmitBuffer(ref context, buffer);
    }
        public void RenderSpotShadow(int index, int splitCount, int tileSize)
        {
            OtherShadow           otherShadow           = otherShadows[index];
            ShadowDrawingSettings shadowDrawingSettings = new ShadowDrawingSettings(cullingResults, otherShadow.VisibleLightIndex);

            cullingResults.ComputeSpotShadowMatricesAndCullingPrimitives(otherShadow.VisibleLightIndex,
                                                                         out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData shadowSplitData);
            shadowDrawingSettings.splitData = shadowSplitData;
            SetShadowMapViewport(index, splitCount, tileSize, out Vector2 offset);
            float inversedSplitCount = 1f / splitCount;

            // !!
            // ??
            float texelSize  = 2f / (tileSize * projectionMatrix.m00);
            float filterSize = texelSize * ((float)shadowSettings.OtherShadowSettings.PCFMode + 1f);
            float bias       = otherShadow.NoramlBias * filterSize * 1.4142136f;

            SetOtherTileData(index, offset, inversedSplitCount, bias);

            otherShadowMatrices[index] = ConvertClipSpaceToTileSpace(projectionMatrix * viewMatrix, offset, inversedSplitCount);
            commandBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
            commandBuffer.SetGlobalDepthBias(0f, otherShadow.SlopeScaleBias);
            ExecuteBuffer();
            renderContext.DrawShadows(ref shadowDrawingSettings);
            commandBuffer.SetGlobalDepthBias(0f, 0f);
        }
    void RenderSpotShadow(ref ScriptableRenderContext context, ref CullingResults cullingResults, int shadowMapSize, int index, ref VisibleLight visibleLight)
    {
        if (shadowData.lights[index] == null)
        {
            return;
        }

        CoreUtils.SetRenderTarget(buffer, shadowData.shadowMaps, ClearFlag.Depth, 0, CubemapFace.Unknown, index);
        SubmitBuffer(ref context, buffer);

        if (shadowData.lights[index].cascades[0] == null)
        {
            return;
        }

        Rect tileViewport = new Rect(0, 0, shadowMapSize, shadowMapSize);

        buffer.SetViewport(new Rect(tileViewport));
        buffer.SetViewProjectionMatrices(shadowData.lights[index].cascades[0].viewMatrix, shadowData.lights[index].cascades[0].projectionMatrix);
        ShaderInput.SetShadowBias(buffer, visibleLight.light.shadowBias);
        SubmitBuffer(ref context, buffer);

        ShadowDrawingSettings shadowSettings = new ShadowDrawingSettings(cullingResults, index);

        shadowSettings.splitData = shadowData.lights[index].cascades[0].splitData;
        context.DrawShadows(ref shadowSettings);
    }
Exemple #4
0
    void RenderSpotShadows(int index, int split, int tileSize)
    {
        ShadowedOtherLight light = shadowedOtherLights[index];
        var shadowSettings       =
            new ShadowDrawingSettings(cullingResults, light.visibleLightIndex)
        {
            useRenderingLayerMaskTest = true
        };

        cullingResults.ComputeSpotShadowMatricesAndCullingPrimitives(
            light.visibleLightIndex, out Matrix4x4 viewMatrix,
            out Matrix4x4 projectionMatrix, out ShadowSplitData splitData
            );
        shadowSettings.splitData = splitData;
        float   texelSize  = 2f / (tileSize * projectionMatrix.m00);
        float   filterSize = texelSize * ((float)settings.other.filter + 1f);
        float   bias       = light.normalBias * filterSize * 1.4142136f;
        Vector2 offset     = SetTileViewport(index, split, tileSize);
        float   tileScale  = 1f / split;

        SetOtherTileData(index, offset, tileScale, bias);
        otherShadowMatrices[index] = ConvertToAtlasMatrix(
            projectionMatrix * viewMatrix, offset, tileScale
            );

        buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
        buffer.SetGlobalDepthBias(0f, light.slopeScaleBias);
        ExecuteBuffer();
        context.DrawShadows(ref shadowSettings);
        buffer.SetGlobalDepthBias(0f, 0f);
    }
Exemple #5
0
	private void RenderDirectionalShadow(CullingResults cull, int sunlightIndex, Light sunlight) {
		ResetRenderTarget(SunlightShadowmapId, true, false, 1, Color.black);
		
		var shadowSettings = new ShadowDrawingSettings(cull, sunlightIndex);
		if (!cull.ComputeDirectionalShadowMatricesAndCullingPrimitives(sunlightIndex, 0, 1, new Vector3(1, 0, 0), @params.sunlightParams.shadowResolution, sunlight.shadowNearPlane, out var viewMatrix, out var projectionMatrix, out var splitData)) {
			ExecuteCurrentBuffer();
			return;
		}
		
		shadowSettings.splitData = splitData;
		
		_currentBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
		
		if (_reversedZBuffer) {
			projectionMatrix.m20 = -projectionMatrix.m20;
			projectionMatrix.m21 = -projectionMatrix.m21;
			projectionMatrix.m22 = -projectionMatrix.m22;
			projectionMatrix.m23 = -projectionMatrix.m23;
		}
		
		var scaleOffset = Matrix4x4.identity;
		scaleOffset.m00 = scaleOffset.m11 = scaleOffset.m22 = scaleOffset.m03 = scaleOffset.m13 = scaleOffset.m23 = 0.5f;
		
		var sunlightInverseVP = scaleOffset * (projectionMatrix * viewMatrix);
		
		_currentBuffer.SetGlobalMatrix(ShaderManager.SUNLIGHT_INVERSE_VP, sunlightInverseVP);
		
		ExecuteCurrentBuffer();
		
		_context.DrawShadows(ref shadowSettings);
	}
Exemple #6
0
        void RenderDirectionalShadows(int index, int split, int tileSize)
        {
            ShadowDirectionalLight light = shadowDirectionalLights[index];
            var     shadowSettings       = new ShadowDrawingSettings(shadowParams.cullingResults, light.visibleLightIndex);
            int     cascadeCount         = shadowParams.settings.directional.cascadeCount;
            int     tileOffset           = index * cascadeCount;
            Vector3 ratios = shadowParams.settings.directional.CasadeRatios;

            for (int i = 0; i < cascadeCount; ++i)
            {
                shadowParams.cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, i, cascadeCount, ratios, tileSize, 0f,
                                                                                                 out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix, out ShadowSplitData splitData);
                shadowSettings.splitData = splitData;
                if (index == 0)
                {
                    //cullingSphere是以摄像机位置为球心的
                    Vector4 cullingSphere = splitData.cullingSphere;
                    cullingSphere.w         *= cullingSphere.w;
                    cascadeCullingSpheres[i] = cullingSphere;
                }
                int tileIndex = tileOffset + i;
                dirShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, SetTileViewPort(tileIndex, split, tileSize), split);
                //绘制指定区域的
                buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
                buffer.SetGlobalDepthBias(50000f, 0f);
                ExecuteBuffer();
                GlobalUniqueParamsForRP.context.DrawShadows(ref shadowSettings);
                buffer.SetGlobalDepthBias(0f, 0f);
            }
        }
Exemple #7
0
    private void RenderPointShadows(int index, int split, int tileSize)
    {
        ShadowedOtherLight    light          = shadowedOtherLights[index];
        ShadowDrawingSettings shadowSettings = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex);

        float texelSize  = 2f / tileSize;
        float filterSize = texelSize * ((float)this.shadowSettings.other.filter + 1f);
        float bias       = light.normalBias * filterSize * 1.4142136f;
        float tileScale  = 1f / split;
        float fovBias    = Mathf.Atan(1f + bias + filterSize) * Mathf.Rad2Deg * 2f - 90f;

        for (int i = 0; i < 6; ++i)
        {
            cullingResults.ComputePointShadowMatricesAndCullingPrimitives(
                light.visibleLightIndex, (CubemapFace)i, fovBias, out Matrix4x4 viewMatrix,
                out Matrix4x4 projectionMatrix, out ShadowSplitData splitData);
            viewMatrix.m11           = -viewMatrix.m11;
            viewMatrix.m12           = -viewMatrix.m12;
            viewMatrix.m13           = -viewMatrix.m13;
            shadowSettings.splitData = splitData;
            int tileIndex = index + i;
            //float texelSize = 2f / (tileSize * projectionMatrix.m00);
            //float filterSize = texelSize * ((float)this.shadowSettings.other.filter + 1f);
            //float bias = light.normalBias * filterSize * 1.4142136f;
            Vector2 offset = SetTileViewport(tileIndex, split, tileSize);
            //float tileScale = 1f / split;
            SetOtherTileData(tileIndex, offset, tileScale, bias);
            otherShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, offset, tileScale);
            buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
            buffer.SetGlobalDepthBias(0f, light.slopeScaleBias);
            ExecuteBuffer();
            context.DrawShadows(ref shadowSettings);
            buffer.SetGlobalDepthBias(0f, 0f);
        }
    }
Exemple #8
0
    void RenderDirectionalShadows(int index, int split, int tileSize)
    {
        var light          = ShadowedDirectionalLights[index];
        var shadowSettings = new ShadowDrawingSettings(cullingResults, index);

        cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
            light.VisibleLightIndex,
            0,
            1,
            Vector3.zero,
            tileSize,
            0f,
            out Matrix4x4 viewMatrix,
            out Matrix4x4 projMatrix,
            out ShadowSplitData shadowSplitData);

        shadowSettings.splitData = shadowSplitData;

        // SetTileViewport(index, split, tileSize);
        DirShadowMatrices[index] = ConvertToAtlasMatrix(
            projMatrix * viewMatrix,
            SetTileViewport(index, split, tileSize),
            split);

        buffer.SetGlobalMatrixArray(DirShadowMatricesID, DirShadowMatrices);
        buffer.SetViewProjectionMatrices(viewMatrix, projMatrix);
        ExecuteBuffer();
        context.DrawShadows(ref shadowSettings);
    }
Exemple #9
0
        void RenderDirectionalShadows(int index, int split, int tileSize)
        {
            ShadowedDirectionalLight light = shadowedDirectionalLights[index];
            var shadowSettings             = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex);

            var     cascadeCount = settings.directional.cascadeCount;
            int     tileOffset   = index * cascadeCount;
            Vector3 ratios       = settings.directional.CascadeRatios;

            float cullingFactor = Mathf.Max(0, 0.8f - settings.directional.cascadeFade);

            for (int i = 0; i < cascadeCount; i++)
            {
                cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, i, cascadeCount,
                                                                                    ratios, tileSize, light.nearPlaneOffset,
                                                                                    out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix,
                                                                                    out ShadowSplitData splitData);
                splitData.shadowCascadeBlendCullingFactor = cullingFactor;
                shadowSettings.splitData = splitData;
                if (index == 0)
                {
                    SetCascadeData(i, splitData.cullingSphere, tileSize);
                }
                int tileOffIndex = tileOffset + i;
                dirShadowMatrices[tileOffIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, SetTileViewport(tileOffIndex, split, tileSize), split);
                buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
                buffer.SetGlobalDepthBias(0f, light.slopeScaleBias);
                ExecuteBuffer();
                context.DrawShadows(ref shadowSettings);
                buffer.SetGlobalDepthBias(0f, 0f);
            }
        }
Exemple #10
0
        public void Render(ref RenderingData context)
        {
            cmd.BeginSample("Shadow");
            cmd.Clear();

            ShadowDrawingSettings drawSetting = new ShadowDrawingSettings(context.cullResult, 0);

            Matrix4x4       view;
            Matrix4x4       proj;
            ShadowSplitData splitData;

            context.cullResult.ComputeDirectionalShadowMatricesAndCullingPrimitives(0, 0, 1, Vector3.zero, (int)context.asset.ShadowSetting.directional.altasSize, 0f, out view, out proj, out splitData);
            drawSetting.splitData = splitData;
            cmd.SetViewProjectionMatrices(view, proj);

            int texSize = (int)context.asset.ShadowSetting.directional.altasSize;

            var ren = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);

            cmd.GetTemporaryRT(dirShadowAtlasId, texSize, texSize, 24, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);
            cmd.SetRenderTarget(dirShadowAtlasId, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            cmd.ClearRenderTarget(true, false, Color.clear);
            context.context.ExecuteCommandBuffer(cmd);
            cmd.Clear();

            context.context.DrawShadows(ref drawSetting);

            // cmd.SetRenderTarget(ren);
            //  context.context.ExecuteCommandBuffer(cmd);

            cmd.EndSample("Shadow");
        }
Exemple #11
0
    void RenderShadows(ScriptableRenderContext context)
    {
        shadowBuffer.GetTemporaryRT(shadowMapId, 512, 512, 16, FilterMode.Bilinear, RenderTextureFormat.Depth);
        shadowMap            = RenderTexture.GetTemporary(512, 512, 16, RenderTextureFormat.Shadowmap);
        shadowMap.filterMode = FilterMode.Bilinear;
        shadowMap.wrapMode   = TextureWrapMode.Clamp;

        CoreUtils.SetRenderTarget(shadowBuffer, shadowMap, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.Depth);
        shadowBuffer.BeginSample("Render Shadows");
        context.ExecuteCommandBuffer(shadowBuffer);
        shadowBuffer.Clear();

        Matrix4x4       viewMatrix, projectMatrix;
        ShadowSplitData splitData;

        cullingResults.ComputeSpotShadowMatricesAndCullingPrimitives(0, out viewMatrix, out projectMatrix, out splitData);
        shadowBuffer.SetViewProjectionMatrices(viewMatrix, projectMatrix);
        context.ExecuteCommandBuffer(shadowBuffer);

        // draw shadow
        var shadowSettings = new ShadowDrawingSettings(cullingResults, 0);

        context.DrawShadows(ref shadowSettings);

        shadowBuffer.EndSample("Render Shadows");
        context.ExecuteCommandBuffer(shadowBuffer);
        shadowBuffer.Clear();
    }
        private void RenderCascadedShadows(ScriptableRenderContext context)
        {
            float tileSize = ShadowMapSize / 2;

            cascadedShadowMap = SetShadowRenderTarget();
            shadowBuffer.BeginSample(shadowBufferName);
            context.ExecuteCommandBuffer(shadowBuffer);
            shadowBuffer.Clear();
            Light shadowLight = cullingResults.visibleLights[0].light;

            shadowBuffer.SetGlobalFloat(shadowBiasId, shadowLight.shadowBias);
            var shadowSettings = new ShadowDrawingSettings(cullingResults, 0);
            var tileMatrix     = Matrix4x4.identity;

            tileMatrix.m00 = tileMatrix.m11 = 0.5f;

            for (int i = 0; i < ShadowCascades; i++)
            {
                Matrix4x4       viewMatrix, projectionMatrix;
                ShadowSplitData splitData;
                cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
                    0, i, ShadowCascades, ShadowCascadeSplit, (int)tileSize,
                    shadowLight.shadowNearPlane,
                    out viewMatrix, out projectionMatrix, out splitData
                    );

                Vector2 tileOffset = ConfigureShadowTile(i, 2, tileSize);
                shadowBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
                context.ExecuteCommandBuffer(shadowBuffer);
                shadowBuffer.Clear();

                shadowSettings.splitData    = splitData;
                cascadeCullingSpheres[i]    = splitData.cullingSphere; // xyz分量描述球的位置,w分量定义球的半径。
                cascadeCullingSpheres[i].w *= splitData.cullingSphere.w;
                Vector3 pos = new Vector3(splitData.cullingSphere.x, splitData.cullingSphere.y, splitData.cullingSphere.z);
                context.DrawShadows(ref shadowSettings);
                CalculateWorldToShadowMatrix(ref viewMatrix, ref projectionMatrix, out worldToShadowCascadeMatrices[i]);
                tileMatrix.m03 = tileOffset.x * 0.5f;
                tileMatrix.m13 = tileOffset.y * 0.5f;
                worldToShadowCascadeMatrices[i] = tileMatrix * worldToShadowCascadeMatrices[i];
            }

            shadowBuffer.DisableScissorRect();
            shadowBuffer.SetGlobalTexture(cascadedShadowMapId, cascadedShadowMap);
            shadowBuffer.SetGlobalVectorArray(cascadeCullingSpheresId, cascadeCullingSpheres);
            shadowBuffer.SetGlobalMatrixArray(worldToShadowCascadeMatricesId, worldToShadowCascadeMatrices);

            float invShadowMapSize = 1f / ShadowMapSize;

            shadowBuffer.SetGlobalVector(cascadedShadowMapSizeId, new Vector4(invShadowMapSize, invShadowMapSize, ShadowMapSize, ShadowMapSize));
            shadowBuffer.SetGlobalFloat(cascadedShadoStrengthId, shadowLight.shadowStrength);
            bool hard = shadowLight.shadows == LightShadows.Hard;

            CoreUtils.SetKeyword(shadowBuffer, cascadedShadowsHardKeyword, hard);
            CoreUtils.SetKeyword(shadowBuffer, cascadedShadowsSoftKeyword, !hard);

            shadowBuffer.EndSample(shadowBufferName);
            context.ExecuteCommandBuffer(shadowBuffer);
            shadowBuffer.Clear();
        }
Exemple #13
0
    void RenderDirectionalShadows(int index, int split, int tileSize)
    {
        ShadowedDirectionalLight light = ShadowedDirectionalLights[index];
        var shadowSettings             =
            new ShadowDrawingSettings(cullingResults, light.visibleLightIndex);
        int cascadeCount = settings.directional.cascadeCount;
        int tileOffset   = index * cascadeCount;

        for (int i = 0; i < cascadeCount; i++)
        {
            cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
                light.visibleLightIndex, i, cascadeCount, cascadeRatio,
                tileSize, 0f, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData shadowSplitData
                );
            shadowSettings.splitData = shadowSplitData;

            int tileIndex = tileOffset + i;
            dirShadowMatrices[tileIndex] = ConvertToAtlasMatrix(
                projMatrix * viewMatrix,
                SetTileViewport(tileIndex, split, tileSize), split
                );
            buffer.SetViewProjectionMatrices(viewMatrix, projMatrix);
            ExecuteBuffer();
            context.DrawShadows(ref shadowSettings);
        }
    }
Exemple #14
0
    void RenderOneSpotShadows(int shadowedLightIdx, int atlasSplitFactor, int atlasTileSize)
    {
        ShadowedLight shadowedLight = _shadowedSpotLights[shadowedLightIdx];

        _cullResults.ComputeSpotShadowMatricesAndCullingPrimitives(shadowedLight.visibleIndex, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData shadowSplitData);
        _cmdBuffer.SetViewProjectionMatrices(viewMatrix, projMatrix);

        Rect tileViewPort = Rect.zero;
        int  tileIdx      = shadowedLightIdx;

        tileViewPort.x     = tileIdx % atlasSplitFactor * atlasTileSize;
        tileViewPort.y     = tileIdx / atlasSplitFactor * atlasTileSize;
        tileViewPort.width = tileViewPort.height = atlasTileSize;
        _cmdBuffer.SetViewport(tileViewPort);
        _cmdBuffer.SetGlobalDepthBias(0, shadowedLight.light.shadowBias);
        ExecuteCommandBuffer();

        int     atlasSize         = atlasSplitFactor * atlasTileSize;
        Vector4 normalizeViewPort = Vector4.zero;
        float   texelSize         = 1f / atlasSize;

        normalizeViewPort.x               = tileViewPort.x / atlasSize + texelSize;
        normalizeViewPort.y               = tileViewPort.y / atlasSize + texelSize;
        normalizeViewPort.z               = tileViewPort.width / atlasSize - texelSize;
        normalizeViewPort.w               = tileViewPort.height / atlasSize - texelSize;
        _spotShadowMatrixs[tileIdx]       = ShadowMatrixToAtlasTileMatrix(projMatrix * viewMatrix, tileViewPort, atlasSize);
        _spotShadowTileViewPorts[tileIdx] = normalizeViewPort;

        ShadowDrawingSettings shadowDrawSetting = new ShadowDrawingSettings(_cullResults, shadowedLight.visibleIndex);

        shadowDrawSetting.splitData = shadowSplitData;
        _srContext.DrawShadows(ref shadowDrawSetting);
        _cmdBuffer.SetGlobalDepthBias(0, 0);
    }
Exemple #15
0
    void RenderDirectionalShadows(int index, int split, int tileSize)
    {
        ShadowedDirectionalLight light = ShadowedDirectionalLights[index];

        //create a shadowdrawsetting based on the light index
        var shadowDSettings = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex)
        {
            useRenderingLayerMaskTest = true
        };                                  //make shadow effcted by rendering layer mask

        //take into count that each light will render max 4 cascades
        int     cascadeCount = shadowSettings.directional.cascadeCount;
        int     tileOffset   = index * cascadeCount;
        Vector3 ratios       = shadowSettings.directional.CascadeRatios;
        //cal the cascade culling factor, make it 0.8-fade to make sure casters in transition not being culled
        float cullingfactor = Mathf.Max(0f, 0.8f - shadowSettings.directional.cascadeFade);

        float tileScale = 1f / split;

        //get the lightviewMatrix, lightprojectionMatrix, clip space box for the dirctional lights
        for (int i = 0; i < cascadeCount; i++)
        {
            cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
                light.visibleLightIndex, i, cascadeCount, ratios, tileSize, light.nearPlaneOffset,
                out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix,
                out ShadowSplitData splitData);
            //make UNITY to cull some shadow caster in large cascade if the smaller cascade is enough
            splitData.shadowCascadeBlendCullingFactor = cullingfactor;
            shadowDSettings.splitData = splitData;
            //assign the cascade culling sphere, all lights uses the same culling spheres and same cascade datas
            if (index == 0)
            {
                SetCascadeData(i, splitData.cullingSphere, tileSize);
            }
            int tileIdex = tileOffset + i;

            //set the render view port and get the offset for modify the dir shadow matrix
            //set the matrix from wolrd space to shadow Atlas space
            dirShadowMatrices[tileIdex] = ConvertToAtlasMatrix(
                projectionMatrix * viewMatrix,
                SetTileViewport(tileIdex, split, tileSize),
                tileScale
                );
            //set the ViewProjectionMatrices for the buffer
            buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);

            //experimental depth bias
            //buffer.SetGlobalDepthBias(50000f, 0f);
            //using slop bias
            buffer.SetGlobalDepthBias(0f, light.slopScaleBias);
            ExecuteBuffer();

            //draw shadow caster on the buffer
            context.DrawShadows(ref shadowDSettings);
            //Debug.Log(shadowDSettings
            buffer.SetGlobalDepthBias(0f, 0f);
        }
    }
Exemple #16
0
        void RenderAdditionalShadowmapAtlas(ref ScriptableRenderContext context, ref CullingResults cullResults, ref LightData lightData, ref ShadowData shadowData)
        {
            NativeArray <VisibleLight> visibleLights = lightData.visibleLights;

            bool          additionalLightHasSoftShadows = false;
            CommandBuffer cmd = CommandBufferPool.Get(k_RenderAdditionalLightShadows);

            using (new ProfilingSample(cmd, k_RenderAdditionalLightShadows))
            {
                int shadowmapWidth  = shadowData.additionalLightsShadowmapWidth;
                int shadowmapHeight = shadowData.additionalLightsShadowmapHeight;

                m_AdditionalLightsShadowmapTexture = RenderTexture.GetTemporary(shadowmapWidth, shadowmapHeight,
                                                                                k_ShadowmapBufferBits, m_AdditionalShadowmapFormat);
                m_AdditionalLightsShadowmapTexture.filterMode = FilterMode.Bilinear;
                m_AdditionalLightsShadowmapTexture.wrapMode   = TextureWrapMode.Clamp;

                SetRenderTarget(cmd, m_AdditionalLightsShadowmapTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
                                ClearFlag.Depth, Color.black, TextureDimension.Tex2D);

                for (int i = 0; i < m_AdditionalShadowCastingLightIndices.Count; ++i)
                {
                    int          shadowLightIndex = m_AdditionalShadowCastingLightIndices[i];
                    VisibleLight shadowLight      = visibleLights[shadowLightIndex];

                    if (m_AdditionalShadowCastingLightIndices.Count > 1)
                    {
                        ShadowUtils.ApplySliceTransform(ref m_AdditionalLightSlices[i], shadowmapWidth, shadowmapHeight);
                    }

                    var     settings   = new ShadowDrawingSettings(cullResults, shadowLightIndex);
                    Vector4 shadowBias = ShadowUtils.GetShadowBias(ref shadowLight, shadowLightIndex,
                                                                   ref shadowData, m_AdditionalLightSlices[i].projectionMatrix, m_AdditionalLightSlices[i].resolution);
                    ShadowUtils.SetupShadowCasterConstantBuffer(cmd, ref shadowLight, shadowBias);
                    ShadowUtils.RenderShadowSlice(cmd, ref context, ref m_AdditionalLightSlices[i], ref settings, m_AdditionalLightSlices[i].projectionMatrix, m_AdditionalLightSlices[i].viewMatrix);
                    additionalLightHasSoftShadows |= shadowLight.light.shadows == LightShadows.Soft;
                }

                SetupAdditionalLightsShadowReceiverConstants(cmd, ref shadowData);
            }

            // We share soft shadow settings for main light and additional lights to save keywords.
            // So we check here if pipeline supports soft shadows and either main light or any additional light has soft shadows
            // to enable the keyword.
            // TODO: In PC and Consoles we can upload shadow data per light and branch on shader. That will be more likely way faster.
            bool mainLightHasSoftShadows = shadowData.supportsMainLightShadows &&
                                           lightData.mainLightIndex != -1 &&
                                           visibleLights[lightData.mainLightIndex].light.shadows == LightShadows.Soft;

            bool softShadows = shadowData.supportsSoftShadows && (mainLightHasSoftShadows || additionalLightHasSoftShadows);

            CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.AdditionalLightShadows, true);
            CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.SoftShadows, softShadows);
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
Exemple #17
0
	private void RenderCascadedDirectionalShadow(CullingResults cull, int sunlightIndex, Light sunlight, float shadowDistance) {
		_currentBuffer.GetTemporaryRTArray(ShaderManager.SUNLIGHT_SHADOWMAP_ARRAY, @params.sunlightParams.shadowResolution, @params.sunlightParams.shadowResolution, @params.sunlightParams.shadowCascades, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);
		
		var shadowCascades = @params.sunlightParams.shadowCascades;
		
		var shadowSettings = new ShadowDrawingSettings(cull, sunlightIndex);
		var sunlightInverseVPArray = new Matrix4x4[shadowCascades];
		var sunlightShadowSplitBoundArray = new Vector4[shadowCascades];
		var scaleOffset = Matrix4x4.identity;
		scaleOffset.m00 = scaleOffset.m11 = scaleOffset.m22 =scaleOffset.m03 = scaleOffset.m13 = scaleOffset.m23 = 0.5f;
		
		_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_BIAS, sunlight.shadowBias);
		_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_NORMAL_BIAS, sunlight.shadowNormalBias);

		for (var i = 0; i < shadowCascades; i++) {
			ResetRenderTarget(SunlightShadowmapArrayId, CubemapFace.Unknown, i, true, false, 1, Color.black);

			if (!cull.ComputeDirectionalShadowMatricesAndCullingPrimitives(sunlightIndex, i, shadowCascades, @params.sunlightParams.shadowCascadeSplits, @params.sunlightParams.shadowResolution, sunlight.shadowNearPlane, out var viewMatrix, out var projectionMatrix, out var splitData)) {
				ExecuteCurrentBuffer();
				continue;
			}
			
			shadowSettings.splitData = splitData;
			
			_currentBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
		
			if (_reversedZBuffer) {
				projectionMatrix.m20 = -projectionMatrix.m20;
				projectionMatrix.m21 = -projectionMatrix.m21;
				projectionMatrix.m22 = -projectionMatrix.m22;
				projectionMatrix.m23 = -projectionMatrix.m23;
			}
		
			sunlightInverseVPArray[i] = scaleOffset * (projectionMatrix * viewMatrix);
			sunlightShadowSplitBoundArray[i] = splitData.cullingSphere;
			sunlightShadowSplitBoundArray[i].w *= splitData.cullingSphere.w;
			
			ExecuteCurrentBuffer();
			
			_context.DrawShadows(ref shadowSettings);
		}

		var inverseShadowmapSize = 1f / @params.sunlightParams.shadowResolution;
		
		_currentBuffer.SetGlobalFloat(ShaderManager.SUNLIGHT_SHADOW_STRENGTH, sunlight.shadowStrength);
		_currentBuffer.SetGlobalFloat(ShaderManager.SUNLIGHT_SHADOW_DISTANCE, shadowDistance);
		_currentBuffer.SetGlobalVector(ShaderManager.SUNLIGHT_SHADOWMAP_SIZE, new Vector4(inverseShadowmapSize, inverseShadowmapSize, @params.sunlightParams.shadowResolution, @params.sunlightParams.shadowResolution));
		_currentBuffer.SetGlobalMatrixArray(ShaderManager.SUNLIGHT_INVERSE_VP_ARRAY, sunlightInverseVPArray);
		_currentBuffer.SetGlobalVectorArray(ShaderManager.SUNLIGHT_SHADOW_SPLIT_BOUND_ARRAY, sunlightShadowSplitBoundArray);
		
		if (sunlight.shadows == LightShadows.Soft) _currentBuffer.EnableShaderKeyword(ShaderManager.SUNLIGHT_SOFT_SHADOWS);
		else _currentBuffer.DisableShaderKeyword(ShaderManager.SUNLIGHT_SOFT_SHADOWS);
		
		ExecuteCurrentBuffer();
	}
Exemple #18
0
        static void RenderShadows(RenderShadowsParameters parameters,
                                  RTHandle atlasRenderTexture,
                                  ShadowDrawingSettings shadowDrawSettings,
                                  ScriptableRenderContext renderContext,
                                  CommandBuffer cmd)
        {
            cmd.SetRenderTarget(atlasRenderTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            cmd.SetGlobalVector(parameters.atlasSizeShaderID, new Vector4(atlasRenderTexture.rt.width, atlasRenderTexture.rt.height, 1.0f / atlasRenderTexture.rt.width, 1.0f / atlasRenderTexture.rt.height));

            // Clear the whole atlas to avoid garbage outside of current request when viewing it.
            if (parameters.debugClearAtlas)
            {
                CoreUtils.DrawFullScreen(cmd, parameters.clearMaterial, null, 0);
            }

            var hdrp = RenderPipelineManager.currentPipeline as HDRenderPipeline;

            foreach (var shadowRequest in parameters.shadowRequests)
            {
                if (shadowRequest.shouldUseCachedShadow)
                {
                    continue;
                }

                cmd.SetGlobalDepthBias(1.0f, shadowRequest.slopeBias);
                cmd.SetViewport(shadowRequest.atlasViewport);

                cmd.SetGlobalFloat(HDShaderIDs._ZClip, shadowRequest.zClip ? 1.0f : 0.0f);
                CoreUtils.DrawFullScreen(cmd, parameters.clearMaterial, null, 0);

                shadowDrawSettings.lightIndex = shadowRequest.lightIndex;
                shadowDrawSettings.splitData  = shadowRequest.splitData;

                // Setup matrices for shadow rendering:
                Matrix4x4 viewProjection = shadowRequest.deviceProjectionYFlip * shadowRequest.view;
                cmd.SetGlobalMatrix(HDShaderIDs._ViewMatrix, shadowRequest.view);
                cmd.SetGlobalMatrix(HDShaderIDs._InvViewMatrix, shadowRequest.view.inverse);
                cmd.SetGlobalMatrix(HDShaderIDs._ProjMatrix, shadowRequest.deviceProjectionYFlip);
                cmd.SetGlobalMatrix(HDShaderIDs._InvProjMatrix, shadowRequest.deviceProjectionYFlip.inverse);
                cmd.SetGlobalMatrix(HDShaderIDs._ViewProjMatrix, viewProjection);
                cmd.SetGlobalMatrix(HDShaderIDs._InvViewProjMatrix, viewProjection.inverse);
                cmd.SetGlobalVectorArray(HDShaderIDs._ShadowClipPlanes, shadowRequest.frustumPlanes);

                // TODO: remove this execute when DrawShadows will use a CommandBuffer
                renderContext.ExecuteCommandBuffer(cmd);
                cmd.Clear();

                renderContext.DrawShadows(ref shadowDrawSettings);

                hdrp?.InvokeShadowMapRender(cmd, shadowRequest.worldTexelSize);
            }
            cmd.SetGlobalFloat(HDShaderIDs._ZClip, 1.0f);   // Re-enable zclip globally
            cmd.SetGlobalDepthBias(0.0f, 0.0f);             // Reset depth bias.
        }
Exemple #19
0
	private void RenderPointLightShadow(CullingResults cull, int shadowLightCount, Light[] shadowLights, int[] shadowLightIndices) {
		var pointLightShadowmapDescriptor = new RenderTextureDescriptor(@params.pointLightParams.shadowResolution, @params.pointLightParams.shadowResolution, RenderTextureFormat.RHalf, 16) {
			autoGenerateMips = false,
			bindMS = false,
			dimension = TextureDimension.CubeArray,
			volumeDepth = shadowLightCount * 6,
			enableRandomWrite = false,
			msaaSamples = 1,
			shadowSamplingMode = ShadowSamplingMode.None,
			sRGB = false,
			useMipMap = false,
			vrUsage = VRTextureUsage.None
		};
		
		_currentBuffer.GetTemporaryRT(ShaderManager.POINT_LIGHT_SHADOWMAP_ARRAY, pointLightShadowmapDescriptor, FilterMode.Bilinear);

		for (var i = 0; i < shadowLightCount; i++) {
			if (!cull.GetShadowCasterBounds(shadowLightIndices[i], out var shadowBounds)) continue;
			var light = shadowLights[i];
			_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_BIAS, light.shadowBias);
			_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_NORMAL_BIAS, light.shadowNormalBias);

			var shadowSettings = new ShadowDrawingSettings(cull, shadowLightIndices[i]);

			for (var j = 0; j < 6; j++) {
				var shadowSlice = i * 6 + j;
				ResetRenderTarget(PointLightShadowmapArrayId, CubemapFace.Unknown, shadowSlice, true, true, 1, Color.white);
				if (!cull.ComputePointShadowMatricesAndCullingPrimitives(shadowLightIndices[i], (CubemapFace) j, 0, out var viewMatrix, out var projectionMatrix, out var splitData)) {
					ExecuteCurrentBuffer();
					continue;
				}

				shadowSettings.splitData = splitData;
				
				Vector3 position = light.transform.position;
				_currentBuffer.SetGlobalVector(ShaderManager.LIGHT_POS, new Vector4(position.x, position.y, position.z, light.range));
				_currentBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
				ExecuteCurrentBuffer();

				_context.DrawShadows(ref shadowSettings);
			}
		}

		var inverseShadowmapSize = 1f / @params.pointLightParams.shadowResolution;
		
		_currentBuffer.SetGlobalVector(ShaderManager.POINT_LIGHT_SHADOWMAP_SIZE, new Vector4(inverseShadowmapSize, inverseShadowmapSize, @params.pointLightParams.shadowResolution, @params.pointLightParams.shadowResolution));

		if (@params.pointLightParams.softShadow) _currentBuffer.EnableShaderKeyword(ShaderManager.POINT_LIGHT_SOFT_SHADOWS);
		else _currentBuffer.DisableShaderKeyword(ShaderManager.POINT_LIGHT_SOFT_SHADOWS);
		
		ExecuteCurrentBuffer();
	}
Exemple #20
0
        private void RenderDirectionalShadows(int index, int tileSize)
        {
            ShadowDirectionalLight light = _shadowDirectionalLights[index];
            var shadowDrawSettings       = new ShadowDrawingSettings(_cullingResults, light.visibleLightIndex);

            _cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, 0, 1,
                                                                                 Vector3.zero, tileSize, 0f, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix,
                                                                                 out ShadowSplitData splitData);
            shadowDrawSettings.splitData = splitData;
            _buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
            ExecuteBuffer();
            _context.DrawShadows(ref shadowDrawSettings);
        }
Exemple #21
0
 public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context,
                                      ref ShadowSliceData shadowSliceData, ref ShadowDrawingSettings settings,
                                      Matrix4x4 proj, Matrix4x4 view)
 {
     cmd.SetViewport(new Rect(shadowSliceData.offsetX, shadowSliceData.offsetY, shadowSliceData.resolution, shadowSliceData.resolution));
     cmd.SetViewProjectionMatrices(view, proj);
     context.ExecuteCommandBuffer(cmd);
     cmd.Clear();
     context.DrawShadows(ref settings);
     cmd.DisableScissorRect();
     context.ExecuteCommandBuffer(cmd);
     cmd.Clear();
 }
Exemple #22
0
 public void SetupShadowSetting(ref RenderFrameContext context)
 {
     RenderShadow = false;
     for (int i = 0; i < context.CullingResult.visibleLights.Length; i++)
     {
         var visibleLight = context.CullingResult.visibleLights[i];
         if (visibleLight.lightType == LightType.Directional && visibleLight.light.shadows != LightShadows.None)
         {
             m_SadowSetting = new ShadowDrawingSettings(context.CullingResult, i);
             RenderShadow   = true;
         }
     }
 }
Exemple #23
0
    void RenderPointShadows(int index, int split, int tileSize)
    {
        ShadowedOtherLight light = shadowedOtherLights[index];

        //6 face FOV is fixed to 90 degree
        float texelSize  = 2f / (tileSize);
        float filterSize = texelSize * ((float)shadowSettings.other.filter + 1f);
        float bias       = light.normalBias * filterSize * 1.414f;
        float tileScale  = 1f / split;

        //create a shadowdrawsetting based on the light index
        var shadowDSettings = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex)
        {
            useRenderingLayerMaskTest = true
        };                                   //make shadow effcted by rendering layer mask

        //FOV bias to extend fov for a bit to get rid of seams
        float fovBias = Mathf.Atan(1f + bias + filterSize) * Mathf.Rad2Deg * 2f - 90f;

        for (int i = 0; i < 6; i++)
        {
            //get matrcies
            cullingResults.ComputePointShadowMatricesAndCullingPrimitives(
                light.visibleLightIndex, (CubemapFace)i, fovBias, out Matrix4x4 viewMatrix,
                out Matrix4x4 projectionMatrix, out ShadowSplitData splitData);

            //negate the unity reverse unity render shadow unside down(Stop light leak)
            viewMatrix.m11 = -viewMatrix.m11;
            viewMatrix.m12 = -viewMatrix.m12;
            viewMatrix.m13 = -viewMatrix.m13;

            shadowDSettings.splitData = splitData;

            int tileIndex = index + i;
            //m00 for projectiona matrix is the projection scale?

            Vector2 offset = SetTileViewport(tileIndex, split, tileSize);
            SetOtherTileData(tileIndex, offset, tileScale, bias);

            otherShadowMatrices[tileIndex] = ConvertToAtlasMatrix(
                projectionMatrix * viewMatrix,
                offset, tileScale
                );

            buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
            buffer.SetGlobalDepthBias(0f, light.slopeScaleBias);
            ExecuteBuffer();
            context.DrawShadows(ref shadowDSettings);
            buffer.SetGlobalDepthBias(0f, 0f);
        }
    }
Exemple #24
0
	private void RenderSpotLightShadow(CullingResults cull, int shadowLightCount, Light[] shadowLights, int[] shadowLightIndices) {
		_currentBuffer.GetTemporaryRTArray(ShaderManager.SPOT_LIGHT_SHADOWMAP_ARRAY, @params.spotLightParams.shadowResolution, @params.spotLightParams.shadowResolution, shadowLightCount, 16, FilterMode.Bilinear, RenderTextureFormat.Shadowmap);
		var shadowLightInverseVPArray = new Matrix4x4[shadowLightCount];
		var scaleOffset = Matrix4x4.identity;
		scaleOffset.m00 = scaleOffset.m11 = scaleOffset.m22 = scaleOffset.m03 = scaleOffset.m13 = scaleOffset.m23 = 0.5f;
		
		for (var i = 0; i < shadowLightCount; i++) {
			var light = shadowLights[i];
			
			_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_BIAS, light.shadowBias);
			_currentBuffer.SetGlobalFloat(ShaderManager.SHADOW_NORMAL_BIAS, light.shadowNormalBias);
			
			var shadowSettings = new ShadowDrawingSettings(cull, shadowLightIndices[i]);
			ResetRenderTarget(SpotLightShadowmapArrayId, CubemapFace.Unknown, i, true, false, 1, Color.black);

			if (!cull.ComputeSpotShadowMatricesAndCullingPrimitives(shadowLightIndices[i], out var viewMatrix, out var projectionMatrix, out var splitData)) {
				ExecuteCurrentBuffer();
				continue;
			}

			shadowSettings.splitData = splitData;
				
			_currentBuffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);

			if (_reversedZBuffer) {
				projectionMatrix.m20 = -projectionMatrix.m20;
				projectionMatrix.m21 = -projectionMatrix.m21;
				projectionMatrix.m22 = -projectionMatrix.m22;
				projectionMatrix.m23 = -projectionMatrix.m23;
			}
			
			shadowLightInverseVPArray[i] = scaleOffset * (projectionMatrix * viewMatrix);
			
			ExecuteCurrentBuffer();
			
			_context.DrawShadows(ref shadowSettings);
		}
		
		Extensions.Resize(ref _spotLightInverseVPBuffer, shadowLightCount);
		_spotLightInverseVPBuffer.SetData(shadowLightInverseVPArray);
		
		var inverseShadowmapSize = 1f / @params.spotLightParams.shadowResolution;
		
		_currentBuffer.SetGlobalBuffer(ShaderManager.SPOT_LIGHT_INVERSE_VP_BUFFER, _spotLightInverseVPBuffer);
		_currentBuffer.SetGlobalVector(ShaderManager.SPOT_LIGHT_SHADOWMAP_SIZE, new Vector4(inverseShadowmapSize, inverseShadowmapSize, @params.spotLightParams.shadowResolution, @params.spotLightParams.shadowResolution));
		
		if (@params.spotLightParams.softShadow) _currentBuffer.EnableShaderKeyword(ShaderManager.SPOT_LIGHT_SOFT_SHADOWS);
		else _currentBuffer.DisableShaderKeyword(ShaderManager.SPOT_LIGHT_SOFT_SHADOWS);
		
		ExecuteCurrentBuffer();
	}
Exemple #25
0
    void RenderDirectionalShadows(int index, int split, int tileSize)
    {
        ShadowDirectionalLight light = ShadowDirectionalLights[index];
        var shadowSettings           = new ShadowDrawingSettings(cullingResults, light.visibleLightIndex)
        {
            useRenderingLayerMaskTest = true
        };
        int     cascadeCount = settings.directional.cascadeCount;
        int     tileOffset   = index * cascadeCount;
        Vector3 ratios       = settings.directional.CascadeRatios;
        //裁剪参数
        float cullingFactor = Mathf.Max(0f, 0.8f - settings.directional.cascadeFade);
        float tileScale     = 1f / split;

        //遍历Cascade的数量
        for (int i = 0; i < cascadeCount; i++)
        {
            //Debug.Log(light.nearPlaneOffset);
            //Unity
            cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(
                light.visibleLightIndex, i, cascadeCount, ratios, tileSize, light.nearPlaneOffset, out Matrix4x4 viewMatrix, out Matrix4x4 projectionMatrix,
                out ShadowSplitData splitData);
            //???????????
            splitData.shadowCascadeBlendCullingFactor = cullingFactor;
            shadowSettings.splitData = splitData;
            if (index == 0)
            {
                //裁剪球 判断片段在哪一级的Cascade里面 w为半径
                Vector4 cullingSphere = splitData.cullingSphere;
                SetCascadeData(i, splitData.cullingSphere, tileSize);
            }

            int tileIndex = tileOffset + i;
            //设置ViewPort大小
            //SetTileViewport(index, split, tileSize);
            //从世界空间到灯光阴影投影空间
            dirShadowMatrices[tileIndex] = ConvertToAtlasMatrix(projectionMatrix * viewMatrix, SetTileViewport(tileIndex, split, tileSize), tileScale);
            buffer.SetViewProjectionMatrices(viewMatrix, projectionMatrix);

            //球覆盖
            buffer.SetGlobalVectorArray(cascadeCullingSpheresId, cascadeCullingSpheres);
            //Cascade数据
            buffer.SetGlobalVectorArray(cascadeDataId, cascadeData);
            //转换物体到灯光空间
            buffer.SetGlobalMatrixArray(dirShadowMatricesId, dirShadowMatrices);
            buffer.SetGlobalDepthBias(0f, light.slopeScaleBias);
            ExecuteBuffer();
            context.DrawShadows(ref shadowSettings);
            buffer.SetGlobalDepthBias(0f, 0f);
        }
    }
        public void RenderShadows(ScriptableRenderContext renderContext, CommandBuffer cmd, CullingResults cullResults)
        {
            // Avoid to do any commands if there is no shadow to draw
            if (m_ShadowRequestCount == 0)
            {
                return;
            }

            // TODO remove DrawShadowSettings, lightIndex and splitData when scriptable culling is available
            ShadowDrawingSettings dss = new ShadowDrawingSettings(cullResults, 0);

            // Clear atlas render targets and draw shadows
            m_Atlas.RenderShadows(renderContext, cmd, dss);
            m_CascadeAtlas.RenderShadows(renderContext, cmd, dss);
        }
    public void RenderDirectionalShadows(int index, int splitCount, int size)
    {
        ShadowedDirectionalLight light = ShadowedDirectionalLights[index];
        var shadowSetting = new ShadowDrawingSettings(_results, light.visibleLightIndex);

        //https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Rendering.CullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives.html
        //利用光源的方向(-transform.forward)方向可以求出view matrix,nearclip 和 最远的阴影距离等可以求出project matrix等等
        _results.ComputeDirectionalShadowMatricesAndCullingPrimitives(light.visibleLightIndex, 0, 1, Vector3.zero,
                                                                      size, 0, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix,
                                                                      out ShadowSplitData shadowSplitData);
        shadowSetting.splitData = shadowSplitData;
        SetTileViewport(index, splitCount, size);
        _commandBuffer.SetViewProjectionMatrices(viewMatrix, projMatrix);
        ExecuteCommandBuffer();
        _context.DrawShadows(ref shadowSetting);
    }
Exemple #28
0
        public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context,
                                             ref ShadowSliceData shadowSliceData, ref ShadowDrawingSettings settings,
                                             Matrix4x4 proj, Matrix4x4 view, Vector4 shadowBias, VisibleLight shadowLight, int cascadeIndex)
        {
            cmd.SetViewport(new Rect(shadowSliceData.offsetX, shadowSliceData.offsetY, shadowSliceData.resolution, shadowSliceData.resolution));
            cmd.EnableScissorRect(new Rect(shadowSliceData.offsetX + 4, shadowSliceData.offsetY + 4, shadowSliceData.resolution - 8, shadowSliceData.resolution - 8));

            cmd.SetViewProjectionMatrices(view, proj);
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            context.DrawShadows(ref settings);
            CustomRenderShadowSlice?.Invoke(cmd, GL.GetGPUProjectionMatrix(proj, true) * view, shadowBias, shadowLight, cascadeIndex);
            cmd.DisableScissorRect();
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
        }
    private bool RenderShadowMaps(ref ScriptableRenderContext context, ref CullingResults cullingResults, ref Matrix4x4 worldToShadowMatrix)
    {
        if (cullingResults.visibleLights.Length == 0)
        {
            return(false);
        }

        int lightIndex = 0; // Just use first light (index 0) for shadows and assume it's a directional light

        bool needToRender = cullingResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(lightIndex, 0, 1, Vector3.forward, 1024, cullingResults.visibleLights[0].light.shadowNearPlane, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix, out ShadowSplitData shadowSplitData);

        if (!needToRender)
        {
            return(false);
        }

        CommandBuffer cb = new CommandBuffer();

        cb.name = "Set up shadow data";
        cb.SetRenderTarget(m_ShadowMap);
        cb.ClearRenderTarget(true, true, Color.clear);
        cb.SetViewProjectionMatrices(viewMatrix, projMatrix);
        context.ExecuteCommandBuffer(cb);

        ShadowDrawingSettings shadowDrawSettings = new ShadowDrawingSettings(cullingResults, lightIndex);

        shadowDrawSettings.splitData = shadowSplitData;

        context.DrawShadows(ref shadowDrawSettings);

        if (SystemInfo.usesReversedZBuffer)
        {
            projMatrix.m20 = -projMatrix.m20;
            projMatrix.m21 = -projMatrix.m21;
            projMatrix.m22 = -projMatrix.m22;
            projMatrix.m23 = -projMatrix.m23;
        }

        var scaleOffset = Matrix4x4.TRS(
            Vector3.one * 0.5f, Quaternion.identity, Vector3.one * 0.5f
            );

        worldToShadowMatrix =
            scaleOffset * (projMatrix * viewMatrix);

        return(true);
    }
Exemple #30
0
        public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context,
                                             ref ShadowSliceData shadowSliceData, ref ShadowDrawingSettings settings,
                                             Matrix4x4 proj, Matrix4x4 view)
        {
            cmd.SetGlobalDepthBias(1.0f, 2.5f); // these values match HDRP defaults (see https://github.com/Unity-Technologies/Graphics/blob/9544b8ed2f98c62803d285096c91b44e9d8cbc47/com.unity.render-pipelines.high-definition/Runtime/Lighting/Shadow/HDShadowAtlas.cs#L197 )

            cmd.SetViewport(new Rect(shadowSliceData.offsetX, shadowSliceData.offsetY, shadowSliceData.resolution, shadowSliceData.resolution));
            cmd.SetViewProjectionMatrices(view, proj);
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            context.DrawShadows(ref settings);
            cmd.DisableScissorRect();
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();

            cmd.SetGlobalDepthBias(0.0f, 0.0f); // Restore previous depth bias values
        }