コード例 #1
0
        public RenderTexture ApplyToAndReturn(RenderTexture source, SleekRenderSettings settings)
        {
            Vector4 luminanceThreshold = HelperExtensions.GetLuminanceThreshold(settings);

            // Changing current Luminance Const value just to make sure that we have the latest settings in our Uniforms
            _brightpassBlurMaterial.SetVector(Uniforms._LuminanceThreshold, luminanceThreshold);
            _brightpassBlurMaterial.SetVector(Uniforms._TexelSize, new Vector2(1f / _blurTextures[0].width, 1f / _blurTextures[0].height));

            var currentTargetRenderTexture  = _blurTextures[0];
            var previousTargetRenderTexture = _blurTextures[0];
            var passes = _blurTextures.Length;

            for (int i = 0; i < passes; i++)
            {
                currentTargetRenderTexture = _blurTextures[i];

                // We use a different material for the first blur pass
                if (i == 0)
                {
                    // Applying downsample + brightpass (stored in Alpha)
                    _renderer.Blit(source, currentTargetRenderTexture, _brightpassBlurMaterial);
                }
                else
                {
                    _downsampleBlurMaterial.SetVector(Uniforms._TexelSize, new Vector2(1f / currentTargetRenderTexture.width, 1f / currentTargetRenderTexture.height));
                    // Applying only blur to our already brightpassed texture
                    _renderer.Blit(previousTargetRenderTexture, currentTargetRenderTexture, _downsampleBlurMaterial);
                }

                previousTargetRenderTexture = currentTargetRenderTexture;
            }

            return(currentTargetRenderTexture);
        }
コード例 #2
0
        private void Precompose(RenderTexture source, bool isBloomEnabled)
        {
            // Setting up vignette effect
            var isVignetteEnabledInSettings = settings.vignetteEnabled;

            if (isVignetteEnabledInSettings && !_isVignetteAlreadyEnabled)
            {
                _preComposeMaterial.EnableKeyword(Keywords.VIGNETTE_ON);
                _isVignetteAlreadyEnabled = true;
            }
            else if (!isVignetteEnabledInSettings && _isVignetteAlreadyEnabled)
            {
                _preComposeMaterial.DisableKeyword(Keywords.VIGNETTE_ON);
                _isVignetteAlreadyEnabled = false;
            }

            if (isVignetteEnabledInSettings)
            {
                // Calculating Vignette parameters once per frame rather than once per pixel
                float vignetteBeginRadius           = settings.vignetteBeginRadius;
                float squareVignetteBeginRaduis     = vignetteBeginRadius * vignetteBeginRadius;
                float vignetteRadii                 = vignetteBeginRadius + settings.vignetteExpandRadius;
                float oneOverVignetteRadiusDistance = 1f / (vignetteRadii - squareVignetteBeginRaduis);

                var vignetteColor = settings.vignetteColor;

                _preComposeMaterial.SetVector(Uniforms._VignetteShape, new Vector4(
                                                  4f * oneOverVignetteRadiusDistance * oneOverVignetteRadiusDistance,
                                                  -oneOverVignetteRadiusDistance * squareVignetteBeginRaduis));

                // Premultiplying Alpha of vignette color
                _preComposeMaterial.SetColor(Uniforms._VignetteColor, new Color(
                                                 vignetteColor.r * vignetteColor.a,
                                                 vignetteColor.g * vignetteColor.a,
                                                 vignetteColor.b * vignetteColor.a,
                                                 vignetteColor.a));
            }

            // Bloom is handled in two different passes (two blurring bloom passes and one precompose pass)
            // So we need to check for whether it's enabled in precompose step too (shader has variants without bloom)
            if (isBloomEnabled)
            {
                _preComposeMaterial.SetTexture(Uniforms._BloomTex, _bloomResultTexture);
                _preComposeMaterial.SetFloat(Uniforms._BloomIntencity, settings.bloomIntensity);
                _preComposeMaterial.SetColor(Uniforms._BloomTint, settings.bloomTint);

                if (!_isBloomAlreadyEnabled)
                {
                    _preComposeMaterial.EnableKeyword(Keywords.BLOOM_ON);
                    _isBloomAlreadyEnabled = true;
                }
            }
            else if (_isBloomAlreadyEnabled)
            {
                _preComposeMaterial.DisableKeyword(Keywords.BLOOM_ON);
                _isBloomAlreadyEnabled = false;
            }

            var sourceRenderTexture = isBloomEnabled ? _bloomResultTexture : source;

            // Finally applying precompose step. It slaps bloom and vignette together
            _passRenderer.Blit(sourceRenderTexture, _preComposeTexture, _preComposeMaterial);
        }