Can jump through multiple dimensions

This commit is contained in:
misabiko 2022-05-15 13:24:19 -04:00
parent 57c9a8f643
commit bdc545832f
2 changed files with 232 additions and 51 deletions

View File

@ -5,8 +5,11 @@ using UnityEngine;
public class WorldSwitcher : MonoBehaviour {
[Range(0, 5)]
[SerializeField] float transitionDuration;
/*[Range(0, 5)]
[SerializeField] float fadeDuration;*/
[SerializeField] AnimationCurve transitionCurve;
//[SerializeField] AnimationCurve fadeCurve;
[SerializeField] WorldInfo[] worldInfos;
[Range(0, 1)]
@ -20,6 +23,7 @@ public class WorldSwitcher : MonoBehaviour {
int currentWorldIndex;
Coroutine transition;
static readonly int UVOffset = Shader.PropertyToID("_UVOffset");
//static readonly int Opacity = Shader.PropertyToID("_Opacity");
void Awake() {
if (worldInfos.Length != 3)
@ -37,11 +41,11 @@ public class WorldSwitcher : MonoBehaviour {
ResetQuadPositions();
}
public void SwitchWorld(bool right) {
public void SwitchWorld(int index) {
if (transition != null)
return;
if (right ? currentWorldIndex == worldInfos.Length - 1 : currentWorldIndex == 0)
if (index == currentWorldIndex || index < 0 || index > worldInfos.Length - 1)
return;
//TODO Block window resize during transition?
@ -51,24 +55,20 @@ public class WorldSwitcher : MonoBehaviour {
}
lastWorldIndex = currentWorldIndex;
if (right)
currentWorldIndex++;
else
currentWorldIndex--;
currentWorldIndex = index;
transition = StartCoroutine(TransitionCamera(right));
transition = StartCoroutine(TransitionCamera(currentWorldIndex - lastWorldIndex < 0));
}
void OnGUI() {
if (GUILayout.Button("Left world"))
SwitchWorld(false);
if (GUILayout.Button("Right world"))
SwitchWorld(true);
}
/*void OnGUI() {
for (int i = 0; i < worldInfos.Length; ++i) {
if (GUILayout.Button($"World {i}"))
SwitchWorld(i);
}
}*/
IEnumerator TransitionCamera(bool fromRight) {
WorldInfo lastWorld = worldInfos[lastWorldIndex];
WorldInfo newWorld = worldInfos[currentWorldIndex];
float startTime = Time.time;
Camera lastCam = lastWorld.camera;
@ -76,34 +76,60 @@ public class WorldSwitcher : MonoBehaviour {
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, GetUVOffset(t, fromRight, currentWorldIndex));
if (fromRight) {
for (int i = lastWorldIndex - 1; i >= currentWorldIndex; --i) {
worldInfos[i].renderQuad.position = GetQuadOffset(lastCam, worldInfos[i], i, fromRight, true, t);
worldInfos[i].material.SetFloat(UVOffset, GetUVOffset(t, fromRight, i));
}
} else {
for (int i = lastWorldIndex + 1; i <= currentWorldIndex; ++i) {
worldInfos[i].renderQuad.position = GetQuadOffset(lastCam, worldInfos[i], i, fromRight, true, t);
worldInfos[i].material.SetFloat(UVOffset, GetUVOffset(t, fromRight, i));
}
}
yield return null;
}
/*startTime = Time.time;
while (Time.time < startTime + fadeDuration) {
worldInfos[currentWorldIndex].material.SetFloat(
Opacity,
1f - fadeCurve.Evaluate((Time.time - startTime) / fadeDuration)
);
yield return null;
}*/
ResetQuadPositions();
transition = null;
}
Vector3 GetQuadOffset(Camera cam, WorldInfo worldInfo, int index, bool fromRight, float t = 0f) {
Vector3 GetQuadOffset(Camera cam, WorldInfo worldInfo, int index, bool fromRight, bool moving, float t = 0f) {
float x = fromRight ?
Mathf.Lerp(1f - tabWidth * (worldInfos.Length - index), index * tabWidth, t) :
Mathf.Lerp((1 + index) * tabWidth, 1f - (worldInfos.Length - 1 - index) * tabWidth, t);
Vector3 quadHalfWidthOffset = Vector3.right * (fromRight ? worldInfo.renderQuad.localScale.x / 2f : -worldInfo.renderQuad.localScale.x / 2f);
Mathf.Lerp((1 + index) * tabWidth, 1f - (worldInfos.Length - 1 - index) * tabWidth, t) :
Mathf.Lerp(1f - tabWidth * (worldInfos.Length - index), index * tabWidth, t);
Vector3 quadHalfWidthOffset = Vector3.right * (fromRight ? -worldInfo.renderQuad.localScale.x / 2f : worldInfo.renderQuad.localScale.x / 2f);
float depthOffset = moving ?
Mathf.Abs(currentWorldIndex - index) * .0001f :
-Mathf.Abs(currentWorldIndex - index) * .0001f;
//TODO Offset epsilon
return cam.ViewportToWorldPoint(new Vector3(
x,
.5f,
quadOffset - Mathf.Abs(currentWorldIndex - index) * .01f)
return cam.ViewportToWorldPoint(
new Vector3(
x,
.5f,
quadOffset + depthOffset
)
) + quadHalfWidthOffset;
}
float GetUVOffset(float t, bool fromRight, int index) {
return fromRight ?
Mathf.Lerp(1f - tabWidth * (worldInfos.Length - index), index * tabWidth, t):
Mathf.Lerp(-1f + tabWidth * (index + 1), -(worldInfos.Length - 1 - index) * tabWidth, t);
Mathf.Lerp(-1f + tabWidth * (index + 1), -(worldInfos.Length - 1 - index) * tabWidth, t):
Mathf.Lerp(1f - tabWidth * (worldInfos.Length - index), index * tabWidth, t);
}
void ResetQuadPositions() {
@ -113,10 +139,11 @@ public class WorldSwitcher : MonoBehaviour {
for (int i = 0; i < worldInfos.Length; ++i) {
bool usingRenderTexture = i != currentWorldIndex;
worldInfos[i].SetUsingRenderTexture(usingRenderTexture, currLayer);
//worldInfos[i].material.SetFloat(Opacity, 1f);
if (usingRenderTexture) {
bool fromRight = i - currentWorldIndex > 0;
worldInfos[i].renderQuad.position = GetQuadOffset(currCam, worldInfos[i], i, fromRight);
bool fromRight = i - currentWorldIndex < 0;
worldInfos[i].renderQuad.position = GetQuadOffset(currCam, worldInfos[i], i, fromRight, false);
worldInfos[i].material.SetFloat(UVOffset, GetUVOffset(0f, fromRight, i));
}
}

View File

@ -8,6 +8,9 @@
},
{
"m_Id": "cbfe1aee551744acbc894c463a2c188f"
},
{
"m_Id": "50c3dc18fed24b0ba806f3b83bce4de9"
}
],
"m_Keywords": [],
@ -50,6 +53,12 @@
},
{
"m_Id": "3d343fd3994f4c97a20f5be920ca830b"
},
{
"m_Id": "5d4cc1151f9b494c91b07e7e4356e4b0"
},
{
"m_Id": "2225cdf0601c41978fd6e9bd600dbee4"
}
],
"m_GroupDatas": [],
@ -83,6 +92,20 @@
"m_SlotId": 2
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "5d4cc1151f9b494c91b07e7e4356e4b0"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "2225cdf0601c41978fd6e9bd600dbee4"
},
"m_SlotId": 0
}
},
{
"m_OutputSlot": {
"m_Node": {
@ -193,6 +216,9 @@
"m_Blocks": [
{
"m_Id": "17bc1a70f130474d866390b817919cc0"
},
{
"m_Id": "2225cdf0601c41978fd6e9bd600dbee4"
}
]
},
@ -253,6 +279,21 @@
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "08c468a3a96e4f6da858f4fceb5579f5",
"m_Id": 0,
"m_DisplayName": "Alpha",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Alpha",
"m_StageCapability": 2,
"m_Value": 1.0,
"m_DefaultValue": 1.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.UVNode",
@ -265,10 +306,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1338.0,
"y": 429.0,
"x": -1559.0,
"y": 415.0000305175781,
"width": 208.0,
"height": 313.0
"height": 313.0000305175781
}
},
"m_Slots": [
@ -373,6 +414,39 @@
"m_DefaultType": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlockNode",
"m_ObjectId": "2225cdf0601c41978fd6e9bd600dbee4",
"m_Group": {
"m_Id": ""
},
"m_Name": "SurfaceDescription.Alpha",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": 0.0,
"y": 0.0,
"width": 0.0,
"height": 0.0
}
},
"m_Slots": [
{
"m_Id": "08c468a3a96e4f6da858f4fceb5579f5"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_SerializedDescriptor": "SurfaceDescription.Alpha"
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@ -465,10 +539,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -653.0,
"y": 429.0,
"x": -874.0,
"y": 415.0000305175781,
"width": 128.0,
"height": 101.0
"height": 100.99996948242188
}
},
"m_Slots": [
@ -554,6 +628,33 @@
"m_ObjectId": "48b7e5faffd240328766c5ac7e1557ee"
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "50c3dc18fed24b0ba806f3b83bce4de9",
"m_Guid": {
"m_GuidSerialized": "77ede6a2-703f-4bc7-ba52-ed484dcc0d5a"
},
"m_Name": "Opacity",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Opacity",
"m_DefaultReferenceName": "_Opacity",
"m_OverrideReferenceName": "",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 1.0,
"m_FloatType": 0,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@ -608,6 +709,41 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.PropertyNode",
"m_ObjectId": "5d4cc1151f9b494c91b07e7e4356e4b0",
"m_Group": {
"m_Id": ""
},
"m_Name": "Property",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -423.9999694824219,
"y": 482.0,
"width": 115.99996948242188,
"height": 34.0
}
},
"m_Slots": [
{
"m_Id": "6afe25286d2a403c9b750ba22c97a754"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_Property": {
"m_Id": "50c3dc18fed24b0ba806f3b83bce4de9"
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.ColorRGBMaterialSlot",
@ -656,6 +792,21 @@
"m_DefaultType": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "6afe25286d2a403c9b750ba22c97a754",
"m_Id": 0,
"m_DisplayName": "Opacity",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Out",
"m_StageCapability": 3,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlockNode",
@ -701,10 +852,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1078.0,
"y": 429.0,
"x": -1299.0,
"y": 415.0000305175781,
"width": 120.0,
"height": 149.0
"height": 148.99996948242188
}
},
"m_Slots": [
@ -897,8 +1048,8 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -948.0,
"y": 496.0,
"x": -1169.0,
"y": 482.0,
"width": 124.0,
"height": 34.0
}
@ -956,10 +1107,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -496.0000305175781,
"y": 406.0,
"width": 207.99993896484376,
"height": 435.00006103515627
"x": -717.0,
"y": 392.0,
"width": 208.00003051757813,
"height": 435.0
}
},
"m_Slots": [
@ -990,7 +1141,7 @@
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewExpanded": false,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
@ -1117,10 +1268,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -690.0000610351563,
"y": 372.0000305175781,
"width": 164.99993896484376,
"height": 33.999969482421878
"x": -911.0,
"y": 358.0,
"width": 165.0,
"height": 34.0
}
},
"m_Slots": [
@ -1152,10 +1303,10 @@
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -809.0,
"y": 429.0,
"x": -1030.0,
"y": 415.0000305175781,
"width": 126.0,
"height": 118.0
"height": 117.99996948242188
}
},
"m_Slots": [
@ -1206,6 +1357,9 @@
},
{
"m_Id": "cbfe1aee551744acbc894c463a2c188f"
},
{
"m_Id": "50c3dc18fed24b0ba806f3b83bce4de9"
}
]
}