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

View File

@ -1,3 +1,4 @@
using System;
using UnityEngine;
namespace TAO.InteractiveMask
@ -70,6 +71,14 @@ namespace TAO.InteractiveMask
}
}
public void Release()
{
foreach (Layer layer in layers)
{
layer.Release();
}
}
public void GUI()
{
using (new GUILayout.HorizontalScope())
@ -100,7 +109,7 @@ namespace TAO.InteractiveMask
using (new GUILayout.VerticalScope())
{
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
{
Clear,