cvr-props/Assets/ABI.CCK/Components/CVRBlitter.cs

68 lines
1.8 KiB
C#
Raw Normal View History

2023-01-22 16:38:23 +01:00
using UnityEngine;
[AddComponentMenu("ChilloutVR/CVR Blitter")]
[HelpURL("https://developers.abinteractive.net/cck/components/blitter/")]
2023-01-22 16:38:23 +01:00
public class CVRBlitter : MonoBehaviour
{
public RenderTexture originTexture;
public RenderTexture destinationTexture;
public Material blitMaterial;
2023-01-22 16:38:23 +01:00
public bool clearEveryFrame;
private bool _isInitialized;
private void Start()
{
_isInitialized = true;
OnEnable();
}
private void OnEnable()
{
if (!_isInitialized) return;
Camera.onPreRender += MyOnPreRender;
}
private void OnDisable()
{
Camera.onPreRender -= MyOnPreRender;
}
private void MyOnPreRender(Camera cam)
{
if (!enabled)
return;
// Don't blit unless it's the main camera
if (cam != Camera.main)
return;
if (originTexture == null
|| destinationTexture == null
|| blitMaterial == null)
{
enabled = false;
return;
}
if (clearEveryFrame)
{
RenderTexture rt = RenderTexture.active;
RenderTexture.active = destinationTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = rt;
}
if (originTexture == destinationTexture)
{
RenderTexture temp = RenderTexture.GetTemporary(destinationTexture.descriptor);
Graphics.Blit(originTexture, temp, blitMaterial);
Graphics.CopyTexture(temp, destinationTexture);
RenderTexture.ReleaseTemporary(temp);
return;
}
Graphics.Blit(originTexture, destinationTexture, blitMaterial);
}
}