Example #1
0
        void Update()
        {
            if (!PluginEntry.IsAvailable)
            {
                return;
            }

            // Plugin lazy initialization
            if (_plugin == System.IntPtr.Zero)
            {
                _plugin = PluginEntry.CreateReceiver(_sourceName, _dataFormat);
                if (_plugin == System.IntPtr.Zero)
                {
                    return;                                // No receiver support
                }
            }

            // Texture update event invocation with lazy initialization
            if (_callback == System.IntPtr.Zero)
            {
                _callback = PluginEntry.GetTextureUpdateCallback();
            }

            if (_sourceTexture == null)
            {
                _sourceTexture           = new Texture2D(8, 8); // Placeholder
                _sourceTexture.hideFlags = HideFlags.DontSave;
            }

            Util.IssueTextureUpdateEvent
                (_callback, _sourceTexture, PluginEntry.GetReceiverID(_plugin));

            // Texture information retrieval
            var width       = PluginEntry.GetFrameWidth(_plugin);
            var height      = PluginEntry.GetFrameHeight(_plugin);
            var frameFormat = PluginEntry.GetFrameFourCC(_plugin);

            this.recievedResolution.x = width;
            this.recievedResolution.y = height;
            this.recievedFormat       = frameFormat;

            if (width == 0 || height == 0)
            {
                return;                            // Not yet ready
            }
            // Source data dimensions
            var sw = width;
            var sh = height;

            var isRGBA = true;
            var alpha  = true;

            switch (frameFormat)
            {
            case FourCC.UYVA:
            case FourCC.UYVY:
            {
                alpha  = frameFormat == FourCC.UYVA;
                sw     = width / 2;
                sh     = height * (alpha ? 3 : 2) / 2;
                isRGBA = false;
            }
            break;
            }

            // Renew the textures when the dimensions are changed.
            if (_sourceTexture.width != sw || _sourceTexture.height != sh)
            {
                Util.Destroy(_sourceTexture);
                Util.Destroy(_receivedTexture);
                _sourceTexture           = new Texture2D(sw, sh, TextureFormat.ARGB32, false, true);
                _sourceTexture.hideFlags = HideFlags.DontSave;

                _sourceTexture.filterMode = FilterMode.Point;
                _sourceTexture.anisoLevel = 0;
            }

            // Blit shader lazy initialization
            if (_blitMaterial == null)
            {
                _blitMaterial           = new Material(Shader.Find("Hidden/KlakNDI/Receiver"));
                _blitMaterial.hideFlags = HideFlags.DontSave;
            }

            // Receiver texture lazy initialization
            if (_targetTexture == null && _receivedTexture == null)
            {
                _receivedTexture           = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                _receivedTexture.hideFlags = HideFlags.DontSave;

                _receivedTexture.useMipMap        = false;
                _receivedTexture.filterMode       = FilterMode.Point;
                _receivedTexture.anisoLevel       = 0;
                _receivedTexture.antiAliasing     = 1;
                _receivedTexture.autoGenerateMips = false;
                _receivedTexture.Create();
            }

            // Texture format conversion using the blit shader
            var receiver = _targetTexture != null ? _targetTexture : _receivedTexture;

            if (isRGBA)
            {
                if (_invertY)
                {
                    Graphics.Blit(_sourceTexture, receiver, _blitMaterial, 2);//pass 2 is invert y pass
                }
                else
                {
                    Graphics.Blit(_sourceTexture, receiver);
                }
            }
            else
            {
                Graphics.Blit(_sourceTexture, receiver, _blitMaterial, alpha ? 1 : 0);
            }
            receiver.IncrementUpdateCount();

            // Renderer override
            if (_targetRenderer != null)
            {
                // Material property block lazy initialization
                if (_propertyBlock == null)
                {
                    _propertyBlock = new MaterialPropertyBlock();
                }

                // Read-modify-write
                _targetRenderer.GetPropertyBlock(_propertyBlock);
                _propertyBlock.SetTexture(_targetMaterialProperty, receiver);
                _targetRenderer.SetPropertyBlock(_propertyBlock);
            }
        }
