cleanup #2

Merged
Mercury merged 6 commits from cleanup into main 2024-02-08 15:31:48 +01:00
5 changed files with 46 additions and 66 deletions
Showing only changes of commit 84381a906b - Show all commits

View file

@ -2,6 +2,8 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// syncs the position of the player glass and the view in the alternate scene. could probably be better but who cares
public class PositionSync : MonoBehaviour
{
public GameObject targetGameobject;

View file

@ -2,6 +2,8 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// swaps the players left hand between being a regular controller and holding the looking glass. when holding the looking glass this hands ray interactor is also disabled
public class SwapLeftHandToGlass : MonoBehaviour
{
public GameObject raycaster;
@ -9,7 +11,6 @@ public class SwapLeftHandToGlass : MonoBehaviour
public GameObject controller2;
bool glassenabled = false;
// Start is called before the first frame update
public void Swap()
{
if(!glassenabled){

View file

@ -1,5 +1,7 @@
using UnityEngine;
// attached to the player cameras, used to trigger the portal rendering code
public class MainCamera : MonoBehaviour {
public Camera ourCamera;

View file

@ -1,5 +1,6 @@
Shader "Custom/Portal"
{
// shader for the portals, suprisingly straightforward so nothing should go wrong here
Properties
{
_InactiveColour ("Inactive Colour", Color) = (1, 1, 1, 1)

View file

@ -2,78 +2,76 @@
using System.Collections.Generic;
using UnityEngine;
public class WorkingPortal : MonoBehaviour {
[Header ("Main Settings")]
public WorkingPortal linkedWorkingPortal;
public MeshRenderer screen;
public int recursionLimit = 5;
public bool visibleFromCamera = true;
// giant mess of code that controlls the portal renderering effects
[Header ("Advanced Settings")]
public float nearClipOffset = 0.05f;
public float nearClipLimit = 0.2f;
// !!! if this breaks please dont try fixing it yourself unless you know what your doing !!!
// if you do wanna try the comments may or may not help you
public class WorkingPortal : MonoBehaviour {
// the portal that this portal is linked to, as in the one that you see the view of when you look through this portal
public WorkingPortal linkedWorkingPortal;
// the mesh renderer that the portal is rendered to.
public MeshRenderer screen;
// something to do with skipping the render function, not sure exactly what the effects are yet
public bool visibleFromCamera = true;
public GameObject myGameObject;
// Private variables
// the view texture that this portals camera is rendered to, this is the view you see when looking through the other portal
public RenderTexture viewTexture;
// the camera this portal uses for rendering
public Camera WorkingPortalCam;
float nearClipOffset = 0.05f;
float nearClipLimit = 0.2f;
Camera playerCam;
Material firstRecursionMat;
MeshFilter screenMeshFilter;
void Awake () {
// bit of jank code by me dont worry about it
myGameObject = gameObject;
// stops our camera from always rendering, now we trigger it manuelly through the Render() function attached to it
WorkingPortalCam.enabled = false;
screenMeshFilter = screen.GetComponent<MeshFilter> ();
// something to do with making the shader itself draw a solid color rather than the camera view
screen.material.SetInt ("displayMask", 1);
}
// Manually render the camera attached to this WorkingPortal
// Called after PreWorkingPortalRender, and before PostWorkingPortalRender
// does a bunch of stuff to render the view through this portal
public void Render (Camera newplayerCam) {
playerCam = newplayerCam;
// Skip rendering the view from this WorkingPortal if player is not looking at the linked WorkingPortal
if (!visibleFromCamera) {
return;
}
playerCam = newplayerCam;
CreateViewTexture ();
// listen i have no idea how the rest of this code actually works so take my comments with a grain of salt
// its something to do with moving the camera so what it sees lines up with what the player should see through the portal, then using screen space coordinates to make it so only the part...
// ... of the view covered by the portal actually gets rendered to the view texture
// this has all been heavily edited from the original to remove unneeded features
var localToWorldMatrix = playerCam.transform.localToWorldMatrix;
var renderPositions = new Vector3[recursionLimit];
var renderRotations = new Quaternion[recursionLimit];
var renderPosition = new Vector3();
var renderRotation = new Quaternion();
int startIndex = 0;
WorkingPortalCam.projectionMatrix = playerCam.projectionMatrix;
for (int i = 0; i < recursionLimit; i++) {
if (i > 0) {
break;
}
localToWorldMatrix = transform.localToWorldMatrix * linkedWorkingPortal.transform.worldToLocalMatrix * localToWorldMatrix;
int renderOrderIndex = recursionLimit - i - 1;
renderPositions[renderOrderIndex] = localToWorldMatrix.GetColumn (3);
renderRotations[renderOrderIndex] = localToWorldMatrix.rotation;
WorkingPortalCam.transform.SetPositionAndRotation (renderPositions[renderOrderIndex], renderRotations[renderOrderIndex]);
startIndex = renderOrderIndex;
}
renderPosition = localToWorldMatrix.GetColumn (3);
renderRotation = localToWorldMatrix.rotation;
// Hide screen so that camera can see through WorkingPortal
screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
linkedWorkingPortal.screen.material.SetInt ("displayMask", 0);
linkedWorkingPortal.screen.material.SetInt ("displayMask", 1);
for (int i = startIndex; i < recursionLimit; i++) {
WorkingPortalCam.transform.SetPositionAndRotation (renderPositions[i], renderRotations[i]);
Debug.Log(string.Format("rendering at: ", renderPositions[i].x, renderPositions[i].y, renderPositions[i].z));
WorkingPortalCam.transform.SetPositionAndRotation (renderPosition, renderRotation);
SetNearClipPlane ();
WorkingPortalCam.Render ();
if (i == startIndex) {
linkedWorkingPortal.screen.material.SetInt ("displayMask", 1);
}
}
// Unhide objects hidden at start of render
screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
}
@ -115,28 +113,4 @@ public class WorkingPortal : MonoBehaviour {
WorkingPortalCam.projectionMatrix = playerCam.projectionMatrix;
}
}
/*
** Some helper/convenience stuff:
*/
int SideOfWorkingPortal (Vector3 pos) {
return System.Math.Sign (Vector3.Dot (pos - transform.position, transform.forward));
}
bool SameSideOfWorkingPortal (Vector3 posA, Vector3 posB) {
return SideOfWorkingPortal (posA) == SideOfWorkingPortal (posB);
}
Vector3 WorkingPortalCamPos {
get {
return WorkingPortalCam.transform.position;
}
}
void OnValidate () {
if (linkedWorkingPortal != null) {
linkedWorkingPortal.linkedWorkingPortal = this;
}
}
}