Skip to content

Commit

Permalink
Added .webm, .mov, .jpg support for backgrounds
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed May 11, 2023
1 parent 3f1b51e commit e53e65d
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RenderTexture:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: DummyBackgroundTexture
m_Name: BackgroundTexture
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
Expand Down
131 changes: 0 additions & 131 deletions Assets/Prefabs/SongAudio.prefab

This file was deleted.

7 changes: 0 additions & 7 deletions Assets/Prefabs/SongAudio.prefab.meta

This file was deleted.

14 changes: 14 additions & 0 deletions Assets/Scenes/PlayScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ GameObject:
- component: {fileID: 240896118}
- component: {fileID: 240896120}
- component: {fileID: 240896119}
- component: {fileID: 240896122}
- component: {fileID: 240896121}
m_Layer: 5
m_Name: Background
Expand Down Expand Up @@ -747,6 +748,19 @@ VideoPlayer:
m_WaitForFirstFrame: 1
m_FrameReadyEventEnabled: 0
m_VideoShaders: []
--- !u!114 &240896122
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 240896117}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2df7b64c06ba35458eca10641fececd, type: 3}
m_Name:
m_EditorClassIdentifier:
rt: {fileID: 8400000, guid: 6f01deee09a9c5846a83d96667de20f1, type: 2}
--- !u!1 &264140536
GameObject:
m_ObjectHideFlags: 0
Expand Down
98 changes: 71 additions & 27 deletions Assets/Script/PlayMode/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using YARG.Settings;
using YARG.Song;
using YARG.UI;
using YARG.Util;
using YARG.Venue;

namespace YARG.PlayMode {
Expand All @@ -40,7 +41,7 @@ public static Play Instance {
public static event PauseStateChangeAction OnPauseToggle;

[SerializeField]
private GameObject soundAudioPrefab;
private RenderTexture backgroundRenderTexture;

public bool SongStarted {
get;
Expand Down Expand Up @@ -88,10 +89,17 @@ public bool Paused {

GameManager.AudioManager.Pause();

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Pause();
}
} else {
Time.timeScale = 1f;

GameManager.AudioManager.Play();

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Play();
}
}
OnPauseToggle(_paused);
}
Expand All @@ -105,6 +113,8 @@ private void Awake() {
ScoreKeeper.Reset();
StarScoreKeeper.Reset();

backgroundRenderTexture.ClearTexture();

// Song
StartSong();
}
Expand Down Expand Up @@ -161,32 +171,8 @@ private void StartSong() {
i++;
}

// Load Background
string backgroundPath = Path.Combine(song.Location, "bg.yarground");
string mp4Path = Path.Combine(song.Location, "bg.mp4");
string pngPath = Path.Combine(song.Location, "bg.png");

if (File.Exists(backgroundPath)) {
// First check for a yarground
var bundle = AssetBundle.LoadFromFile(backgroundPath);
var bg = bundle.LoadAsset<GameObject>("Assets/_Background.prefab");
var bgInstance = Instantiate(bg);

bgInstance.GetComponent<BundleBackgroundManager>().Bundle = bundle;
} else if (File.Exists(mp4Path)) {
// If not, check for a video
GameUI.Instance.videoPlayer.url = mp4Path;
GameUI.Instance.videoPlayer.enabled = true;
GameUI.Instance.videoPlayer.Play();
} else if (File.Exists(pngPath)) {
// Otherwise, load an image
var png = ImageHelper.LoadTextureFromFile(pngPath);

GameUI.Instance.background.texture = png;
} else {
// No background file, we load a random video
// TODO: Add custom videos folder, load here
}
// Load background (venue, video, image, etc.)
LoadBackground();

SongStarted = true;

Expand All @@ -207,6 +193,59 @@ private void StartSong() {
}
}

private void LoadBackground() {
// Try a yarground first

string backgroundPath = Path.Combine(song.Location, "bg.yarground");
if (File.Exists(backgroundPath)) {
// First check for a yarground
var bundle = AssetBundle.LoadFromFile(backgroundPath);
var bg = bundle.LoadAsset<GameObject>("Assets/_Background.prefab");
var bgInstance = Instantiate(bg);

bgInstance.GetComponent<BundleBackgroundManager>().Bundle = bundle;
return;
}

// Next, a video

string[] videoPaths = {
"bg.mp4",
"bg.mov",
"bg.webm",
};

foreach (var file in videoPaths) {
var path = Path.Combine(song.Location, file);

if (File.Exists(path)) {
GameUI.Instance.videoPlayer.url = path;
GameUI.Instance.videoPlayer.enabled = true;

return;
}
}

// Finally, an image

string[] imagePaths = {
"bg.png",
"bg.jpg",
"bg.jpeg",
};

foreach (var file in imagePaths) {
var path = Path.Combine(song.Location, file);

if (File.Exists(path)) {
var png = ImageHelper.LoadTextureFromFile(path);

GameUI.Instance.background.texture = png;
return;
}
}
}

private void LoadChart() {
// Add main file
var files = new List<string> {
Expand Down Expand Up @@ -253,6 +292,10 @@ private IEnumerator StartAudio() {
yield return null;
}

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Play();
}

GameManager.AudioManager.Play();
audioStarted = true;
}
Expand Down Expand Up @@ -447,6 +490,7 @@ public void Exit() {
// Unpause just in case
Time.timeScale = 1f;

backgroundRenderTexture.ClearTexture();
_tracks.Clear();

GameManager.Instance.LoadScene(SceneIndex.MENU);
Expand Down
1 change: 1 addition & 0 deletions Assets/Script/UI/GameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GameUI : MonoBehaviour {
public GameObject pauseMenu;
public RawImage background;
public VideoPlayer videoPlayer;

public static GameUI Instance {
get;
private set;
Expand Down
10 changes: 10 additions & 0 deletions Assets/Script/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,15 @@ public static Rect ToViewportSpaceCentered(this RectTransform transform, bool h

return rect;
}

/// <summary>
/// Clears the render texture.
/// </summary>
public static void ClearTexture(this RenderTexture rt) {
RenderTexture old = RenderTexture.active;
RenderTexture.active = rt;
GL.Clear(true, true, Color.clear);
RenderTexture.active = old;
}
}
}

0 comments on commit e53e65d

Please sign in to comment.