Update MaskCamera.cs

- RT now gets generated at runtime.
- Added RT release on destroy.
- Fixed material property assignment.
- Changed naming.
This commit is contained in:
max 2021-03-04 13:39:11 +01:00
parent 1d177a7cf9
commit e94f11424f
2 changed files with 93 additions and 18 deletions

View File

@ -6,41 +6,79 @@ namespace TAO.InteractiveMask
[RequireComponent(typeof(MaskRenderer))] [RequireComponent(typeof(MaskRenderer))]
public class MaskCamera : MonoBehaviour public class MaskCamera : MonoBehaviour
{ {
public Mode mode = Mode.EachFrame; public Mode mode = Mode.Auto;
[SerializeField] [SerializeField]
private Camera maskCamera = null; private Camera maskCamera = null;
private MaskRenderer maskRenderer = null; private MaskRenderer maskRenderer = null;
public RenderTexture target = null; [SerializeField]
private int resolution = 512;
[SerializeField]
private RenderTextureFormat format = RenderTextureFormat.Default;
public RenderTexture Target
{
get; private set;
}
private RenderTexture source = null; private RenderTexture source = null;
public List<Material> materials = new List<Material>(); public List<Material> materials = new List<Material>();
private int targetWidth = 0;
public bool debugGui = false; public bool debugGui = false;
private void Awake() private void Awake()
{ {
maskRenderer = GetComponent<MaskRenderer>(); Target = CreateRenderTexture(resolution, format);
source = new RenderTexture(Target);
source = new RenderTexture(target);
maskCamera.targetTexture = source; maskCamera.targetTexture = source;
maskRenderer.target = target; switch (mode)
{
case Mode.Auto:
{
maskCamera.enabled = true;
}
break;
case Mode.Manual:
{
maskCamera.enabled = false;
}
break;
default:
break;
}
maskRenderer = GetComponent<MaskRenderer>();
maskRenderer.target = Target;
maskRenderer.source = source; maskRenderer.source = source;
targetWidth = target.width;
maskRenderer.Init(); maskRenderer.Init();
foreach (var m in materials)
{
m.SetTexture("_Mask", Target);
}
} }
private void Update() private void OnDestroy()
{ {
if (mode == Mode.EachFrame) Target.Release();
source.Release();
maskRenderer.Release();
}
private void LateUpdate()
{
switch (mode)
{ {
Render(); case Mode.Auto:
{
Render();
}
break;
case Mode.Manual:
break;
default:
break;
} }
} }
@ -48,7 +86,19 @@ namespace TAO.InteractiveMask
{ {
SnapCameraPosition(); SnapCameraPosition();
maskCamera.Render(); switch (mode)
{
case Mode.Auto:
break;
case Mode.Manual:
{
maskCamera.Render();
}
break;
default:
break;
}
maskRenderer.Render(); maskRenderer.Render();
foreach (var m in materials) foreach (var m in materials)
@ -59,7 +109,7 @@ namespace TAO.InteractiveMask
private void SnapCameraPosition() private void SnapCameraPosition()
{ {
float pws = (1.0f / targetWidth) * maskCamera.orthographicSize; float pws = (1.0f / resolution) * maskCamera.orthographicSize;
// Snap position to pixel. // Snap position to pixel.
Vector3 newPos = Vector3.zero; Vector3 newPos = Vector3.zero;
@ -69,6 +119,14 @@ namespace TAO.InteractiveMask
maskCamera.transform.position = newPos; maskCamera.transform.position = newPos;
} }
public static RenderTexture CreateRenderTexture(int resolution, RenderTextureFormat format)
{
var rt = new RenderTexture(resolution, resolution, 0, format, 0);
rt.Create();
return rt;
}
private void OnGUI() private void OnGUI()
{ {
if (debugGui) if (debugGui)
@ -95,7 +153,7 @@ namespace TAO.InteractiveMask
public enum Mode public enum Mode
{ {
EachFrame, Auto,
Manual Manual
} }
} }

View File

@ -1,3 +1,4 @@
using System;
using UnityEngine; using UnityEngine;
namespace TAO.InteractiveMask namespace TAO.InteractiveMask
@ -70,6 +71,14 @@ namespace TAO.InteractiveMask
} }
} }
public void Release()
{
foreach (Layer layer in layers)
{
layer.Release();
}
}
public void GUI() public void GUI()
{ {
using (new GUILayout.HorizontalScope()) using (new GUILayout.HorizontalScope())
@ -100,7 +109,7 @@ namespace TAO.InteractiveMask
using (new GUILayout.VerticalScope()) using (new GUILayout.VerticalScope())
{ {
GUILayout.Label(texture, GUILayout.Width(256), GUILayout.Height(256)); GUILayout.Label(texture, GUILayout.Width(256), GUILayout.Height(256));
GUILayout.Label(string.Format("{0}\n{1}x{2}\n{3}", texture.name, texture.width, texture.height, texture.graphicsFormat)); GUILayout.Label(string.Format("{0}\n{1}x{2}\n{3}\n{4}", texture.name, texture.width, texture.height, texture.graphicsFormat, target.format));
} }
} }
} }
@ -185,6 +194,14 @@ namespace TAO.InteractiveMask
} }
} }
public void Release()
{
if (PersistentTarget)
{
PersistentTarget.Release();
}
}
public enum Type public enum Type
{ {
Clear, Clear,