예제 #1
0
        public static void ApplySliceTransform(ref ShadowSliceData shadowSliceData, int altaWidth, int altaHeight)
        {
            Matrix4x4 sliceTransform    = Matrix4x4.identity;
            float     oneOverAltaWidht  = 1f / altaWidth;
            float     oneOverAltaHeight = 1f / altaHeight;

            sliceTransform.m00 = shadowSliceData.resolution * oneOverAltaWidht;
            sliceTransform.m11 = shadowSliceData.resolution * oneOverAltaHeight;
            sliceTransform.m03 = shadowSliceData.offsetX * oneOverAltaWidht;
            sliceTransform.m13 = shadowSliceData.offsetY * oneOverAltaHeight;

            shadowSliceData.shadowTransform = sliceTransform * shadowSliceData.shadowTransform;
        }
예제 #2
0
        public static void RenderShadowSlice(CommandBuffer cmd, ref ScriptableRenderContext context, ref ShadowSliceData shadowSliceData,
                                             ref DrawShadowsSettings settings, Matrix4x4 proj, Matrix4x4 view)
        {
            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);
            cmd.DisableScissorRect();
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
        }
예제 #3
0
        public static bool ExtractDirectionalLightMatrix(ref CullResults cullResults, ref ShadowData shadowData,
                                                         int shadowLightIndex, int cascadeIndex, int shadowResolution, float shadowNearPlane, float shadowFarPlane,
                                                         out Vector4 cascadeSplitDistance, out ShadowSliceData shadowSliceData, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix)
        {
            ShadowSplitData splitData;
            Vector3         splitRotio = Vector3.one;

            if (shadowNearPlane > 1f)
            {
                float r = Mathf.Pow(shadowFarPlane / shadowNearPlane, 1f / shadowData.directionalLightCascadeCount);
                splitRotio = new Vector3(shadowNearPlane * r, shadowNearPlane * r * r, shadowNearPlane * r * r * r) / shadowFarPlane;
            }
            else
            {
                if (shadowData.directionalLightCascadeCount == 1)
                {
                    splitRotio = new Vector3(1f, 0f, 0f);
                }
                else if (shadowData.directionalLightCascadeCount == 2)
                {
                    splitRotio = new Vector3(0.25f, 1f, 0f);
                }
                else
                {
                    splitRotio = new Vector3(0.067f, 0.2f, 0.467f);
                }
            }
            bool success = cullResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(shadowLightIndex, cascadeIndex,
                                                                                            shadowData.directionalLightCascadeCount, splitRotio, shadowResolution, shadowNearPlane, out viewMatrix, out projMatrix, out splitData);

            cascadeSplitDistance            = splitData.cullingSphere;
            shadowSliceData.offsetX         = (cascadeIndex % 2) * shadowResolution;
            shadowSliceData.offsetY         = (cascadeIndex / 2) * shadowResolution;
            shadowSliceData.resolution      = shadowResolution;
            shadowSliceData.shadowTransform = GetShadowTransform(projMatrix, viewMatrix);

            if (shadowData.directionalLightCascadeCount > 1)
            {
                ApplySliceTransform(ref shadowSliceData, shadowData.directionalShadowAltasRes, shadowData.directionalShadowAltasRes);
            }

            return(success);
        }