Added basic portals

This commit is contained in:
2026-01-15 18:18:59 +00:00
parent 6fb0c358e1
commit 18c19c7c14
15 changed files with 914 additions and 2 deletions

View File

@@ -0,0 +1,14 @@
using UnityEngine;
namespace InterfaceOff.WorldScene
{
public class PlayerController : MonoBehaviour
{
private static PlayerController Instance;
public static void SetPos(Vector3 pos)
{
Instance.transform.position = pos;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 42603e4f95b24efa85355a04450c20e2
timeCreated: 1768499006

View File

@@ -0,0 +1,76 @@
using UnityEngine;
namespace InterfaceOff.WorldScene
{
[RequireComponent(typeof(Camera))]
public class PortalCamera : MonoBehaviour
{
[Header("Components")]
[SerializeField] Shader m_Shader;
[SerializeField] Camera m_WorldCamera;
// Private members //
PortalManager m_DisplayPortal;
PortalManager m_CapturePortal;
RenderTexture m_RenderTexture;
Material m_RenderMaterial;
Camera m_Camera;
Vector3 m_Rot;
// Initialistion function for the camera
public void InitCamera(MeshRenderer[] renderers, PortalManager creator, Vector3 rot)
{
// Transfers the passed rotation to be stored within the class
m_Rot = rot;
// Stores both portals
m_CapturePortal = creator.Linked();
m_DisplayPortal = creator;
// Gets the camera from the component
m_Camera = gameObject.GetComponent<Camera>();
m_Camera.fieldOfView = m_WorldCamera.fieldOfView;
// Creates the render texture
RenderTextureDescriptor descriptor = new(Screen.width, Screen.height);
m_RenderTexture = new RenderTexture(descriptor);
// Creates a material from the Cutout shader
// Needs to be created via code as all the materials have different settings
m_RenderMaterial = new Material(m_Shader);
// Links the camera to the mesh renderer
m_Camera.targetTexture =
m_RenderTexture; // Sets it's camera to display to the render texture instead of the screen
m_RenderMaterial.mainTexture =
m_RenderTexture; // Sets the material to use the render texture as it's texture
foreach (Renderer renderer in renderers)
{
renderer.material = m_RenderMaterial;
}
}
void LateUpdate()
{
// Gets the offset of the player from the display portal
Vector3 offset = m_DisplayPortal.PlayerOffset();
// Translates it to the capture portal and assigns it to the camera position
Transform t = m_CapturePortal.transform.parent;
transform.position = t.position + offset;
// Calculate angle stuff (not needed because angle always stays the same in this duct tape version)
//float angle = Quaternion.Angle(m_DisplayPortal.transform.parent.rotation,
// m_CapturePortal.transform.parent.rotation);
//Quaternion rotDif = Quaternion.AngleAxis(angle, Vector3.up);
//Vector3 newCamDir = rotDif * CameraController.Instance().transform.forward;
//Vector3 d = new(0f, m_CapturePortal.CamDif(), 0f);
//transform.parent.eulerAngles = Quaternion.LookRotation(newCamDir, Vector3.up).eulerAngles + m_Rot + d;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ecef1a55a76c4edd8a7a6154094c3d86
timeCreated: 1768498426

View File

@@ -0,0 +1,62 @@
using UnityEngine;
namespace InterfaceOff.WorldScene
{
public class PortalManager : MonoBehaviour
{
[Header("References")]
[SerializeField] GameObject m_OtherPortal;
[SerializeField] Camera m_PlayerCamera;
[Header("Set References")]
[SerializeField] GameObject m_CameraPrefab;
[SerializeField] MeshRenderer[] m_Renderers;
[SerializeField] Transform m_PlayerPoint;
// Private variables //
PortalManager m_OtherManager;
[SerializeField] PortalCamera m_PortalCamera;
// Gets the other end of the portal
public PortalManager Linked() => m_OtherManager;
// Gets the location of the player relative to the portal
public Vector3 PlayerOffset() => m_PlayerPoint.localPosition;
// Start is called before the first frame update
void Start()
{
// Validates that it is a portal
m_OtherManager = m_OtherPortal.GetComponentInChildren<PortalManager>();
if (m_OtherManager == null)
{
Debug.LogError("OtherPortal was not valid portal");
return;
}
// Creates the camera in top-level heirachry and stores the PortalCamera script
GameObject cam = Instantiate(m_CameraPrefab, transform.root.parent);
m_PortalCamera = cam.GetComponentInChildren<PortalCamera>();
// Initialises the camera so it renders to the portal and not the screen
m_PortalCamera.InitCamera(m_Renderers, this, transform.parent.localEulerAngles * 2.0f);
}
void LateUpdate() => m_PlayerPoint.position = m_PlayerCamera.transform.position;
// When something enters the portal
private void OnTriggerEnter(Collider other)
{
// Changing the state if it is not the player will cause issues
if (!other.CompareTag("Player"))
{
return;
}
// Calculates if the player is going towards the portal
Vector3 difference = m_PlayerCamera.transform.position - transform.position;
PlayerController.SetPos(m_OtherManager.transform.position + difference - new Vector3(0, 1.0f, 0));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 68fb822b7ca1a9e489bf21abe980ed13
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: