void Update() { PluginEntry.Poll(); if (_receiver == System.IntPtr.Zero) { // The receiver hasn't been set up yet; try to get one. SearchAndCreateTexture(); } else { // We've received textures via this receiver // but now it's disconnected from the sender -> Destroy it. if (PluginEntry.GetTexturePointer(_receiver) != System.IntPtr.Zero && PluginEntry.DetectDisconnection(_receiver)) { OnDestroy(); } } if (_receiver != System.IntPtr.Zero) { if (_sharedTexture == null) { // Try to initialize the shared texture. var ptr = PluginEntry.GetTexturePointer(_receiver); if (ptr != System.IntPtr.Zero) { _sharedTexture = Texture2D.CreateExternalTexture( PluginEntry.GetTextureWidth(_receiver), PluginEntry.GetTextureHeight(_receiver), TextureFormat.ARGB32, false, false, ptr ); } } else { // Update external objects. if (_targetTexture != null) { Graphics.Blit(_sharedTexture, _targetTexture, _fixupMaterial, 1); } else { if (_fixedTexture == null) { _fixedTexture = new RenderTexture(_sharedTexture.width, _sharedTexture.height, 0); } Graphics.Blit(_sharedTexture, _fixedTexture, _fixupMaterial, 1); } if (_targetRenderer != null) { _propertyBlock.SetTexture(_targetMaterialProperty, receivedTexture); _targetRenderer.SetPropertyBlock(_propertyBlock); } } } }
void SendRenderTexture(RenderTexture source) { if (currentSenderName != senderName) { DisposePlugin(); } // Plugin lazy initialization if (_plugin == System.IntPtr.Zero) { _plugin = PluginEntry.CreateSender(senderName, source.width, source.height); currentSenderName = senderName; if (_plugin == System.IntPtr.Zero) { return; // Spout may not be ready. } } // Shared texture lazy initialization if (_sharedTexture == null) { var ptr = PluginEntry.GetTexturePointer(_plugin); if (ptr != System.IntPtr.Zero) { _sharedTexture = Texture2D.CreateExternalTexture( PluginEntry.GetTextureWidth(_plugin), PluginEntry.GetTextureHeight(_plugin), TextureFormat.ARGB32, false, false, ptr ); _sharedTexture.hideFlags = HideFlags.DontSave; } } // Shared texture update if (_sharedTexture != null) { // Blit shader lazy initialization if (_blitMaterial == null) { _blitMaterial = new Material(Shader.Find("Hidden/Spout/Blit")); _blitMaterial.hideFlags = HideFlags.DontSave; } // Blit shader parameters _blitMaterial.SetFloat("_ClearAlpha", _alphaSupport ? 0 : 1); // We can't directly blit to the shared texture (as it lacks // render buffer functionality), so we temporarily allocate a // render texture as a middleman, blit the source to it, then // copy it to the shared texture using the CopyTexture API. var tempRT = RenderTexture.GetTemporary (_sharedTexture.width, _sharedTexture.height); Graphics.Blit(source, tempRT, _blitMaterial, 0); Graphics.CopyTexture(tempRT, _sharedTexture); RenderTexture.ReleaseTemporary(tempRT); } }
void SendRenderTexture(RenderTexture source) { // Plugin lazy initialization if (_plugin == System.IntPtr.Zero) { _plugin = PluginEntry.CreateSender(name, source.width, source.height); if (_plugin == System.IntPtr.Zero) { return; // Spout may not be ready. } } // Shared texture lazy initialization //TextureFormat format = TextureFormat.ARGB32; // TABULA: original KlakSpout, DX11 only TextureFormat format = TextureFormat.BGRA32; // TABULA: FaçadeSignage compatibilty, DX9/DX11 if (_sharedTexture == null) { var ptr = PluginEntry.GetTexturePointer(_plugin); if (ptr != System.IntPtr.Zero) { _sharedTexture = Texture2D.CreateExternalTexture( PluginEntry.GetTextureWidth(_plugin), PluginEntry.GetTextureHeight(_plugin), format, false, false, ptr ); _sharedTexture.hideFlags = HideFlags.DontSave; } } // Shared texture update if (_sharedTexture != null) { // Blit shader lazy initialization if (_blitMaterial == null) { _blitMaterial = new Material(Shader.Find("Hidden/Spout/Blit")); _blitMaterial.hideFlags = HideFlags.DontSave; } // Blit shader parameters _blitMaterial.SetFloat("_ClearAlpha", _alphaSupport ? 0 : 1); // We can't directly blit to the shared texture (as it lacks // render buffer functionality), so we temporarily allocate a // render texture as a middleman, blit the source to it, then // copy it to the shared texture using the CopyTexture API. var tempRT = RenderTexture.GetTemporary (_sharedTexture.width, _sharedTexture.height, 0, RenderTextureFormat.BGRA32); // tabula: using BGRA32 also for temp Graphics.Blit(source, tempRT, _blitMaterial, 0); Graphics.CopyTexture(tempRT, _sharedTexture); RenderTexture.ReleaseTemporary(tempRT); } }
void OnRenderImage(RenderTexture source, RenderTexture destination) { // Lazy initialization for the shared texture. if (_sharedTexture == null) { var ptr = PluginEntry.GetTexturePointer(_sender); if (ptr != System.IntPtr.Zero) { _sharedTexture = Texture2D.CreateExternalTexture( PluginEntry.GetTextureWidth(_sender), PluginEntry.GetTextureHeight(_sender), TextureFormat.ARGB32, false, false, ptr ); } } // Update the shared texture. if (_sharedTexture != null) { // Lazy initialization for the fix-up shader. if (_fixupMaterial == null) { _fixupMaterial = new Material(Shader.Find("Hidden/Spout/Fixup")); } // Parameters for the fix-up shader. _fixupMaterial.SetFloat("_ClearAlpha", _clearAlpha ? 1 : 0); // Apply the fix-up shader. var tempRT = RenderTexture.GetTemporary(_sharedTexture.width, _sharedTexture.height); Graphics.Blit(source, tempRT, _fixupMaterial, 0); // Copy the result to the shared texture. Graphics.CopyTexture(tempRT, _sharedTexture); // Release temporaries. RenderTexture.ReleaseTemporary(tempRT); } if (this.sendOnly) { // Clear previous image to render GUI. RenderTexture prev = RenderTexture.active; RenderTexture.active = null; GL.Clear(true, true, _camera.backgroundColor); RenderTexture.active = prev; } else { // Just transfer the source to the destination. Graphics.Blit(source, destination); } }
void Update() { // Release the plugin instance when the previously established // connection is now invalid. if (_plugin != System.IntPtr.Zero && !PluginEntry.CheckValid(_plugin)) { Util.IssuePluginEvent(PluginEntry.Event.Dispose, _plugin); _plugin = System.IntPtr.Zero; } // Plugin lazy initialization if (_plugin == System.IntPtr.Zero) { _plugin = PluginEntry.CreateReceiver(_sourceName); if (_plugin == System.IntPtr.Zero) { return; // Spout may not be ready. } } Util.IssuePluginEvent(PluginEntry.Event.Update, _plugin); // Texture information retrieval var ptr = PluginEntry.GetTexturePointer(_plugin); var width = PluginEntry.GetTextureWidth(_plugin); var height = PluginEntry.GetTextureHeight(_plugin); // Resource validity check if (_sharedTexture != null) { if (ptr != _sharedTexture.GetNativeTexturePtr() || width != _sharedTexture.width || height != _sharedTexture.height) { // Not match: Destroy to get refreshed. Util.Destroy(_sharedTexture); } } // Shared texture lazy (re)initialization if (_sharedTexture == null && ptr != System.IntPtr.Zero) { _sharedTexture = Texture2D.CreateExternalTexture( width, height, TextureFormat.ARGB32, false, false, ptr ); _sharedTexture.hideFlags = HideFlags.DontSave; // Destroy the previously allocated receiver texture to // refresh specifications. if (_receivedTexture == null) { Util.Destroy(_receivedTexture); } } // Texture format conversion with the blit shader if (_sharedTexture != null) { // Blit shader lazy initialization if (_blitMaterial == null) { _blitMaterial = new Material(Shader.Find("Hidden/Spout/Blit")); _blitMaterial.hideFlags = HideFlags.DontSave; } if (_targetTexture != null) { // Blit the shared texture to the target texture. Graphics.Blit(_sharedTexture, _targetTexture, _blitMaterial, 1); } else { // Receiver texture lazy initialization if (_receivedTexture == null) { _receivedTexture = new RenderTexture (_sharedTexture.width, _sharedTexture.height, 0); _receivedTexture.hideFlags = HideFlags.DontSave; } // Blit the shared texture to the receiver texture. Graphics.Blit(_sharedTexture, _receivedTexture, _blitMaterial, 1); } } // Renderer override if (_targetRenderer != null && receivedTexture != null) { // Material property block lazy initialization if (_propertyBlock == null) { _propertyBlock = new MaterialPropertyBlock(); } // Read-modify-write _targetRenderer.GetPropertyBlock(_propertyBlock); _propertyBlock.SetTexture(_targetMaterialProperty, receivedTexture); _targetRenderer.SetPropertyBlock(_propertyBlock); } }