Skip to content

Commit

Permalink
fix #125; UICapturedEffectImage does not work with Lightweight Render…
Browse files Browse the repository at this point in the history
… Pipeline LWRP
  • Loading branch information
mob-sakai committed Sep 26, 2018
1 parent 385c082 commit bab2d98
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 146 deletions.
231 changes: 86 additions & 145 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectCapturedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,20 @@ protected override void OnPopulateMesh(VertexHelper vh)
/// </summary>
public void GetDesamplingSize(DesamplingRate rate, out int w, out int h)
{
var cam = canvas.worldCamera ?? Camera.main;
h = cam.pixelHeight;
w = cam.pixelWidth;
#if UNITY_EDITOR
if (!Application.isPlaying)
{
var res = UnityEditor.UnityStats.screenRes.Split('x');
w = int.Parse(res[0]);
h = int.Parse(res[1]);
}
else
#endif
{
w = Screen.width;
h = Screen.height;
}

if (rate == DesamplingRate.None)
return;

Expand All @@ -212,6 +223,7 @@ public void GetDesamplingSize(DesamplingRate rate, out int w, out int h)
/// </summary>
public void Capture()
{
// Fit to screen.
var rootCanvas = canvas.rootCanvas;
if (m_FitToScreen)
{
Expand All @@ -222,10 +234,7 @@ public void Capture()
rectTransform.position = rootTransform.position;
}

// Camera for command buffer.
_camera = canvas.worldCamera ?? Camera.main;

// Cache id for RT.
// Cache some ids.
if (s_CopyId == 0)
{
s_CopyId = Shader.PropertyToID("_UIEffectCapturedImage_ScreenCopyId");
Expand All @@ -234,24 +243,26 @@ public void Capture()

s_EffectFactorId = Shader.PropertyToID("_EffectFactor");
s_ColorFactorId = Shader.PropertyToID("_ColorFactor");
s_BackBufferId = new RenderTargetIdentifier (-1);
s_CommandBuffer = new CommandBuffer();
}

// If size of generated result RT has changed, relese it.
int w, h;
GetDesamplingSize(m_DesamplingRate, out w, out h);
// If target texture exists, release result RT.
if (m_TargetTexture)
{
_rtToRelease = _rt;
_Release(ref _rt);
}
else
{
// If size of result RT has changed, release it.
int w, h;
GetDesamplingSize (m_DesamplingRate, out w, out h);
if (_rt && (_rt.width != w || _rt.height != h))
{
_rtToRelease = _rt;
_rt = null;
_Release (ref _rt);
}

// Generate result RT.
// Generate RT for result.
if (_rt == null)
{
_rt = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
Expand All @@ -266,124 +277,73 @@ public void Capture()

void SetupCommandBuffer()
{
if (_buffer != null)
{
return;
}

bool isOverlay = canvas.rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay;
int w, h;
GetDesamplingSize(m_DesamplingRate, out w, out h);

var rtId = new RenderTargetIdentifier(m_TargetTexture ? m_TargetTexture : _rt);

// Material for effect.
Material mat = effectMaterial;

_buffer = new CommandBuffer();
_buffer.name = mat ? mat.name : "noeffect";
if (_rt)
if(s_CommandBuffer == null)
{
_rt.name = _buffer.name;
s_CommandBuffer = new CommandBuffer ();
}

// Copy to temporary RT.
_buffer.GetTemporaryRT(s_CopyId, -1, -1, 0, m_FilterMode);

if (isOverlay)
s_CommandBuffer.name = mat ? mat.name : "noeffect";
if (_rt)
{
if (!s_renderedResult)
{
s_renderedResult = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false, false);
s_renderedResult.filterMode = FilterMode.Point;
}
else if (s_renderedResult.width != Screen.width || s_renderedResult.height != Screen.height)
{
s_renderedResult.Resize(Screen.width, Screen.height, TextureFormat.ARGB32, false);
}
_rt.name = s_CommandBuffer.name;
}

// [1] Capture from back buffer (back buffer -> copied screen).
int w, h;
GetDesamplingSize(DesamplingRate.None, out w, out h);
s_CommandBuffer.GetTemporaryRT(s_CopyId, w, h, 0, m_FilterMode);
#if UNITY_EDITOR
if (!Application.isPlaying)
{
RenderTexture.active = Resources.FindObjectsOfTypeAll<RenderTexture>().FirstOrDefault(x=>x.name == "GameView RT");
s_renderedResult.ReadPixels(new Rect(0, 0, s_renderedResult.width, s_renderedResult.height), 0, 0);
s_renderedResult.Apply(false, false);
RenderTexture.active = null;
}
s_CommandBuffer.Blit(Resources.FindObjectsOfTypeAll<RenderTexture>().FirstOrDefault(x => x.name == "GameView RT"), s_CopyId);
#else
s_CommandBuffer.Blit(s_BackBufferId, s_CopyId);
#endif

_buffer.Blit(new RenderTargetIdentifier(s_renderedResult), s_CopyId);
}
else
{
_buffer.Blit(BuiltinRenderTextureType.CurrentActive, s_CopyId);
}
// Set properties for effect.
s_CommandBuffer.SetGlobalVector(s_EffectFactorId, new Vector4(toneLevel, 0));
s_CommandBuffer.SetGlobalVector(s_ColorFactorId, new Vector4(effectColor.r, effectColor.g, effectColor.b, effectColor.a));

// Set properties.
_buffer.SetGlobalVector(s_EffectFactorId, new Vector4(toneLevel, 0));
_buffer.SetGlobalVector(s_ColorFactorId, new Vector4(effectColor.r, effectColor.g, effectColor.b, effectColor.a));
// [2] Apply base effect with reduction buffer (copied screen -> effect1).
GetDesamplingSize (m_ReductionRate, out w, out h);
s_CommandBuffer.GetTemporaryRT (s_EffectId1, w, h, 0, m_FilterMode);
s_CommandBuffer.Blit(s_CopyId, s_EffectId1, mat, 0);
s_CommandBuffer.ReleaseTemporaryRT(s_CopyId);

// Blit without effect.
if (!mat)
{
_buffer.Blit(s_CopyId, rtId);
_buffer.ReleaseTemporaryRT(s_CopyId);
}
// Blit with effect.
else
// Iterate blurring operation.
if (m_BlurMode != BlurMode.None)
{
GetDesamplingSize(m_ReductionRate, out w, out h);
_buffer.GetTemporaryRT(s_EffectId1, w, h, 0, m_FilterMode);

// Apply base effect (copied screen -> effect1).
_buffer.Blit(s_CopyId, s_EffectId1, mat, 0);
_buffer.ReleaseTemporaryRT(s_CopyId);

// Iterate the operation.
if (m_BlurMode != BlurMode.None)
s_CommandBuffer.GetTemporaryRT(s_EffectId2, w, h, 0, m_FilterMode);
for (int i = 0; i < m_BlurIterations; i++)
{
_buffer.GetTemporaryRT(s_EffectId2, w, h, 0, m_FilterMode);
for (int i = 0; i < m_BlurIterations; i++)
{
// Apply effect (effect1 -> effect2, or effect2 -> effect1).
_buffer.SetGlobalVector(s_EffectFactorId, new Vector4(blur, 0));
_buffer.Blit(s_EffectId1, s_EffectId2, mat, 1);
_buffer.SetGlobalVector(s_EffectFactorId, new Vector4(0, blur));
_buffer.Blit(s_EffectId2, s_EffectId1, mat, 1);
}
_buffer.ReleaseTemporaryRT(s_EffectId2);
// [3] Apply blurring with reduction buffer (effect1 -> effect2, or effect2 -> effect1).
s_CommandBuffer.SetGlobalVector(s_EffectFactorId, new Vector4(blur, 0));
s_CommandBuffer.Blit(s_EffectId1, s_EffectId2, mat, 1);
s_CommandBuffer.SetGlobalVector(s_EffectFactorId, new Vector4(0, blur));
s_CommandBuffer.Blit(s_EffectId2, s_EffectId1, mat, 1);
}

_buffer.Blit(s_EffectId1, rtId);
_buffer.ReleaseTemporaryRT(s_EffectId1);
s_CommandBuffer.ReleaseTemporaryRT(s_EffectId2);
}

// [4] Copy to result RT.
s_CommandBuffer.Blit(s_EffectId1, new RenderTargetIdentifier (capturedTexture));
s_CommandBuffer.ReleaseTemporaryRT(s_EffectId1);

#if UNITY_EDITOR
// Start rendering for editor mode.
if (!Application.isPlaying)
if(!Application.isPlaying)
{
_cameraEvent = CameraEvent.AfterEverything;
_camera.AddCommandBuffer(_cameraEvent, _buffer);
texture = null;
_SetDirty();
_SetDirty ();
Graphics.ExecuteCommandBuffer (s_CommandBuffer);

UnityEditor.EditorApplication.delayCall += () =>
{
_Release(false);
texture = capturedTexture;
_SetDirty();
};
_Release(false);
texture = capturedTexture;
_SetDirty ();
return;
}
#endif

// Start rendering coroutine by CanvasScaler.
var scaler = canvas.rootCanvas.GetComponent<CanvasScaler>();
scaler.StartCoroutine(
isOverlay
? _CoUpdateTextureOnNextFrameOverlay()
: _CoUpdateTextureOnNextFrame()
);
// Execute command buffer.
canvas.rootCanvas.GetComponent<CanvasScaler> ().StartCoroutine(_CoUpdateTextureOnNextFrame ());
}

/// <summary>
Expand Down Expand Up @@ -457,18 +417,15 @@ protected void UpdateMaterial(bool ignoreInPlayMode)
//################################
// Private Members.
//################################
CameraEvent _cameraEvent = CameraEvent.AfterEverything;
Camera _camera;
RenderTexture _rt;
RenderTexture _rtToRelease;
CommandBuffer _buffer;

public Texture2D s_renderedResult;
static int s_CopyId;
static int s_EffectId1;
static int s_EffectId2;
static int s_EffectFactorId;
static int s_ColorFactorId;
static CommandBuffer s_CommandBuffer;
static RenderTargetIdentifier s_BackBufferId;

/// <summary>
/// Release genarated objects.
Expand All @@ -482,22 +439,23 @@ void _Release(bool releaseRT)
_Release(ref _rt);
}

if (_buffer != null)
if (s_CommandBuffer != null)
{
if (_camera != null)
_camera.RemoveCommandBuffer(_cameraEvent, _buffer);
_buffer.Release();
_buffer = null;
}
s_CommandBuffer.Clear();

_Release(ref _rtToRelease);
if(releaseRT)
{
s_CommandBuffer.Release ();
s_CommandBuffer = null;
}
}
}

[System.Diagnostics.Conditional("UNITY_EDITOR")]
void _SetDirty()
{
#if UNITY_EDITOR
if(Application.isPlaying)
if(!Application.isPlaying)
{
UnityEditor.EditorUtility.SetDirty(this);
}
Expand All @@ -522,33 +480,16 @@ void _Release(ref RenderTexture obj)
/// <summary>
/// Set texture on next frame.
/// </summary>
IEnumerator _CoUpdateTextureOnNextFrame()
IEnumerator _CoUpdateTextureOnNextFrame ()
{
// Add command buffer to camera.
_cameraEvent = CameraEvent.AfterEverything;
_camera.AddCommandBuffer(_cameraEvent, _buffer);
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame ();

_Release(false);
texture = m_TargetTexture ? m_TargetTexture : _rt;
}
// Execute command buffer.
Graphics.ExecuteCommandBuffer (s_CommandBuffer);

/// <summary>
/// Set texture on next frame (for overlay).
/// </summary>
IEnumerator _CoUpdateTextureOnNextFrameOverlay()
{
// Add command buffer to camera.
yield return new WaitForEndOfFrame();
s_renderedResult.ReadPixels(new Rect(0, 0, s_renderedResult.width, s_renderedResult.height), 0, 0);
s_renderedResult.Apply(false, false);

_cameraEvent = CameraEvent.BeforeForwardOpaque;
_camera.AddCommandBuffer(_cameraEvent, _buffer);
texture = m_TargetTexture ? m_TargetTexture : _rt;

yield return new WaitForEndOfFrame();
_Release(false);
_Release (false);
texture = capturedTexture;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Shader "UI/Hidden/UI-EffectCapture"
fixed4 frag(v2f_img IN) : SV_Target
{
half effectFactor = _EffectFactor.x;
half blurFactor = _EffectFactor.z;
fixed4 colorFactor = _ColorFactor;

#if PIXEL
Expand Down

0 comments on commit bab2d98

Please sign in to comment.