void OnDestroy() { // RT are not released automatically if (BlurredScreen) { BlurredScreen.Release(); } }
public void OnBeforeBlur() { if (BlurredScreen == null || !BlurredScreen.IsCreated() || Downsample != lastDownsample || !BlurRegion.Approximately(lastBlurRegion)) { CreateNewBlurredScreen(); lastDownsample = Downsample; lastBlurRegion = BlurRegion; } }
protected virtual void ProgressiveBlur(RenderTexture sourceRt) { //Resize final texture if base downsample changed if (Downsample != lastDownsample || !BlurRegion.Equals(lastBlurRegion)) { CreateNewBlurredScreen(); lastDownsample = Downsample; lastBlurRegion = BlurRegion; } if (BlurredScreen.IsCreated()) { BlurredScreen.DiscardContents(); } //Relative blur size to maintain same look across multiple resolution material.SetFloat(_sizePropId, Size * ScreenSize); int firstDownsampleFactor = iteration > 0 ? 1 : 0; int rtW = BlurredScreen.width >> firstDownsampleFactor; //= width / (downsample + 1)^2 int rtH = BlurredScreen.height >> firstDownsampleFactor; RenderTexture tmpRt = RenderTexture.GetTemporary(rtW, rtH, 0, sourceRt.format); tmpRt.filterMode = FilterMode.Bilinear; sourceRt.filterMode = FilterMode.Bilinear; //Initial downsample material.SetVector(_cropRegionPropId, new Vector4(BlurRegion.xMin, BlurRegion.yMin, BlurRegion.xMax, BlurRegion.yMax)); Graphics.Blit(sourceRt, tmpRt, material, 1); //Downsample. (iteration - 1) pass for (int i = 2; i <= iteration; i++) { ProgressiveResampling(i, ref tmpRt); } //Upsample. (iteration - 1) pass for (int i = iteration - 1; i >= 1; i--) { ProgressiveResampling(i, ref tmpRt); } //Final upsample. Blit to blurredRt and release tmp Graphics.Blit(tmpRt, BlurredScreen, material, 0); RenderTexture.ReleaseTemporary(tmpRt); }
protected virtual void ProgressiveBlur(RenderTexture source) { //Resize global texture if base downsample changed if (Downsample != lastDownsample) { BlurredScreen = new RenderTexture( Cam.pixelWidth >> Downsample, Cam.pixelHeight >> Downsample, 0); lastDownsample = Downsample; } if (BlurredScreen.IsCreated()) { BlurredScreen.DiscardContents(); } //Relative blur size to maintain same look across multiple resolution material.SetFloat("size", Size * ScreenSize); int firstDownsampleFactor = iteration > 0 ? 1 : 0 + Downsample; //= width / (downsample + 1)^2 int rtW = source.width >> firstDownsampleFactor; int rtH = source.height >> firstDownsampleFactor; RenderTexture rt = RenderTexture.GetTemporary(rtW, rtH, 0, source.format); //Bilinear filtering mode increase blurriness when scaled rt.filterMode = FilterMode.Bilinear; source.filterMode = FilterMode.Bilinear; //Initial downsample Graphics.Blit(source, rt, material, 0); //Downsample. (iteration - 1) pass for (int i = 2; i < Iteration + 1; i++) { ProgressiveResampling(source, i, ref rt); } //Upsample. (iteration - 1) pass for (int i = Iteration - 1; i > 0; i--) { ProgressiveResampling(source, i, ref rt); } //Final upsample. Blit to blurredRt Graphics.Blit(rt, BlurredScreen, material, 0); RenderTexture.ReleaseTemporary(rt); }
protected virtual void CreateNewBlurredScreen() { if (BlurredScreen) { BlurredScreen.Release(); } BlurredScreen = new RenderTexture(Mathf.RoundToInt(Cam.pixelWidth * BlurRegion.width) >> Downsample, Mathf.RoundToInt(Cam.pixelHeight * BlurRegion.height) >> Downsample, 0) { name = $"{gameObject.name} Translucent Image Source", filterMode = FilterMode.Bilinear, }; BlurredScreen.Create(); }