forked from cgvr/DeltaVR
Initial Commit
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
/************************************************************************************
|
||||
|
||||
See SampleFramework license.txt for license terms. Unless required by applicable law
|
||||
or agreed to in writing, the sample code is provided “AS IS” WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the license for specific
|
||||
language governing permissions and limitations under the license.
|
||||
|
||||
************************************************************************************/
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Procedurally generated capsule mesh which matches the character controller size.
|
||||
/// This was originally created for visualizing the capsule in the HMD but it could be adapted to other purposes.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
public class CharacterCapsule : MonoBehaviour
|
||||
{
|
||||
private CharacterController _character;
|
||||
private MeshFilter _meshFilter;
|
||||
|
||||
private float _height;
|
||||
private float _radius;
|
||||
|
||||
[Range(4,32)]
|
||||
public int SubdivisionsU;
|
||||
|
||||
[Range(4, 32)]
|
||||
public int SubdivisionsV;
|
||||
|
||||
private int _subdivisionU;
|
||||
private int _subdivisionV;
|
||||
|
||||
private Vector3[] _vertices;
|
||||
private int[] _triangles;
|
||||
|
||||
// Update is called once per frame
|
||||
void Update ()
|
||||
{
|
||||
if (_character == null)
|
||||
{
|
||||
_character = GetComponentInParent<CharacterController>();
|
||||
if (_character == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (_height == _character.height
|
||||
&& _radius == _character.radius
|
||||
&& _subdivisionU == SubdivisionsU
|
||||
&& _subdivisionV == SubdivisionsV)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_height = _character.height;
|
||||
_radius = _character.radius;
|
||||
_subdivisionU = SubdivisionsU;
|
||||
_subdivisionV = SubdivisionsV;
|
||||
|
||||
List<Vector3> verts = new List<Vector3>();
|
||||
|
||||
var vector = new Vector3(1,0,0);
|
||||
|
||||
// Generate the mesh
|
||||
var topOffset = new Vector3(0, _height/2.0f - _radius, 0);
|
||||
var bottomOffset = new Vector3(0, _radius - _height/2.0f, 0);
|
||||
|
||||
// Add all the necessary vertices
|
||||
verts.Add(new Vector3(0, _height/2.0f, 0));
|
||||
|
||||
for (int u = SubdivisionsU - 1; u >= 0; u--)
|
||||
{
|
||||
float uf = (float) u / (float) SubdivisionsU;
|
||||
for (int v = 0; v < SubdivisionsV; v++)
|
||||
{
|
||||
float vf = (float) v / (float) SubdivisionsV;
|
||||
var q = Quaternion.Euler(0, vf * 360.0f, uf * 90.0f);
|
||||
var vert = q * vector;
|
||||
vert *= _radius;
|
||||
var v1 = vert + topOffset;
|
||||
verts.Add(v1);
|
||||
}
|
||||
}
|
||||
|
||||
for (int u = 0; u < SubdivisionsU; u++)
|
||||
{
|
||||
float uf = (float)u / (float)SubdivisionsU;
|
||||
for (int v = 0; v < SubdivisionsV; v++)
|
||||
{
|
||||
float vf = (float)v / (float)SubdivisionsV;
|
||||
var q = Quaternion.Euler(0, vf * 360.0f + 180.0f, uf * 90.0f);
|
||||
var vert = q * vector;
|
||||
vert *= _radius;
|
||||
var v2 = bottomOffset - vert;
|
||||
verts.Add(v2);
|
||||
}
|
||||
}
|
||||
verts.Add(new Vector3(0, -_height / 2.0f, 0));
|
||||
|
||||
|
||||
// Setup all the triangles
|
||||
|
||||
List<int> tris = new List<int>();
|
||||
int index;
|
||||
int i;
|
||||
|
||||
// top cap
|
||||
for (int v = 0; v < SubdivisionsV; v++)
|
||||
{
|
||||
i = 0;
|
||||
tris.Add(i);
|
||||
tris.Add(v);
|
||||
tris.Add(v+1);
|
||||
}
|
||||
tris.Add(0);
|
||||
tris.Add(SubdivisionsV);
|
||||
tris.Add(1);
|
||||
|
||||
// top hemisphere
|
||||
for (int u = 0; u < SubdivisionsU - 1; u++)
|
||||
{
|
||||
index = u * SubdivisionsV + 1;
|
||||
for (int v = 0; v < SubdivisionsV - 1; v++)
|
||||
{
|
||||
i = index + v;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
|
||||
tris.Add(i + 1);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV + 1);
|
||||
}
|
||||
i = index + SubdivisionsV - 1;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
}
|
||||
|
||||
// center tube
|
||||
index = (SubdivisionsU - 1) * SubdivisionsV + 1;
|
||||
for (int v = 0; v < SubdivisionsV - 1; v++)
|
||||
{
|
||||
i = index + v;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
|
||||
tris.Add(i + 1);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV + 1);
|
||||
}
|
||||
i = index + SubdivisionsV - 1;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
|
||||
// bottom hemisphere
|
||||
for (int u = 0; u < SubdivisionsU - 1; u++)
|
||||
{
|
||||
index = u * SubdivisionsV + (SubdivisionsU * SubdivisionsV) + 1;
|
||||
for (int v = 0; v < SubdivisionsV - 1; v++)
|
||||
{
|
||||
i = index + v;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
|
||||
tris.Add(i + 1);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV + 1);
|
||||
}
|
||||
i = index + SubdivisionsV - 1;
|
||||
tris.Add(i);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
|
||||
tris.Add(i + 1 - SubdivisionsV);
|
||||
tris.Add(i + SubdivisionsV);
|
||||
tris.Add(i + 1);
|
||||
}
|
||||
|
||||
// bottom cap
|
||||
var last = verts.Count - 1;
|
||||
var lastRow = last - SubdivisionsV;
|
||||
for (int v = 0; v < SubdivisionsV; v++)
|
||||
{
|
||||
i = 0;
|
||||
tris.Add(last);
|
||||
tris.Add(lastRow + v + 1);
|
||||
tris.Add(lastRow + v);
|
||||
}
|
||||
tris.Add(last);
|
||||
tris.Add(lastRow);
|
||||
tris.Add(last - 1);
|
||||
|
||||
|
||||
_vertices = verts.ToArray();
|
||||
_triangles = tris.ToArray();
|
||||
|
||||
_meshFilter = gameObject.GetComponent<MeshFilter>();
|
||||
_meshFilter.mesh = new Mesh();
|
||||
_meshFilter.sharedMesh.vertices = _vertices;
|
||||
_meshFilter.sharedMesh.triangles = _triangles;
|
||||
_meshFilter.sharedMesh.RecalculateNormals();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f9cfd9856723ca4f922ba9af9e15fe4
|
||||
timeCreated: 1505495386
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,285 @@
|
||||
/************************************************************************************
|
||||
|
||||
See SampleFramework license.txt for license terms. Unless required by applicable law
|
||||
or agreed to in writing, the sample code is provided “AS IS” WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied. See the license for specific
|
||||
language governing permissions and limitations under the license.
|
||||
|
||||
************************************************************************************/
|
||||
|
||||
#define DEBUG_LOCOMOTION_PANEL
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine.UI;
|
||||
using Debug = UnityEngine.Debug;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class LocomotionSampleSupport : MonoBehaviour
|
||||
{
|
||||
private LocomotionController lc;
|
||||
private bool inMenu = false;
|
||||
private LocomotionTeleport TeleportController
|
||||
{
|
||||
get
|
||||
{
|
||||
return lc.GetComponent<LocomotionTeleport>();
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
lc = FindObjectOfType<LocomotionController>();
|
||||
DebugUIBuilder.instance.AddButton("Node Teleport w/ A", SetupNodeTeleport);
|
||||
DebugUIBuilder.instance.AddButton("Dual-stick teleport", SetupTwoStickTeleport);
|
||||
DebugUIBuilder.instance.AddButton("L Strafe R Teleport", SetupLeftStrafeRightTeleport);
|
||||
//DebugUIBuilder.instance.AddButton("R Turn L Teleport", SetupRightTurnLeftTeleport);
|
||||
DebugUIBuilder.instance.AddButton("Walk Only", SetupWalkOnly);
|
||||
|
||||
// This is just a quick hack-in, need a prefab-based way of setting this up easily.
|
||||
EventSystem eventSystem = FindObjectOfType<EventSystem>();
|
||||
if (eventSystem == null)
|
||||
{
|
||||
Debug.LogError("Need EventSystem");
|
||||
}
|
||||
SetupTwoStickTeleport();
|
||||
|
||||
// SAMPLE-ONLY HACK:
|
||||
// Due to restrictions on how Unity project settings work, we just hackily set up default
|
||||
// to ignore the water layer here. In your own project, you should set up your collision
|
||||
// layers properly through the Unity editor.
|
||||
Physics.IgnoreLayerCollision(0, 4);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if(OVRInput.GetDown(OVRInput.Button.Two) || OVRInput.GetDown(OVRInput.Button.Start))
|
||||
{
|
||||
if (inMenu) DebugUIBuilder.instance.Hide();
|
||||
else DebugUIBuilder.instance.Show();
|
||||
inMenu = !inMenu;
|
||||
}
|
||||
}
|
||||
|
||||
[Conditional("DEBUG_LOCOMOTION_PANEL")]
|
||||
static void Log(string msg)
|
||||
{
|
||||
Debug.Log(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method will ensure only one specific type TActivate in a given group of components derived from the same TCategory type is enabled.
|
||||
/// This is used by the sample support code to select between different targeting, input, aim, and other handlers.
|
||||
/// </summary>
|
||||
/// <typeparam name="TCategory"></typeparam>
|
||||
/// <typeparam name="TActivate"></typeparam>
|
||||
/// <param name="target"></param>
|
||||
public static TActivate ActivateCategory<TCategory, TActivate>(GameObject target) where TCategory : MonoBehaviour where TActivate : MonoBehaviour
|
||||
{
|
||||
var components = target.GetComponents<TCategory>();
|
||||
Log("Activate " + typeof(TActivate) + " derived from " + typeof(TCategory) + "[" + components.Length + "]");
|
||||
TActivate result = null;
|
||||
for (int i = 0; i < components.Length; i++)
|
||||
{
|
||||
var c = (MonoBehaviour)components[i];
|
||||
var active = c.GetType() == typeof(TActivate);
|
||||
Log(c.GetType() + " is " + typeof(TActivate) + " = " + active);
|
||||
if (active)
|
||||
{
|
||||
result = (TActivate)c;
|
||||
}
|
||||
if (c.enabled != active)
|
||||
{
|
||||
c.enabled = active;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This generic method is used for activating a specific set of components in the LocomotionController. This is just one way
|
||||
/// to achieve the goal of enabling one component of each category (input, aim, target, orientation and transition) that
|
||||
/// the teleport system requires.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput"></typeparam>
|
||||
/// <typeparam name="TAim"></typeparam>
|
||||
/// <typeparam name="TTarget"></typeparam>
|
||||
/// <typeparam name="TOrientation"></typeparam>
|
||||
/// <typeparam name="TTransition"></typeparam>
|
||||
protected void ActivateHandlers<TInput, TAim, TTarget, TOrientation, TTransition>()
|
||||
where TInput : TeleportInputHandler
|
||||
where TAim : TeleportAimHandler
|
||||
where TTarget : TeleportTargetHandler
|
||||
where TOrientation : TeleportOrientationHandler
|
||||
where TTransition : TeleportTransition
|
||||
{
|
||||
ActivateInput<TInput>();
|
||||
ActivateAim<TAim>();
|
||||
ActivateTarget<TTarget>();
|
||||
ActivateOrientation<TOrientation>();
|
||||
ActivateTransition<TTransition>();
|
||||
}
|
||||
|
||||
protected void ActivateInput<TActivate>() where TActivate : TeleportInputHandler
|
||||
{
|
||||
ActivateCategory<TeleportInputHandler, TActivate>();
|
||||
}
|
||||
|
||||
protected void ActivateAim<TActivate>() where TActivate : TeleportAimHandler
|
||||
{
|
||||
ActivateCategory<TeleportAimHandler, TActivate>();
|
||||
}
|
||||
|
||||
protected void ActivateTarget<TActivate>() where TActivate : TeleportTargetHandler
|
||||
{
|
||||
ActivateCategory<TeleportTargetHandler, TActivate>();
|
||||
}
|
||||
|
||||
protected void ActivateOrientation<TActivate>() where TActivate : TeleportOrientationHandler
|
||||
{
|
||||
ActivateCategory<TeleportOrientationHandler, TActivate>();
|
||||
}
|
||||
|
||||
protected void ActivateTransition<TActivate>() where TActivate : TeleportTransition
|
||||
{
|
||||
ActivateCategory<TeleportTransition, TActivate>();
|
||||
}
|
||||
|
||||
protected TActivate ActivateCategory<TCategory, TActivate>() where TCategory : MonoBehaviour where TActivate : MonoBehaviour
|
||||
{
|
||||
return ActivateCategory<TCategory, TActivate>(lc.gameObject);
|
||||
}
|
||||
|
||||
protected void UpdateToggle(Toggle toggle, bool enabled)
|
||||
{
|
||||
if (enabled != toggle.isOn)
|
||||
{
|
||||
toggle.isOn = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
void SetupNonCap()
|
||||
{
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.InputMode = TeleportInputHandlerTouch.InputModes.SeparateButtonsForAimAndTeleport;
|
||||
input.AimButton = OVRInput.RawButton.A;
|
||||
input.TeleportButton = OVRInput.RawButton.A;
|
||||
}
|
||||
|
||||
void SetupTeleportDefaults()
|
||||
{
|
||||
TeleportController.enabled = true;
|
||||
//lc.PlayerController.SnapRotation = true;
|
||||
lc.PlayerController.RotationEitherThumbstick = false;
|
||||
//lc.PlayerController.FixedSpeedSteps = 0;
|
||||
TeleportController.EnableMovement(false, false, false, false);
|
||||
TeleportController.EnableRotation(false, false, false, false);
|
||||
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.InputMode = TeleportInputHandlerTouch.InputModes.CapacitiveButtonForAimAndTeleport;
|
||||
input.AimButton = OVRInput.RawButton.A;
|
||||
input.TeleportButton = OVRInput.RawButton.A;
|
||||
input.CapacitiveAimAndTeleportButton = TeleportInputHandlerTouch.AimCapTouchButtons.A;
|
||||
input.FastTeleport = false;
|
||||
|
||||
var hmd = TeleportController.GetComponent<TeleportInputHandlerHMD>();
|
||||
hmd.AimButton = OVRInput.RawButton.A;
|
||||
hmd.TeleportButton = OVRInput.RawButton.A;
|
||||
|
||||
var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
|
||||
orient.Thumbstick = OVRInput.Controller.LTouch;
|
||||
}
|
||||
|
||||
|
||||
protected GameObject AddInstance(GameObject template, string label)
|
||||
{
|
||||
var go = Instantiate(template);
|
||||
go.transform.SetParent(transform, false);
|
||||
go.name = label;
|
||||
return go;
|
||||
}
|
||||
|
||||
// Teleport between node with A buttons. Display laser to node. Allow snap turns.
|
||||
void SetupNodeTeleport()
|
||||
{
|
||||
SetupTeleportDefaults();
|
||||
SetupNonCap();
|
||||
//lc.PlayerController.SnapRotation = true;
|
||||
//lc.PlayerController.FixedSpeedSteps = 1;
|
||||
lc.PlayerController.RotationEitherThumbstick = true;
|
||||
TeleportController.EnableRotation(true, false, false, true);
|
||||
ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerLaser, TeleportTargetHandlerNode, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.AimingController = OVRInput.Controller.RTouch;
|
||||
//var input = TeleportController.GetComponent<TeleportAimHandlerLaser>();
|
||||
//input.AimingController = OVRInput.Controller.RTouch;
|
||||
}
|
||||
|
||||
// Symmetrical controls. Forward or back on stick initiates teleport, then stick allows orient.
|
||||
// Snap turns allowed.
|
||||
void SetupTwoStickTeleport()
|
||||
{
|
||||
SetupTeleportDefaults();
|
||||
TeleportController.EnableRotation(true, false, false, true);
|
||||
TeleportController.EnableMovement(false, false, false, false);
|
||||
//lc.PlayerController.SnapRotation = true;
|
||||
lc.PlayerController.RotationEitherThumbstick = true;
|
||||
//lc.PlayerController.FixedSpeedSteps = 1;
|
||||
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleportForwardBackOnly;
|
||||
input.AimingController = OVRInput.Controller.Touch;
|
||||
ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
|
||||
var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
|
||||
orient.Thumbstick = OVRInput.Controller.Touch;
|
||||
}
|
||||
|
||||
/*
|
||||
void SetupRightTurnLeftTeleport()
|
||||
{
|
||||
SetupTeleportDefaults();
|
||||
TeleportController.EnableRotation(true, false, false, false);
|
||||
TeleportController.EnableMovement(false, false, false, false);
|
||||
lc.PlayerController.SnapRotation = true;
|
||||
lc.PlayerController.FixedSpeedSteps = 1;
|
||||
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleport;
|
||||
input.AimingController = OVRInput.Controller.LTouch;
|
||||
|
||||
ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
|
||||
var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
|
||||
orient.Thumbstick = OVRInput.Controller.LTouch;
|
||||
}
|
||||
*/
|
||||
|
||||
// Shut down teleport. Basically reverts to OVRPlayerController.
|
||||
void SetupWalkOnly()
|
||||
{
|
||||
SetupTeleportDefaults();
|
||||
TeleportController.enabled = false;
|
||||
lc.PlayerController.EnableLinearMovement = true;
|
||||
//lc.PlayerController.SnapRotation = true;
|
||||
lc.PlayerController.RotationEitherThumbstick = false;
|
||||
//lc.PlayerController.FixedSpeedSteps = 1;
|
||||
}
|
||||
|
||||
//
|
||||
void SetupLeftStrafeRightTeleport()
|
||||
{
|
||||
SetupTeleportDefaults();
|
||||
TeleportController.EnableRotation(true, false, false, true);
|
||||
TeleportController.EnableMovement(true, false, false, false);
|
||||
//lc.PlayerController.SnapRotation = true;
|
||||
//lc.PlayerController.FixedSpeedSteps = 1;
|
||||
|
||||
var input = TeleportController.GetComponent<TeleportInputHandlerTouch>();
|
||||
input.InputMode = TeleportInputHandlerTouch.InputModes.ThumbstickTeleportForwardBackOnly;
|
||||
input.AimingController = OVRInput.Controller.RTouch;
|
||||
ActivateHandlers<TeleportInputHandlerTouch, TeleportAimHandlerParabolic, TeleportTargetHandlerPhysical, TeleportOrientationHandlerThumbstick, TeleportTransitionBlink>();
|
||||
var orient = TeleportController.GetComponent<TeleportOrientationHandlerThumbstick>();
|
||||
orient.Thumbstick = OVRInput.Controller.RTouch;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d18bd321411666d42af10047cfc9d02f
|
||||
timeCreated: 1507048455
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user