Initial commit

This commit is contained in:
Thorbjoern
2025-05-26 00:46:28 +02:00
commit e5bca03433
3896 changed files with 1434297 additions and 0 deletions
@@ -0,0 +1,107 @@
using System;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace Fragilem17.MirrorsAndPortals
{
public class PortalExampleCameraController : MonoBehaviour
{
private Vector2 _rotation = Vector2.zero;
public float Speed = 1;
public float MouseSpeed = 4;
public Transform lookTarget;
public Transform moveTarget;
#if ENABLE_INPUT_SYSTEM
public InputAction moveAction;
public InputAction xrControllerAction;
public InputAction rotateAction;
[Tooltip("Action to toggle between free look and editor cursor")]
public InputAction toggleMouseLookAction;
private void OnEnable()
{
moveAction.Enable();
xrControllerAction.Enable();
rotateAction.Enable();
toggleMouseLookAction.Enable();
}
private void OnDisable()
{
moveAction.Disable();
xrControllerAction.Disable();
rotateAction.Disable();
toggleMouseLookAction.Disable();
}
#endif
void Update()
{
#if ENABLE_LEGACY_INPUT_MANAGER && !ENABLE_INPUT_SYSTEM
moveTarget.position += lookTarget.forward * Time.deltaTime * Input.GetAxis("Vertical") * Speed;
moveTarget.position += lookTarget.right * Time.deltaTime * Input.GetAxis("Horizontal") * Speed;
if (!Cursor.visible && Cursor.lockState == CursorLockMode.Locked)
{
_rotation.y = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * MouseSpeed * 0.2f;
_rotation.x = transform.localEulerAngles.x + -Input.GetAxis("Mouse Y") * MouseSpeed * 0.2f;
//_rotation.x = Mathf.Clamp(_rotation.x, -40f, 40f);
// rotate z towards 0
float deltaZ = 0 - transform.localEulerAngles.z;
if (transform.localEulerAngles.z > 180f)
{
// rotate z towards 360
deltaZ = 360 - transform.localEulerAngles.z;
}
transform.localEulerAngles = new Vector3(_rotation.x, _rotation.y, transform.localEulerAngles.z + (deltaZ * 0.04f));
}
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
#endif
#if ENABLE_INPUT_SYSTEM
moveTarget.position += lookTarget.forward * Time.deltaTime * xrControllerAction.ReadValue<Vector2>().y * Speed;
moveTarget.position += lookTarget.right * Time.deltaTime * xrControllerAction.ReadValue<Vector2>().x * Speed;
moveTarget.position += lookTarget.forward * Time.deltaTime * moveAction.ReadValue<Vector3>().z * Speed;
moveTarget.position += lookTarget.right * Time.deltaTime * moveAction.ReadValue<Vector3>().x * Speed;
moveTarget.position += lookTarget.up * Time.deltaTime * moveAction.ReadValue<Vector3>().y * Speed;
if (!Cursor.visible && Cursor.lockState == CursorLockMode.Locked)
{
_rotation.y = transform.localEulerAngles.y + rotateAction.ReadValue<Vector2>().x * Time.deltaTime * MouseSpeed;
_rotation.x = transform.localEulerAngles.x + -rotateAction.ReadValue<Vector2>().y * Time.deltaTime * MouseSpeed;
//_rotation.x = Mathf.Clamp(_rotation.x, -40f, 40f);
// rotate z towards 0
float deltaZ = 0 - transform.localEulerAngles.z;
if (transform.localEulerAngles.z > 180f)
{
// rotate z towards 360
deltaZ = 360 - transform.localEulerAngles.z;
}
transform.localEulerAngles = new Vector3(_rotation.x, _rotation.y, transform.localEulerAngles.z + (deltaZ * 0.04f));
}
if (toggleMouseLookAction.ReadValue<float>() > 0)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
#endif
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: baa4c9e80bfd498419de1d2478bafbb7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 228871
packageName: Portals for VR
packageVersion: 1.2.1
assetPath: Assets/Fragilem17/Portals for VR/Demo/Scripts/PortalExampleCameraController.cs
uploadId: 710348
@@ -0,0 +1,221 @@
using Fragilem17.MirrorsAndPortals;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.XR;
#endif
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace Fragilem17.MirrorsAndPortals
{
public class PortalGunExample : MonoBehaviour
{
public LayerMask layerMask;
public Portal portal1;
public Portal portal2;
private Dictionary<Portal, Vector3> _portalSizes;
private Portal _targetPortal;
public new Light light;
private bool _shooting = false;
public bool alignWithViewDirectionOnHorizontalSurfaces = false;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
public InputAction ShootAction1;
public InputAction ShootAction2;
private void OnEnable()
{
ShootAction1.Enable();
ShootAction2.Enable();
}
private void OnDisable()
{
ShootAction1.Disable();
ShootAction2.Disable();
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
private UnityEngine.XR.InputDevice _rightHandDevice;
void OnEnable()
{
List<InputDevice> allDevices = new List<InputDevice>();
InputDevices.GetDevices(allDevices);
foreach (InputDevice device in allDevices)
InputDevices_deviceConnected(device);
InputDevices.deviceConnected += InputDevices_deviceConnected;
}
private void OnDisable()
{
InputDevices.deviceConnected -= InputDevices_deviceConnected;
}
private void InputDevices_deviceConnected(InputDevice device)
{
bool discardedValue;
if (device.TryGetFeatureValue(CommonUsages.primaryButton, out discardedValue))
{
if ((device.characteristics & InputDeviceCharacteristics.Right) == InputDeviceCharacteristics.Right)
{
_rightHandDevice = device;
}
}
}
#endif
private void Start()
{
_portalSizes = new Dictionary<Portal, Vector3>();
if (portal1 && portal2)
{
_portalSizes[portal1] = portal1.transform.localScale;
_portalSizes[portal2] = portal2.transform.localScale;
}
}
// Update is called once per frame
void Update()
{
_targetPortal = portal1;
bool shouldShoot = false;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if (!_shooting && ShootAction1.ReadValue<float>() > 0)
{
shouldShoot = true;
_shooting = true;
}
if (!_shooting && ShootAction2.ReadValue<float>() > 0)
{
shouldShoot = true;
_shooting = true;
_targetPortal = portal2;
}
if (_shooting && ShootAction1.ReadValue<float>() == 0 && ShootAction2.ReadValue<float>() == 0)
{
_shooting = false;
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
bool buttonValue;
_rightHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out buttonValue);
//Debug.Log("buttonValue: " + buttonValue);
if (!_shooting && (Input.GetMouseButton(0) || (_rightHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out buttonValue) && buttonValue)))
{
shouldShoot = true;
_shooting = true;
}
if (!_shooting && (Input.GetMouseButton(1) || (_rightHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out buttonValue) && buttonValue)))
{
shouldShoot = true;
_shooting = true;
_targetPortal = portal2;
}
if (_shooting && !(Input.GetMouseButton(0) || Input.GetMouseButton(1) || (_rightHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primaryButton, out buttonValue) && buttonValue) || (_rightHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.secondaryButton, out buttonValue) && buttonValue)))
{
_shooting = false;
}
#endif
if (shouldShoot)
{
//Debug.DrawRay(transform.position, transform.forward);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out hit, 40, layerMask))
{
if (hit.collider)
{
StopAllCoroutines();
StartCoroutine(FadeInOut());
_targetPortal.transform.position = hit.point;
_targetPortal.transform.rotation = Quaternion.FromToRotation(Vector3.back, hit.normal);
_targetPortal.transform.localScale = Vector3.zero;
_targetPortal.wallCollider = hit.collider.gameObject.GetComponentInChildren<Collider>();
Vector3 euler = _targetPortal.transform.rotation.eulerAngles;
_targetPortal.transform.rotation = Quaternion.Euler(euler.x, euler.y, 0);
// is it a vertical wall?
float vertical = Mathf.Abs(Vector3.Dot(_targetPortal.transform.up, Vector3.up));
//Debug.Log("verical: " + vertical);
if (alignWithViewDirectionOnHorizontalSurfaces && vertical < 0.5f)
{
// Calculate the direction from the hit point to the player's position
Vector3 playerDirection = transform.position - hit.point;
// Calculate a new forward direction for the portal that is opposite to the direction towards the player
Vector3 portalForward = -playerDirection.normalized;
// Create a new rotation that aligns the portal's up direction with the direction towards the player, and its forward direction with the wall's normal
Quaternion finalRotation = Quaternion.LookRotation(hit.normal, portalForward);
// Set the rotation of the portal object
_targetPortal.transform.rotation = finalRotation;
_targetPortal.transform.Rotate(_targetPortal.transform.InverseTransformDirection(_targetPortal.transform.up), 180);
}
_targetPortal.transform.localScale = Vector3.zero;
StartCoroutine(Scale(_targetPortal, 1));
}
}
}
}
IEnumerator FadeInOut()
{
if (light != null)
{
light.enabled = true;
yield return StartCoroutine(Fade(5f));
yield return StartCoroutine(Fade(0));
light.enabled = false;
}
}
IEnumerator Fade(float to)
{
float diff = (to - light.intensity);
while (Mathf.Abs(diff) > 0.01f)
{
//Debug.Log("diff " + diff);
light.intensity += diff * 0.5f;
diff = (to - light.intensity);
yield return null;
}
}
IEnumerator Scale(Portal portal, float to)
{
float diff = ((to * _portalSizes[portal].x) - portal.transform.localScale.x);
while (Mathf.Abs(diff) > 0.01f)
{
//Debug.Log("diff " + diff);
portal.transform.localScale += _portalSizes[portal] * diff * 0.1f;
diff = ((to * _portalSizes[portal].x) - portal.transform.localScale.x);
yield return null;
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 2fd6aff72615e5b479c0c70c7995efa3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 228871
packageName: Portals for VR
packageVersion: 1.2.1
assetPath: Assets/Fragilem17/Portals for VR/Demo/Scripts/PortalGunExample.cs
uploadId: 710348
@@ -0,0 +1,143 @@
using Fragilem17.MirrorsAndPortals;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Fragilem17.MirrorsAndPortals
{
[ExecuteInEditMode]
public class RayCastingExample : MonoBehaviour
{
public Transform ForwardGO;
public LayerMask layerMask;
public float maxDistance = 10;
public LineRenderer LineRendererPrefab;
private List<LineRenderer> LineRenderInstances;
public int maxRecursions = 10;
public bool ChangeColorOfPortalOnHit = false;
public Color ColorToChangePortalTo = Color.white;
private void OnEnable()
{
if (LineRendererPrefab)
{
LineRenderInstances = new List<LineRenderer>();
LineRenderInstances.Add(LineRendererPrefab);
}
}
private void OnDisable()
{
if (LineRenderInstances != null)
{
foreach (LineRenderer lr in LineRenderInstances)
{
if (lr != LineRendererPrefab)
{
#if UNITY_EDITOR
DestroyImmediate(lr.gameObject);
#else
Destroy(lr.gameObject);
#endif
}
}
LineRenderInstances = null;
}
}
void Update()
{
if (!LineRendererPrefab || !ForwardGO || LineRenderInstances == null || LineRenderInstances.Count == 0)
{
return;
}
HideLines();
RayCast(ForwardGO.position, ForwardGO.forward, 0);
}
private void HideLines()
{
foreach (LineRenderer lr in LineRenderInstances)
{
if (lr)
{
lr.gameObject.SetActive(false);
}
}
}
private void RayCast(Vector3 startPosition, Vector3 direction, int iteration)
{
if (iteration < maxRecursions)
{
Vector3 endPosition;
Ray ray = new Ray(startPosition, direction);
iteration = iteration + 1;
if (Physics.Raycast(ray, out RaycastHit hit, maxDistance, layerMask))
{
endPosition = hit.point;
Portal portal = hit.collider.gameObject.GetComponent<Portal>();
if (portal)
{
// where the ray hits the portal we set the rays new original to be the hitPoint
ray.origin = endPosition;
// Then we portal the ray
portal.PortalRay(ref ray);
if (ChangeColorOfPortalOnHit)
{
portal.PortalSurface.MyMaterialInstance.SetColor("_Tint", ColorToChangePortalTo);
}
// The ray is now pointing outwards of the other portal
// we recurse in this function so we can try and find another portal
RayCast(ray.origin, ray.direction, iteration);
}
}
else
{
// we didn't hit anything so we just add the endPos to be a meter from our startPos
endPosition = startPosition + (direction * 1f);
}
DrawLineRenderer(startPosition, endPosition, iteration);
//Debug.DrawLine(startPosition, endPosition, Color.blue);
}
}
private void DrawLineRenderer(Vector3 startPosition, Vector3 endPosition, int iteration)
{
iteration = iteration - 1;
LineRenderer lineRenderer;
if (LineRenderInstances.Count > iteration)
{
lineRenderer = LineRenderInstances[iteration];
}
else
{
lineRenderer = Instantiate<LineRenderer>(LineRendererPrefab, ForwardGO);
lineRenderer.gameObject.hideFlags = HideFlags.DontSave;
LineRenderInstances.Add(lineRenderer);
}
if (lineRenderer != null)
{
Vector3[] positions = new Vector3[] {
startPosition,
endPosition
};
lineRenderer.gameObject.SetActive(true);
lineRenderer.SetPositions(positions);
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: a602e9657ffe72d44b9b2332df236a49
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 228871
packageName: Portals for VR
packageVersion: 1.2.1
assetPath: Assets/Fragilem17/Portals for VR/Demo/Scripts/RayCastingExample.cs
uploadId: 710348
@@ -0,0 +1,99 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.XR;
#endif
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace Fragilem17.MirrorsAndPortals
{
public class SphereGun : MonoBehaviour
{
public GameObject Prefab;
public float force = 1;
private float _delay = 0;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
public InputAction ShootAction1;
private void OnEnable()
{
ShootAction1.Enable();
}
private void OnDisable()
{
ShootAction1.Disable();
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
private UnityEngine.XR.InputDevice _leftHandDevice;
void OnEnable()
{
List<InputDevice> allDevices = new List<InputDevice>();
InputDevices.GetDevices(allDevices);
foreach (InputDevice device in allDevices)
InputDevices_deviceConnected(device);
InputDevices.deviceConnected += InputDevices_deviceConnected;
}
private void OnDisable()
{
InputDevices.deviceConnected -= InputDevices_deviceConnected;
}
private void InputDevices_deviceConnected(InputDevice device)
{
bool discardedValue;
if (device.TryGetFeatureValue(CommonUsages.primaryButton, out discardedValue))
{
if ((device.characteristics & InputDeviceCharacteristics.Left) == InputDeviceCharacteristics.Left)
{
_leftHandDevice = device;
}
}
}
#endif
// Update is called once per frame
void Update()
{
_delay += Time.deltaTime;
bool shoot = false;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
shoot = (ShootAction1 != null && ShootAction1.ReadValue<float>() > 0.25f && _delay > 0.5f);
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
float buttonValue = 1;
shoot = ((Input.GetKey(KeyCode.Space) || (_leftHandDevice.TryGetFeatureValue(UnityEngine.XR.CommonUsages.trigger, out buttonValue) && buttonValue > 0.2f)) && _delay > 0.5f);
//Debug.Log(buttonValue);
#endif
if (shoot)
{
_delay = 0;
GameObject s = Instantiate(Prefab, null, true);
s.name = "SphereGunBullet";
s.transform.position = transform.position + (transform.forward * 0.5f);
s.transform.rotation = transform.rotation;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
s.GetComponent<Rigidbody>().AddForce(s.transform.forward * force * ShootAction1.ReadValue<float>(), ForceMode.Impulse);
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
s.GetComponent<Rigidbody>().AddForce(s.transform.forward * force * buttonValue, ForceMode.Impulse);
#endif
}
}
}
}
@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: dd2fe7f18d9da4142a67b1d70c910273
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 228871
packageName: Portals for VR
packageVersion: 1.2.1
assetPath: Assets/Fragilem17/Portals for VR/Demo/Scripts/SphereGun.cs
uploadId: 710348