void OnGUI()
        {
            var count = PluginEntry.CountSharedObjects();

            EditorGUILayout.Space();
            EditorGUI.indentLevel++;

            if (count == 0)
            {
                EditorGUILayout.LabelField("No sender detected.");
            }
            else
            {
                EditorGUILayout.LabelField(count + " sender(s) detected.");
            }

            for (var i = 0; i < count; i++)
            {
                var name = PluginEntry.GetSharedObjectNameString(i);
                if (name != null)
                {
                    EditorGUILayout.LabelField("- " + name);
                }
            }

            EditorGUI.indentLevel--;
        }
        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);
                    }
                }
            }
        }
示例#3
0
        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);
            }
        }
        // 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);
            }
        }
        // Scan available Spout sources and store their names into the given
        // collection object.
        public static void GetSourceNames(ICollection <string> store)
        {
            store.Clear();
            var count = PluginEntry.ScanSharedObjects();

            for (var i = 0; i < count; i++)
            {
                store.Add(PluginEntry.GetSharedObjectNameString(i));
            }
        }
示例#6
0
        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);
            }
        }
        // Scan available Spout sources and return their names via a newly
        // allocated string array.
        public static string[] GetSourceNames()
        {
            var count = PluginEntry.ScanSharedObjects();
            var names = new string [count];

            for (var i = 0; i < count; i++)
            {
                names[i] = PluginEntry.GetSharedObjectNameString(i);
            }
            return(names);
        }
示例#8
0
        // Create and show the source name dropdown.
        void ShowSourceNameDropdown(Rect rect)
        {
            var menu  = new GenericMenu();
            var count = PluginEntry.ScanSharedObjects();

            for (var i = 0; i < count; i++)
            {
                var name = PluginEntry.GetSharedObjectNameString(i);
                menu.AddItem(new GUIContent(name), false, OnSelectSource, name);
            }
            menu.DropDown(rect);
        }
示例#9
0
        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);
            }
        }
示例#10
0
        void OnEnable()
        {
            _camera = GetComponent <Camera>();

            if (_resolution.x == 0)
            {
                _resolution.x = Screen.width;
            }
            if (_resolution.y == 0)
            {
                _resolution.y = Screen.height;
            }

            _sender = PluginEntry.CreateSender(name, _resolution.x, _resolution.y);
        }
示例#11
0
        IssuePluginEvent(PluginEntry.Event pluginEvent, System.IntPtr ptr)
        {
            if (_commandBuffer == null)
            {
                _commandBuffer = new CommandBuffer();
            }

            _commandBuffer.IssuePluginEventAndData(
                PluginEntry.GetRenderEventFunc(), (int)pluginEvent, ptr
                );

            Graphics.ExecuteCommandBuffer(_commandBuffer);

            _commandBuffer.Clear();
        }
        void OnDestroy()
        {
            if (_receiver != System.IntPtr.Zero)
            {
                PluginEntry.DestroySharedObject(_receiver);
                _receiver = System.IntPtr.Zero;
            }

            if (_sharedTexture != null)
            {
                Destroy(_sharedTexture);
                _sharedTexture = null;
            }

            if (_fixedTexture != null)
            {
                Destroy(_fixedTexture);
                _fixedTexture = null;
            }
        }
示例#13
0
        void OnDisable()
        {
            if (_sender != System.IntPtr.Zero)
            {
                PluginEntry.DestroySharedObject(_sender);
                _sender = System.IntPtr.Zero;
            }

            if (_sharedTexture != null)
            {
                if (Application.isPlaying)
                {
                    Destroy(_sharedTexture);
                }
                else
                {
                    DestroyImmediate(_sharedTexture);
                }
                _sharedTexture = null;
            }
        }
        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);
            }
        }
示例#15
0
 void Update()
 {
     PluginEntry.Poll();
 }
示例#16
0
        void OnEnable()
        {
            var camera = GetComponent <Camera>();

            _sender = PluginEntry.CreateSender(name, camera.pixelWidth, camera.pixelHeight);
        }