Esempio n. 1
0
        // We do a LateUpdate() to allow for any changes in the texture that may have happened in Update()
        void LateUpdate()
        {
            if (_setNativeSize)
            {
                SetNativeSize();
            }

            if (_lastTexture != mainTexture)
            {
                _lastTexture = mainTexture;
                SetVerticesDirty();
                SetMaterialDirty();
            }

            if (HasValidTexture())
            {
                if (mainTexture != null)
                {
                    Orientation orientation = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
                    if (mainTexture.width != _lastWidth || mainTexture.height != _lastHeight || orientation != _lastOrientation)
                    {
                        _lastWidth       = mainTexture.width;
                        _lastHeight      = mainTexture.height;
                        _lastOrientation = orientation;
                        SetVerticesDirty();
                        SetMaterialDirty();
                    }
                }
            }

            if (Application.isPlaying)
            {
                if (!_isUserMaterial)
                {
                    UpdateInternalMaterial();
                }
            }

            if (material != null && _mediaPlayer != null)
            {
                // TODO: only run when dirty
                VideoRender.SetupMaterialForMedia(materialForRendering, _mediaPlayer);
            }
        }
Esempio n. 2
0
        public override void SetNativeSize()
        {
            Texture tex = mainTexture;

            if (tex != null)
            {
                int w = Mathf.RoundToInt(tex.width * uvRect.width);
                int h = Mathf.RoundToInt(tex.height * uvRect.height);

                if (_mediaPlayer != null)
                {
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM && !(!UNITY_EDITOR && UNITY_ANDROID)
                    if (_mediaPlayer.Info != null)
                    {
                        Orientation ori = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
                        if (ori == Orientation.Portrait || ori == Orientation.PortraitFlipped)
                        {
                            w = Mathf.RoundToInt(tex.height * uvRect.width);
                            h = Mathf.RoundToInt(tex.width * uvRect.height);
                        }
                    }
#endif
                    if (_mediaPlayer.TextureProducer != null)
                    {
                        if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.LeftRight ||
                            _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.LeftRight)
                        {
                            w /= 2;
                        }
                        else if (_mediaPlayer.TextureProducer.GetTextureAlphaPacking() == AlphaPacking.TopBottom ||
                                 _mediaPlayer.TextureProducer.GetTextureStereoPacking() == StereoPacking.TopBottom)
                        {
                            h /= 2;
                        }
                    }
                }

                rectTransform.anchorMax = rectTransform.anchorMin;
                rectTransform.sizeDelta = new Vector2(w, h);
            }
        }
Esempio n. 3
0
        public override void SetNativeSize()
        {
            Texture tex = mainTexture;

            if (tex != null)
            {
                int w = Mathf.RoundToInt(tex.width * uvRect.width);
                int h = Mathf.RoundToInt(tex.height * uvRect.height);

                if (_mediaPlayer != null)
                {
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS
                    if (_mediaPlayer.Info != null)
                    {
                        Orientation ori = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform());
                        if (ori == Orientation.Portrait || ori == Orientation.PortraitFlipped)
                        {
                            w = Mathf.RoundToInt(tex.height * uvRect.width);
                            h = Mathf.RoundToInt(tex.width * uvRect.height);
                        }
                    }
#endif
                    if (_mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || _mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
                    {
                        w /= 2;
                    }
                    else if (_mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || _mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
                    {
                        h /= 2;
                    }
                }

                rectTransform.anchorMax = rectTransform.anchorMin;
                rectTransform.sizeDelta = new Vector2(w, h);
            }
        }
Esempio n. 4
0
        private Vector4 GetDrawingDimensions(ScaleMode scaleMode, ref Rect uvRect)
        {
            Vector4 returnSize = Vector4.zero;

            if (mainTexture != null)
            {
                var padding = Vector4.zero;

                var textureSize = new Vector2(mainTexture.width, mainTexture.height);
                {
                    // Adjust textureSize based on orientation
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
                    if (HasValidTexture())
                    {
                        Matrix4x4 m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
                        textureSize   = m.MultiplyVector(textureSize);
                        textureSize.x = Mathf.Abs(textureSize.x);
                        textureSize.y = Mathf.Abs(textureSize.y);
                    }
#endif
                    // Adjust textureSize based on alpha packing
                    if (_mediaPlayer != null)
                    {
                        if (_mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || _mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
                        {
                            textureSize.x /= 2f;
                        }
                        else if (_mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || _mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
                        {
                            textureSize.y /= 2f;
                        }
                    }
                }

                Rect r = GetPixelAdjustedRect();

                // Fit the above textureSize into rectangle r
                int spriteW = Mathf.RoundToInt(textureSize.x);
                int spriteH = Mathf.RoundToInt(textureSize.y);

                var size = new Vector4(padding.x / spriteW,
                                       padding.y / spriteH,
                                       (spriteW - padding.z) / spriteW,
                                       (spriteH - padding.w) / spriteH);


                {
                    if (textureSize.sqrMagnitude > 0.0f)
                    {
                        if (scaleMode == ScaleMode.ScaleToFit)
                        {
                            float spriteRatio = textureSize.x / textureSize.y;
                            float rectRatio   = r.width / r.height;

                            if (spriteRatio > rectRatio)
                            {
                                float oldHeight = r.height;
                                r.height = r.width * (1.0f / spriteRatio);
                                r.y     += (oldHeight - r.height) * rectTransform.pivot.y;
                            }
                            else
                            {
                                float oldWidth = r.width;
                                r.width = r.height * spriteRatio;
                                r.x    += (oldWidth - r.width) * rectTransform.pivot.x;
                            }
                        }
                        else if (scaleMode == ScaleMode.ScaleAndCrop)
                        {
                            float aspectRatio = textureSize.x / textureSize.y;
                            float screenRatio = r.width / r.height;
                            if (screenRatio > aspectRatio)
                            {
                                float adjust = aspectRatio / screenRatio;
                                uvRect = new Rect(uvRect.xMin, (uvRect.yMin * adjust) + (1f - adjust) * 0.5f, uvRect.width, adjust * uvRect.height);
                            }
                            else
                            {
                                float adjust = screenRatio / aspectRatio;
                                uvRect = new Rect(uvRect.xMin * adjust + (0.5f - adjust * 0.5f), uvRect.yMin, adjust * uvRect.width, uvRect.height);
                            }
                        }
                    }
                }

                returnSize = new Vector4(r.x + r.width * size.x,
                                         r.y + r.height * size.y,
                                         r.x + r.width * size.z,
                                         r.y + r.height * size.w);
            }

            return(returnSize);
        }
Esempio n. 5
0
        private void _OnFillVBO(List <UIVertex> vbo)
        {
            _flipY = false;
            if (HasValidTexture())
            {
                _flipY = _mediaPlayer.TextureProducer.RequiresVerticalFlip();
            }

            Rect    uvRect = m_UVRect;
            Vector4 v      = GetDrawingDimensions(_scaleMode, ref uvRect);

#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
            Matrix4x4 m = Matrix4x4.identity;
            if (HasValidTexture())
            {
                m = Helper.GetMatrixForOrientation(Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()));
            }
#endif
            vbo.Clear();

            var vert = UIVertex.simpleVert;
            vert.color = color;

            vert.position = new Vector2(v.x, v.y);

            vert.uv0 = new Vector2(uvRect.xMin, uvRect.yMin);
            if (_flipY)
            {
                vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMin);
            }
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
            vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
#endif
            vbo.Add(vert);

            vert.position = new Vector2(v.x, v.w);
            vert.uv0      = new Vector2(uvRect.xMin, uvRect.yMax);
            if (_flipY)
            {
                vert.uv0 = new Vector2(uvRect.xMin, 1.0f - uvRect.yMax);
            }
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
            vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
#endif
            vbo.Add(vert);

            vert.position = new Vector2(v.z, v.w);
            vert.uv0      = new Vector2(uvRect.xMax, uvRect.yMax);
            if (_flipY)
            {
                vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMax);
            }
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
            vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
#endif
            vbo.Add(vert);

            vert.position = new Vector2(v.z, v.y);
            vert.uv0      = new Vector2(uvRect.xMax, uvRect.yMin);
            if (_flipY)
            {
                vert.uv0 = new Vector2(uvRect.xMax, 1.0f - uvRect.yMin);
            }
#if UNITY_PLATFORM_SUPPORTS_VIDEOTRANSFORM
            vert.uv0 = m.MultiplyPoint3x4(vert.uv0);
#endif
            vbo.Add(vert);
        }