Example #2
0
        void Update()
        {
            if (!PluginEntry.IsAvailable)
            {
                return;
            }

            _dataTime  = 1.0f / _FPS;
            _dataTime2 = 1.0f / _FPS2;


            // Plugin lazy initialization
            if (_plugin == System.IntPtr.Zero)
            {
                _plugin = PluginEntry.CreateReceiver(_sourceName);
                if (_plugin == System.IntPtr.Zero)
                {
                    return;                                // No receiver support
                }
            }

            // Texture update event invocation with lazy initialization
            if (_callback == System.IntPtr.Zero)
            {
                _callback = PluginEntry.GetTextureUpdateCallback();
            }

            if (_sourceTexture == null)
            {
                _sourceTexture           = new Texture2D(8, 8); // Placeholder
                _sourceTexture.hideFlags = HideFlags.DontSave;
            }

            Util.IssueTextureUpdateEvent
                (_callback, _sourceTexture, PluginEntry.GetReceiverID(_plugin));

            // Texture information retrieval
            var width  = PluginEntry.GetFrameWidth(_plugin);
            var height = PluginEntry.GetFrameHeight(_plugin);

            if (width == 0 || height == 0)
            {
                return;                            // Not yet ready
            }
            // Source data dimensions
            var alpha = PluginEntry.GetFrameFourCC(_plugin) == FourCC.UYVA;
            var sw    = width / 2;
            var sh    = height * (alpha ? 3 : 2) / 2;

            // Renew the textures when the dimensions are changed.
            if (_sourceTexture.width != sw || _sourceTexture.height != sh)
            {
                Util.Destroy(_sourceTexture);
                Util.Destroy(_receivedTexture);
                _sourceTexture            = new Texture2D(sw, sh, TextureFormat.RGBA32, false, true);
                _sourceTexture.hideFlags  = HideFlags.DontSave;
                _sourceTexture.filterMode = FilterMode.Point;
            }

            // Receiver texture lazy initialization
            if (_targetTextureOne == null && _receivedTexture == null)
            {
                _receivedTexture           = new RenderTexture(width, height, 0);
                _receivedTexture.hideFlags = HideFlags.DontSave;
            }

            if (_backTexture == null)
            {
                _backTexture           = new RenderTexture(width, height, 0);
                _backTexture.hideFlags = HideFlags.DontSave;
            }
            if (_frontTexture == null)
            {
                _frontTexture           = new RenderTexture(width, height, 0);
                _frontTexture.hideFlags = HideFlags.DontSave;
            }
            if (_blendMaterial == null)
            {
                _blendMaterial           = new Material(Shader.Find("Hidden/Marrow/BlendReceiver"));
                _blendMaterial.hideFlags = HideFlags.DontSave;
                _blendMaterial.SetTexture("_BackTex", _backTexture);
            }

            _timer  += Time.deltaTime;
            _timer2 += Time.deltaTime;

            if (_timer > _dataTime)
            {
                _showBack = !_showBack;
                _timer    = 0.0f;
                // Debug.Log("Blit!!");
                if (_showBack)
                {
                    Graphics.Blit(_sourceTexture, _frontTexture, _blendMaterial, 0);
                }
                else
                {
                    Graphics.Blit(_sourceTexture, _backTexture, _blendMaterial, 0);
                }
            }
            if (!_showBack)
            {
                _blendFactor = _timer / _dataTime;
            }
            else
            {
                _blendFactor = 1 - (_timer / _dataTime);
            }
            _blendMaterial.SetFloat("_BlendFactor", _blendFactor);
            Graphics.Blit(_frontTexture, _targetTextureOne, _blendMaterial, 1);
            Graphics.Blit(_frontTexture, _targetTextureThree, _blendMaterial, 3);
            if (_timer2 > _dataTime2)
            {
                _timer2 = 0;
                Graphics.Blit(_sourceTexture, _targetTextureTwo, _blendMaterial, 2);
                _targetTextureTwo.IncrementUpdateCount();
            }

            // Texture format conversion using the blit shader
            _targetTextureOne.IncrementUpdateCount();
            _targetTextureThree.IncrementUpdateCount();


            // Renderer override
            if (_targetRenderer != null)
            {
                // Material property block lazy initialization
                if (_propertyBlock == null)
                {
                    _propertyBlock = new MaterialPropertyBlock();
                }

                // Read-modify-write
                _targetRenderer.GetPropertyBlock(_propertyBlock);
                _propertyBlock.SetTexture(_targetMaterialProperty, _targetTextureOne);
                _propertyBlock.SetTexture(_targetMaterialProperty, _targetTextureTwo);
                _propertyBlock.SetTexture(_targetMaterialProperty, _targetTextureThree);
                _targetRenderer.SetPropertyBlock(_propertyBlock);
            }
        }
Example #3
0
        void Update()
        {
            if (!PluginEntry.IsAvailable)
            {
                return;
            }

            // Plugin lazy initialization
            if (_plugin == System.IntPtr.Zero)
            {
                _plugin = PluginEntry.CreateReceiver(_sourceName);
                if (_plugin == System.IntPtr.Zero)
                {
                    return;                                // No receiver support
                }
            }

            // Texture update event invocation with lazy initialization
            if (_callback == System.IntPtr.Zero)
            {
                _callback = PluginEntry.GetTextureUpdateCallback();
            }

            if (_sourceTexture == null)
            {
                _sourceTexture           = new Texture2D(8, 8); // Placeholder
                _sourceTexture.hideFlags = HideFlags.DontSave;
            }

            Util.IssueTextureUpdateEvent
                (_callback, _sourceTexture, PluginEntry.GetReceiverID(_plugin));

            // Texture information retrieval
            var width  = PluginEntry.GetFrameWidth(_plugin);
            var height = PluginEntry.GetFrameHeight(_plugin);

            if (width == 0 || height == 0)
            {
                return;                            // Not yet ready
            }
            // Source data dimensions
            var alpha = PluginEntry.GetFrameFourCC(_plugin) == FourCC.UYVA;
            var sw    = width / 2;
            var sh    = height * (alpha ? 3 : 2) / 2;

            // Renew the textures when the dimensions are changed.
            if (_sourceTexture.width != sw || _sourceTexture.height != sh)
            {
                Util.Destroy(_sourceTexture);
                Util.Destroy(_receivedTexture);
                _sourceTexture            = new Texture2D(sw, sh, TextureFormat.RGBA32, false, true);
                _sourceTexture.hideFlags  = HideFlags.DontSave;
                _sourceTexture.filterMode = FilterMode.Point;
            }

            // Blit shader lazy initialization
            if (_blitMaterial == null)
            {
                _blitMaterial           = new Material(Shader.Find("Hidden/KlakNDI/ReceiverOneHalf"));
                _blitMaterial.hideFlags = HideFlags.DontSave;
            }
            if (_blitMaterialTwo == null)
            {
                _blitMaterialTwo           = new Material(Shader.Find("Hidden/KlakNDI/ReceiverSecondHalf"));
                _blitMaterialTwo.hideFlags = HideFlags.DontSave;
            }

            // Receiver texture lazy initialization
            if (_targetTexture == null && _receivedTexture == null)
            {
                _receivedTexture           = new RenderTexture(width, height, 0);
                _receivedTexture.hideFlags = HideFlags.DontSave;
            }

            // Texture format conversion using the blit shader
            var receiver    = _targetTexture != null ? _targetTexture : _receivedTexture;
            var receiverTwo = _targetTextureTwo != null ? _targetTextureTwo : _receivedTexture;

            Graphics.Blit(_sourceTexture, receiver, _blitMaterial, alpha ? 1 : 0);
            Graphics.Blit(_sourceTexture, receiverTwo, _blitMaterialTwo, alpha ? 1 : 0);

            receiver.IncrementUpdateCount();

            // Renderer override
            if (_targetRenderer != null)
            {
                // Material property block lazy initialization
                if (_propertyBlock == null)
                {
                    _propertyBlock = new MaterialPropertyBlock();
                }

                // Read-modify-write
                _targetRenderer.GetPropertyBlock(_propertyBlock);
                _propertyBlock.SetTexture(_targetMaterialProperty, receiver);
                _targetRenderer.SetPropertyBlock(_propertyBlock);
            }
        }