示例#1
0
        Matrix4x4 GetJitteredProjectionMatrix(Matrix4x4 origProj)
        {
            // The variance between 0 and the actual halton sequence values reveals noticeable
            // instability in Unity's shadow maps, so we avoid index 0.
            float jitterX = HaltonSequence.Get((taaFrameIndex & 1023) + 1, 2) - 0.5f;
            float jitterY = HaltonSequence.Get((taaFrameIndex & 1023) + 1, 3) - 0.5f;

            taaJitter = new Vector4(jitterX, jitterY, jitterX / camera.pixelWidth, jitterY / camera.pixelHeight);

            const int kMaxSampleCount = 8;

            if (++taaFrameIndex >= kMaxSampleCount)
            {
                taaFrameIndex = 0;
            }

            Matrix4x4 proj;

            if (camera.orthographic)
            {
                float vertical   = camera.orthographicSize;
                float horizontal = vertical * camera.aspect;

                var offset = taaJitter;
                offset.x *= horizontal / (0.5f * camera.pixelWidth);
                offset.y *= vertical / (0.5f * camera.pixelHeight);

                float left   = offset.x - horizontal;
                float right  = offset.x + horizontal;
                float top    = offset.y + vertical;
                float bottom = offset.y - vertical;

                proj = Matrix4x4.Ortho(left, right, bottom, top, camera.nearClipPlane, camera.farClipPlane);
            }
            else
            {
                var planes = origProj.decomposeProjection;

                float vertFov  = Math.Abs(planes.top) + Math.Abs(planes.bottom);
                float horizFov = Math.Abs(planes.left) + Math.Abs(planes.right);

                var planeJitter = new Vector2(jitterX * horizFov / camera.pixelWidth,
                                              jitterY * vertFov / camera.pixelHeight);

                planes.left   += planeJitter.x;
                planes.right  += planeJitter.x;
                planes.top    += planeJitter.y;
                planes.bottom += planeJitter.y;

                proj = Matrix4x4.Frustum(planes);
            }

            return(proj);
        }
示例#2
0
        public Vector2 Sample(Vector2 maximum, Vector2 minimum = default)
        {
            var offset = minimum;
            var scale  = maximum - minimum;
            var sample = new Vector2(
                HaltonSequence.Get(indexInSequence, horizontalRadix),
                HaltonSequence.Get(indexInSequence, verticalRadix)
                );

            indexInSequence++;

            return((sample * scale) + offset);
        }
        private Matrix4x4 GetJitteredProjectionMatrix(Matrix4x4 origProj, Camera UnityCamera)
        {
            float jitterX = HaltonSequence.Get((FrameIndex & 1023) + 1, 2) - 0.5f;
            float jitterY = HaltonSequence.Get((FrameIndex & 1023) + 1, 3) - 0.5f;

            TAAJitter = new float2(jitterX, jitterY);
            float4 taaJitter = new float4(jitterX, jitterY, jitterX / UnityCamera.pixelRect.size.x, jitterY / UnityCamera.pixelRect.size.y);

            if (++FrameIndex >= 8)
            {
                FrameIndex = 0;
            }

            Matrix4x4 proj;

            if (UnityCamera.orthographic)
            {
                float vertical   = UnityCamera.orthographicSize;
                float horizontal = vertical * UnityCamera.aspect;

                var offset = taaJitter;
                offset.x *= horizontal / (0.5f * UnityCamera.pixelRect.size.x);
                offset.y *= vertical / (0.5f * UnityCamera.pixelRect.size.y);

                float left   = offset.x - horizontal;
                float right  = offset.x + horizontal;
                float top    = offset.y + vertical;
                float bottom = offset.y - vertical;

                proj = Matrix4x4.Ortho(left, right, bottom, top, UnityCamera.nearClipPlane, UnityCamera.farClipPlane);
            }
            else
            {
                var planes = origProj.decomposeProjection;

                float vertFov  = Math.Abs(planes.top) + Math.Abs(planes.bottom);
                float horizFov = Math.Abs(planes.left) + Math.Abs(planes.right);

                var planeJitter = new Vector2(jitterX * horizFov / UnityCamera.pixelRect.size.x, jitterY * vertFov / UnityCamera.pixelRect.size.y);

                planes.left   += planeJitter.x;
                planes.right  += planeJitter.x;
                planes.top    += planeJitter.y;
                planes.bottom += planeJitter.y;

                proj = Matrix4x4.Frustum(planes);
            }

            return(proj);
        }