2023-01-22 16:38:23 +01:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
|
[AddComponentMenu("ChilloutVR/CVR Blitter")]
|
|
|
|
|
[HelpURL("https://developers.abinteractive.net/cck/components/blitter/")]
|
2023-01-22 16:38:23 +01:00
|
|
|
|
public class CVRBlitter : MonoBehaviour
|
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
|
public RenderTexture originTexture;
|
|
|
|
|
public RenderTexture destinationTexture;
|
|
|
|
|
public Material blitMaterial;
|
|
|
|
|
|
2023-01-22 16:38:23 +01:00
|
|
|
|
public bool clearEveryFrame;
|
2024-08-03 22:24:42 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|