143 lines
4.0 KiB
C#
143 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class WorldSwitcher : MonoBehaviour {
|
|
[Range(0, 5)]
|
|
[SerializeField] float transitionDuration;
|
|
|
|
[SerializeField] AnimationCurve transitionCurve;
|
|
[SerializeField] WorldInfo[] worldInfos;
|
|
|
|
[Range(0, 1)]
|
|
[SerializeField] float tabWidth;
|
|
|
|
//TODO Hardcode
|
|
[SerializeField]
|
|
float quadOffset;
|
|
|
|
int lastWorldIndex;
|
|
int currentWorldIndex;
|
|
Coroutine transition;
|
|
static readonly int UVOffset = Shader.PropertyToID("_UVOffset");
|
|
|
|
void Awake() {
|
|
if (worldInfos.Length != 3)
|
|
Debug.LogWarning("For now, WorldSwitcher should have 3 worlds.");
|
|
|
|
currentWorldIndex = 1;
|
|
lastWorldIndex = 0;
|
|
|
|
for (int i = 0; i < worldInfos.Length; i++) {
|
|
worldInfos[i].material = worldInfos[i].renderQuad.GetComponent<Renderer>().material;
|
|
|
|
worldInfos[i].GenerateRenderTexture();
|
|
}
|
|
|
|
ResetQuadPositions();
|
|
}
|
|
|
|
public void SwitchWorld(bool right) {
|
|
if (right ? currentWorldIndex == worldInfos.Length - 1 : currentWorldIndex == 0)
|
|
return;
|
|
|
|
//TODO Block window resize during transition?
|
|
foreach (WorldInfo worldInfo in worldInfos) {
|
|
if (Screen.width != worldInfo.texture.width || Screen.height != worldInfo.texture.height)
|
|
worldInfo.GenerateRenderTexture();
|
|
}
|
|
|
|
lastWorldIndex = currentWorldIndex;
|
|
if (right)
|
|
currentWorldIndex++;
|
|
else
|
|
currentWorldIndex--;
|
|
|
|
transition = StartCoroutine(TransitionCamera(right));
|
|
}
|
|
|
|
void OnGUI() {
|
|
if (GUILayout.Button("Left world"))
|
|
SwitchWorld(false);
|
|
if (GUILayout.Button("Right world"))
|
|
SwitchWorld(true);
|
|
}
|
|
|
|
IEnumerator TransitionCamera(bool fromRight) {
|
|
WorldInfo lastWorld = worldInfos[lastWorldIndex];
|
|
WorldInfo newWorld = worldInfos[currentWorldIndex];
|
|
|
|
float startTime = Time.time;
|
|
Camera lastCam = lastWorld.camera;
|
|
|
|
while (Time.time < startTime + transitionDuration) {
|
|
float t = transitionCurve.Evaluate((Time.time - startTime) / transitionDuration);
|
|
|
|
newWorld.renderQuad.position = GetQuadOffset(lastCam, newWorld, currentWorldIndex, fromRight, t);
|
|
newWorld.material.SetFloat(UVOffset, fromRight ? 1f - t - tabWidth : -1f + t + tabWidth);
|
|
|
|
yield return null;
|
|
}
|
|
|
|
ResetQuadPositions();
|
|
transition = null;
|
|
}
|
|
|
|
Vector3 GetQuadOffset(Camera cam, WorldInfo worldInfo, int index, bool fromRight, float t = 0f) {
|
|
float x = fromRight ?
|
|
Mathf.Lerp(1f - tabWidth * (worldInfos.Length - index), 0f, t) :
|
|
Mathf.Lerp(index * tabWidth, 1f, t);
|
|
Vector3 quadHalfWidthOffset = Vector3.right * (fromRight ? worldInfo.renderQuad.localScale.x / 2f : -worldInfo.renderQuad.localScale.x / 2f);
|
|
|
|
//TODO Offset epsilon
|
|
return cam.ViewportToWorldPoint(new Vector3(x, .5f, quadOffset - (t != 0 ? .01f : 0f))) + quadHalfWidthOffset;
|
|
}
|
|
|
|
void ResetQuadPositions() {
|
|
Camera currCam = worldInfos[currentWorldIndex].camera;
|
|
|
|
for (int i = 0; i < worldInfos.Length; ++i) {
|
|
bool usingRenderTexture = i != currentWorldIndex;
|
|
worldInfos[i].SetUsingRenderTexture(usingRenderTexture);
|
|
|
|
if (usingRenderTexture) {
|
|
bool fromRight = i - currentWorldIndex > 0;
|
|
worldInfos[i].renderQuad.position = GetQuadOffset(currCam, worldInfos[i], i, fromRight);
|
|
worldInfos[i].material.SetFloat(UVOffset, fromRight ? 1f - tabWidth : -1f + tabWidth);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
struct WorldInfo {
|
|
public Camera camera;
|
|
[HideInInspector] public RenderTexture texture;
|
|
public Transform renderQuad;
|
|
[HideInInspector] public Material material;
|
|
static readonly int CameraTexture = Shader.PropertyToID("_CameraTexture");
|
|
|
|
public void SetUsingRenderTexture(bool usingTexture) {
|
|
if (usingTexture) {
|
|
camera.targetTexture = texture;
|
|
renderQuad.gameObject.SetActive(true);
|
|
}else {
|
|
camera.targetTexture = null;
|
|
renderQuad.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void GenerateRenderTexture() {
|
|
bool usingTexture = !ReferenceEquals(camera.targetTexture, null);
|
|
if (usingTexture)
|
|
camera.targetTexture = null;
|
|
|
|
renderQuad.localScale = new Vector3(camera.aspect, 1f, 1f);
|
|
texture = new RenderTexture(Screen.width, Screen.height, 32);
|
|
material.SetTexture(CameraTexture, texture);
|
|
|
|
|
|
if (usingTexture)
|
|
camera.targetTexture = texture;
|
|
}
|
|
} |