Commit 0135cc8f authored by Martin Finkel's avatar Martin Finkel
Browse files

script: Simply texture update mechanism

We can use the Update() function from MonoBehaviour instead of our own custom coroutine
parent be8a6546
using UnityEngine;
using System;
using System.Collections;
using LibVLCSharp.Shared;
public class UseRenderingPlugin : MonoBehaviour
......@@ -19,8 +18,6 @@ void Awake()
_mediaPlayer = new MediaPlayer(_libVLC);
StartCoroutine("CallPluginAtEndOfFrames");
PlayPause();
}
......@@ -57,6 +54,7 @@ public void PlayPause()
else
{
playing = true;
if(_mediaPlayer.Media == null)
{
_mediaPlayer.Media = new Media(_libVLC, "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", FromType.FromLocation);
......@@ -73,47 +71,39 @@ public void Stop ()
playing = false;
_mediaPlayer?.Stop();
GetComponent<Renderer>().material.mainTexture = null;
tex = null;
}
void Start()
void Update()
{
}
if(!playing) return;
private IEnumerator CallPluginAtEndOfFrames()
{
while (true)
if (tex == null)
{
// Wait until all frame rendering is done
yield return new WaitForEndOfFrame();
// If received size is not null, it and scale the texture
uint i_videoHeight = 0;
uint i_videoWidth = 0;
// We may not receive video size the first time
if (GetComponent<Renderer>().material.mainTexture == null && playing)
_mediaPlayer.Size(0, ref i_videoWidth, ref i_videoHeight);
var texptr = _mediaPlayer.GetTexture(out bool updated);
if (i_videoWidth != 0 && i_videoHeight != 0 && updated && texptr != IntPtr.Zero)
{
// If received size is not null, it and scale the texture
uint i_videoHeight = 0;
uint i_videoWidth = 0;
_mediaPlayer.Size(0, ref i_videoWidth, ref i_videoHeight);
IntPtr texptr = _mediaPlayer.GetTexture(out bool updated);
if (i_videoWidth != 0 && i_videoHeight != 0 && updated && texptr != IntPtr.Zero)
{
Debug.Log("Creating texture with height " + i_videoHeight + " and width " + i_videoWidth);
tex = Texture2D.CreateExternalTexture((int)i_videoWidth,
(int)i_videoHeight,
TextureFormat.RGBA32,
false,
true,
texptr);
GetComponent<Renderer>().material.mainTexture = tex;
}
Debug.Log("Creating texture with height " + i_videoHeight + " and width " + i_videoWidth);
tex = Texture2D.CreateExternalTexture((int)i_videoWidth,
(int)i_videoHeight,
TextureFormat.RGBA32,
false,
true,
texptr);
GetComponent<Renderer>().material.mainTexture = tex;
}
else if (tex != null && playing)
}
else if (tex != null)
{
var texptr = _mediaPlayer.GetTexture(out bool updated);
if (updated)
{
IntPtr texptr = _mediaPlayer.GetTexture(out bool updated);
if (updated)
{
tex.UpdateExternalTexture(texptr);
}
tex.UpdateExternalTexture(texptr);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment