// Search the texture list and create a receiver when found one. void SearchAndCreateTexture() { var name = PluginEntry.SearchSharedObjectNameString(_nameFilter); if (name != null) { _receiver = PluginEntry.CreateReceiver(name); } }
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); } }