Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make blur distance independent of resolution & FOV #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions Assets/FrostedGlass/Scripts/CommandBufferBlur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class CommandBufferBlur : MonoBehaviour

Vector2 _ScreenResolution = Vector2.zero;
RenderTextureFormat _TextureFormat = RenderTextureFormat.ARGB32;
float _OldFov = 0;
float _OldStepDist = 0;

public float blurStepDistance = 0.1f;

public void Cleanup()
{
Expand Down Expand Up @@ -71,43 +75,47 @@ void Initialize()

int numIterations = 4;

Vector2[] sizes = {
new Vector2(Screen.width, Screen.height),
new Vector2(Screen.width / 2, Screen.height / 2),
new Vector2(Screen.width / 4, Screen.height / 4),
new Vector2(Screen.width / 8, Screen.height / 8),
};
float stepSize = blurStepDistance / _Camera.fieldOfView;
Vector2 offsets = new Vector2(stepSize * _Camera.pixelHeight / _Camera.pixelWidth, stepSize);



for (int i = 0; i < numIterations; ++i)
{
int screenCopyID = Shader.PropertyToID("_ScreenCopyTexture");
_CommandBuffer.GetTemporaryRT(screenCopyID, -1, -1, 0, FilterMode.Bilinear, _TextureFormat);
_CommandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, screenCopyID);

int curSize = (int)Mathf.Pow(2,i);
int curW = _Camera.pixelWidth/curSize;
int curH = _Camera.pixelHeight/curSize;

int blurredID = Shader.PropertyToID("_Grab" + i + "_Temp1");
int blurredID2 = Shader.PropertyToID("_Grab" + i + "_Temp2");
_CommandBuffer.GetTemporaryRT(blurredID, (int)sizes[i].x, (int)sizes[i].y, 0, FilterMode.Bilinear, _TextureFormat);
_CommandBuffer.GetTemporaryRT(blurredID2, (int)sizes[i].x, (int)sizes[i].y, 0, FilterMode.Bilinear, _TextureFormat);
_CommandBuffer.GetTemporaryRT(blurredID, curW, curH, 0, FilterMode.Bilinear, _TextureFormat);
_CommandBuffer.GetTemporaryRT(blurredID2, curW, curH, 0, FilterMode.Bilinear, _TextureFormat);

_CommandBuffer.Blit(screenCopyID, blurredID);
_CommandBuffer.ReleaseTemporaryRT(screenCopyID);

_CommandBuffer.SetGlobalVector("offsets", new Vector4(2.0f / sizes[i].x, 0, 0, 0));
_CommandBuffer.SetGlobalVector("offsets", new Vector4(offsets.x*curSize, 0, 0, 0));
_CommandBuffer.Blit(blurredID, blurredID2, _Material);
_CommandBuffer.SetGlobalVector("offsets", new Vector4(0, 2.0f / sizes[i].y, 0, 0));
_CommandBuffer.SetGlobalVector("offsets", new Vector4(0, offsets.y*curSize, 0, 0));
_CommandBuffer.Blit(blurredID2, blurredID, _Material);

_CommandBuffer.SetGlobalTexture("_GrabBlurTexture_" + i, blurredID);
}

_Camera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _CommandBuffer);

_ScreenResolution = new Vector2(Screen.width, Screen.height);
_ScreenResolution = new Vector2(_Camera.pixelWidth, _Camera.pixelHeight);
_OldFov = _Camera.fieldOfView;
_OldStepDist = blurStepDistance;
}

void OnPreRender()
{
if (_ScreenResolution != new Vector2(Screen.width, Screen.height))
if ((_ScreenResolution != new Vector2(_Camera.pixelWidth, _Camera.pixelHeight)) || (_Camera.fieldOfView != _OldFov) || (_OldStepDist != blurStepDistance))
Cleanup();

Initialize();
Expand Down