You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.6 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UC;
using UnityEngine;
namespace Spout
{
public class ImageEffectSpoutSender : MonoBehaviour
{
public string sharingName = "UnitySender";
public Spout.TextureFormat textureFormat = Spout.TextureFormat.DXGI_FORMAT_R8G8B8A8_UNORM;
[AutoUI]
public bool yFlip = true;
[SerializeField]
[Header("Automatic")]
RenderTexture output;
private bool senderIsCreated;
private const int _createAttempts = 5;
private int _attempts = 0;
private bool textureSizeHasChanged = false;
[SerializeField]
private Shader m_Shader;
public Shader shader
{
get
{
if (m_Shader == null)
m_Shader = Shader.Find("Unlit/AlphaPremultiplied");
return m_Shader;
}
}
private Material m_Material;
public Material material
{
get
{
if (m_Material == null)
m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(shader);
return m_Material;
}
}
private void OnEnable()
{
if (!ImageEffectHelper.IsSupported(shader, false, false, this))
enabled = false;
}
void OnRenderImage(RenderTexture s, RenderTexture d)
{
bool isValid = (output != null) && s.width == output.width && s.height == output.height;
if (!isValid)
{
if (output != null)
output.Release();
output = new RenderTexture(s.width, s.height, s.depth, RenderTextureFormat.ARGB32);
output.Create();
}
textureSizeHasChanged = !isValid;
//if (s.format == output.format)
// Graphics.CopyTexture(s, output);
//else
material.SetInt("yFlip", yFlip?1:0);
Graphics.Blit(s, output, material);
Graphics.Blit(s, d);
}
void OnDisable()
{
_CloseSender();
if (m_Material != null)
DestroyImmediate(m_Material);
m_Material = null;
}
void Update()
{
if (output == null) return;
if (textureSizeHasChanged) _CloseSender();
if (senderIsCreated)
Spout.instance.UpdateSender(sharingName, output);
else
_CreateSender(output);
}
private void _CreateSender(Texture texture)
{
if (texture == null) return;
if (!Spout.instance.enabled) return;
if (!senderIsCreated)
{
Debug.Log("Sender is not created, creating one");
senderIsCreated = Spout.instance.CreateSender(sharingName, texture, (int)textureFormat);
}
_attempts++;
if (_attempts > _createAttempts)
Debug.LogWarning(System.String.Format("There are problems with creating the sender {0}. Please check your settings or restart Unity.", sharingName));
Spout.instance.OnSenderStopped -= OnSenderStoppedDelegate;
Spout.instance.OnSenderStopped += OnSenderStoppedDelegate;
Spout.instance.OnAllSendersStopped -= OnAllSendersStoppedDelegate;
Spout.instance.OnAllSendersStopped += OnAllSendersStoppedDelegate;
Spout.instance.OnEnabled -= _OnSpoutEnabled;
Spout.instance.OnEnabled += _OnSpoutEnabled;
}
private void _OnSpoutEnabled()
{
//Debug.Log("SpoutSender._OnSpoutEnabled");
if (enabled)
{
//force a reconnection
enabled = !enabled;
enabled = !enabled;
}
}
private void _CloseSender()
{
Debug.Log("SpoutSender._CloseSender:" + sharingName);
if (senderIsCreated) Spout.CloseSender(sharingName);
_CloseSenderCleanUpData();
}
private void OnSenderStoppedDelegate(object sender, TextureShareEventArgs e)
{
//Debug.Log("SpoutSender.OnSenderStoppedDelegate:"+e.sharingName);
if (e.sharingName == sharingName)
{
_CloseSenderCleanUpData();
}
}
private void OnAllSendersStoppedDelegate()
{
_CloseSenderCleanUpData();
}
private void _CloseSenderCleanUpData()
{
senderIsCreated = false;
}
}
}