Esempio n. 6
0
        private IEnumerator ExtractFrameCoroutine(Texture2D target, ProcessExtractedFrame callback, double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
        {
#if (!UNITY_EDITOR && UNITY_ANDROID) || UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_IOS || UNITY_TVOS
            Texture2D result = target;

            Texture frame = null;

            if (_controlInterface != null)
            {
                if (timeSeconds >= 0f)
                {
                    Pause();

                    // If the right frame is already available (or close enough) just grab it
                    if (TextureProducer.GetTexture() != null && (System.Math.Abs(_controlInterface.GetCurrentTime() - timeSeconds) < (timeThresholdMs / 1000.0)))
                    {
                        frame = TextureProducer.GetTexture();
                    }
                    else
                    {
                        int preSeekFrameCount = _textureInterface.GetTextureFrameCount();

                        // Seek to the frame
                        if (accurateSeek)
                        {
                            _controlInterface.Seek(timeSeconds);
                        }
                        else
                        {
                            _controlInterface.SeekFast(timeSeconds);
                        }

                        // Wait for the new frame to arrive
                        if (!_controlInterface.WaitForNextFrame(GetDummyCamera(), preSeekFrameCount))
                        {
                            // If WaitForNextFrame fails (e.g. in android single threaded), we run the below code to asynchronously wait for the frame
                            int currFc        = TextureProducer.GetTextureFrameCount();
                            int iterations    = 0;
                            int maxIterations = 50;

                            //+1 as often there will be an extra frame produced after pause (so we need to wait for the second frame instead)
                            while ((currFc + 1) >= TextureProducer.GetTextureFrameCount() && iterations++ < maxIterations)
                            {
                                yield return(null);
                            }
                        }
                        frame = TextureProducer.GetTexture();
                    }
                }
                else
                {
                    frame = TextureProducer.GetTexture();
                }
            }
            if (frame != null)
            {
                result = Helper.GetReadableTexture(frame, TextureProducer.RequiresVerticalFlip(), Helper.GetOrientation(Info.GetTextureTransform()), target);
            }
#else
            Texture2D result = ExtractFrame(target, timeSeconds, accurateSeek, timeoutMs, timeThresholdMs);
#endif
            callback(result);

            yield return(null);
        }
Esempio n. 7
0
        // "target" can be null or you can pass in an existing texture.
        public Texture2D ExtractFrame(Texture2D target, double timeSeconds = -1.0, bool accurateSeek = true, int timeoutMs = 1000, int timeThresholdMs = 100)
        {
            Texture2D result = target;

            // Extract frames returns the internal frame of the video player
            Texture frame = ExtractFrame(timeSeconds, accurateSeek, timeoutMs, timeThresholdMs);

            if (frame != null)
            {
                result = Helper.GetReadableTexture(frame, TextureProducer.RequiresVerticalFlip(), Helper.GetOrientation(Info.GetTextureTransform()), target);
            }

            return(result);
        }