Portal rendering 1/2 done

This commit is contained in:
Pasha Bibko
2025-04-01 19:21:37 +01:00
parent 8610868924
commit 70a71a06e5
19 changed files with 2904 additions and 118 deletions

View File

@@ -0,0 +1,50 @@
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class PortalCamera : MonoBehaviour
{
[Header("Components")]
[SerializeField] Shader m_Shader;
// Private members //
PortalManager m_DisplayPortal;
PortalManager m_CapturePortal;
RenderTexture m_RenderTexture;
Material m_RenderMaterial;
Camera m_Camera;
// Initialistion function for the camera
public void InitCamera(MeshRenderer renderer, PortalManager creator)
{
//
m_CapturePortal = creator.Linked();
m_DisplayPortal = creator;
// Gets the camera from the component
m_Camera = gameObject.GetComponent<Camera>();
// 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
renderer.material = m_RenderMaterial; // Set's the renderer to use the material
}
// Update is called every frame
void Update()
{
// Calculates the player distance from where the portal will be rendererd
Vector3 offset = CameraController.Instance().transform.position - m_DisplayPortal.pos;
transform.parent.position = m_CapturePortal.TranslateOffset(offset);
}
}

View File

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

View File

@@ -0,0 +1,45 @@
using System.ComponentModel;
using UnityEngine;
public class PortalManager : MonoBehaviour
{
[Header("References")]
[SerializeField] GameObject m_OtherPortal;
[Header("Set References")]
[SerializeField] GameObject m_CameraPrefab;
[SerializeField] MeshRenderer m_PortalRenderer;
[SerializeField] Transform m_CapturePoint;
PortalManager m_OtherManager;
PortalCamera m_PortalCamera;
public PortalManager Linked() => m_OtherManager;
public Vector3 pos => transform.parent.position;
public Quaternion rot => transform.parent.rotation;
// 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_PortalRenderer, this);
}
public Vector3 TranslateOffset(Vector3 offset)
{
m_CapturePoint.localPosition = offset;
return m_CapturePoint.position;
}
}

View File

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