How Do I Reset a Render Texture to Black when I stop a video from playing?
When I first play my applications my render textures appear as black. When I play a video and pause it the video stops playing and pauses as expected. When I do VideoPlayer.Stop() the video stops but the video player still shows where the video stopped.
I would like the video player to show either black or preferably another color to show there is a video screen. The render texture material is Sprites/Default as suggested from this video that I used to create my video players. The steps used to use render textures for videos is about halfway through the video.
I don't know where to go from here. There has been very little I have been able to find on using and scripting for render textures for videos in Unity.
Answer by tillmonkthered · Jul 31, 2018 at 03:46 PM
I was having a similar problem. I found that if you can get a reference to the RenderTexture, you can call its Release() method, and this seems to do the required reset.
https://docs.unity3d.com/ScriptReference/RenderTexture.Release.html
It's been a while since I took a look at this. I tried Release() as below where I selected the render texture I wanted to reset. I also tried DiscardContents(). Neither of them cleared out the render texture.
public void ClearOutRenderTexture(RenderTexture renderTexture)
{
renderTexture.Release();
}
I was seeing a flicker in editor of the old contents every time I restarted a video - this is a perfect solution to that. Thank you!
Answer by pamelacook · Jun 19, 2019 at 10:42 PM
It took me a while but I was able to find a solution in the Unity forum. I decided to make the background transparent.
public void ClearOutRenderTexture(RenderTexture renderTexture)
{
RenderTexture rt = RenderTexture.active;
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = rt;
}
Here's the link where I found the solution.
Clear Renter Texture Link
Answer by Fenikkel · Jan 28, 2021 at 09:19 AM
Release it works for me:
m_VideoPlayer.targetTexture.Release();
But in the documentation says that use it only "when you are finished using them"
So maybe make it transparent it's the best solution:
RenderTexture rt = RenderTexture.active;
RenderTexture.active = m_VideoPlayer.targetTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = rt;
Answer by Rs · Mar 28 at 10:49 AM
The answers that suggest to reset the texture to black using GL.Clear are perfectly valid. But sometimes it can be sufficient to just set the clip to a black video, so here's the alternative:
make a black, 1 or 2 seconds black video
Which can be created with ffmpeg
ffmpeg -loop 1 -i black.png -c:v libx264 -t 2 -pix_fmt yuv420p blackvideo.mp4
Put blackvideo.mp4 in a Resources folder
Create a 'Clear' extension method for VideoPlayer
using UnityEngine; using UnityEngine.Video;
public static class ExtensionMethods {
private static VideoClip _blackVideoClip; private static VideoClip blackVideoClip { get { if(_blackVideoClip == null) { _blackVideoClip = Resources.Load<VideoClip>("blackvideo"); } return _blackVideoClip; } } public static void Clear(this VideoPlayer _vp) { _vp.clip = blackVideoClip; } }
Use the extension method
myVideoPlayer.Clear();