Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Created June 19, 2017 07:40
Show Gist options
  • Save keiranlovett/674619ea79e4c0bf882fc9b713960216 to your computer and use it in GitHub Desktop.
Save keiranlovett/674619ea79e4c0bf882fc9b713960216 to your computer and use it in GitHub Desktop.
Unity VR Tunneling
using UnityEngine;
using System.Collections;
public class Tunneling : MonoBehaviour {
#region Public Fields
[Header("Angular Velocity")]
/// <summary>
/// Angular velocity calculated for this Transform. DO NOT USE HMD!
/// </summary>
[Tooltip("Angular velocity calculated for this Transform.\nDO NOT USE HMD!")]
public Transform refTransform;
/// <summary>
/// Below this angular velocity, effect will not kick in. Degrees per second
/// </summary>
[Tooltip("Below this angular velocity, effect will not kick in.\nDegrees per second")]
public float minAngVel = 0f;
/// <summary>
/// At/above this angular velocity, effect will be maxed out. Degrees per second
/// </summary>
[Tooltip("At/above this angular velocity, effect will be maxed out.\nDegrees per second")]
public float maxAngVel = 180f;
[Header("Effect Settings")]
/// <summary>
/// Screen coverage at max angular velocity.
/// </summary>
[Range(0f,1f)][Tooltip("Screen coverage at max angular velocity.\n(1-this) is radius of visible area at max effect (screen space).")]
public float maxEffect = 0.75f;
/// <summary>
/// Feather around cut-off as fraction of screen.
/// </summary>
[Range(0f, 0.5f)][Tooltip("Feather around cut-off as fraction of screen.")]
public float feather = 0.1f;
/// <summary>
/// Smooth out radius over time. 0 for no smoothing.
/// </summary>
[Tooltip("Smooth out radius over time. 0 for no smoothing.")]
public float smoothTime = 0.15f;
#endregion
#region Smoothing
private float _avSlew;
private float _av;
#endregion
#region Shader property IDs
private int _propAV;
private int _propFeather;
#endregion
#region Misc Fields
private Vector3 _lastFwd;
private Material _m;
#endregion
#region Messages
void Awake () {
_m = new Material(Shader.Find("Hidden/Tunnelling"));
if (refTransform == null){
refTransform = transform;
}
_propAV = Shader.PropertyToID("_AV");
_propFeather = Shader.PropertyToID("_Feather");
}
void Update(){
Vector3 fwd = refTransform.forward;
float av = Vector3.Angle(_lastFwd, fwd) / Time.deltaTime;
av = (av - minAngVel) / (maxAngVel - minAngVel);
av = Mathf.Clamp01(av);
av *= maxEffect;
_av = Mathf.SmoothDamp(_av, av, ref _avSlew, smoothTime);
_m.SetFloat(_propAV, _av);
_m.SetFloat(_propFeather, feather);
_lastFwd = fwd;
}
void OnRenderImage(RenderTexture src, RenderTexture dest){
Graphics.Blit(src, dest, _m);
}
void OnDestroy(){
Destroy(_m);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment