desert portal

This commit is contained in:
2022-11-27 12:12:02 +01:00
parent bc61d04541
commit 15f3857302
65 changed files with 3412 additions and 1127 deletions

View File

@@ -5,7 +5,9 @@
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:fe685ec1767f73d42b749ea8045bfe43",
"GUID:8804073475ba36c47830e8e19dc699ce"
"GUID:776d03a35f1b52c4a9aed9f56d7b4229",
"GUID:8804073475ba36c47830e8e19dc699ce",
"GUID:2d68e204354e44f2a2ecf3cfa9213c5f"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using NaughtyAttributes;
using UnityEngine;
namespace EscapeRoomEngine.Portal.Runtime
{
[RequireComponent(typeof(Collider))]
public class Portal : MonoBehaviour
public class Portal : DoorState
{
public static readonly Matrix4x4 HalfRotation = Matrix4x4.Rotate(Quaternion.Euler(0, 180, 0));
@@ -16,30 +19,46 @@ namespace EscapeRoomEngine.Portal.Runtime
/// <summary>
/// The camera that will draw the view for this portal.
/// </summary>
public PortalCamera portalCamera;
[BoxGroup("Internal")] public PortalCamera portalCamera;
/// <summary>
/// The transform marking the edge of the portal plane.
/// </summary>
[BoxGroup("Internal")] public Transform portalTransform;
private bool Connected => linkedPortal != null;
private readonly List<PortalDriver> _closePortalDrivers = new();
private void Awake()
{
// check whether the other portal is set up
if (!linkedPortal || linkedPortal.linkedPortal != this) throw new Exception("Other portal not set up correctly.");
// check whether the collider is set up correctly
if (!GetComponent<Collider>().isTrigger) throw new Exception("Collider must be a trigger.");
DoorEvent += (_, type) =>
{
if (type == DoorEventType.Connected)
{
linkedPortal = FromDoorState(Module.ConnectedDoorState);
portalCamera.screen.gameObject.SetActive(true);
portalCamera.enabled = true;
}
};
}
private void FixedUpdate()
{
for (var i = 0; i < _closePortalDrivers.Count; i++)
if (Connected)
{
var portalDriver = _closePortalDrivers[i];
if (portalDriver.entrySide < 0 && CalculateSide(portalDriver.transform) >= 0) // must have entered from the front and exited the back
for (var i = 0; i < _closePortalDrivers.Count; i++)
{
StopTrackingDriver(portalDriver);
linkedPortal.StartTrackingDriver(portalDriver, -1);
portalDriver.Teleport(this, linkedPortal);
i--; // decrease the loop counter because the list is one element smaller now
var portalDriver = _closePortalDrivers[i];
if (portalDriver.entrySide < 0 && CalculateSide(portalDriver.transform) >= 0) // must have entered from the front and exited the back
{
StopTrackingDriver(portalDriver);
linkedPortal.StartTrackingDriver(portalDriver, -1);
portalDriver.Teleport(this, linkedPortal);
i--; // decrease the loop counter because the list is one element smaller now
}
}
}
}
@@ -59,24 +78,41 @@ namespace EscapeRoomEngine.Portal.Runtime
private void OnTriggerEnter(Collider other)
{
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver && !_closePortalDrivers.Contains(portalDriver))
if (Connected)
{
StartTrackingDriver(portalDriver, CalculateSide(portalDriver.transform));
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver && !_closePortalDrivers.Contains(portalDriver))
{
StartTrackingDriver(portalDriver, CalculateSide(portalDriver.transform));
}
}
}
private void OnTriggerExit(Collider other)
{
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver)
StopTrackingDriver(portalDriver);
if (Connected)
{
var portalDriver = other.GetComponent<PortalDriver>();
if (portalDriver)
{
StopTrackingDriver(portalDriver);
}
}
}
private int CalculateSide(Transform portalDriverTransform)
{
var t = transform;
return Math.Sign(Vector3.Dot(t.forward, portalDriverTransform.position - t.position));
return Math.Sign(Vector3.Dot(portalTransform.forward, portalDriverTransform.position - portalTransform.position));
}
private static Portal FromDoorState(DoorState state)
{
if (state is Portal portal)
{
return portal;
}
throw new WrongTypeException(typeof(Portal), state.GetType(), typeof(DoorState));
}
}
}

View File

@@ -29,7 +29,7 @@ namespace EscapeRoomEngine.Portal.Runtime
/// <summary>
/// The mesh where the rendered texture will be drawn on.
/// </summary>
[SerializeField] private MeshRenderer screen;
[SerializeField] public MeshRenderer screen;
private PlayerCamera _playerCamera;
private Camera _camera;
@@ -47,12 +47,14 @@ namespace EscapeRoomEngine.Portal.Runtime
private void OnEnable()
{
_camera.enabled = true;
RenderPipelineManager.beginCameraRendering += Render;
}
private void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= Render;
_camera.enabled = false;
}
private void Render(ScriptableRenderContext scriptableRenderContext, Camera _)
@@ -63,8 +65,6 @@ namespace EscapeRoomEngine.Portal.Runtime
// don't render this portal if it is not visible
return;
var t = portal.transform;
screen.enabled = false;
foreach (var eye in Eyes)
@@ -83,15 +83,16 @@ namespace EscapeRoomEngine.Portal.Runtime
}
// position portal camera
var m = t.localToWorldMatrix * Portal.HalfRotation * portal.linkedPortal.transform.worldToLocalMatrix *
var m = portal.portalTransform.localToWorldMatrix * Portal.HalfRotation *
portal.linkedPortal.portalTransform.worldToLocalMatrix *
_playerCamera.GetEyeTransform(eye).localToWorldMatrix;
transform.SetPositionAndRotation(m.GetPosition(), m.rotation);
_camera.projectionMatrix = _playerCamera.camera.GetStereoProjectionMatrix(eye);
// set camera clip plane to portal (otherwise the wall behind the portal would be rendered)
// calculating the clip plane: https://computergraphics.stackexchange.com/a/1506
var n = -t.forward; // clip plane normal
var portalPlane = new Plane(n, t.position); // clip plane in world space
var n = -portal.portalTransform.forward; // clip plane normal
var portalPlane = new Plane(n, portal.portalTransform.position); // clip plane in world space
var clipPlane = _camera.worldToCameraMatrix.inverse.transpose *
new Vector4(n.x, n.y, n.z, portalPlane.distance); // vector format clip plane in camera space
if (-portalPlane.GetDistanceToPoint(transform.position) >= minNearClipPlane)

View File

@@ -73,13 +73,13 @@ namespace EscapeRoomEngine.Portal.Runtime
public void Teleport(Portal from, Portal to)
{
var m = to.transform.localToWorldMatrix * Portal.HalfRotation * from.transform.worldToLocalMatrix *
traveller.localToWorldMatrix;
var m = to.portalTransform.localToWorldMatrix * Portal.HalfRotation *
from.portalTransform.worldToLocalMatrix * traveller.localToWorldMatrix;
traveller.SetPositionAndRotation(m.GetPosition(), m.rotation);
if (_rigidbody)
if (_rigidbody && !_rigidbody.isKinematic)
{
_rigidbody.velocity = to.transform.TransformDirection(
Portal.HalfRotation.rotation * from.transform.InverseTransformDirection(_rigidbody.velocity));
_rigidbody.velocity = to.portalTransform.TransformDirection(
Portal.HalfRotation.rotation * from.portalTransform.InverseTransformDirection(_rigidbody.velocity));
}
}
}