comments and portal code cleanup

This commit is contained in:
LOLthecool 2024-02-07 19:26:35 +00:00
parent 53ecdcbea5
commit 84381a906b
5 changed files with 46 additions and 66 deletions

View file

@ -2,6 +2,8 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; 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 class PositionSync : MonoBehaviour
{ {
public GameObject targetGameobject; public GameObject targetGameobject;

View file

@ -2,6 +2,8 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; 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 class SwapLeftHandToGlass : MonoBehaviour
{ {
public GameObject raycaster; public GameObject raycaster;
@ -9,7 +11,6 @@ public class SwapLeftHandToGlass : MonoBehaviour
public GameObject controller2; public GameObject controller2;
bool glassenabled = false; bool glassenabled = false;
// Start is called before the first frame update
public void Swap() public void Swap()
{ {
if(!glassenabled){ if(!glassenabled){

View file

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

View file

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

View file

@ -2,77 +2,75 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class WorkingPortal : MonoBehaviour { // giant mess of code that controlls the portal renderering effects
[Header ("Main Settings")]
public WorkingPortal linkedWorkingPortal;
public MeshRenderer screen;
public int recursionLimit = 5;
public bool visibleFromCamera = true;
[Header ("Advanced Settings")] // !!! if this breaks please dont try fixing it yourself unless you know what your doing !!!
public float nearClipOffset = 0.05f;
public float nearClipLimit = 0.2f; // 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; 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; public RenderTexture viewTexture;
// the camera this portal uses for rendering
public Camera WorkingPortalCam; public Camera WorkingPortalCam;
float nearClipOffset = 0.05f;
float nearClipLimit = 0.2f;
Camera playerCam; Camera playerCam;
Material firstRecursionMat;
MeshFilter screenMeshFilter; MeshFilter screenMeshFilter;
void Awake () { void Awake () {
// bit of jank code by me dont worry about it
myGameObject = gameObject; myGameObject = gameObject;
// stops our camera from always rendering, now we trigger it manuelly through the Render() function attached to it
WorkingPortalCam.enabled = false; WorkingPortalCam.enabled = false;
screenMeshFilter = screen.GetComponent<MeshFilter> (); 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); screen.material.SetInt ("displayMask", 1);
} }
// Manually render the camera attached to this WorkingPortal // does a bunch of stuff to render the view through this portal
// Called after PreWorkingPortalRender, and before PostWorkingPortalRender
public void Render (Camera newplayerCam) { 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) { if (!visibleFromCamera) {
return; return;
} }
playerCam = newplayerCam;
CreateViewTexture (); 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 localToWorldMatrix = playerCam.transform.localToWorldMatrix;
var renderPositions = new Vector3[recursionLimit]; var renderPosition = new Vector3();
var renderRotations = new Quaternion[recursionLimit]; var renderRotation = new Quaternion();
int startIndex = 0;
WorkingPortalCam.projectionMatrix = playerCam.projectionMatrix; WorkingPortalCam.projectionMatrix = playerCam.projectionMatrix;
for (int i = 0; i < recursionLimit; i++) { localToWorldMatrix = transform.localToWorldMatrix * linkedWorkingPortal.transform.worldToLocalMatrix * localToWorldMatrix;
if (i > 0) { renderPosition = localToWorldMatrix.GetColumn (3);
break; renderRotation = localToWorldMatrix.rotation;
}
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;
}
// Hide screen so that camera can see through WorkingPortal // Hide screen so that camera can see through WorkingPortal
screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly; 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 (renderPosition, renderRotation);
WorkingPortalCam.transform.SetPositionAndRotation (renderPositions[i], renderRotations[i]); SetNearClipPlane ();
Debug.Log(string.Format("rendering at: ", renderPositions[i].x, renderPositions[i].y, renderPositions[i].z)); WorkingPortalCam.Render ();
SetNearClipPlane ();
WorkingPortalCam.Render ();
if (i == startIndex) {
linkedWorkingPortal.screen.material.SetInt ("displayMask", 1);
}
}
// Unhide objects hidden at start of render // Unhide objects hidden at start of render
screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On; screen.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
@ -115,28 +113,4 @@ public class WorkingPortal : MonoBehaviour {
WorkingPortalCam.projectionMatrix = playerCam.projectionMatrix; 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;
}
}
} }