Skip to content

Commit

Permalink
Fix video playback resolution
Browse files Browse the repository at this point in the history
- Previously the video playback logic was much more complicated, but I replaced the Unity example code (more or less) and it seems to work.
- See https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
  • Loading branch information
drojf committed Feb 4, 2024
1 parent c9c64c6 commit fd85ab0
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions Assets.Scripts.Core.Scene/GameVideoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ public class GameVideoPlayer : MonoBehaviour
{
private VideoPlayer player;

private Mesh mesh;

private MeshFilter meshFilter;

private MeshRenderer meshRenderer;

private Material material;

private RenderTexture renderTexture;

private const string shaderDefaultName = "MGShader/LayerShader";

private bool isActive;

public static GameVideoPlayer CreateVideoPlayer(GameObject parent, string videoPath, Vector2Int size)
Expand All @@ -35,42 +23,33 @@ public static GameVideoPlayer CreateVideoPlayer(GameObject parent, string videoP
return gameVideoPlayer;
}

// Rounds a float to the nearest even number (multiple of 2), then converts to an int
private int RoundNearestEven(float value)
{
return Mathf.RoundToInt(Mathf.Round(value / 2f) * 2f);
}

// Note: In the vanilla game, the PlayVideo(path, video_size_x, video_size_y) takes two extra arguments (which I think is the resolution the video should play at)
// However after mod changes, these are no longer used, and can be set to 0.
// Note: Previously the video playback logic was much more complicated, but I replaced the Unity example code (more or less) and it seems to work.
// See https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
public void Initialize(string path, Vector2Int size)
{
// NOTE: the video will be drawn ontop of whatever is already on the screen
// so you should add a DrawScene( "black", 500 ); just before the movie is played.
float screen_aspect = GameSystem.Instance.AspectRatio;
int screen_width = RoundNearestEven(screen_aspect * 480f);
int screen_height = 480;
// Note: Unity recommends using Camera.main instead of Camera.current, however I could only get this to work with Camera.current
GameObject camera = Camera.current.gameObject;

meshFilter = base.gameObject.AddComponent<MeshFilter>();
meshRenderer = base.gameObject.AddComponent<MeshRenderer>();
material = new Material(Shader.Find("MGShader/LayerShader"));
meshRenderer.material = material;
meshFilter.mesh = MGHelper.CreateMesh(screen_width, screen_height, LayerAlignment.AlignCenter, false, 0);
renderTexture = new RenderTexture(screen_width, screen_height, 32);
material.SetTexture("_Primary", renderTexture);
player = base.gameObject.AddComponent<VideoPlayer>();
player.renderMode = VideoRenderMode.RenderTexture;
player.targetTexture = renderTexture;
player = camera.AddComponent<VideoPlayer>();
player.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
player.url = path;
player.isLooping = false;
player.aspectRatio = VideoAspectRatio.FitInside;
player.SetDirectAudioVolume(0, GameSystem.Instance.AudioController.GetVolumeByType(Assets.Scripts.Core.Audio.AudioType.BGM) * (25f / 32f));
player.loopPointReached += OnFinish;

GameSystem.Instance.AddWait(new Wait(0f, WaitTypes.WaitForInput, EndPlayback));
isActive = true;
}

public void EndPlayback()
{
isActive = false;
// In the vanilla game, the video player ('player') was attached to the base game object, so it would be destroyed automatically
// However now that the video player is attached to the camera, we need to delete it manually
Object.Destroy(player);
Object.Destroy(base.gameObject);
}

Expand Down

0 comments on commit fd85ab0

Please sign in to comment.