mirror of
https://github.com/maxartz15/InteractiveMask.git
synced 2024-11-22 13:35:39 +01:00
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:
parent
1d177a7cf9
commit
e94f11424f
@ -6,49 +6,99 @@ 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)
|
||||||
maskRenderer.source = source;
|
{
|
||||||
|
case Mode.Auto:
|
||||||
targetWidth = target.width;
|
{
|
||||||
|
maskCamera.enabled = true;
|
||||||
maskRenderer.Init();
|
}
|
||||||
|
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();
|
Render();
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case Mode.Manual:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Render()
|
public void Render()
|
||||||
{
|
{
|
||||||
SnapCameraPosition();
|
SnapCameraPosition();
|
||||||
|
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case Mode.Auto:
|
||||||
|
break;
|
||||||
|
case Mode.Manual:
|
||||||
|
{
|
||||||
maskCamera.Render();
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user