public Vector3F GetSunlight()
        {
            if (SunDirection.Y >= 0)
            {
                //----- Sun is above horizon.
                return(GetTransmittance(SunDirection) * SunIntensity);
            }

            //----- Sun is below horizon.
            // Get angle.
            float sunAngle = (float)MathHelper.ToDegrees(Math.Asin(SunDirection.Y));

            if (sunAngle < -5)
            {
                return(Vector3F.Zero);
            }

            // Sample horizon instead of real direction.
            Vector3F direction = SunDirection;

            direction.Y = 0;
            if (!direction.TryNormalize())
            {
                return(Vector3F.Zero);
            }

            Vector3F horizonSunlight = GetTransmittance(direction) * SunIntensity;

            // Lerp horizon sunlight to 0 at -5°.
            float f = 1 - MathHelper.Clamp(-sunAngle / 5.0f, 0, 1);

            return(horizonSunlight * f);
        }
Example #2
0
        private void CreateGuiControls()
        {
            var panel = SampleFramework.AddOptions("Shadows");

            // ----- Light node controls
            var lightNodePanel = SampleHelper.AddGroupBox(panel, "Light Nodes");

            SampleHelper.AddDropDown(
                lightNodePanel,
                "Light type",
                new[] { "Spotlight", "PointLight" },
                0,
                selectedItem =>
            {
                bool enableSpotlight      = (selectedItem == "Spotlight");
                _spotlightNode.IsEnabled  = enableSpotlight;
                _pointLightNode.IsEnabled = !enableSpotlight;
            });

            SampleHelper.AddSlider(
                lightNodePanel,
                "Range",
                "F2",
                1,
                30,
                10,
                value =>
            {
                _spotlight.Range  = value;
                _pointLight.Range = value;
            });

            SampleHelper.AddSlider(
                lightNodePanel,
                "Y position",
                "F2",
                0,
                10,
                1,
                value =>
            {
                foreach (var node in new[] { _spotlightNode, _pointLightNode })
                {
                    var pose        = node.PoseWorld;
                    pose.Position.Y = value;
                    node.PoseWorld  = pose;
                }
            });

            SampleHelper.AddSlider(
                lightNodePanel,
                "X rotation",
                "F0",
                -90,
                90,
                1,
                value =>
            {
                var pose                 = _spotlightNode.PoseWorld;
                pose.Orientation         = Matrix33F.CreateRotationX(MathHelper.ToRadians(value));
                _spotlightNode.PoseWorld = pose;
            });

            SampleHelper.AddSlider(
                lightNodePanel,
                "Spotlight angle",
                "F2",
                1,
                89,
                MathHelper.ToDegrees(_spotlight.CutoffAngle),
                value =>
            {
                float angle             = MathHelper.ToRadians(value);
                _spotlight.FalloffAngle = 0.8f * angle;
                _spotlight.CutoffAngle  = angle;
            });

            // ----- Shadow controls
            var shadowPanel = SampleHelper.AddGroupBox(panel, "Shadow");

            SampleHelper.AddSlider(
                shadowPanel,
                "Shadow map resolution",
                "F0",
                16,
                1024,
                _standardShadow.PreferredSize,
                value =>
            {
                _standardShadow.PreferredSize = (int)value;
                _cubeMapShadow.PreferredSize  = (int)value;
            });

            SampleHelper.AddCheckBox(
                shadowPanel,
                "Prefer 16 bit",
                _standardShadow.Prefer16Bit,
                isChecked =>
            {
                _standardShadow.Prefer16Bit = isChecked;
                _cubeMapShadow.Prefer16Bit  = isChecked;
            });

            SampleHelper.AddSlider(
                shadowPanel,
                "Depth bias",
                "F2",
                0,
                10,
                _standardShadow.DepthBias,
                value =>
            {
                _standardShadow.DepthBias = value;
                _cubeMapShadow.DepthBias  = value;
            });

            SampleHelper.AddSlider(
                shadowPanel,
                "Normal offset",
                "F2",
                0,
                10,
                _standardShadow.NormalOffset,
                value =>
            {
                _standardShadow.NormalOffset = value;
                _cubeMapShadow.NormalOffset  = value;
            });

            SampleHelper.AddSlider(
                shadowPanel,
                "Number of samples",
                "F0",
                -1,
                32,
                _standardShadow.NumberOfSamples,
                value =>
            {
                _standardShadow.NumberOfSamples = (int)value;
                _cubeMapShadow.NumberOfSamples  = (int)value;
            });

            SampleHelper.AddSlider(
                shadowPanel,
                "Filter radius",
                "F2",
                0,
                10,
                _standardShadow.FilterRadius,
                value =>
            {
                _standardShadow.FilterRadius = value;
                _cubeMapShadow.FilterRadius  = value;
            });

            SampleHelper.AddSlider(
                shadowPanel,
                "Jitter resolution",
                "F0",
                1,
                10000,
                _standardShadow.JitterResolution,
                value =>
            {
                _standardShadow.JitterResolution = value;
                _cubeMapShadow.JitterResolution  = value;
            });

            SampleFramework.ShowOptionsWindow("Shadows");
        }