├── .gitignore ├── Assets ├── LineDrawer.cs ├── LineDrawer.cs.meta ├── Materials.meta ├── Materials │ ├── GLSLTex3DShader.shader │ ├── GLSLTex3DShader.shader.meta │ ├── Tex3dMaterial.mat │ └── Tex3dMaterial.mat.meta ├── Math3D.cs ├── Math3D.cs.meta ├── Matrix4.cs ├── Matrix4.cs.meta ├── OVR.meta ├── OVR │ ├── Editor.meta │ ├── Editor │ │ ├── OVRBuild.cs │ │ ├── OVRBuild.cs.meta │ │ ├── OVRCameraControllerEditor.cs │ │ ├── OVRCameraControllerEditor.cs.meta │ │ ├── OVRDeviceEditor.cs │ │ ├── OVRDeviceEditor.cs.meta │ │ ├── OVREditorGUIUtility.cs │ │ ├── OVREditorGUIUtility.cs.meta │ │ ├── OVRPlayerControllerEditor.cs │ │ ├── OVRPlayerControllerEditor.cs.meta │ │ ├── OVRPrefabs.cs │ │ └── OVRPrefabs.cs.meta │ ├── OVRImageEffects.meta │ ├── OVRImageEffects │ │ ├── OVRImageEffectBase.cs │ │ ├── OVRImageEffectBase.cs.meta │ │ ├── OVRLensCorrection.cs │ │ ├── OVRLensCorrection.cs.meta │ │ ├── OVRLensCorrection.mat │ │ ├── OVRLensCorrection.mat.meta │ │ ├── OVRLensCorrection.shader │ │ └── OVRLensCorrection.shader.meta │ ├── Prefabs.meta │ ├── Prefabs │ │ ├── OVRCameraController.prefab │ │ ├── OVRCameraController.prefab.meta │ │ ├── OVRPlayerController.prefab │ │ └── OVRPlayerController.prefab.meta │ ├── Scripts.meta │ ├── Scripts │ │ ├── OVRCamera.cs │ │ ├── OVRCamera.cs.meta │ │ ├── OVRCameraController.cs │ │ ├── OVRCameraController.cs.meta │ │ ├── OVRComponent.cs │ │ ├── OVRComponent.cs.meta │ │ ├── OVRCrosshair.cs │ │ ├── OVRCrosshair.cs.meta │ │ ├── OVRDevice.cs │ │ ├── OVRDevice.cs.meta │ │ ├── OVRGamepadController.cs │ │ ├── OVRGamepadController.cs.meta │ │ ├── OVRMainMenu.cs │ │ ├── OVRMainMenu.cs.meta │ │ ├── OVRPlayerController.cs │ │ ├── OVRPlayerController.cs.meta │ │ ├── OVRPresetManager.cs │ │ └── OVRPresetManager.cs.meta │ ├── Textures.meta │ └── Textures │ │ ├── Black.png │ │ ├── Black.png.meta │ │ ├── CircleCrossHair.png │ │ ├── CircleCrossHair.png.meta │ │ ├── Crosshair.tga │ │ ├── Crosshair.tga.meta │ │ ├── Icons.meta │ │ └── Icons │ │ ├── OculusIcon.png │ │ └── OculusIcon.png.meta ├── Plugins.meta ├── Plugins │ ├── MITLicense_XInputDotNetPureDLL.txt │ ├── MITLicense_XInputDotNetPureDLL.txt.meta │ ├── MITLicense_XInputInterfaceDLL.txt │ ├── MITLicense_XInputInterfaceDLL.txt.meta │ ├── OculusPlugin.dll │ ├── OculusPlugin.dll.meta │ ├── XInputDotNetPure.dll │ ├── XInputDotNetPure.dll.meta │ ├── XInputInterface.dll │ └── XInputInterface.dll.meta ├── Scripts.meta ├── Scripts │ ├── CreateVolumetricAssets.cs │ ├── CreateVolumetricAssets.cs.meta │ ├── FlowField.cs │ ├── FlowField.cs.meta │ ├── MouseOrbit.js │ ├── MouseOrbit.js.meta │ ├── Perlin.cs │ ├── Perlin.cs.meta │ ├── Rotate.cs │ ├── Rotate.cs.meta │ ├── VolumeRenderer.cs │ └── VolumeRenderer.cs.meta ├── TestScene.unity ├── TestScene.unity.meta ├── Textures3D.meta ├── Textures3D │ ├── pyroclasticNoise.asset │ └── pyroclasticNoise.asset.meta ├── Timer.cs └── Timer.cs.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── InputManager.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Store this file at the root of your local repository. 2 | .DS_Store 3 | Library 4 | Temp 5 | Build 6 | Builds 7 | *.csproj 8 | *.pidb 9 | *.sln 10 | *.userprefs 11 | *.unityproj 12 | *.cache 13 | *.suo 14 | -------------------------------------------------------------------------------- /Assets/LineDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | 6 | //HAS TO BE ATTACHED TO THE CAMERA TO ACTUALLY DRAW ANYTHING 7 | //Pro only 8 | //How to get access to it: 9 | // LineDrawer ld = Camera.main.GetComponent(); 10 | 11 | public class LineDrawer : MonoBehaviour { 12 | private List _lineData; 13 | private List _colorData; 14 | private Color _color; 15 | private Material _lineMaterial; 16 | 17 | public void Start(){ 18 | Debug.Log("LineDrawer::Start()"); 19 | _color = Color.white; 20 | _lineData = new List(); 21 | _colorData = new List(); 22 | createLineMaterial(); 23 | } 24 | 25 | private void createLineMaterial() { 26 | if( !_lineMaterial ) { 27 | _lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" + 28 | "SubShader { Pass { " + 29 | " Blend SrcAlpha OneMinusSrcAlpha " + 30 | " ZWrite Off Cull Off Fog { Mode Off } " + 31 | " BindChannels {" + 32 | " Bind \"vertex\", vertex Bind \"color\", color }" + 33 | "} } }" ); 34 | _lineMaterial.hideFlags = HideFlags.HideAndDontSave; 35 | _lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; 36 | } 37 | } 38 | 39 | public void setColor(Color c){ 40 | _color = c; 41 | } 42 | 43 | public void drawLine(Vector3 p1, Vector3 p2, Color c1, Color c2){ 44 | _lineData.Add(p1); 45 | _lineData.Add(p2); 46 | _colorData.Add(c1); 47 | _colorData.Add(c2); 48 | } 49 | 50 | public void drawLine(Vector3 p1, Vector3 p2){ 51 | _lineData.Add(p1); 52 | _lineData.Add(p2); 53 | _colorData.Add(_color); 54 | _colorData.Add(_color); 55 | } 56 | 57 | public void OnPostRender() { 58 | //Debug.Log("Drawing " + _lineData.Count/2 + " lines"); 59 | // set the current material 60 | _lineMaterial.SetPass( 0 ); 61 | GL.PushMatrix(); 62 | GL.Begin( GL.LINES ); 63 | for(int i = 0; i < _lineData.Count; ++i){ 64 | GL.Color(_colorData[i]); 65 | GL.Vertex(_lineData[i]); 66 | } 67 | GL.End(); 68 | GL.PopMatrix(); 69 | 70 | _lineData.Clear(); 71 | _colorData.Clear(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Assets/LineDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8a83b7db8d0fd754d9c5252d6cb7a723 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5def8b58545fdd549b215a8be83601ec 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/GLSLTex3DShader.shader: -------------------------------------------------------------------------------- 1 | 2 | //Shaders start with the Shader keyword followed by shader's name in "" 3 | //Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom 4 | //Refer here : http://unity3d.com/support/documentation/Components/SL-Shader.html 5 | Shader "Custom/RayCastGLSL" 6 | { 7 | Properties 8 | { 9 | g_eyePos ("Eye Position", Vector) = (1.0, 1.0, 1.0, 1.0) 10 | g_lightPos ("Light Position", Vector) = (1.0, 1.0, 1.0, 1.0) 11 | g_lightIntensity ("Light Intensity", float) = 1.0 12 | g_absorption ("Absorption", float) = 1.0 13 | g_densityTex ("Density Texture", 3D) = "white"{} 14 | g_tex ("Text Texture", 2D) = "white"{} 15 | g_ambientLight ("Ambient Light", Color) = (0.2,0.2, 0.2, 1.0) 16 | 17 | } 18 | //Refer here : http://unity3d.com/support/documentation/Components/SL-SubShader.html 19 | SubShader 20 | { 21 | Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} 22 | LOD 200 23 | 24 | ZWrite Off 25 | Blend SrcAlpha OneMinusSrcAlpha 26 | //ColorMask RGB 27 | 28 | //Refer here : http://unity3d.com/support/documentation/Components/SL-Pass.html 29 | Pass 30 | { 31 | //Refer here : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html 32 | GLSLPROGRAM 33 | // Upgrade NOTE: excluded shader from DX11 and Xbox360; 34 | // has structs without semantics (struct v2f members position,uvw) 35 | //#pragma exclude_renderers d3d11 xbox360 36 | #pragma vertex vert 37 | #pragma fragment frag 38 | #pragma target 4.0 39 | #pragma glsl 40 | #pragma only_renderers opengl 41 | #include "UnityCG.glslinc" 42 | 43 | //uniform keyword not needed in unity but putting it in to be explicit 44 | uniform vec4 g_eyePos; 45 | uniform vec4 g_lightPos; 46 | uniform float g_lightIntensity; 47 | uniform float g_absorption; 48 | uniform sampler3D g_densityTex; 49 | uniform sampler2D g_tex; 50 | uniform vec4 g_ambientLight; 51 | 52 | varying vec3 textureCoordinates; 53 | 54 | #ifdef VERTEX 55 | void main() 56 | { 57 | gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 58 | textureCoordinates = vec3(gl_Color.xyz); 59 | //textureCoordinates.x = gl_Color.x * 0.5 - 0.5; 60 | //textureCoordinates.y = gl_Color.y * 0.5 - 0.5; 61 | //textureCoordinates.z = gl_Color.z * 0.5 - 0.5; 62 | 63 | } 64 | #endif 65 | 66 | 67 | #ifdef FRAGMENT 68 | void main() 69 | { 70 | //gl_FragColor = texture3D(g_densityTex, textureCoordinates); 71 | 72 | 73 | vec3 eyePos = vec3(g_eyePos.xyz); 74 | vec3 lightPos = vec3(g_lightPos.xyz); 75 | float absorption = g_absorption; 76 | float lightIntensity = g_lightIntensity; 77 | 78 | // diagonal of the cube 79 | float maxDist = sqrt(3.0); 80 | 81 | int numSamples = 128; 82 | float scale = maxDist/float(numSamples); 83 | 84 | int numLightSamples = 64; 85 | float lscale = maxDist / float(numLightSamples); 86 | 87 | // assume all coordinates are in texture space 88 | vec3 pos = textureCoordinates.xyz; 89 | vec3 eyeDir = normalize(pos-eyePos)*scale; 90 | 91 | float d = 0.0; 92 | float T = 1.0; 93 | vec3 Lo = vec3(0); 94 | for (int i=0; i < numSamples; ++i) 95 | { 96 | // sample density 97 | float density = texture3D(g_densityTex, pos).x; 98 | // skip empty space 99 | if (density > 0.0) 100 | { 101 | // attenuate ray-throughput 102 | d += density*scale*absorption; 103 | 104 | } 105 | pos += eyeDir; 106 | //pos = clamp(pos,0.0,1.0); 107 | } 108 | 109 | gl_FragColor.xyz = vec3(1); 110 | gl_FragColor.w = d; 111 | 112 | /* 113 | // transmittance 114 | float T = 1.0; 115 | // in-scattered radiance 116 | vec3 Lo = vec3(0.0); 117 | 118 | for (int i=0; i < numSamples; ++i) 119 | { 120 | // sample density 121 | float density = texture3D(g_densityTex, pos).x; 122 | // skip empty space 123 | if (density > 0.0) 124 | { 125 | // attenuate ray-throughput 126 | //T *= 1.0-density*scale*absorption; 127 | T *= 1.0-density*scale*absorption; 128 | if (T <= 0.01) 129 | break; 130 | 131 | // point light dir in texture space 132 | vec3 lightDir = normalize(lightPos-pos)*lscale; 133 | 134 | // sample light 135 | float Tl = 1.0; // transmittance along light ray 136 | vec3 lpos = pos + lightDir; 137 | 138 | for (int s=0; s < numLightSamples; ++s) 139 | { 140 | float ld = texture3D(g_densityTex, lpos).x; 141 | Tl *= 1.0-absorption*lscale*ld; 142 | //Tl *= 1.0-absorption*ld; 143 | 144 | if (Tl <= 0.01) 145 | break; 146 | 147 | lpos += lightDir; 148 | } 149 | 150 | vec3 Li = vec3(lightIntensity*Tl); 151 | Lo += Li*T*density*scale; 152 | } 153 | pos += eyeDir; 154 | } 155 | 156 | gl_FragColor.xyz = Lo + g_ambientLight.xyz; 157 | gl_FragColor.w = 1.0-T; 158 | */ 159 | 160 | } 161 | #endif 162 | ENDGLSL 163 | } 164 | } 165 | Fallback "Diffuse" 166 | } -------------------------------------------------------------------------------- /Assets/Materials/GLSLTex3DShader.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a9d9ec9c2d9bebf42bb167139891636d 3 | ShaderImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Materials/Tex3dMaterial.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/Materials/Tex3dMaterial.mat -------------------------------------------------------------------------------- /Assets/Materials/Tex3dMaterial.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: aead9b5e54ecf89449b969171b62dd41 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Math3D.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | public class Math3D : MonoBehaviour { 6 | 7 | private static Transform tempChild = null; 8 | private static Transform tempParent = null; 9 | 10 | public static void Init(){ 11 | 12 | tempChild = (new GameObject("Math3d_TempChild")).transform; 13 | tempParent = (new GameObject("Math3d_TempParent")).transform; 14 | 15 | tempChild.gameObject.hideFlags = HideFlags.HideAndDontSave; 16 | DontDestroyOnLoad(tempChild.gameObject); 17 | 18 | tempParent.gameObject.hideFlags = HideFlags.HideAndDontSave; 19 | DontDestroyOnLoad(tempParent.gameObject); 20 | 21 | //set the parent 22 | tempChild.parent = tempParent; 23 | } 24 | 25 | 26 | //increase or decrease the length of vector by size 27 | public static Vector3 AddVectorLength(Vector3 vector, float size){ 28 | 29 | //get the vector length 30 | float magnitude = Vector3.Magnitude(vector); 31 | 32 | //change the length 33 | magnitude += size; 34 | 35 | //normalize the vector 36 | Vector3 vectorNormalized = Vector3.Normalize(vector); 37 | 38 | //scale the vector 39 | return Vector3.Scale(vectorNormalized, new Vector3(magnitude, magnitude, magnitude)); 40 | } 41 | 42 | //create a vector of direction "vector" with length "size" 43 | public static Vector3 SetVectorLength(Vector3 vector, float size){ 44 | 45 | //normalize the vector 46 | Vector3 vectorNormalized = Vector3.Normalize(vector); 47 | 48 | //scale the vector 49 | return vectorNormalized *= size; 50 | } 51 | 52 | 53 | //caclulate the rotational difference from A to B 54 | public static Quaternion SubtractRotation(Quaternion B, Quaternion A){ 55 | 56 | Quaternion C = Quaternion.Inverse(A) * B; 57 | return C; 58 | } 59 | 60 | //Find the line of intersection between two planes. The planes are defined by a normal and a point on that plane. 61 | //The outputs are a point on the line and a vector which indicates it's direction. If the planes are not parallel, 62 | //the function outputs true, otherwise false. 63 | public static bool PlanePlaneIntersection(out Vector3 linePoint, out Vector3 lineVec, Vector3 plane1Normal, Vector3 plane1Position, Vector3 plane2Normal, Vector3 plane2Position){ 64 | 65 | linePoint = Vector3.zero; 66 | lineVec = Vector3.zero; 67 | 68 | //We can get the direction of the line of intersection of the two planes by calculating the 69 | //cross product of the normals of the two planes. Note that this is just a direction and the line 70 | //is not fixed in space yet. We need a point for that to go with the line vector. 71 | lineVec = Vector3.Cross(plane1Normal, plane2Normal); 72 | 73 | //Next is to calculate a point on the line to fix it's position in space. This is done by finding a vector from 74 | //the plane2 location, moving parallel to it's plane, and intersecting plane1. To prevent rounding 75 | //errors, this vector also has to be perpendicular to lineDirection. To get this vector, calculate 76 | //the cross product of the normal of plane2 and the lineDirection. 77 | Vector3 ldir = Vector3.Cross(plane2Normal, lineVec); 78 | 79 | float denominator = Vector3.Dot(plane1Normal, ldir); 80 | 81 | //Prevent divide by zero and rounding errors by requiring about 5 degrees angle between the planes. 82 | if(Mathf.Abs(denominator) > 0.006f){ 83 | 84 | Vector3 plane1ToPlane2 = plane1Position - plane2Position; 85 | float t = Vector3.Dot(plane1Normal, plane1ToPlane2) / denominator; 86 | linePoint = plane2Position + t * ldir; 87 | 88 | return true; 89 | } 90 | 91 | //output not valid 92 | else{ 93 | return false; 94 | } 95 | } 96 | 97 | //Get the intersection between a line and a plane. 98 | //If the line and plane are not parallel, the function outputs true, otherwise false. 99 | public static bool LinePlaneIntersection(out Vector3 intersection, Vector3 linePoint, Vector3 lineVec, Vector3 planeNormal, Vector3 planePoint){ 100 | 101 | float length; 102 | float dotNumerator; 103 | float dotDenominator; 104 | Vector3 vector; 105 | intersection = Vector3.zero; 106 | 107 | //calculate the distance between the linePoint and the line-plane intersection point 108 | dotNumerator = Vector3.Dot((planePoint - linePoint), planeNormal); 109 | dotDenominator = Vector3.Dot(lineVec, planeNormal); 110 | 111 | //line and plane are not parallel 112 | if(dotDenominator != 0.0f){ 113 | length = dotNumerator / dotDenominator; 114 | 115 | //create a vector from the linePoint to the intersection point 116 | vector = SetVectorLength(lineVec, length); 117 | 118 | //get the coordinates of the line-plane intersection point 119 | intersection = linePoint + vector; 120 | 121 | return true; 122 | } 123 | 124 | //output not valid 125 | else{ 126 | return false; 127 | } 128 | } 129 | 130 | //Calculate the intersection point of two lines. Returns true if lines intersect, otherwise false. 131 | //Note that in 3d, two lines do not intersect most of the time. So if the two lines are not in the 132 | //same plane, use ClosestPointsOnTwoLines() instead. 133 | public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){ 134 | 135 | intersection = Vector3.zero; 136 | 137 | Vector3 lineVec3 = linePoint2 - linePoint1; 138 | Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2); 139 | Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2); 140 | 141 | float planarFactor = Vector3.Dot(lineVec3, crossVec1and2); 142 | 143 | //Lines are not coplanar. Take into account rounding errors. 144 | if((planarFactor >= 0.00001f) || (planarFactor <= -0.00001f)){ 145 | 146 | return false; 147 | } 148 | 149 | //Note: sqrMagnitude does x*x+y*y+z*z on the input vector. 150 | float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude; 151 | 152 | if((s >= 0.0f) && (s <= 1.0f)){ 153 | 154 | intersection = linePoint1 + (lineVec1 * s); 155 | return true; 156 | } 157 | 158 | else{ 159 | return false; 160 | } 161 | } 162 | 163 | //Two non-parallel lines which may or may not touch each other have a point on each line which are closest 164 | //to each other. This function finds those two points. If the lines are not parallel, the function 165 | //outputs true, otherwise false. 166 | public static bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){ 167 | 168 | closestPointLine1 = Vector3.zero; 169 | closestPointLine2 = Vector3.zero; 170 | 171 | float a = Vector3.Dot(lineVec1, lineVec1); 172 | float b = Vector3.Dot(lineVec1, lineVec2); 173 | float e = Vector3.Dot(lineVec2, lineVec2); 174 | 175 | float d = a*e - b*b; 176 | 177 | //lines are not parallel 178 | if(d != 0.0f){ 179 | 180 | Vector3 r = linePoint1 - linePoint2; 181 | float c = Vector3.Dot(lineVec1, r); 182 | float f = Vector3.Dot(lineVec2, r); 183 | 184 | float s = (b*f - c*e) / d; 185 | float t = (a*f - c*b) / d; 186 | 187 | closestPointLine1 = linePoint1 + lineVec1 * s; 188 | closestPointLine2 = linePoint2 + lineVec2 * t; 189 | 190 | return true; 191 | } 192 | 193 | else{ 194 | return false; 195 | } 196 | } 197 | 198 | //This function returns a point which is a projection from a point to a line. 199 | public static Vector3 ProjectPointOnLine(Vector3 linePoint, Vector3 lineVec, Vector3 point){ 200 | 201 | //get vector from point on line to point in space 202 | Vector3 linePointToPoint = point - linePoint; 203 | 204 | float t = Vector3.Dot(linePointToPoint, lineVec); 205 | 206 | return linePoint + lineVec * t; 207 | } 208 | 209 | //This function returns a point which is a projection from a point to a plane. 210 | public static Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point){ 211 | 212 | float distance; 213 | Vector3 translationVector; 214 | 215 | //First calculate the distance from the point to the plane: 216 | distance = SignedDistancePlanePoint(planeNormal, planePoint, point); 217 | 218 | //Reverse the sign of the distance 219 | distance *= -1; 220 | 221 | //Get a translation vector 222 | translationVector = SetVectorLength(planeNormal, distance); 223 | 224 | //Translate the point to form a projection 225 | return point + translationVector; 226 | } 227 | 228 | //Projects a vector onto a plane. The output is not normalized. 229 | public static Vector3 ProjectVectorOnPlane(Vector3 planeNormal, Vector3 vector){ 230 | 231 | return vector - (Vector3.Dot(vector, planeNormal) * planeNormal); 232 | } 233 | 234 | //Get the shortest distance between a point and a plane. The output is signed so it holds information 235 | //as to which side of the plane normal the point is. 236 | public static float SignedDistancePlanePoint(Vector3 planeNormal, Vector3 planePoint, Vector3 point){ 237 | 238 | return Vector3.Dot(planeNormal, (point - planePoint)); 239 | } 240 | 241 | //This function calculates a signed (+ or - sign instead of being ambiguous) dot product. It is basically used 242 | //to figure out whether a vector is positioned to the left or right of another vector. The way this is done is 243 | //by calculating a vector perpendicular to one of the vectors and using that as a reference. This is because 244 | //the result of a dot product only has signed information when an angle is transitioning between more or less 245 | //then 90 degrees. 246 | public static float SignedDotProduct(Vector3 vectorA, Vector3 vectorB, Vector3 normal){ 247 | 248 | Vector3 perpVector; 249 | float dot; 250 | 251 | //Use the geometry object normal and one of the input vectors to calculate the perpendicular vector 252 | perpVector = Vector3.Cross(normal, vectorA); 253 | 254 | //Now calculate the dot product between the perpendicular vector (perpVector) and the other input vector 255 | dot = Vector3.Dot(perpVector, vectorB); 256 | 257 | return dot; 258 | } 259 | 260 | //Calculate the angle between a vector and a plane. The plane is made by a normal vector. 261 | //Output is in radians. 262 | public static float AngleVectorPlane(Vector3 vector, Vector3 normal){ 263 | 264 | float dot; 265 | float angle; 266 | 267 | //calculate the the dot product between the two input vectors. This gives the cosine between the two vectors 268 | dot = Vector3.Dot(vector, normal); 269 | 270 | //this is in radians 271 | angle = (float)Math.Acos(dot); 272 | 273 | return 1.570796326794897f - angle; //90 degrees - angle 274 | } 275 | 276 | //Calculate the dot product as an angle 277 | public static float DotProductAngle(Vector3 vec1, Vector3 vec2){ 278 | 279 | double dot; 280 | double angle; 281 | 282 | //get the dot product 283 | dot = Vector3.Dot(vec1, vec2); 284 | 285 | //Clamp to prevent NaN error. Shouldn't need this in the first place, but there could be a rounding error issue. 286 | if(dot < -1.0f){ 287 | dot = -1.0f; 288 | } 289 | if(dot > 1.0f){ 290 | dot =1.0f; 291 | } 292 | 293 | //Calculate the angle. The output is in radians 294 | //This step can be skipped for optimization... 295 | angle = Math.Acos(dot); 296 | 297 | return (float)angle; 298 | } 299 | 300 | //Convert a plane defined by 3 points to a plane defined by a vector and a point. 301 | //The plane point is the middle of the triangle defined by the 3 points. 302 | public static void PlaneFrom3Points(out Vector3 planeNormal, out Vector3 planePoint, Vector3 pointA, Vector3 pointB, Vector3 pointC){ 303 | 304 | planeNormal = Vector3.zero; 305 | planePoint = Vector3.zero; 306 | 307 | //Make two vectors from the 3 input points, originating from point A 308 | Vector3 AB = pointB - pointA; 309 | Vector3 AC = pointC - pointA; 310 | 311 | //Calculate the normal 312 | planeNormal = Vector3.Normalize(Vector3.Cross(AB, AC)); 313 | 314 | //Get the points in the middle AB and AC 315 | Vector3 middleAB = pointA + (AB / 2.0f); 316 | Vector3 middleAC = pointA + (AC / 2.0f); 317 | 318 | //Get vectors from the middle of AB and AC to the point which is not on that line. 319 | Vector3 middleABtoC = pointC - middleAB; 320 | Vector3 middleACtoB = pointB - middleAC; 321 | 322 | //Calculate the intersection between the two lines. This will be the center 323 | //of the triangle defined by the 3 points. 324 | //We could use LineLineIntersection instead of ClosestPointsOnTwoLines but due to rounding errors 325 | //this sometimes doesn't work. 326 | Vector3 temp; 327 | ClosestPointsOnTwoLines(out planePoint, out temp, middleAB, middleABtoC, middleAC, middleACtoB); 328 | } 329 | 330 | //Returns the forward vector of a quaternion 331 | public static Vector3 GetForwardVector(Quaternion q){ 332 | 333 | return q * Vector3.forward; 334 | } 335 | 336 | //Returns the up vector of a quaternion 337 | public static Vector3 GetUpVector(Quaternion q){ 338 | 339 | return q * Vector3.up; 340 | } 341 | 342 | //Returns the right vector of a quaternion 343 | public static Vector3 GetRightVector(Quaternion q){ 344 | 345 | return q * Vector3.right; 346 | } 347 | 348 | //Gets a quaternion from a matrix 349 | public static Quaternion QuaternionFromMatrix(Matrix4x4 m){ 350 | 351 | return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1)); 352 | } 353 | 354 | //Gets a position from a matrix 355 | public static Vector3 PositionFromMatrix(Matrix4x4 m){ 356 | 357 | Vector4 vector4Position = m.GetColumn(3); 358 | return new Vector3(vector4Position.x, vector4Position.y, vector4Position.z); 359 | } 360 | 361 | //This is an alternative for Quaternion.LookRotation. Instead of aligning the forward and up vector of the game 362 | //object with the input vectors, a custom direction can be used instead of the fixed forward and up vectors. 363 | //alignWithVector and alignWithNormal are in world space. 364 | //customForward and customUp are in object space. 365 | //Usage: use alignWithVector and alignWithNormal as if you are using the default LookRotation function. 366 | //Set customForward and customUp to the vectors you wish to use instead of the default forward and up vectors. 367 | public static void LookRotationExtended(ref GameObject gameObjectInOut, Vector3 alignWithVector, Vector3 alignWithNormal, Vector3 customForward, Vector3 customUp){ 368 | 369 | //Set the rotation of the destination 370 | Quaternion rotationA = Quaternion.LookRotation(alignWithVector, alignWithNormal); 371 | 372 | //Set the rotation of the custom normal and up vectors. 373 | //When using the default LookRotation function, this would be hard coded to the forward and up vector. 374 | Quaternion rotationB = Quaternion.LookRotation(customForward, customUp); 375 | 376 | //Calculate the rotation 377 | gameObjectInOut.transform.rotation = rotationA * Quaternion.Inverse(rotationB); 378 | } 379 | 380 | //This function transforms one object as if it was parented to the other. 381 | //Before using this function, the Init() function must be called 382 | //Input: parentRotation and parentPosition: the current parent transform. 383 | //Input: startParentRotation and startParentPosition: the transform of the parent object at the time the objects are parented. 384 | //Input: startChildRotation and startChildPosition: the transform of the child object at the time the objects are parented. 385 | //Output: childRotation and childPosition. 386 | //All transforms are in world space. 387 | public static void TransformWithParent(out Quaternion childRotation, out Vector3 childPosition, Quaternion parentRotation, Vector3 parentPosition, Quaternion startParentRotation, Vector3 startParentPosition, Quaternion startChildRotation, Vector3 startChildPosition){ 388 | 389 | childRotation = Quaternion.identity; 390 | childPosition = Vector3.zero; 391 | 392 | //set the parent start transform 393 | tempParent.rotation = startParentRotation; 394 | tempParent.position = startParentPosition; 395 | tempParent.localScale = Vector3.one; //to prevent scale wandering 396 | 397 | //set the child start transform 398 | tempChild.rotation = startChildRotation; 399 | tempChild.position = startChildPosition; 400 | tempChild.localScale = Vector3.one; //to prevent scale wandering 401 | 402 | //translate and rotate the child by moving the parent 403 | tempParent.rotation = parentRotation; 404 | tempParent.position = parentPosition; 405 | 406 | //get the child transform 407 | childRotation = tempChild.rotation; 408 | childPosition = tempChild.position; 409 | } 410 | 411 | //With this function you can align a triangle of an object with any transform. 412 | //Usage: gameObjectInOut is the game object you want to transform. 413 | //alignWithVector, alignWithNormal, and alignWithPosition is the transform with which the triangle of the object should be aligned with. 414 | //triangleForward, triangleNormal, and trianglePosition is the transform of the triangle from the object. 415 | //alignWithVector, alignWithNormal, and alignWithPosition are in world space. 416 | //triangleForward, triangleNormal, and trianglePosition are in object space. 417 | //trianglePosition is the mesh position of the triangle. The effect of the scale of the object is handled automatically. 418 | //trianglePosition can be set at any position, it does not have to be at a vertex or in the middle of the triangle. 419 | public static void PreciseAlign(ref GameObject gameObjectInOut, Vector3 alignWithVector, Vector3 alignWithNormal, Vector3 alignWithPosition, Vector3 triangleForward, Vector3 triangleNormal, Vector3 trianglePosition){ 420 | 421 | //Set the rotation. 422 | LookRotationExtended(ref gameObjectInOut, alignWithVector, alignWithNormal, triangleForward, triangleNormal); 423 | 424 | //Get the world space position of trianglePosition 425 | Vector3 trianglePositionWorld = gameObjectInOut.transform.TransformPoint(trianglePosition); 426 | 427 | //Get a vector from trianglePosition to alignWithPosition 428 | Vector3 translateVector = alignWithPosition - trianglePositionWorld; 429 | 430 | //Now transform the object so the triangle lines up correctly. 431 | gameObjectInOut.transform.Translate(translateVector, Space.World); 432 | } 433 | } 434 | 435 | //Convert a position, direction, and normal vector to a transform 436 | /*void VectorsToTransform(ref GameObject gameObjectInOut, Vector3 positionVector, Vector3 directionVector, Vector3 normalVector){ 437 | 438 | gameObjectInOut.transform.position = positionVector; 439 | gameObjectInOut.transform.rotation = Quaternion.LookRotation(directionVector, normalVector); 440 | }*/ -------------------------------------------------------------------------------- /Assets/Math3D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0c7b2221be4174545892f0181c874a87 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Matrix4.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Matrix4 { 5 | public float[] m; 6 | 7 | private int IX(int i, int j){ 8 | return i + 4* j; 9 | } 10 | 11 | public Matrix4(){ 12 | m = new float[16]; 13 | identity(); 14 | } 15 | 16 | public void identity(){ 17 | for(int i = 0; i < 4; ++i){ 18 | for(int j = 0; j < 4; ++j){ 19 | if(i == j) 20 | m[IX(i,j)] = 1.0f; 21 | else 22 | m[IX(i,j)] = 0.0f; 23 | } 24 | } 25 | } 26 | 27 | public void rotationXY(float radians){ 28 | /* 29 | 1 0 0 0 30 | 0 1 0 0 31 | 0 0 c -s 32 | 0 0 s c 33 | */ 34 | identity(); 35 | float c = Mathf.Cos(radians); 36 | float s = Mathf.Sin(radians); 37 | m[IX(2,2)] = c; 38 | m[IX(2,3)] = -s; 39 | m[IX(3,2)] = s; 40 | m[IX(3,3)] = c; 41 | 42 | } 43 | 44 | 45 | public void rotationZX(float radians){ 46 | /* 47 | 1 0 0 0 48 | 0 c 0 -s 49 | 0 0 1 0 50 | 0 s 0 c 51 | */ 52 | identity(); 53 | float c = Mathf.Cos(radians); 54 | float s = Mathf.Sin(radians); 55 | m[IX(1,1)] = c; 56 | m[IX(2,3)] = -s; 57 | m[IX(3,1)] = s; 58 | m[IX(3,3)] = c; 59 | 60 | } 61 | 62 | 63 | public void rotationXW(float radians){ 64 | /* 65 | 1 0 0 0 66 | 0 c -s 0 67 | 0 s c 0 68 | 0 0 0 1 69 | */ 70 | identity(); 71 | float c = Mathf.Cos(radians); 72 | float s = Mathf.Sin(radians); 73 | m[IX(1,1)] = c; 74 | m[IX(1,2)] = -s; 75 | m[IX(2,1)] = s; 76 | m[IX(2,2)] = c; 77 | 78 | } 79 | 80 | public void rotationYZ(float radians){ 81 | /* 82 | c 0 0 -s 83 | 0 1 0 0 84 | 0 0 1 0 85 | s 0 0 c 86 | */ 87 | identity(); 88 | float c = Mathf.Cos(radians); 89 | float s = Mathf.Sin(radians); 90 | m[IX(0,0)] = c; 91 | m[IX(0,3)] = -s; 92 | m[IX(3,0)] = s; 93 | m[IX(3,3)] = c; 94 | 95 | } 96 | 97 | public void rotationWY(float radians){ 98 | /* 99 | c 0 -s 0 100 | 0 1 0 0 101 | s 0 c 0 102 | 0 0 0 1 103 | */ 104 | identity(); 105 | float c = Mathf.Cos(radians); 106 | float s = Mathf.Sin(radians); 107 | m[IX(0,0)] = c; 108 | m[IX(0,2)] = -s; 109 | m[IX(2,0)] = s; 110 | m[IX(2,2)] = c; 111 | 112 | } 113 | 114 | public void rotationZW(float radians){ 115 | /* 116 | c -s 0 0 117 | s c 0 0 118 | 0 0 1 0 119 | 0 0 0 1 120 | */ 121 | identity(); 122 | float c = Mathf.Cos(radians); 123 | float s = Mathf.Sin(radians); 124 | m[IX(0,0)] = c; 125 | m[IX(0,1)] = -s; 126 | m[IX(1,0)] = s; 127 | m[IX(1,1)] = c; 128 | 129 | } 130 | 131 | public Vector4 multiplyVec4(Vector4 v){ 132 | Vector4 r = new Vector4(); 133 | r.x = Vector4.Dot(v,new Vector4(m[IX(0,0)], m[IX(1,0)], m[IX(2,0)], m[IX(3,0)])); 134 | r.y = Vector4.Dot(v,new Vector4(m[IX(0,1)], m[IX(1,1)], m[IX(2,1)], m[IX(3,1)])); 135 | r.z = Vector4.Dot(v,new Vector4(m[IX(0,2)], m[IX(1,2)], m[IX(2,2)], m[IX(3,2)])); 136 | r.w = Vector4.Dot(v,new Vector4(m[IX(0,3)], m[IX(1,3)], m[IX(2,3)], m[IX(3,3)])); 137 | return r; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /Assets/Matrix4.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 87eeb93d175d7f14fac8fae54d1ca51a 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 869fc009969608e4a9071237c584be0d 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 76c19abd24bec62459b5f0d26fdd9a85 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRBuild.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRBuild.cs 4 | Content : Rift editor interface. 5 | This script adds the ability to build demo from within Unity and from 6 | command line 7 | Created : February 14, 2013 8 | Authors : Peter Giokaris 9 | 10 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 11 | 12 | Use of this software is subject to the terms of the Oculus LLC license 13 | agreement provided at the time of installation or download, or which 14 | otherwise accompanies this software in either electronic or hard copy form. 15 | 16 | ************************************************************************************/ 17 | using UnityEngine; 18 | using UnityEditor; 19 | 20 | //------------------------------------------------------------------------------------- 21 | // ***** OculusBuild 22 | // 23 | // OculusBuild adds menu functionality for a user to build the currently selected scene, 24 | // and to also build and run the standalone build. These menu items can be found under the 25 | // Oculus/Build menu from the main Unity Editor menu bar. 26 | // 27 | 28 | class OculusBuild 29 | { 30 | // Build the standalone Windows demo and place into main project folder 31 | [MenuItem ("Oculus/Build/StandaloneWindows")] 32 | static void PerformBuildStandaloneWindows () 33 | { 34 | if(Application.isEditor) 35 | { 36 | string[] scenes = { EditorApplication.currentScene }; 37 | BuildPipeline.BuildPlayer(scenes, "OculusUnityDemoScene.exe", BuildTarget.StandaloneWindows, BuildOptions.None); 38 | } 39 | } 40 | 41 | // Build the standalone Windows demo and place into main project folder, and then run 42 | [MenuItem ("Oculus/Build/StandaloneWindows - Run")] 43 | static void PerformBuildStandaloneWindowsRun () 44 | { 45 | if(Application.isEditor) 46 | { 47 | string[] scenes = { EditorApplication.currentScene }; 48 | BuildPipeline.BuildPlayer(scenes, "OculusUnityDemoScene.exe", BuildTarget.StandaloneWindows, BuildOptions.AutoRunPlayer); 49 | } 50 | else 51 | { 52 | string[] scenes = { "Assets/Tuscany/Scenes/VRDemo_Tuscany.unity" }; 53 | BuildPipeline.BuildPlayer(scenes, "OculusUnityDemoScene.exe", BuildTarget.StandaloneWindows, BuildOptions.AutoRunPlayer); 54 | } 55 | } 56 | } 57 | 58 | //------------------------------------------------------------------------------------- 59 | // ***** OculusBuildDemo 60 | // 61 | // OculusBuild allows for command line building of the Oculus main demo (Tuscany). 62 | // 63 | class OculusBuildDemo 64 | { 65 | static void PerformBuildStandaloneWindows () 66 | { 67 | string[] scenes = { "Assets/Tuscany/Scenes/VRDemo_Tuscany.unity" }; 68 | BuildPipeline.BuildPlayer(scenes, "OculusUnityDemoScene.exe", BuildTarget.StandaloneWindows, BuildOptions.None); 69 | } 70 | 71 | static void PerformBuildStandaloneWindowsRun () 72 | { 73 | string[] scenes = { "Assets/Tuscany/Scenes/VRDemo_Tuscany.unity" }; 74 | BuildPipeline.BuildPlayer(scenes, "OculusUnityDemoScene.exe", BuildTarget.StandaloneWindows, BuildOptions.AutoRunPlayer); 75 | } 76 | } -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRBuild.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fef522d0247215a43be6b1a8819bd940 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRCameraControllerEditor.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRCameraControllerEditor.cs 4 | Content : Player controller interface. 5 | This script adds editor functionality to the OVRCameraController 6 | Created : March 06, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEditor; 17 | using UnityEngine; 18 | using System.Collections.Generic; 19 | 20 | [CustomEditor(typeof(OVRCameraController))] 21 | 22 | //------------------------------------------------------------------------------------- 23 | // ***** OVRCameraControllerEditor 24 | // 25 | // OVRCameraControllerEditor adds extra functionality in the inspector for the currently 26 | // selected OVRCameraController. 27 | // 28 | public class OVRCameraControllerEditor : Editor 29 | { 30 | // target component 31 | private OVRCameraController m_Component; 32 | 33 | // OnEnable 34 | void OnEnable() 35 | { 36 | m_Component = (OVRCameraController)target; 37 | } 38 | 39 | // OnDestroy 40 | void OnDestroy() 41 | { 42 | } 43 | 44 | // OnInspectorGUI 45 | public override void OnInspectorGUI() 46 | { 47 | GUI.color = Color.white; 48 | 49 | Undo.SetSnapshotTarget(m_Component, "OVRCameraController"); 50 | 51 | { 52 | /* 53 | m_Component.NeckPosition = EditorGUILayout.Vector3Field("Neck Position", m_Component.NeckPosition); 54 | m_Component.EyeCenterPosition = EditorGUILayout.Vector3Field("Eye Center Position", m_Component.EyeCenterPosition); 55 | OVREditorGUIUtility.Separator(); 56 | m_Component.TrackerRotatesY = EditorGUILayout.Toggle("Tracker Rotates Y", m_Component.TrackerRotatesY); 57 | OVREditorGUIUtility.Separator(); 58 | m_Component.BackgroundColor = EditorGUILayout.ColorField("Background Color", m_Component.BackgroundColor); 59 | OVREditorGUIUtility.Separator(); 60 | */ 61 | DrawDefaultInspector (); 62 | } 63 | 64 | if (GUI.changed) 65 | { 66 | Undo.CreateSnapshot(); 67 | Undo.RegisterSnapshot(); 68 | EditorUtility.SetDirty(target); 69 | } 70 | 71 | Undo.ClearSnapshotTarget(); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRCameraControllerEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e6dc85b66406d9046b220fab7f3b3dd0 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRDeviceEditor.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRDeviceEditor.cs 4 | Content : Rift editor interface. 5 | This script adds editor functionality to the OVRRift class 6 | Created : February 14, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEditor; 17 | using UnityEngine; 18 | using System.Collections.Generic; 19 | 20 | [CustomEditor(typeof(OVRDevice))] 21 | 22 | //------------------------------------------------------------------------------------- 23 | // ***** OVRDeviceEditor 24 | // 25 | // OVRDeviceEditor adds extra functionality into the inspector for the currently selected 26 | // OVRRift component. 27 | // 28 | public class OVRDeviceEditor : Editor 29 | { 30 | // target component 31 | private OVRDevice m_Component; 32 | 33 | // OnEnable 34 | void OnEnable() 35 | { 36 | m_Component = (OVRDevice)target; 37 | } 38 | 39 | // OnDestroy 40 | void OnDestroy() 41 | { 42 | } 43 | 44 | // OnInspectorGUI 45 | public override void OnInspectorGUI() 46 | { 47 | GUI.color = Color.white; 48 | { 49 | m_Component.InitialPredictionTime = EditorGUILayout.Slider("Prediction Time", m_Component.InitialPredictionTime, 0, 0.1f); 50 | m_Component.InitialAccelGain = EditorGUILayout.Slider("Accel Gain", m_Component.InitialAccelGain, 0, 0.1f); 51 | } 52 | 53 | if (GUI.changed) 54 | { 55 | EditorUtility.SetDirty(target); 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRDeviceEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: acc8173aa7a3e77418239a55877f081f 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVREditorGUIUtility.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVREditorGUIUtility.cs 4 | Content : Player controller interface. 5 | This script adds extended editor functionality 6 | Created : January 17, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEngine; 17 | using UnityEditor; 18 | 19 | //------------------------------------------------------------------------------------- 20 | // ***** OVREditorGUIUtility 21 | // 22 | // OVREditorGUIUtility contains a collection of GUI utilities for editor classes. 23 | // 24 | public static class OVREditorGUIUtility 25 | { 26 | public static void Separator() 27 | { 28 | GUI.color = new Color(1, 1, 1, 0.25f); 29 | GUILayout.Box("", "HorizontalSlider", GUILayout.Height(16)); 30 | GUI.color = Color.white; 31 | } 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVREditorGUIUtility.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 670148e1fd209b84dac5465524339484 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRPlayerControllerEditor.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRPlayerControllerEditor.cs 4 | Content : Player controller interface. 5 | This script adds editor functionality to the OVRPlayerController 6 | Created : January 17, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEditor; 17 | using UnityEngine; 18 | using System.Collections.Generic; 19 | 20 | [CustomEditor(typeof(OVRPlayerController))] 21 | 22 | //------------------------------------------------------------------------------------- 23 | // ***** OVRPlayerControllerEditor 24 | // 25 | // OVRPlayerControllerEditor adds extra functionality in the inspector for the currently 26 | // selected OVRPlayerController. 27 | // 28 | public class OVRPlayerControllerEditor : Editor 29 | { 30 | // target component 31 | private OVRPlayerController m_Component; 32 | 33 | // foldouts 34 | private static bool m_MotorFoldout; 35 | private static bool m_PhysicsFoldout; 36 | 37 | // OnEnable 38 | void OnEnable() 39 | { 40 | m_Component = (OVRPlayerController)target; 41 | } 42 | 43 | // OnDestroy 44 | void OnDestroy() 45 | { 46 | } 47 | 48 | // OnInspectorGUI 49 | public override void OnInspectorGUI() 50 | { 51 | GUI.color = Color.white; 52 | 53 | { 54 | m_MotorFoldout = EditorGUILayout.Foldout(m_MotorFoldout, "Motor"); 55 | 56 | if (m_MotorFoldout) 57 | { 58 | m_Component.Acceleration = EditorGUILayout.Slider("Acceleration", m_Component.Acceleration, 0, 1); 59 | m_Component.Damping = EditorGUILayout.Slider("Damping", m_Component.Damping, 0, 1); 60 | m_Component.JumpForce = EditorGUILayout.Slider("Jump Force", m_Component.JumpForce, 0, 10); 61 | m_Component.RotationAmount = EditorGUILayout.Slider("Rotation Amount", m_Component.RotationAmount, 0, 5); 62 | 63 | OVREditorGUIUtility.Separator(); 64 | } 65 | 66 | m_PhysicsFoldout = EditorGUILayout.Foldout(m_PhysicsFoldout, "Physics"); 67 | 68 | if (m_PhysicsFoldout) 69 | { 70 | m_Component.GravityModifier = EditorGUILayout.Slider("Gravity Modifier", m_Component.GravityModifier, 0, 1); 71 | 72 | OVREditorGUIUtility.Separator(); 73 | } 74 | } 75 | 76 | if (GUI.changed) 77 | { 78 | EditorUtility.SetDirty(target); 79 | } 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRPlayerControllerEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c9d2ea2e435b2374b8fc71a3b8c18678 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRPrefabs.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRPrefabs.cs 4 | Content : Prefab creation editor interface. 5 | This script adds the ability to add OVR prefabs into the scene. 6 | Created : February 19, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEngine; 17 | using System.Collections; 18 | using UnityEditor; 19 | 20 | //------------------------------------------------------------------------------------- 21 | // ***** OVRPrefabs 22 | // 23 | // OculusPrefabs adds menu items under the Oculus main menu. It allows for quick creation 24 | // of the main Oculus prefabs without having to open the Prefab folder and dragging/dropping 25 | // into the scene. 26 | class OVRPrefabs 27 | { 28 | [MenuItem ("Oculus/Prefabs/OVRCameraController")] 29 | static void CreateOVRCameraController () 30 | { 31 | Object ovrcam = AssetDatabase.LoadAssetAtPath ("Assets/OVR/Prefabs/OVRCameraController.prefab", typeof(UnityEngine.Object)); 32 | PrefabUtility.InstantiatePrefab(ovrcam); 33 | } 34 | 35 | [MenuItem ("Oculus/Prefabs/OVRPlayerController")] 36 | static void CreateOVRPlayerController () 37 | { 38 | Object ovrcam = AssetDatabase.LoadAssetAtPath ("Assets/OVR/Prefabs/OVRPlayerController.prefab", typeof(UnityEngine.Object)); 39 | PrefabUtility.InstantiatePrefab(ovrcam); 40 | } 41 | } -------------------------------------------------------------------------------- /Assets/OVR/Editor/OVRPrefabs.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 819d67a67d183c34ab9970fb25b42d13 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3cc6d579e468e8b4e8a9830a1b2f3628 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRImageEffectBase.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRImageEffectBase.cs 4 | Content : Full screen image effect. 5 | This script is a base class for Unity image effects 6 | component 7 | Created : February 21, 2013 8 | Authors : Peter Giokaris 9 | 10 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 11 | 12 | Use of this software is subject to the terms of the Oculus LLC license 13 | agreement provided at the time of installation or download, or which 14 | otherwise accompanies this software in either electronic or hard copy form. 15 | 16 | ************************************************************************************/ 17 | using UnityEngine; 18 | 19 | [RequireComponent(typeof(Camera))] 20 | [AddComponentMenu("")] 21 | 22 | //------------------------------------------------------------------------------------- 23 | // ***** OVRImageEffectBase 24 | // 25 | // OVRImageEffectBase is a base class to be used for full screen image effects. 26 | // It will keep the effect from being enabled if Unity does not support it. 27 | // 28 | public class OVRImageEffectBase : MonoBehaviour 29 | { 30 | /// Provides a shader property that is set in the inspector 31 | /// and a material instantiated from the shader 32 | public Material material; 33 | 34 | protected void Start () 35 | { 36 | // Disable if we don't support image effects 37 | if (!SystemInfo.supportsImageEffects) 38 | { 39 | enabled = false; 40 | return; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRImageEffectBase.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3708edefcf101cc4bb996c983224db49 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRLensCorrection.cs 4 | Content : Full screen image effect. 5 | This script is used to add full-screen lens correction on a camera 6 | component 7 | Created : January 17, 2013 8 | Authors : Peter Giokaris 9 | 10 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 11 | 12 | Use of this software is subject to the terms of the Oculus LLC license 13 | agreement provided at the time of installation or download, or which 14 | otherwise accompanies this software in either electronic or hard copy form. 15 | 16 | ************************************************************************************/ 17 | 18 | using UnityEngine; 19 | 20 | [AddComponentMenu("Image Effects/OVRLensCorrection")] 21 | 22 | //------------------------------------------------------------------------------------- 23 | // ***** OVRLensCorrection 24 | // 25 | // OVRLensCorrection contains the variables required to set material properties 26 | // for the lens correction image effect. 27 | // 28 | public class OVRLensCorrection : OVRImageEffectBase 29 | { 30 | [HideInInspector] 31 | public Vector2 _Center = new Vector2(0.5f, 0.5f); 32 | [HideInInspector] 33 | public Vector2 _ScaleIn = new Vector2(1.0f, 1.0f); 34 | [HideInInspector] 35 | public Vector2 _Scale = new Vector2(1.0f, 1.0f); 36 | [HideInInspector] 37 | public Vector4 _HmdWarpParam = new Vector4(1.0f, 0.0f, 0.0f, 0.0f); 38 | 39 | // Called by camera to get lens correction values 40 | public Material GetMaterial() 41 | { 42 | // Set material properties 43 | material.SetVector("_Center", _Center); 44 | material.SetVector("_Scale", _Scale); 45 | material.SetVector("_ScaleIn", _ScaleIn); 46 | material.SetVector("_HmdWarpParam", _HmdWarpParam); 47 | 48 | return material; 49 | } 50 | } -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f5be85f8438ca3145aac56e800a217cf 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/OVRImageEffects/OVRLensCorrection.mat -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fa77fe97b0170804ab2b486d1cf3042f 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.shader: -------------------------------------------------------------------------------- 1 | //************************************************************************************ 2 | // 3 | // Filename : OVRLensCorrection.shader 4 | // Content : Full screen shader 5 | // This shader warps the final camera image to match the lens curvature on the Rift. 6 | // Created : January 17, 2013 7 | // Authors : Peter Giokaris 8 | // 9 | // Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | // 11 | // Use of this software is subject to the terms of the Oculus LLC license 12 | // agreement provided at the time of installation or download, or which 13 | // otherwise accompanies this software in either electronic or hard copy form. 14 | // 15 | //************************************************************************************/ 16 | 17 | Shader "OVRLensCorrection" 18 | { 19 | Properties 20 | { 21 | _MainTex ("Base (RGB)", 2D) = "" {} 22 | } 23 | 24 | // Shader code pasted into all further CGPROGRAM blocks 25 | CGINCLUDE 26 | 27 | #include "UnityCG.cginc" 28 | 29 | struct v2f 30 | { 31 | float4 pos : POSITION; 32 | float2 uv : TEXCOORD0; 33 | }; 34 | 35 | sampler2D _MainTex; 36 | 37 | v2f vert( appdata_img v ) 38 | { 39 | v2f o; 40 | o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 41 | o.uv = v.texcoord.xy; 42 | return o; 43 | } 44 | 45 | float2 _Center = float2(0,0); 46 | float2 _ScaleIn = float2(0,0); 47 | float2 _Scale = float2(0,0); 48 | float4 _HmdWarpParam = float4(0,0,0,0); 49 | 50 | // Scales input texture coordinates for distortion. 51 | // ScaleIn maps texture coordinates to Scales to ([-1, 1] * scaleFactor), 52 | // where scaleFactor compensates input for K1 and K2, to allow full screen size to be used. 53 | // Scale factor that fits into screen size can be determined by solving this 54 | // equation for Scale: 1 = Scale * (K0 + K1 * Scale^2 + K2 * Scale^4). 55 | float2 HmdWarp(float2 in01) 56 | { 57 | float2 vecFromCenter = (in01 - _Center) * _ScaleIn; // Scales to [-1, 1] 58 | float rSq= vecFromCenter.x * vecFromCenter.x + vecFromCenter.y * vecFromCenter.y; 59 | float2 vecResult = vecFromCenter * (_HmdWarpParam.x + _HmdWarpParam.y * rSq + _HmdWarpParam.z * rSq * rSq); 60 | return _Center + _Scale * vecResult; 61 | } 62 | 63 | half4 GetColor(float2 uv) 64 | { 65 | float2 tc = HmdWarp(uv); 66 | 67 | if (any(clamp(tc, float2(0.0,0.0), float2(1.0, 1.0)) - tc)) 68 | return 0; 69 | else 70 | return tex2D (_MainTex, tc); 71 | } 72 | 73 | half4 frag(v2f i) : COLOR 74 | { 75 | float2 tc = i.uv; 76 | half4 c = 0; 77 | c += GetColor(tc); 78 | return c; 79 | } 80 | 81 | ENDCG 82 | 83 | Subshader { 84 | Pass { 85 | ZTest Always Cull Off ZWrite Off 86 | Fog { Mode off } 87 | 88 | CGPROGRAM 89 | #pragma fragmentoption ARB_precision_hint_fastest 90 | #pragma vertex vert 91 | #pragma fragment frag 92 | ENDCG 93 | } 94 | 95 | } 96 | 97 | Fallback off 98 | 99 | } // shader -------------------------------------------------------------------------------- /Assets/OVR/OVRImageEffects/OVRLensCorrection.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a93bda1d6550334418dd32b81cab531f 3 | ShaderImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Prefabs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bfe39332fdb4cb044b619b3e4b890396 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Prefabs/OVRCameraController.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Prefabs/OVRCameraController.prefab -------------------------------------------------------------------------------- /Assets/OVR/Prefabs/OVRCameraController.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9da535ecc7e009544bd58323d6696a4d 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Prefabs/OVRPlayerController.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Prefabs/OVRPlayerController.prefab -------------------------------------------------------------------------------- /Assets/OVR/Prefabs/OVRPlayerController.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c4ecf12c830d51943a783cd2b729db35 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 859d6d3fcffe1bb41ac06f93d267fc9d 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCamera.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRCamera.cs 4 | Content : Interface to camera class 5 | Created : January 8, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | 16 | //#define MSAA_ENABLED // Not available in Unity yet 17 | 18 | using UnityEngine; 19 | using System.Runtime.InteropServices; 20 | 21 | [RequireComponent(typeof(Camera))] 22 | 23 | //------------------------------------------------------------------------------------- 24 | // ***** OVRCamera 25 | // 26 | // OVRCamera is used to render into a Unity Camera class. 27 | // This component handles reading the Rift tracker and positioning the camera position 28 | // and rotation. It also is responsible for properly rendering the final output, which 29 | // also the final lens correction pass. 30 | // 31 | public class OVRCamera : OVRComponent 32 | { 33 | 34 | // PRIVATE MEMBERS 35 | 36 | // If CameraTextureScale is not 1.0f, we will render to this texture 37 | private RenderTexture CameraTexture = null; 38 | 39 | // PRIVATE STATIC MEMBERS 40 | 41 | // rotation from sensor, used by all cameras 42 | // Final translation of cam location dependant on EyePosition from neck 43 | private static Quaternion DirQ = Quaternion.identity; 44 | 45 | // Default material, just blit texture over to final buffer 46 | static Material BlitMaterial = null; 47 | 48 | // Color only material, used for drawing quads on-screen 49 | static Material ColorOnlyMaterial = null; 50 | static Color QuadColor = Color.red; 51 | 52 | 53 | // PUBLIC MEMBERS 54 | 55 | // camera position... 56 | // From root of camera to neck (translation only) 57 | [HideInInspector] 58 | public Vector3 NeckPosition = new Vector3(0.0f, 0.7f, 0.0f); 59 | // From neck to eye (rotation and translation; x will be different for each eye) 60 | [HideInInspector] 61 | public Vector3 EyePosition = new Vector3(0.0f, 0.09f, 0.16f); 62 | 63 | 64 | // PUBLIC STATIC MEMBERS 65 | 66 | // Scaled size of final render buffer 67 | // A value of 1 will not create a render buffer but will render directly to final 68 | // backbuffer 69 | public static float CameraTextureScale = 1.0f; 70 | 71 | // Set externally by player controller to tell the cameras which way to face 72 | // And if we want rotation of parent to be set by camera (Y only) 73 | public static float YRotation = 0.0f; 74 | public static bool SetParentYRotation = false; 75 | public static Quaternion OrientationOffset = Quaternion.identity; 76 | 77 | // Use this to decide where rendering should take place 78 | // Setting to true allows for better latency, but some systems 79 | // (such as Pro water) will break 80 | public static bool CallCameraInPreRender = false; 81 | 82 | // Use this to turn on wire-mode 83 | public static bool WireMode = false; 84 | 85 | // Use this to turn on/off Prediction 86 | public static bool PredictionOn = true; 87 | 88 | 89 | // * * * * * * * * * * * * * 90 | 91 | // Awake 92 | new void Awake() 93 | { 94 | base.Awake (); 95 | 96 | // Material used to blit from one render texture to another 97 | if(BlitMaterial == null) 98 | { 99 | BlitMaterial = new Material ( 100 | "Shader \"BlitCopy\" {\n" + 101 | " SubShader { Pass {\n" + 102 | " ZTest Off Cull Off ZWrite Off Fog { Mode Off }\n" + 103 | " SetTexture [_MainTex] { combine texture}" + 104 | " }}\n" + 105 | "Fallback Off }" 106 | ); 107 | } 108 | 109 | // Material used for drawing color only polys into a render texture 110 | // Used by Latency tester 111 | if(ColorOnlyMaterial == null) 112 | { 113 | ColorOnlyMaterial = new Material ( 114 | 115 | "Shader \"Solid Color\" {\n" + 116 | "Properties {\n" + 117 | "_Color (\"Color\", Color) = (1,1,1)\n" + 118 | "}\n" + 119 | "SubShader {\n" + 120 | "Color [_Color]\n" + 121 | "Pass {}\n" + 122 | "}\n" + 123 | "}" 124 | ); 125 | } 126 | } 127 | 128 | // Start 129 | new void Start() 130 | { 131 | base.Start (); 132 | 133 | // NOTE: MSAA TEXTURES NOT AVAILABLE YET 134 | // Set CameraTextureScale (increases the size of the texture we are rendering into 135 | // for a better pixel match when post processing the image through lens distortion) 136 | #if MSAA_ENABLED 137 | CameraTextureScale = OVRDevice.DistortionScale(); 138 | #endif 139 | // If CameraTextureScale is not 1.0f, create a new texture and assign to target texture 140 | // Otherwise, fall back to normal camera rendering 141 | if((CameraTexture == null) && (CameraTextureScale > 1.0f)) 142 | { 143 | int w = (int)(Screen.width / 2.0f * CameraTextureScale); 144 | int h = (int)(Screen.height * CameraTextureScale); 145 | CameraTexture = new RenderTexture( w, h, 24); // 24 bit colorspace 146 | 147 | // NOTE: MSAA TEXTURES NOT AVAILABLE YET 148 | // This value should be the default for MSAA textures 149 | //CameraTexture.antiAliasing = 2; 150 | // Set it within the project 151 | #if MSAA_ENABLED 152 | CameraTexture.antiAliasing = QualitySettings.antiAliasing; 153 | #endif 154 | } 155 | } 156 | 157 | // Update 158 | new void Update() 159 | { 160 | base.Update (); 161 | } 162 | 163 | // OnPreCull 164 | void OnPreCull() 165 | { 166 | // NOTE: Setting the camera here increases latency, but ensures 167 | // that all Unity sub-systems that rely on camera location before 168 | // being set to render are satisfied. 169 | if(CallCameraInPreRender == false) 170 | SetCameraOrientation(); 171 | 172 | } 173 | 174 | // OnPreRender 175 | void OnPreRender() 176 | { 177 | // NOTE: Better latency performance here, but messes up water rendering and other 178 | // systems that rely on the camera to be set before PreCull takes place. 179 | if(CallCameraInPreRender == true) 180 | SetCameraOrientation(); 181 | 182 | if(WireMode == true) 183 | GL.wireframe = true; 184 | 185 | // Set new buffers and clear color and depth 186 | if(CameraTexture != null) 187 | { 188 | Graphics.SetRenderTarget(CameraTexture); 189 | GL.Clear (true, true, gameObject.camera.backgroundColor); 190 | } 191 | } 192 | 193 | // OnPostRender 194 | void OnPostRender() 195 | { 196 | if(WireMode == true) 197 | GL.wireframe = false; 198 | } 199 | 200 | // OnRenderImage 201 | void OnRenderImage (RenderTexture source, RenderTexture destination) 202 | { 203 | bool flipImage = true; 204 | 205 | // Use either source input or CameraTexutre, if it exists 206 | RenderTexture SourceTexture = source; 207 | 208 | if (CameraTexture != null) 209 | { 210 | SourceTexture = CameraTexture; 211 | flipImage = false; // If MSAA is on, this will be true 212 | } 213 | else 214 | { 215 | // Check if quality settings are set 216 | if(QualitySettings.antiAliasing == 0) 217 | { 218 | flipImage = false; // If MSAA is on, this will be true 219 | } 220 | } 221 | 222 | // Render into source texture before lens correction 223 | Camera c = gameObject.camera; 224 | RenderPreLensCorrection(ref c, ref SourceTexture); 225 | 226 | // Replace null material with lens correction material 227 | Material material = GetComponent().GetMaterial(); 228 | 229 | 230 | // Draw to final destination 231 | Blit(SourceTexture, null, material, flipImage); 232 | 233 | // Run latency test by drawing out quads to the destination buffer 234 | LatencyTest(destination); 235 | 236 | } 237 | 238 | // SetCameraOrientation 239 | void SetCameraOrientation() 240 | { 241 | Quaternion q = Quaternion.identity; 242 | Vector3 dir = Vector3.forward; 243 | 244 | // Main camera has a depth of 0, so it will be rendered first 245 | if(gameObject.camera.depth == 0.0f) 246 | { 247 | // If desired, update parent transform y rotation here 248 | // This is useful if we want to track the current location of 249 | // of the head. 250 | // TODO: Future support for x and z, and possibly change to a quaternion 251 | if(SetParentYRotation == true) 252 | { 253 | Vector3 a = gameObject.camera.transform.rotation.eulerAngles; 254 | a.x = 0; 255 | a.z = 0; 256 | gameObject.transform.parent.transform.eulerAngles = a; 257 | } 258 | 259 | // Read sensor here (prediction on or off) 260 | if(PredictionOn == false) 261 | OVRDevice.GetOrientation(ref DirQ); 262 | else 263 | OVRDevice.GetPredictedOrientation(ref DirQ); 264 | 265 | // This needs to go as close to reading Rift orientation inputs 266 | OVRDevice.ProcessLatencyInputs(); 267 | } 268 | 269 | // Calculate the rotation Y offset that is getting updated externally 270 | // (i.e. like a controller rotation) 271 | q = Quaternion.Euler(0.0f, YRotation, 0.0f); 272 | dir = q * Vector3.forward; 273 | q.SetLookRotation(dir, Vector3.up); 274 | 275 | // Multiply the offset orientation first 276 | q = OrientationOffset * q; 277 | 278 | // Multiply in the current HeadQuat (q is now the latest best rotation) 279 | q = q * DirQ; 280 | 281 | // * * * 282 | // Update camera rotation 283 | gameObject.camera.transform.rotation = q; 284 | 285 | // * * * 286 | // Update camera position (first add Offset to parent transform) 287 | gameObject.camera.transform.position = 288 | gameObject.camera.transform.parent.transform.position + NeckPosition; 289 | 290 | // Adjust neck by taking eye position and transforming through q 291 | gameObject.camera.transform.position += q * EyePosition; 292 | 293 | // PGG Alternate calculation for above... 294 | //Vector3 EyePositionNoX = EyePosition; EyePositionNoX.x = 0.0f; 295 | //gameObject.camera.transform.position += q * EyePositionNoX; 296 | //gameObject.camera.ResetWorldToCameraMatrix(); 297 | //Matrix4x4 m = camera.worldToCameraMatrix; 298 | //Matrix4x4 tm = Matrix4x4.identity; 299 | //tm.SetColumn (3, new Vector4 (-EyePosition.x, 0.0f, 0.0f, 1)); 300 | //gameObject.camera.worldToCameraMatrix = tm * m; 301 | 302 | } 303 | 304 | // CreatePerspectiveMatrix 305 | // We will create our own perspective matrix 306 | void CreatePerspectiveMatrix(ref Matrix4x4 m) 307 | { 308 | float nearClip = gameObject.camera.nearClipPlane; 309 | float farClip = gameObject.camera.farClipPlane; 310 | float tanHalfFov = Mathf.Tan(Mathf.Deg2Rad * gameObject.camera.fov * 0.5f); 311 | float ar = gameObject.camera.aspect; 312 | m.m00 = 1.0f / (ar * tanHalfFov); 313 | m.m11 = 1.0f / tanHalfFov; 314 | m.m22 = farClip / (nearClip - farClip); 315 | m.m32 = -1.0f; 316 | m.m23 = (farClip * nearClip) / (nearClip - farClip); 317 | m.m33 = 0.0f; 318 | } 319 | 320 | // Blit - Copies one render texture onto another through a material 321 | // flip will flip the render horizontally 322 | void Blit (RenderTexture source, RenderTexture dest, Material m, bool flip) 323 | { 324 | Material material = m; 325 | 326 | // Default to blitting material if one doesn't get passed in 327 | if(material == null) 328 | material = BlitMaterial; 329 | 330 | // Make the destination texture the target for all rendering 331 | RenderTexture.active = dest; 332 | 333 | // Assign the source texture to a property from a shader 334 | source.SetGlobalShaderProperty ("_MainTex"); 335 | 336 | // Set up the simple Matrix 337 | GL.PushMatrix (); 338 | GL.LoadOrtho (); 339 | for(int i = 0; i < material.passCount; i++) 340 | { 341 | material.SetPass(i); 342 | DrawQuad(flip); 343 | } 344 | GL.PopMatrix (); 345 | } 346 | 347 | // DrawQuad 348 | void DrawQuad(bool flip) 349 | { 350 | GL.Begin (GL.QUADS); 351 | 352 | if(flip == true) 353 | { 354 | GL.TexCoord2( 0.0f, 1.0f ); GL.Vertex3( 0.0f, 0.0f, 0.1f ); 355 | GL.TexCoord2( 1.0f, 1.0f ); GL.Vertex3( 1.0f, 0.0f, 0.1f ); 356 | GL.TexCoord2( 1.0f, 0.0f ); GL.Vertex3( 1.0f, 1.0f, 0.1f ); 357 | GL.TexCoord2( 0.0f, 0.0f ); GL.Vertex3( 0.0f, 1.0f, 0.1f ); 358 | } 359 | else 360 | { 361 | GL.TexCoord2( 0.0f, 0.0f ); GL.Vertex3( 0.0f, 0.0f, 0.1f ); 362 | GL.TexCoord2( 1.0f, 0.0f ); GL.Vertex3( 1.0f, 0.0f, 0.1f ); 363 | GL.TexCoord2( 1.0f, 1.0f ); GL.Vertex3( 1.0f, 1.0f, 0.1f ); 364 | GL.TexCoord2( 0.0f, 1.0f ); GL.Vertex3( 0.0f, 1.0f, 0.1f ); 365 | } 366 | 367 | GL.End(); 368 | } 369 | 370 | 371 | 372 | // LatencyTest 373 | void LatencyTest(RenderTexture dest) 374 | { 375 | byte r = 0,g = 0, b = 0; 376 | 377 | // See if we get a string back to send to the debug out 378 | string s = Marshal.PtrToStringAnsi(OVRDevice.GetLatencyResultsString()); 379 | if (s != null) 380 | { 381 | string result = 382 | "\n\n---------------------\nLATENCY TEST RESULTS:\n---------------------\n"; 383 | result += s; 384 | result += "\n\n\n"; 385 | print(result); 386 | } 387 | 388 | if(OVRDevice.DisplayLatencyScreenColor(ref r, ref g, ref b) == false) 389 | return; 390 | 391 | RenderTexture.active = dest; 392 | Material material = ColorOnlyMaterial; 393 | QuadColor.r = (float)r / 255.0f; 394 | QuadColor.g = (float)g / 255.0f; 395 | QuadColor.b = (float)b / 255.0f; 396 | material.SetColor("_Color", QuadColor); 397 | GL.PushMatrix(); 398 | material.SetPass(0); 399 | GL.LoadOrtho(); 400 | GL.Begin(GL.QUADS); 401 | GL.Vertex3(0.3f,0.3f,0); 402 | GL.Vertex3(0.3f,0.7f,0); 403 | GL.Vertex3(0.7f,0.7f,0); 404 | GL.Vertex3(0.7f,0.3f,0); 405 | GL.End(); 406 | GL.PopMatrix(); 407 | 408 | } 409 | 410 | 411 | /////////////////////////////////////////////////////////// 412 | // PUBLIC FUNCTIONS 413 | /////////////////////////////////////////////////////////// 414 | 415 | // RenderPreLensCorrection 416 | public virtual void RenderPreLensCorrection(ref Camera camera, ref RenderTexture target) 417 | { 418 | // Render into target here. 419 | // A GUI system should be rendered here. 420 | // One can query the camera to decide which is left and which is right 421 | } 422 | 423 | // SetPerspectiveOffset 424 | public void SetPerspectiveOffset(ref Vector3 offset) 425 | { 426 | // NOTE: Unity skyboxes do not currently use the projection matrix, so 427 | // if one wants to use a skybox with the Rift it must be implemented 428 | // manually 429 | gameObject.camera.ResetProjectionMatrix(); 430 | Matrix4x4 m = Matrix4x4.identity;// = gameObject.camera.projectionMatrix; 431 | CreatePerspectiveMatrix(ref m); 432 | Matrix4x4 tm = Matrix4x4.identity; 433 | tm.SetColumn (3, new Vector4 (offset.x, offset.y, 0.0f, 1)); 434 | gameObject.camera.projectionMatrix = tm * m; 435 | } 436 | 437 | 438 | } 439 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCamera.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 467c1e6bb64de664bbba9800bb135078 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCameraController.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRCameraController.cs 4 | Content : Camera controller interface. 5 | This script is used to interface the OVR cameras. 6 | Created : January 8, 2013 7 | Authors : Peter Giokaris 8 | 9 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 10 | 11 | Use of this software is subject to the terms of the Oculus LLC license 12 | agreement provided at the time of installation or download, or which 13 | otherwise accompanies this software in either electronic or hard copy form. 14 | 15 | ************************************************************************************/ 16 | using UnityEngine; 17 | using System.Collections.Generic; 18 | 19 | //------------------------------------------------------------------------------------- 20 | // ***** OVRCameraController 21 | // 22 | // OVRCameraController is a component that allows for easy handling of the lower level cameras. 23 | // It is the main interface between Unity and the cameras. 24 | // This is attached to a prefab that makes it easy to add a Rift into a scene. 25 | // 26 | // All camera control should be done through this component. 27 | // 28 | public class OVRCameraController : OVRComponent 29 | { 30 | // PRIVATE MEMBERS 31 | private bool UpdateCamerasDirtyFlag = false; 32 | private Camera CameraLeft, CameraRight = null; 33 | private float IPD = 0.064f; // in millimeters 34 | private float IPDOffsetLeft, IPDOffsetRight = 0.0f; // normalized screen space 35 | private float LensOffsetLeft, LensOffsetRight = 0.0f; // normalized screen space 36 | private float VerticalFOV = 90.0f; // in degrees 37 | private float AspectRatio = 1.0f; 38 | private float DistK0, DistK1, DistK2, DistK3 = 0.0f; // lens distortion parameters 39 | 40 | // Initial orientation of the camera, can be used to always set the zero orientation of 41 | // the cameras to follow a set forward facing orientation. 42 | private Quaternion OrientationOffset = Quaternion.identity; 43 | // Set Y rotation here; this will offset the y rotation of the cameras. 44 | private float YRotation = 0.0f; 45 | 46 | // PUBLIC MEMBERS 47 | // Camera positioning: 48 | // From root of camera to neck (translation only) 49 | public Vector3 NeckPosition = new Vector3(0.0f, 0.7f, 0.0f); 50 | // From neck to eye (rotation and translation; x will be different for each eye) 51 | public Vector3 EyeCenterPosition = new Vector3(0.0f, 0.15f, 0.09f); 52 | // Set this transform with an objec that the camera orientation should follow. 53 | // NOTE: Best not to set this with the OVRCameraController IF TrackerRotatesY is 54 | // on, since this will lead to uncertain output 55 | public Transform FollowOrientation = null; 56 | // Set to true if we want the rotation of the camera controller to be influenced by tracker 57 | public bool TrackerRotatesY = false; 58 | // Set the background color for both cameras 59 | public Color BackgroundColor = new Color(0.192f, 0.302f, 0.475f, 1.0f); 60 | // Set the near and far clip plane for both cameras 61 | public float NearClipPlane = 0.15f; 62 | public float FarClipPlane = 1000.0f; 63 | 64 | // * * * * * * * * * * * * * 65 | 66 | // Awake 67 | new void Awake() 68 | { 69 | base.Awake(); 70 | } 71 | 72 | // Start 73 | new void Start() 74 | { 75 | base.Start(); 76 | 77 | // Get the cameras 78 | Camera[] cameras = gameObject.GetComponentsInChildren(); 79 | 80 | for (int i = 0; i < cameras.Length; i++) 81 | { 82 | if(cameras[i].name == "CameraLeft") 83 | CameraLeft = cameras[i]; 84 | 85 | if(cameras[i].name == "CameraRight") 86 | CameraRight = cameras[i]; 87 | } 88 | 89 | if((CameraLeft == null) || (CameraRight == null)) 90 | Debug.LogWarning("WARNING: Unity Cameras in OVRCameraController not found!"); 91 | 92 | // Get the required Rift infromation needed to set cameras 93 | InitCameraControllerVariables(); 94 | 95 | // Initialize the cameras 96 | UpdateCamerasDirtyFlag = true; 97 | UpdateCameras(); 98 | 99 | SetMaximumVisualQuality(); 100 | 101 | } 102 | 103 | // Update 104 | new void Update() 105 | { 106 | base.Update(); 107 | UpdateCameras(); 108 | } 109 | 110 | // InitCameraControllerVariables 111 | // Made public so that it can be called by classes that require information about the 112 | // camera to be present when initing variables in 'Start' 113 | public void InitCameraControllerVariables() 114 | { 115 | // Get the IPD value (distance between eyes in meters) 116 | OVRDevice.GetIPD(ref IPD); 117 | 118 | // Get the values for both IPD and lens distortion correction shift. We don't normally 119 | // need to set the PhysicalLensOffset once it's been set here. 120 | OVRDevice.GetPhysicalLensOffsetsFromIPD(IPD, ref IPDOffsetLeft, ref IPDOffsetRight); 121 | LensOffsetLeft = IPDOffsetLeft; 122 | LensOffsetRight = IPDOffsetRight; 123 | 124 | // Using the calculated FOV, based on distortion parameters, yeilds the best results. 125 | // However, public functions will allow to override the FOV if desired 126 | VerticalFOV = OVRDevice.VerticalFOV(); 127 | 128 | // Store aspect ratio as well 129 | AspectRatio = OVRDevice.CalculateAspectRatio(); 130 | 131 | OVRDevice.GetDistortionCorrectionCoefficients(ref DistK0, ref DistK1, ref DistK2, ref DistK3); 132 | 133 | // Get our initial world orientation of the cameras from the scene (we can grab it from 134 | // the set FollowOrientation object or this OVRCameraController gameObject) 135 | if(FollowOrientation != null) 136 | OrientationOffset = FollowOrientation.rotation; 137 | else 138 | OrientationOffset = transform.rotation; 139 | } 140 | 141 | // InitCameras 142 | void UpdateCameras() 143 | { 144 | // Values that influence the stereo camera orientation up and above the tracker 145 | if(FollowOrientation != null) 146 | OrientationOffset = FollowOrientation.rotation; 147 | 148 | OVRCamera.OrientationOffset = OrientationOffset; 149 | OVRCamera.YRotation = YRotation; 150 | 151 | // Values that tell the stereo cameras how to influence OVRCameraController 152 | OVRCamera.SetParentYRotation = TrackerRotatesY; 153 | 154 | if(UpdateCamerasDirtyFlag == false) 155 | return; 156 | 157 | float lensOffset = 0.5f + (LensOffsetLeft * 0.5f); 158 | float eyePositionOffset = -IPD * 0.5f; 159 | ConfigureCamera(ref CameraLeft, lensOffset, IPDOffsetLeft, eyePositionOffset); 160 | 161 | lensOffset = 0.5f + (LensOffsetRight * 0.5f); 162 | eyePositionOffset = IPD * 0.5f; 163 | ConfigureCamera(ref CameraRight, lensOffset, IPDOffsetRight, eyePositionOffset); 164 | 165 | UpdateCamerasDirtyFlag = false; 166 | } 167 | 168 | // SetCamera 169 | bool ConfigureCamera(ref Camera camera, float lensOffset, float ipdOffset, float eyePositionOffset) 170 | { 171 | Vector3 LensOffset = Vector3.zero; 172 | Vector3 EyePosition = EyeCenterPosition; 173 | 174 | // Vertical FOV 175 | camera.fov = VerticalFOV; 176 | 177 | // Aspect ratio 178 | camera.aspect = AspectRatio; 179 | 180 | // IPD offset for image 181 | // NOTE: We are recalculating the prespective matrix, so we must make 182 | // sure that VerticalFOV in camera is set prior to this 183 | LensOffset.x = ipdOffset; 184 | camera.GetComponent().SetPerspectiveOffset(ref LensOffset); 185 | 186 | // Centre of lens correction 187 | camera.GetComponent()._Center.x = lensOffset; 188 | 189 | // Lens correction 190 | ConfigureCameraLensCorrection(ref camera); 191 | 192 | // Set camera variables that pertain to the neck and eye position 193 | // NOTE: We will want to add a scale vlue here in the event that the player 194 | // grows or shrinks in the world. This keeps head modelling behaviour 195 | // accurate 196 | camera.GetComponent().NeckPosition = NeckPosition; 197 | EyePosition.x = eyePositionOffset; 198 | 199 | camera.GetComponent().EyePosition = EyePosition; 200 | 201 | // Background color 202 | camera.backgroundColor = BackgroundColor; 203 | 204 | // Clip Planes 205 | camera.nearClipPlane = NearClipPlane; 206 | camera.farClipPlane = FarClipPlane; 207 | 208 | return true; 209 | } 210 | 211 | // SetCameraLensCorrection 212 | void ConfigureCameraLensCorrection(ref Camera camera) 213 | { 214 | // Get the distortion scale and aspect ratio to use when calculating distortion shader 215 | float distortionScale = 1.0f / OVRDevice.DistortionScale(); 216 | float aspectRatio = OVRDevice.CalculateAspectRatio(); 217 | 218 | // These values are different in the SDK World Demo; Unity renders each camera to a buffer 219 | // that is normalized, so we will respect this rule when calculating the distortion inputs 220 | float NormalizedWidth = 1.0f; 221 | float NormalizedHeight = 1.0f; 222 | 223 | OVRLensCorrection lc = camera.GetComponent(); 224 | 225 | lc._Scale.x = (NormalizedWidth / 2.0f) * distortionScale; 226 | lc._Scale.y = (NormalizedHeight / 2.0f) * distortionScale * aspectRatio; 227 | lc._ScaleIn.x = (2.0f / NormalizedWidth); 228 | lc._ScaleIn.y = (2.0f / NormalizedHeight) / aspectRatio; 229 | lc._HmdWarpParam.x = DistK0; 230 | lc._HmdWarpParam.y = DistK1; 231 | lc._HmdWarpParam.z = DistK2; 232 | } 233 | 234 | /////////////////////////////////////////////////////////// 235 | // PUBLIC FUNCTIONS 236 | /////////////////////////////////////////////////////////// 237 | 238 | // SetCameras - Should we want to re-target the cameras 239 | public void SetCameras(ref Camera cameraLeft, ref Camera cameraRight) 240 | { 241 | CameraLeft = cameraLeft; 242 | CameraRight = cameraRight; 243 | UpdateCamerasDirtyFlag = true; 244 | } 245 | 246 | // Get/SetIPD 247 | public void GetIPD(ref float ipd) 248 | { 249 | ipd = IPD; 250 | } 251 | public void SetIPD(float ipd) 252 | { 253 | IPD = ipd; 254 | OVRDevice.GetPhysicalLensOffsetsFromIPD(IPD, ref IPDOffsetLeft, ref IPDOffsetRight); 255 | UpdateCamerasDirtyFlag = true; 256 | 257 | } 258 | 259 | // Get/SetIPDOffsets (note: Setting IPD directly isn't something we would 260 | // normally do, since it is best set by SetIPD, above 261 | public void GetIPDOffsets(ref float offsetLeft, ref float offsetRight) 262 | { 263 | offsetLeft = IPDOffsetLeft; 264 | offsetRight = IPDOffsetRight; 265 | } 266 | public void SetIPDOffsets(float offsetLeft, float offsetRight) 267 | { 268 | IPDOffsetLeft = offsetLeft; 269 | IPDOffsetRight = offsetRight; 270 | UpdateCamerasDirtyFlag = true; 271 | } 272 | 273 | // Get/SetPhysicalLensOffsets 274 | public void GetPhysicalLensOffsets(ref float offsetLeft, ref float offsetRight) 275 | { 276 | offsetLeft = LensOffsetLeft; 277 | offsetRight = LensOffsetRight; 278 | } 279 | public void SetPhysicalLensOffsets(float offsetLeft, float offsetRight) 280 | { 281 | LensOffsetLeft = offsetLeft; 282 | LensOffsetRight = offsetRight; 283 | UpdateCamerasDirtyFlag = true; 284 | } 285 | 286 | //Get/SetVerticalFOV 287 | public void GetVerticalFOV(ref float verticalFOV) 288 | { 289 | verticalFOV = VerticalFOV; 290 | } 291 | public void SetVerticalFOV(float verticalFOV) 292 | { 293 | VerticalFOV = verticalFOV; 294 | UpdateCamerasDirtyFlag = true; 295 | } 296 | 297 | //Get/SetAspectRatio 298 | public void GetAspectRatio(ref float aspecRatio) 299 | { 300 | aspecRatio = AspectRatio; 301 | } 302 | public void SetAspectRatio(float aspectRatio) 303 | { 304 | AspectRatio = aspectRatio; 305 | UpdateCamerasDirtyFlag = true; 306 | } 307 | 308 | // Get/SetDistortionCoefs 309 | public void GetDistortionCoefs(ref float distK0, 310 | ref float distK1, 311 | ref float distK2, 312 | ref float distK3) 313 | { 314 | distK0 = DistK0; 315 | distK1 = DistK1; 316 | distK2 = DistK2; 317 | distK3 = DistK3; 318 | } 319 | public void SetDistortionCoefs(float distK0, 320 | float distK1, 321 | float distK2, 322 | float distK3) 323 | { 324 | DistK0 = distK0; 325 | DistK1 = distK1; 326 | DistK2 = distK2; 327 | DistK3 = distK3; 328 | UpdateCamerasDirtyFlag = true; 329 | } 330 | 331 | // Get/SetNeckPosition 332 | public void GetNeckPosition(ref Vector3 neckPosition) 333 | { 334 | neckPosition = NeckPosition; 335 | } 336 | public void SetNeckPosition(Vector3 neckPosition) 337 | { 338 | NeckPosition = neckPosition; 339 | UpdateCamerasDirtyFlag = true; 340 | } 341 | 342 | // Get/SetEyeCenterPosition 343 | public void GetEyeCenterPosition(ref Vector3 eyeCenterPosition) 344 | { 345 | eyeCenterPosition = EyeCenterPosition; 346 | } 347 | public void SetEyeCenterPosition(Vector3 eyeCenterPosition) 348 | { 349 | EyeCenterPosition = eyeCenterPosition; 350 | UpdateCamerasDirtyFlag = true; 351 | } 352 | 353 | // Get/SetOrientationOffset 354 | public void GetOrientationOffset(ref Quaternion orientationOffset) 355 | { 356 | orientationOffset = OrientationOffset; 357 | } 358 | public void SetOrientationOffset(Quaternion orientationOffset) 359 | { 360 | OrientationOffset = orientationOffset; 361 | } 362 | 363 | // Get/SetYRotation 364 | public void GetYRotation(ref float yRotation) 365 | { 366 | yRotation = YRotation; 367 | } 368 | public void SetYRotation(float yRotation) 369 | { 370 | YRotation = yRotation; 371 | } 372 | 373 | // Get/SetTrackerRotatesY 374 | public void GetTrackerRotatesY(ref bool trackerRotatesY) 375 | { 376 | trackerRotatesY = TrackerRotatesY; 377 | } 378 | public void SetTrackerRotatesY(bool trackerRotatesY) 379 | { 380 | TrackerRotatesY = trackerRotatesY; 381 | } 382 | 383 | 384 | /////////////////////////////////////////////////////////// 385 | // STATIC PUBLIC FUNCTIONS 386 | /////////////////////////////////////////////////////////// 387 | 388 | // SetMaximumVisualQuality 389 | static public void SetMaximumVisualQuality() 390 | { 391 | QualitySettings.softVegetation = true; 392 | QualitySettings.maxQueuedFrames = 0; 393 | QualitySettings.anisotropicFiltering = AnisotropicFiltering.ForceEnable; 394 | QualitySettings.vSyncCount = 1; 395 | } 396 | 397 | } 398 | 399 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCameraController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: df9f338034892c44ebb62d97894772f1 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRComponent.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRComponent.cs 4 | Content : Base component OVR class 5 | Created : January 8, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | using UnityEngine; 16 | using System.Collections.Generic; 17 | 18 | 19 | //------------------------------------------------------------------------------------- 20 | // ***** OVRComponent 21 | // 22 | // OVRComponent is the base class for many of the OVR classes. It is used to provide base 23 | // functionality to classes derived from it. 24 | // 25 | // NOTE: It is important that any overloaded functions in derived classes call 26 | // base. to allow for base functionality. 27 | // 28 | public class OVRComponent : MonoBehaviour 29 | { 30 | protected float DeltaTime = 1.0f; 31 | 32 | // Awake 33 | public virtual void Awake() 34 | { 35 | } 36 | 37 | // Start 38 | public virtual void Start() 39 | { 40 | } 41 | 42 | // Update 43 | public virtual void Update() 44 | { 45 | // If we are running at 60fps, DeltaTime will be set to 1.0 46 | DeltaTime = (Time.deltaTime * 60.0f); 47 | } 48 | 49 | // LateUpdate 50 | public virtual void LateUpdate() 51 | { 52 | } 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRComponent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9db415bcf6a81974a999d784ee02177c 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCrosshair.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRCrosshair.cs 4 | Content : Implements a hud cross-hair 5 | Created : January 8, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | using UnityEngine; 16 | 17 | //------------------------------------------------------------------------------------- 18 | // ***** OVRCrosshair 19 | // 20 | // OVRCrosshair is a component that adds a stereoscoppic cross-hair into a scene. 21 | // It currently draws into the scene after the view has been rendered, therefore there 22 | // is no distortion correction on it. 23 | // 24 | // The static member CursorOnScreen allows other systems to know if the Cursor is visable 25 | // or not. 26 | // 27 | public class OVRCrosshair : MonoBehaviour 28 | { 29 | // crosshair texture 30 | public Texture ImageCrosshair = null; 31 | public float StereoSpread = 0.0f; 32 | public float FadeTime = 1.0f; 33 | public float FadeScale = 0.8f; 34 | public float CrosshairDistance = 0.0f; 35 | 36 | public float DeadZoneX = 75.0f; 37 | public float DeadZoneY = 75.0f; 38 | public float ScaleSpeedX = 7.0f; 39 | public float ScaleSpeedY = 7.0f; 40 | 41 | private bool DisplayCrosshair; 42 | private bool CollisionWithGeometry; 43 | private float FadeVal; 44 | private Camera MainCam; 45 | private float LensOffsetLeft = 0.0f; 46 | private float LensOffsetRight = 0.0f; 47 | 48 | private float XL = 0.0f; 49 | private float YL = 0.0f; 50 | 51 | // Start 52 | void Start() 53 | { 54 | DisplayCrosshair = false; 55 | CollisionWithGeometry = false; 56 | FadeVal = 0.0f; 57 | MainCam = Camera.main; 58 | 59 | // Initialize screen location of cursor 60 | XL = Screen.width * 0.25f; 61 | YL = Screen.height * 0.5f; 62 | 63 | // Get the values for both IPD and lens distortion correction shift 64 | OVRDevice.GetPhysicalLensOffsets(ref LensOffsetLeft, ref LensOffsetRight); 65 | } 66 | 67 | // Update 68 | void Update() 69 | { 70 | // Do not do these tests within OnGUI since they will be called twice 71 | ShouldDisplayCrosshair(); 72 | CollisionWithGeometryCheck(); 73 | } 74 | 75 | // OnGUI 76 | void OnGUI() 77 | { 78 | if ((DisplayCrosshair == true) && (CollisionWithGeometry == false)) 79 | FadeVal += Time.deltaTime / FadeTime; 80 | else 81 | FadeVal -= Time.deltaTime / FadeTime; 82 | 83 | FadeVal = Mathf.Clamp(FadeVal, 0.0f, 1.0f); 84 | 85 | // Check to see if crosshair influences mouse rotation 86 | OVRPlayerController.AllowMouseRotation = false; 87 | 88 | if ((ImageCrosshair != null) && (FadeVal != 0.0f)) 89 | { 90 | // Assume cursor is on-screen (unless it goes into the dead-zone) 91 | // Other systems will check this to see if it is false for example 92 | // allowing rotation to take place 93 | OVRPlayerController.AllowMouseRotation = true; 94 | 95 | GUI.color = new Color(1, 1, 1, FadeVal * FadeScale); 96 | 97 | float ah = StereoSpread / 2.0f // required to adjust for physical lens shift 98 | - 0.5f * ((LensOffsetLeft * (float)Screen.width / 2)); 99 | 100 | // Calculate X 101 | XL += Input.GetAxis("Mouse X") * 0.5f * ScaleSpeedX; 102 | if(XL < DeadZoneX) 103 | { 104 | OVRPlayerController.AllowMouseRotation = false; 105 | XL = DeadZoneX - 0.001f; 106 | } 107 | else if (XL > (Screen.width * 0.5f) - DeadZoneX) 108 | { 109 | OVRPlayerController.AllowMouseRotation = false; 110 | XL = Screen.width * 0.5f - DeadZoneX + 0.001f; 111 | } 112 | 113 | // Calculate Y 114 | YL -= Input.GetAxis("Mouse Y") * ScaleSpeedY; 115 | if(YL < DeadZoneY) 116 | { 117 | //CursorOnScreen = false; 118 | if(YL < 0.0f) YL = 0.0f; 119 | } 120 | else if (YL > Screen.height - DeadZoneY) 121 | { 122 | //CursorOnScreen = false; 123 | if(YL > Screen.height) YL = Screen.height; 124 | } 125 | 126 | // Finally draw cursor 127 | if(OVRPlayerController.AllowMouseRotation == true) 128 | { 129 | // Left 130 | GUI.DrawTexture(new Rect( XL - (ImageCrosshair.width * 0.5f) - ah , 131 | YL - (ImageCrosshair.height * 0.5f), 132 | ImageCrosshair.width, 133 | ImageCrosshair.height), 134 | ImageCrosshair); 135 | 136 | float XR = XL + Screen.width * 0.5f; 137 | float YR = YL; 138 | 139 | // Right 140 | GUI.DrawTexture(new Rect( XR - (ImageCrosshair.width * 0.5f) + ah, 141 | YR - (ImageCrosshair.height * 0.5f), 142 | ImageCrosshair.width, 143 | ImageCrosshair.height), 144 | ImageCrosshair); 145 | } 146 | 147 | GUI.color = Color.white; 148 | } 149 | } 150 | 151 | // ShouldDisplayCrosshair 152 | bool ShouldDisplayCrosshair() 153 | { 154 | if(Input.GetKeyDown (KeyCode.C)) 155 | { 156 | if(DisplayCrosshair == false) 157 | { 158 | DisplayCrosshair = true; 159 | 160 | // Always initialize screen location of cursor to center 161 | XL = Screen.width * 0.25f; 162 | YL = Screen.height * 0.5f; 163 | } 164 | else 165 | DisplayCrosshair = false; 166 | } 167 | 168 | return DisplayCrosshair; 169 | } 170 | 171 | // CollisionWithGeometry 172 | bool CollisionWithGeometryCheck() 173 | { 174 | CollisionWithGeometry = false; 175 | 176 | Vector3 startPos = MainCam.transform.position; 177 | Vector3 dir = Vector3.forward; 178 | dir = MainCam.transform.rotation * dir; 179 | dir *= CrosshairDistance; 180 | Vector3 endPos = startPos + dir; 181 | 182 | RaycastHit hit; 183 | if (Physics.Linecast(startPos, endPos, out hit)) 184 | { 185 | if (!hit.collider.isTrigger) 186 | { 187 | CollisionWithGeometry = true; 188 | } 189 | } 190 | 191 | return CollisionWithGeometry; 192 | } 193 | 194 | } 195 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRCrosshair.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c8df947a743dbe249849490063ad3025 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRDevice.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRDevice.cs 4 | Content : Interface for the Oculus Rift Device 5 | Created : February 14, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | using UnityEngine; 16 | using System; 17 | using System.Runtime.InteropServices; 18 | 19 | //------------------------------------------------------------------------------------- 20 | // ***** OVRDevice 21 | // 22 | // OVRDevice is the main interface to the Oculus Rift hardware. It includes wrapper functions 23 | // for all exported C++ functions, as well as helper functions that use the stored Oculus 24 | // variables to help set up camera behavior. 25 | // 26 | // This component is added to the OVRCameraController prefab. It can be part of any 27 | // game object that one sees fit to place it. However, it should only be declared once, 28 | // since there are public members that allow for tweaking certain Rift values in the 29 | // Unity inspector. 30 | // 31 | public class OVRDevice : MonoBehaviour 32 | { 33 | // Imported functions from 34 | // OVRPlugin.dll (PC) 35 | // OVRPlugin.so (Linux, Android) 36 | // OVRPlugin.bundle (OSX) 37 | 38 | // SENSOR FUNCTIONS 39 | [DllImport ("OculusPlugin")] 40 | private static extern bool OVR_Initialize(); 41 | [DllImport ("OculusPlugin")] 42 | private static extern bool OVR_Destroy(); 43 | [DllImport ("OculusPlugin")] 44 | private static extern int OVR_GetSensorCount(); 45 | [DllImport ("OculusPlugin")] 46 | private static extern bool OVR_IsHMDPresent(); 47 | [DllImport ("OculusPlugin")] 48 | private static extern bool OVR_IsSensorPresent(int sensor); 49 | [DllImport ("OculusPlugin")] 50 | private static extern bool OVR_GetSensorOrientation(int sensorID, 51 | ref float w, 52 | ref float x, 53 | ref float y, 54 | ref float z); 55 | [DllImport ("OculusPlugin")] 56 | private static extern bool OVR_GetSensorPredictedOrientation(int sensorID, 57 | ref float w, 58 | ref float x, 59 | ref float y, 60 | ref float z); 61 | [DllImport ("OculusPlugin")] 62 | private static extern bool OVR_GetSensorPredictionTime(int sensorID, ref float predictionTime); 63 | [DllImport ("OculusPlugin")] 64 | private static extern bool OVR_SetSensorPredictionTime(int sensorID, float predictionTime); 65 | [DllImport ("OculusPlugin")] 66 | private static extern bool OVR_GetSensorAccelGain(int sensorID, ref float accelGain); 67 | [DllImport ("OculusPlugin")] 68 | private static extern bool OVR_SetSensorAccelGain(int sensorID, float accelGain); 69 | [DllImport ("OculusPlugin")] 70 | private static extern bool OVR_ResetSensorOrientation(int sensorID); 71 | 72 | // DISPLAY FUNCTIONS 73 | [DllImport ("OculusPlugin")] 74 | private static extern System.IntPtr OVR_GetDisplayDeviceName(); 75 | [DllImport ("OculusPlugin")] 76 | private static extern bool OVR_GetScreenResolution(ref int hResolution, ref int vResolution); 77 | [DllImport ("OculusPlugin")] 78 | private static extern bool OVR_GetScreenSize(ref float hSize, ref float vSize); 79 | [DllImport ("OculusPlugin")] 80 | private static extern bool OVR_GetEyeToScreenDistance(ref float eyeToScreenDistance); 81 | [DllImport ("OculusPlugin")] 82 | private static extern bool OVR_GetEyeOffset(ref float leftEye, ref float rightEye); 83 | [DllImport ("OculusPlugin")] 84 | private static extern bool OVR_GetScreenVCenter(ref float vCenter); 85 | [DllImport ("OculusPlugin")] 86 | private static extern bool OVR_GetDistortionCoefficients(ref float k0, 87 | ref float k1, 88 | ref float k2, 89 | ref float k3); 90 | 91 | // LATENCY TEST FUNCTIONS 92 | [DllImport ("OculusPlugin")] 93 | private static extern void OVR_ProcessLatencyInputs(); 94 | [DllImport ("OculusPlugin")] 95 | private static extern bool OVR_DisplayLatencyScreenColor(ref byte r, 96 | ref byte g, 97 | ref byte b); 98 | [DllImport ("OculusPlugin")] 99 | private static extern System.IntPtr OVR_GetLatencyResultsString(); 100 | 101 | 102 | // PUBLIC 103 | public float InitialPredictionTime = 0.05f; // 50 ms 104 | public float InitialAccelGain = 0.05f; // default value 105 | 106 | // STATIC 107 | private static bool OVRInit = false; 108 | 109 | public static int SensorCount = 0; 110 | 111 | public static String DisplayDeviceName; 112 | 113 | public static int HResolution, VResolution = 0; // pixels 114 | public static float HScreenSize, VScreenSize = 0.0f; // meters 115 | public static float EyeToScreenDistance = 0.0f; // meters 116 | public static float LeftEyeOffset, RightEyeOffset = 0.0f; // meters 117 | public static float ScreenVCenter = 0.0f; // meters 118 | public static float DistK0, DistK1, DistK2, DistK3 = 0.0f; 119 | 120 | // The physical offset of the lenses, used for shifting both IPD and lens distortion 121 | private static float LensOffsetLeft, LensOffsetRight = 0.0f; 122 | 123 | // Fit to top of the image (default is 5" display) 124 | private static float DistortionFitX = 0.0f; 125 | private static float DistortionFitY = 1.0f; 126 | 127 | // Copied from initialized public variables set in editor 128 | private static float PredictionTime = 0.0f; 129 | private static float AccelGain = 0.0f; 130 | 131 | // Used to reduce the size of render distortion and give better fidelity 132 | // Accessed with a public static function 133 | private static float DistortionFitScale = 0.7f; // Optimized for DK1 (7") 134 | 135 | 136 | // * * * * * * * * * * * * * 137 | 138 | // Awake 139 | void Awake () 140 | { 141 | OVRInit = OVR_Initialize(); 142 | 143 | if(OVRInit == false) 144 | return; 145 | 146 | SensorCount = OVR_GetSensorCount(); 147 | 148 | // * * * * * * * 149 | // DISPLAY SETUP 150 | 151 | // We will get the HMD so that we can eventually target it within Unity 152 | DisplayDeviceName += Marshal.PtrToStringAnsi(OVR_GetDisplayDeviceName()); 153 | 154 | OVR_GetScreenResolution (ref HResolution, ref VResolution); 155 | OVR_GetScreenSize (ref HScreenSize, ref VScreenSize); 156 | OVR_GetEyeToScreenDistance(ref EyeToScreenDistance); 157 | OVR_GetEyeOffset (ref LeftEyeOffset, ref RightEyeOffset); 158 | OVR_GetScreenVCenter (ref ScreenVCenter); 159 | OVR_GetDistortionCoefficients( ref DistK0, ref DistK1, ref DistK2, ref DistK3); 160 | 161 | // Distortion fit parameters based on if we are using a 5" (Prototype, DK2+) or 7" (DK1) 162 | if (HScreenSize < 0.140f) // 5.5" 163 | { 164 | DistortionFitX = 0.0f; 165 | DistortionFitY = 1.0f; 166 | 167 | // Don't shrink as much (5.5" has denser pixels) 168 | DistortionFitScale = 1.0f; 169 | } 170 | else // 7" 171 | { 172 | DistortionFitX = -1.0f; 173 | DistortionFitY = 0.0f; 174 | } 175 | 176 | // Calculate the lens offsets for each eye and store 177 | CalculatePhysicalIPDOffsets(ref LensOffsetLeft, ref LensOffsetRight); 178 | 179 | // * * * * * * * 180 | // SENSOR SETUP 181 | 182 | // PredictionTime set, to init sensor directly 183 | if(PredictionTime > 0.0f) 184 | OVR_SetSensorPredictionTime(0, PredictionTime); 185 | else 186 | SetPredictionTime(0, InitialPredictionTime); 187 | 188 | // AcelGain set, used to correct gyro with accel. 189 | // Default value is appropriate for typical use. 190 | if(AccelGain > 0.0f) 191 | OVR_SetSensorAccelGain(0, AccelGain); 192 | else 193 | SetAccelGain(0, InitialAccelGain); 194 | 195 | // Always do a reset of the Sensor when we init 196 | ResetOrientation(0); 197 | } 198 | 199 | // Start (Note: make sure to always have a Start function for classes that have 200 | // editors attached to them) 201 | void Start() 202 | { 203 | } 204 | 205 | // Destroy 206 | void OnDestroy() 207 | { 208 | OVR_Destroy(); 209 | OVRInit = false; 210 | } 211 | 212 | 213 | // * * * * * * * * * * * * 214 | // PUBLIC FUNCTIONS 215 | // * * * * * * * * * * * * 216 | 217 | // Inited - Check to see if system has been initialized 218 | public static bool IsInitialized() 219 | { 220 | return OVRInit; 221 | } 222 | 223 | // HMDPreset 224 | public static bool IsHMDPresent() 225 | { 226 | return OVR_IsHMDPresent(); 227 | } 228 | 229 | // SensorPreset 230 | public static bool IsSensorPresent(int sensor) 231 | { 232 | return OVR_IsSensorPresent(sensor); 233 | } 234 | 235 | // GetOrientation 236 | public static bool GetOrientation(ref Quaternion q) 237 | { 238 | float w = 0, x = 0, y = 0, z = 0; 239 | 240 | if (OVR_GetSensorOrientation(0, ref w, ref x, ref y, ref z) == true) 241 | { 242 | q.w = w; 243 | 244 | // Change the co-ordinate system from right-handed to Unity left-handed 245 | /* 246 | q.x = x; 247 | q.y = y; 248 | q.z = -z; 249 | q = Quaternion.Inverse(q); 250 | */ 251 | 252 | // The following does the exact same conversion as above 253 | q.x = -x; 254 | q.y = -y; 255 | q.z = z; 256 | 257 | return true; 258 | } 259 | 260 | return false; 261 | } 262 | 263 | // GetPredictedOrientation 264 | public static bool GetPredictedOrientation(ref Quaternion q) 265 | { 266 | float w = 0, x = 0, y = 0, z = 0; 267 | 268 | if (OVR_GetSensorPredictedOrientation(0, ref w, ref x, ref y, ref z) == true) 269 | { 270 | 271 | q.w = w; 272 | q.x = -x; 273 | q.y = -y; 274 | q.z = z; 275 | 276 | return true; 277 | } 278 | 279 | return false; 280 | 281 | } 282 | 283 | // ResetOrientation 284 | public static bool ResetOrientation(int sensor) 285 | { 286 | return OVR_ResetSensorOrientation(sensor); 287 | } 288 | 289 | // GetPredictionTime 290 | public static float GetPredictionTime(int sensor) 291 | { 292 | // return OVRSensorsGetPredictionTime(sensor, ref predictonTime); 293 | return PredictionTime; 294 | } 295 | 296 | // SetPredictionTime 297 | public static bool SetPredictionTime(int sensor, float predictionTime) 298 | { 299 | if ( (predictionTime > 0.0f) && 300 | (OVR_SetSensorPredictionTime(sensor, predictionTime) == true)) 301 | { 302 | PredictionTime = predictionTime; 303 | return true; 304 | } 305 | 306 | return false; 307 | } 308 | 309 | // GetAccelGain 310 | public static float GetAccelGain(int sensor) 311 | { 312 | return AccelGain; 313 | } 314 | 315 | // SetAccelGain 316 | public static bool SetAccelGain(int sensor, float accelGain) 317 | { 318 | if ( (accelGain > 0.0f) && 319 | (OVR_SetSensorAccelGain(sensor, accelGain) == true)) 320 | { 321 | AccelGain = accelGain; 322 | return true; 323 | } 324 | 325 | return false; 326 | } 327 | 328 | // GetDistortionCorrectionCoefficients 329 | public static bool GetDistortionCorrectionCoefficients(ref float k0, 330 | ref float k1, 331 | ref float k2, 332 | ref float k3) 333 | { 334 | if(!OVRInit) 335 | return false; 336 | 337 | k0 = DistK0; 338 | k1 = DistK1; 339 | k2 = DistK2; 340 | k3 = DistK3; 341 | 342 | return true; 343 | } 344 | 345 | // SetDistortionCorrectionCoefficients 346 | public static bool SetDistortionCorrectionCoefficients(float k0, 347 | float k1, 348 | float k2, 349 | float k3) 350 | { 351 | if(!OVRInit) 352 | return false; 353 | 354 | DistK0 = k0; 355 | DistK1 = k1; 356 | DistK2 = k2; 357 | DistK3 = k3; 358 | 359 | return true; 360 | } 361 | 362 | // GetPhysicalLensOffsets 363 | public static bool GetPhysicalLensOffsets(ref float lensOffsetLeft, 364 | ref float lensOffsetRight) 365 | { 366 | if(!OVRInit) 367 | return false; 368 | 369 | lensOffsetLeft = LensOffsetLeft; 370 | lensOffsetRight = LensOffsetRight; 371 | 372 | return true; 373 | } 374 | 375 | // GetIPD 376 | public static bool GetIPD(ref float IPD) 377 | { 378 | if(!OVRInit) 379 | return false; 380 | 381 | IPD = LeftEyeOffset + RightEyeOffset; 382 | 383 | return true; 384 | } 385 | 386 | // GetPhysicalLensOffsetsFromIPD 387 | public static bool GetPhysicalLensOffsetsFromIPD(float IPD, 388 | ref float LensOffsetLeft, 389 | ref float LensOffsetRight) 390 | { 391 | LensOffsetLeft = 0.0f; 392 | LensOffsetRight = 0.0f; 393 | 394 | if(!OVRInit) 395 | return false; 396 | 397 | float halfIPD = IPD * 0.5f; 398 | float halfHSS = HScreenSize * 0.5f; 399 | LensOffsetLeft = (((halfHSS - halfIPD ) / halfHSS) * 2.0f) - 1.0f; 400 | LensOffsetRight = ((halfIPD / halfHSS) * 2.0f) - 1.0f; 401 | 402 | return true; 403 | } 404 | 405 | // CalculateAspectRatio 406 | public static float CalculateAspectRatio() 407 | { 408 | if(Application.isEditor) 409 | return (Screen.width * 0.5f) / Screen.height; 410 | else 411 | return (HResolution * 0.5f) / VResolution; 412 | } 413 | 414 | // VerticalFOV 415 | // Compute Vertical FOV based on distance, distortion, etc. 416 | // Distance from vertical center to render vertical edge perceived through the lens. 417 | // This will be larger then normal screen size due to magnification & distortion. 418 | public static float VerticalFOV() 419 | { 420 | if(!OVRInit) 421 | { 422 | return 90.0f; 423 | } 424 | 425 | float percievedHalfScreenDistance = (VScreenSize / 2) * DistortionScale(); 426 | float VFov = Mathf.Rad2Deg * 2.0f * 427 | Mathf.Atan(percievedHalfScreenDistance / EyeToScreenDistance); 428 | 429 | return VFov; 430 | } 431 | 432 | // DistortionScale - Used to adjust size of shader based on 433 | // shader K values to maximize screen size 434 | public static float DistortionScale() 435 | { 436 | if(OVRInit) 437 | { 438 | float ds = 0.0f; 439 | 440 | // Compute distortion scale from DistortionFitX & DistortionFitY. 441 | // Fit value of 0.0 means "no fit". 442 | if ((Mathf.Abs(DistortionFitX) < 0.0001f) && (Math.Abs(DistortionFitY) < 0.0001f)) 443 | { 444 | ds = 1.0f; 445 | } 446 | else 447 | { 448 | // Convert fit value to distortion-centered coordinates before fit radius 449 | // calculation. 450 | float stereoAspect = 0.5f * Screen.width / Screen.height; 451 | float dx = (DistortionFitX * DistortionFitScale) - LensOffsetLeft; 452 | float dy = (DistortionFitY * DistortionFitScale) / stereoAspect; 453 | float fitRadius = Mathf.Sqrt(dx * dx + dy * dy); 454 | ds = CalcScale(fitRadius); 455 | } 456 | 457 | if(ds != 0.0f) 458 | return ds; 459 | 460 | } 461 | 462 | return 1.0f; // no scale 463 | } 464 | 465 | // LatencyProcessInputs 466 | public static void ProcessLatencyInputs() 467 | { 468 | OVR_ProcessLatencyInputs(); 469 | } 470 | 471 | // LatencyProcessInputs 472 | public static bool DisplayLatencyScreenColor(ref byte r, ref byte g, ref byte b) 473 | { 474 | return OVR_DisplayLatencyScreenColor(ref r, ref g, ref b); 475 | } 476 | 477 | // LatencyGetResultsString 478 | public static System.IntPtr GetLatencyResultsString() 479 | { 480 | return OVR_GetLatencyResultsString(); 481 | } 482 | 483 | // Computes scale that should be applied to the input render texture 484 | // before distortion to fit the result in the same screen size. 485 | // The 'fitRadius' parameter specifies the distance away from distortion center at 486 | // which the input and output coordinates will match, assuming [-1,1] range. 487 | static float CalcScale(float fitRadius) 488 | { 489 | float s = fitRadius; 490 | // This should match distortion equation used in shader. 491 | float ssq = s * s; 492 | float scale = s * (DistK0 + DistK1 * ssq + DistK2 * ssq * ssq + DistK3 * ssq * ssq * ssq); 493 | return scale / fitRadius; 494 | } 495 | 496 | // CalculatePhysicalIPDOffsets - Used to offset both the IPD of camera (perspective shift) 497 | // and distortion shift 498 | static bool CalculatePhysicalIPDOffsets(ref float leftOffset, ref float rightOffset) 499 | { 500 | leftOffset = 0.0f; 501 | rightOffset = 0.0f; 502 | 503 | if(!OVRInit) 504 | return false; 505 | 506 | float halfHSS = HScreenSize * 0.5f; 507 | leftOffset = (((halfHSS - LeftEyeOffset) / halfHSS) * 2.0f) - 1.0f; 508 | rightOffset = ((RightEyeOffset / halfHSS) * 2.0f) - 1.0f; 509 | 510 | return true; 511 | } 512 | 513 | } 514 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRDevice.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7e933e81d3c20c74ea6fdc708a67e3a5 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRGamepadController.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRGamepadController.cs 4 | Content : Interface to XBox360 controller 5 | Created : January 8, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | 16 | using UnityEngine; 17 | #if UNITY_STANDALONE_WIN 18 | using XInputDotNetPure; 19 | #else 20 | // Different input device interface for other platforms goes here 21 | #endif 22 | 23 | //------------------------------------------------------------------------------------- 24 | // ***** OVRGamepadController 25 | // 26 | // OVRGamepadController is an interface class to a gamepad controller. 27 | // 28 | // On Windows machines, the gamepad must be XInput-compliant. 29 | // 30 | public class OVRGamepadController : MonoBehaviour 31 | { 32 | // Only Windows supports XInput-compliant controllers 33 | #if UNITY_STANDALONE_WIN 34 | 35 | private static bool playerIndexSet = false; 36 | private static PlayerIndex playerIndex; 37 | 38 | public static GamePadState state; 39 | 40 | private GamePadState testState; 41 | 42 | // * * * * * * * * * * * * * 43 | 44 | // Start 45 | void Start() 46 | { 47 | } 48 | 49 | // Update 50 | void Update() 51 | { 52 | // Find a PlayerIndex, for a single player game 53 | if (!playerIndexSet) 54 | { 55 | for (int i = 0; i < 4; ++i) 56 | { 57 | PlayerIndex pidx = (PlayerIndex)i; 58 | testState = GamePad.GetState(pidx); 59 | 60 | if (testState.IsConnected) 61 | { 62 | Debug.Log(string.Format("GamePad {0} found", pidx)); 63 | playerIndex = pidx; 64 | playerIndexSet = true; 65 | } 66 | } 67 | } 68 | 69 | state = GamePad.GetState(playerIndex); 70 | } 71 | 72 | // CheckButton 73 | private static bool CheckButton(ButtonState bState) 74 | { 75 | if(!playerIndexSet) return false; 76 | if(bState == ButtonState.Pressed) return true; 77 | return false; 78 | } 79 | 80 | // * * * * * * * * * * * * * 81 | // Analog 82 | public static float GetAxisLeftX() 83 | { 84 | if(!playerIndexSet) return 0.0f; 85 | return state.ThumbSticks.Left.X; 86 | } 87 | public static float GetAxisLeftY() 88 | { 89 | if(!playerIndexSet) return 0.0f; 90 | return state.ThumbSticks.Left.Y; 91 | } 92 | public static float GetAxisRightX() 93 | { 94 | if(!playerIndexSet) return 0.0f; 95 | return state.ThumbSticks.Right.X; 96 | } 97 | public static float GetAxisRightY() 98 | { 99 | if(!playerIndexSet) return 0.0f; 100 | return state.ThumbSticks.Right.Y; 101 | } 102 | public static float GetTriggerLeft() 103 | { 104 | if(!playerIndexSet) return 0.0f; 105 | return state.Triggers.Left; 106 | } 107 | public static float GetTriggerRight() 108 | { 109 | if(!playerIndexSet) return 0.0f; 110 | return state.Triggers.Right; 111 | } 112 | // * * * * * * * * * * * * * 113 | // DPad 114 | public static bool GetDPadUp() 115 | { 116 | return CheckButton(state.DPad.Up); 117 | } 118 | public static bool GetDPadDown() 119 | { 120 | return CheckButton(state.DPad.Down); 121 | } 122 | public static bool GetDPadLeft() 123 | { 124 | return CheckButton(state.DPad.Left); 125 | } 126 | public static bool GetDPadRight() 127 | { 128 | return CheckButton(state.DPad.Right); 129 | } 130 | // * * * * * * * * * * * * * 131 | // Buttons 132 | public static bool GetButtonStart() 133 | { 134 | return CheckButton(state.Buttons.Start); 135 | } 136 | public static bool GetButtonBack() 137 | { 138 | return CheckButton(state.Buttons.Back); 139 | } 140 | public static bool GetButtonA() 141 | { 142 | return CheckButton(state.Buttons.A); 143 | } 144 | public static bool GetButtonB() 145 | { 146 | return CheckButton(state.Buttons.B); 147 | } 148 | public static bool GetButtonX() 149 | { 150 | return CheckButton(state.Buttons.X); 151 | } 152 | public static bool GetButtonY() 153 | { 154 | return CheckButton(state.Buttons.Y); 155 | } 156 | public static bool GetButtonLShoulder() 157 | { 158 | return CheckButton(state.Buttons.LeftShoulder); 159 | } 160 | public static bool GetButtonRShoulder() 161 | { 162 | return CheckButton(state.Buttons.RightShoulder); 163 | } 164 | public static bool GetButtonLStick() 165 | { 166 | return CheckButton(state.Buttons.LeftStick); 167 | } 168 | public static bool GetButtonRStick() 169 | { 170 | return CheckButton(state.Buttons.RightStick); 171 | } 172 | #else 173 | public static float GetAxisLeftX() 174 | { 175 | return 0; 176 | } 177 | public static float GetAxisLeftY() 178 | { 179 | return 0; 180 | } 181 | public static float GetAxisRightX() 182 | { 183 | return 0; 184 | } 185 | public static float GetAxisRightY() 186 | { 187 | return 0; 188 | } 189 | public static float GetTriggerLeft() 190 | { 191 | return 0; 192 | } 193 | public static float GetTriggerRight() 194 | { 195 | return 0; 196 | } 197 | // DPad 198 | public static bool GetDPadUp() 199 | { 200 | return false; 201 | } 202 | public static bool GetDPadDown() 203 | { 204 | return false; 205 | } 206 | public static bool GetDPadLeft() 207 | { 208 | return false; 209 | } 210 | public static bool GetDPadRight() 211 | { 212 | return false; 213 | } 214 | // Buttons 215 | public static bool GetButtonStart() 216 | { 217 | return false; 218 | } 219 | public static bool GetButtonBack() 220 | { 221 | return false; 222 | } 223 | public static bool GetButtonA() 224 | { 225 | return false; 226 | } 227 | public static bool GetButtonB() 228 | { 229 | return false; 230 | } 231 | public static bool GetButtonX() 232 | { 233 | return false; 234 | } 235 | public static bool GetButtonY() 236 | { 237 | return false; 238 | } 239 | public static bool GetButtonLShoulder() 240 | { 241 | return false; 242 | } 243 | public static bool GetButtonRShoulder() 244 | { 245 | return false; 246 | } 247 | public static bool GetButtonLStick() 248 | { 249 | return false; 250 | } 251 | public static bool GetButtonRStick() 252 | { 253 | return false; 254 | } 255 | #endif 256 | } 257 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRGamepadController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1bf170db1c6a44a44941f0cfb6048fd4 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRMainMenu.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRMainMenu.cs 4 | Content : Main script to run various Unity scenes 5 | Created : January 8, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | using UnityEngine; 16 | using System.Collections; 17 | using XInputDotNetPure; 18 | 19 | //------------------------------------------------------------------------------------- 20 | // ***** OVRMainMenu 21 | // 22 | // OVRMainMenu is used to control the loading of different scenes. It also renders out 23 | // a menu that allows a user to modify various Rift settings, and allow for storing 24 | // these settings for recall later. 25 | // 26 | // A user of this component can add as many scenes that they would like to be able to 27 | // have access to. 28 | // 29 | // OVRMainMenu is currently attached to the OVRPlayerController prefab for convenience, 30 | // but can safely removed from it and added to another GameObject that is used for general 31 | // Unity logic. 32 | // 33 | public class OVRMainMenu : MonoBehaviour 34 | { 35 | // PUBLIC 36 | public float FadeInTime = 3.0f; 37 | public Texture FadeInTexture = null; 38 | public Font FontReplaceSmall = null; 39 | public Font FontReplaceLarge = null; 40 | 41 | // Scenes to show onscreen 42 | public string [] SceneNames; 43 | public string [] Scenes; 44 | 45 | // Spacing for scenes menu 46 | private int StartX = 240; 47 | private int StartY = 300; 48 | private int WidthX = 300; 49 | private int WidthY = 28; 50 | private int StepY = 30; 51 | private int StereoSpreadX = -40; 52 | 53 | // Spacing for variables that users can change 54 | private int VRVarsSX = 300; 55 | private int VRVarsSY = 425; 56 | private int VRVarsWidthX = 170; 57 | private int VRVarsWidthY = 28; 58 | 59 | 60 | 61 | // Handle to camera controller 62 | private OVRCameraController CameraController = null; 63 | 64 | // Controller buttons 65 | private bool PrevStartDown; 66 | private bool PrevHatDown; 67 | private bool PrevHatUp; 68 | 69 | private bool ShowVRVars; 70 | 71 | private bool OldSpaceHit; 72 | 73 | // FPS 74 | private float UpdateInterval = 0.5f; 75 | private float Accum = 0; 76 | private int Frames = 0; 77 | private float TimeLeft = 0; 78 | private string strFPS = "FPS: 0"; 79 | 80 | // IPD shift from physical IPD 81 | public float IPDIncrement = 0.0025f; 82 | private string strIPD = "IPD: 0.000"; 83 | 84 | // Prediction (in ms) 85 | public float PredictionIncrement = 0.001f; // 1 ms 86 | private string strPrediction = "Pred: OFF"; 87 | 88 | // FOV Variables 89 | public float FOVIncrement = 0.2f; 90 | private string strFOV = "FOV: 0.0f"; 91 | 92 | // Distortion Variables 93 | public float DistKIncrement = 0.001f; 94 | private string strDistortion = "Dist k1: 0.00f k2 0.00f"; 95 | 96 | // Height adjustment 97 | public float HeightIncrement = 0.01f; 98 | private string strHeight = "Height: 0.0f"; 99 | 100 | // Speed and rotation adjustment 101 | public float SpeedRotationIncrement = 0.05f; 102 | private string strSpeedRotationMultipler = "Spd. X: 0.0f Rot. X: 0.0f"; 103 | 104 | private bool LoadingLevel = false; 105 | private float AlphaFadeValue = 1.0f; 106 | private int CurrentLevel = 0; 107 | 108 | // Rift detection 109 | private bool HMDPresent = false; 110 | private bool SensorPresent = false; 111 | private float RiftPresentTimeout = 0.0f; 112 | private string strRiftPresent = ""; 113 | 114 | // Create a delegate for update functions 115 | private delegate void updateFunctions(); 116 | private updateFunctions UpdateFunctions; 117 | 118 | // STATIC VARIABLES 119 | 120 | // Can be checked to see if level selection is showing 121 | // (used to disable systems like movement input etc.) 122 | public static bool sShowLevels = false; 123 | 124 | 125 | 126 | // * * * * * * * * * * * * * 127 | 128 | // Awake 129 | void Awake() 130 | { 131 | OVRCameraController[] CameraControllers; 132 | CameraControllers = gameObject.GetComponentsInChildren(); 133 | 134 | if(CameraControllers.Length == 0) 135 | Debug.LogWarning("No OVRCameraController attached."); 136 | else if (CameraControllers.Length > 1) 137 | Debug.LogWarning("More then 1 OVRCameraController attached."); 138 | else 139 | CameraController = CameraControllers[0]; 140 | } 141 | 142 | // Start 143 | void Start() 144 | { 145 | AlphaFadeValue = 1.0f; 146 | CurrentLevel = 0; 147 | PrevStartDown = false; 148 | PrevHatDown = false; 149 | PrevHatUp = false; 150 | ShowVRVars = false; 151 | OldSpaceHit = false; 152 | strFPS = "FPS: 0"; 153 | LoadingLevel = false; 154 | 155 | sShowLevels = false; 156 | 157 | // Ensure that camera controller variables have been properly 158 | // initialized before we start reading them 159 | if(CameraController != null) 160 | CameraController.InitCameraControllerVariables(); 161 | 162 | // Save default values initially 163 | StoreSnapshot("DEFAULT"); 164 | 165 | // Make sure to hide cursor 166 | Screen.showCursor = false; 167 | Screen.lockCursor = true; 168 | 169 | // Add delegates to update; useful for ordering menu tasks, if required 170 | UpdateFunctions += UpdateFPS; 171 | UpdateFunctions += UpdateIPD; 172 | UpdateFunctions += UpdatePrediction; 173 | UpdateFunctions += UpdateFOV; 174 | UpdateFunctions += UpdateDistortionCoefs; 175 | UpdateFunctions += UpdateHeightOffset; 176 | UpdateFunctions += UpdateSpeedAndRotationMultiplier; 177 | UpdateFunctions += UpdateSelectCurrentLevel; 178 | UpdateFunctions += UpdateHandleSnapshots; 179 | UpdateFunctions += UpdateResetOrientation; 180 | 181 | // Check for HMD and sensor 182 | CheckIfRiftPresent(); 183 | 184 | // Init static members 185 | sShowLevels = false; 186 | } 187 | 188 | // Update 189 | void Update() 190 | { 191 | if(LoadingLevel == true) 192 | return; 193 | 194 | UpdateFunctions(); 195 | 196 | // Toggle Fullscreen 197 | if(Input.GetKeyDown(KeyCode.F11)) 198 | Screen.fullScreen = !Screen.fullScreen; 199 | 200 | // Escape Application 201 | if (Input.GetKeyDown(KeyCode.Escape)) 202 | Application.Quit(); 203 | } 204 | 205 | // UpdateFPS 206 | void UpdateFPS() 207 | { 208 | TimeLeft -= Time.deltaTime; 209 | Accum += Time.timeScale/Time.deltaTime; 210 | ++Frames; 211 | 212 | // Interval ended - update GUI text and start new interval 213 | if( TimeLeft <= 0.0 ) 214 | { 215 | // display two fractional digits (f2 format) 216 | float fps = Accum / Frames; 217 | 218 | if(ShowVRVars == true)// limit gc 219 | strFPS = System.String.Format("FPS: {0:F2}",fps); 220 | 221 | TimeLeft += UpdateInterval; 222 | Accum = 0.0f; 223 | Frames = 0; 224 | } 225 | } 226 | 227 | // UpdateIPD 228 | void UpdateIPD() 229 | { 230 | if(Input.GetKeyDown (KeyCode.Equals)) 231 | { 232 | float ipd = 0; 233 | CameraController.GetIPD(ref ipd); 234 | ipd += IPDIncrement; 235 | CameraController.SetIPD (ipd); 236 | } 237 | else if(Input.GetKeyDown (KeyCode.Minus)) 238 | { 239 | float ipd = 0; 240 | CameraController.GetIPD(ref ipd); 241 | ipd -= IPDIncrement; 242 | CameraController.SetIPD (ipd); 243 | } 244 | 245 | if(ShowVRVars == true)// limit gc 246 | { 247 | float ipd = 0; 248 | CameraController.GetIPD (ref ipd); 249 | strIPD = System.String.Format("IPD (ms): {0:F4}", ipd * 1000.0f); 250 | } 251 | } 252 | 253 | // UpdatePrediction 254 | void UpdatePrediction() 255 | { 256 | // Turn prediction on/off 257 | if(Input.GetKeyDown (KeyCode.P)) 258 | { 259 | if( OVRCamera.PredictionOn == false) 260 | OVRCamera.PredictionOn = true; 261 | else 262 | OVRCamera.PredictionOn = false; 263 | } 264 | 265 | // Update prediction value (only if prediction is on) 266 | if(OVRCamera.PredictionOn == true) 267 | { 268 | float pt = OVRDevice.GetPredictionTime(0); 269 | if(Input.GetKeyDown (KeyCode.Comma)) 270 | pt -= PredictionIncrement; 271 | else if(Input.GetKeyDown (KeyCode.Period)) 272 | pt += PredictionIncrement; 273 | 274 | OVRDevice.SetPredictionTime(0, pt); 275 | 276 | // re-get the prediction time to make sure it took 277 | pt = OVRDevice.GetPredictionTime(0) * 1000.0f; 278 | 279 | if(ShowVRVars == true)// limit gc 280 | strPrediction = System.String.Format ("Pred (ms): {0:F3}", pt); 281 | } 282 | else 283 | { 284 | strPrediction = "Pred: OFF"; 285 | } 286 | } 287 | 288 | // UpdateFOV 289 | void UpdateFOV() 290 | { 291 | if(Input.GetKeyDown (KeyCode.LeftBracket)) 292 | { 293 | float cfov = 0; 294 | CameraController.GetVerticalFOV(ref cfov); 295 | cfov -= FOVIncrement; 296 | CameraController.SetVerticalFOV(cfov); 297 | } 298 | else if (Input.GetKeyDown (KeyCode.RightBracket)) 299 | { 300 | float cfov = 0; 301 | CameraController.GetVerticalFOV(ref cfov); 302 | cfov += FOVIncrement; 303 | CameraController.SetVerticalFOV(cfov); 304 | } 305 | 306 | if(ShowVRVars == true)// limit gc 307 | { 308 | float cfov = 0; 309 | CameraController.GetVerticalFOV(ref cfov); 310 | strFOV = System.String.Format ("FOV (deg): {0:F3}", cfov); 311 | } 312 | } 313 | 314 | // UpdateDistortionCoefs 315 | void UpdateDistortionCoefs() 316 | { 317 | float Dk0 = 0.0f; 318 | float Dk1 = 0.0f; 319 | float Dk2 = 0.0f; 320 | float Dk3 = 0.0f; 321 | 322 | // * * * * * * * * * 323 | // Get the distortion coefficients to apply to shader 324 | CameraController.GetDistortionCoefs(ref Dk0, ref Dk1, ref Dk2, ref Dk3); 325 | 326 | if(Input.GetKeyDown(KeyCode.Alpha1)) 327 | Dk1 -= DistKIncrement; 328 | else if (Input.GetKeyDown(KeyCode.Alpha2)) 329 | Dk1 += DistKIncrement; 330 | 331 | if(Input.GetKeyDown(KeyCode.Alpha3)) 332 | Dk2 -= DistKIncrement; 333 | else if (Input.GetKeyDown(KeyCode.Alpha4)) 334 | Dk2 += DistKIncrement; 335 | 336 | CameraController.SetDistortionCoefs(Dk0, Dk1, Dk2, Dk3); 337 | 338 | if(ShowVRVars == true)// limit gc 339 | strDistortion = 340 | System.String.Format ("DST k1: {0:F3} k2 {1:F3}", Dk1, Dk2); 341 | } 342 | 343 | // UpdateHeightOffset 344 | void UpdateHeightOffset() 345 | { 346 | if(Input.GetKeyDown(KeyCode.Alpha5)) 347 | { 348 | Vector3 neckPosition = Vector3.zero; 349 | CameraController.GetNeckPosition(ref neckPosition); 350 | neckPosition.y -= HeightIncrement; 351 | CameraController.SetNeckPosition(neckPosition); 352 | } 353 | else if (Input.GetKeyDown(KeyCode.Alpha6)) 354 | { 355 | Vector3 neckPosition = Vector3.zero;; 356 | CameraController.GetNeckPosition(ref neckPosition); 357 | neckPosition.y += HeightIncrement; 358 | CameraController.SetNeckPosition(neckPosition); 359 | } 360 | 361 | if(ShowVRVars == true)// limit gc 362 | { 363 | Vector3 rootPosition = new Vector3(0.0f, 1.0f, 0.0f); 364 | Vector3 neckPosition = Vector3.zero; 365 | CameraController.GetNeckPosition(ref neckPosition); 366 | Vector3 eyePosition = Vector3.zero; 367 | CameraController.GetEyeCenterPosition(ref eyePosition); 368 | 369 | // default capsule is 2.0m, but the center offset is at 1.0m 370 | float ph = rootPosition.y + neckPosition.y + eyePosition.y; 371 | 372 | strHeight = System.String.Format ("Player Height (m): {0:F3}", ph); 373 | } 374 | } 375 | 376 | // UpdateSpeedAndRotationMultiplier 377 | void UpdateSpeedAndRotationMultiplier() 378 | { 379 | if(Input.GetKeyDown(KeyCode.Alpha7)) 380 | OVRPlayerController.MoveScaleMultiplier -= SpeedRotationIncrement; 381 | else if (Input.GetKeyDown(KeyCode.Alpha8)) 382 | OVRPlayerController.MoveScaleMultiplier += SpeedRotationIncrement; 383 | 384 | if(Input.GetKeyDown(KeyCode.Alpha9)) 385 | OVRPlayerController.RotationScaleMultiplier -= SpeedRotationIncrement; 386 | else if (Input.GetKeyDown(KeyCode.Alpha0)) 387 | OVRPlayerController.RotationScaleMultiplier += SpeedRotationIncrement; 388 | 389 | if(ShowVRVars == true)// limit gc 390 | strSpeedRotationMultipler = System.String.Format ("Spd.X: {0:F2} Rot.X: {1:F2}", 391 | OVRPlayerController.MoveScaleMultiplier, 392 | OVRPlayerController.RotationScaleMultiplier); 393 | } 394 | 395 | 396 | 397 | // UpdateSelectCurrentLevel 398 | void UpdateSelectCurrentLevel() 399 | { 400 | ShowLevels(); 401 | 402 | if(sShowLevels == false) 403 | return; 404 | 405 | CurrentLevel = GetCurrentLevel(); 406 | 407 | if((Scenes.Length != 0) && 408 | ((OVRGamepadController.GetButtonA() == true) || 409 | Input.GetKeyDown(KeyCode.Return))) 410 | { 411 | Application.LoadLevel(Scenes[CurrentLevel]); 412 | } 413 | } 414 | 415 | // ShowLevels 416 | bool ShowLevels() 417 | { 418 | if(Scenes.Length == 0) 419 | { 420 | sShowLevels = false; 421 | return sShowLevels; 422 | } 423 | 424 | bool curStartDown = false; 425 | if(OVRGamepadController.GetButtonStart() == true) 426 | curStartDown = true; 427 | 428 | if((PrevStartDown == false) && (curStartDown == true) || 429 | Input.GetKeyDown(KeyCode.RightShift) ) 430 | { 431 | if(sShowLevels == true) 432 | sShowLevels = false; 433 | else 434 | sShowLevels = true; 435 | } 436 | 437 | PrevStartDown = curStartDown; 438 | 439 | return sShowLevels; 440 | } 441 | 442 | // GetCurrentLevel 443 | int GetCurrentLevel() 444 | { 445 | bool curHatDown = false; 446 | if(OVRGamepadController.GetDPadDown() == true) 447 | curHatDown = true; 448 | 449 | bool curHatUp = false; 450 | if(OVRGamepadController.GetDPadDown() == true) 451 | curHatUp = true; 452 | 453 | if((PrevHatDown == false) && (curHatDown == true) || 454 | Input.GetKeyDown(KeyCode.DownArrow)) 455 | { 456 | CurrentLevel = (CurrentLevel + 1) % SceneNames.Length; 457 | } 458 | else if((PrevHatUp == false) && (curHatUp == true) || 459 | Input.GetKeyDown(KeyCode.UpArrow)) 460 | { 461 | CurrentLevel--; 462 | if(CurrentLevel < 0) 463 | CurrentLevel = SceneNames.Length - 1; 464 | } 465 | 466 | PrevHatDown = curHatDown; 467 | PrevHatUp = curHatUp; 468 | 469 | return CurrentLevel; 470 | } 471 | 472 | // GUI 473 | 474 | // OnGUI 475 | void OnGUI() 476 | { 477 | if(LoadingLevel == true) 478 | return; 479 | 480 | // Fade in screen 481 | if(AlphaFadeValue > 0.0f) 482 | { 483 | AlphaFadeValue -= Mathf.Clamp01(Time.deltaTime / FadeInTime); 484 | if(AlphaFadeValue < 0.0f) AlphaFadeValue = 0.0f; 485 | GUI.color = new Color(0, 0, 0, AlphaFadeValue); 486 | GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), FadeInTexture ); 487 | } 488 | else 489 | { 490 | // If true, we are displaying information about the Rift not being detected 491 | // So do not show anything else 492 | if(ShowRiftDetected() == true) 493 | return; 494 | 495 | //if(GUI 496 | 497 | GUIShowLevels(); 498 | GUIShowVRVariables(); 499 | } 500 | } 501 | 502 | // GUIShowLevels 503 | void GUIShowLevels() 504 | { 505 | if(sShowLevels == true) 506 | { 507 | // Darken the background by rendering fade texture 508 | GUI.color = new Color(0, 0, 0, 0.7f); 509 | GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), FadeInTexture ); 510 | GUI.color = Color.white; 511 | 512 | for (int i = 0; i < SceneNames.Length; i++) 513 | { 514 | Color c; 515 | if(i == CurrentLevel) 516 | c = Color.yellow; 517 | else 518 | c = Color.black; 519 | 520 | int y = StartY + (i * StepY); 521 | 522 | GUIStereoBox (StartX, y, WidthX, WidthY, ref SceneNames[i], c); 523 | } 524 | } 525 | } 526 | 527 | // GUIShowVRVariables 528 | void GUIShowVRVariables() 529 | { 530 | bool SpaceHit = Input.GetKey("space"); 531 | if ((OldSpaceHit == false) && (SpaceHit == true)) 532 | { 533 | if(ShowVRVars == true) 534 | ShowVRVars = false; 535 | else 536 | ShowVRVars = true; 537 | } 538 | 539 | OldSpaceHit = SpaceHit; 540 | 541 | if(ShowVRVars == false) 542 | return; 543 | 544 | int y = VRVarsSY; 545 | 546 | // FPS 547 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 548 | ref strFPS, Color.green); 549 | y += StepY; 550 | // Prediction code 551 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 552 | ref strPrediction, Color.white); 553 | // IPD 554 | y += StepY; 555 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 556 | ref strIPD, Color.yellow); 557 | // FOV 558 | y += StepY; 559 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 560 | ref strFOV, Color.white); 561 | // Player Height 562 | y += StepY; 563 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 564 | ref strHeight, Color.yellow); 565 | // Speed Rotation Multiplier 566 | y += StepY; 567 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 568 | ref strSpeedRotationMultipler, Color.white); 569 | // Distortion k values 570 | y += StepY; 571 | GUIStereoBox (VRVarsSX, y, VRVarsWidthX, VRVarsWidthY, 572 | ref strDistortion, Color.red); 573 | } 574 | 575 | // GUIStereoBox - Values based on pixels in DK1 resolution of W: (1280 / 2) H: 800 576 | void GUIStereoBox(int X, int Y, int wX, int wY, ref string text, Color color) 577 | { 578 | float ploLeft = 0, ploRight = 0; 579 | float sSX = (float)Screen.width / 1280.0f; 580 | 581 | float sSY = ((float)Screen.height / 800.0f); 582 | 583 | CameraController.GetPhysicalLensOffsets(ref ploLeft, ref ploRight); 584 | int xL = (int)((float)X * sSX); 585 | int sSpreadX = (int)((float)StereoSpreadX * sSX); 586 | int xR = (Screen.width / 2) + xL + sSpreadX - 587 | // required to adjust for physical lens shift 588 | (int)(ploLeft * (float)Screen.width / 2); 589 | int y = (int)((float)Y * sSY); 590 | 591 | GUI.contentColor = color; 592 | 593 | int sWX = (int)((float)wX * sSX); 594 | int sWY = (int)((float)wY * sSY); 595 | 596 | // Change font size based on screen scale 597 | if(Screen.height > 800) 598 | GUI.skin.font = FontReplaceLarge; 599 | else 600 | GUI.skin.font = FontReplaceSmall; 601 | 602 | GUI.Box(new Rect(xL, y, sWX, sWY), text); 603 | GUI.Box(new Rect(xR, y, sWX, sWY), text); 604 | } 605 | 606 | // SNAPSHOT MANAGEMENT 607 | 608 | // UpdateHandleSnapshots 609 | void UpdateHandleSnapshots() 610 | { 611 | // Default shapshot 612 | if(Input.GetKeyDown(KeyCode.F2)) 613 | LoadSnapshot ("DEFAULT"); 614 | 615 | // Snapshot 1 616 | if(Input.GetKeyDown(KeyCode.F3)) 617 | { 618 | if(Input.GetKey(KeyCode.Tab)) 619 | StoreSnapshot ("SNAPSHOT1"); 620 | else 621 | LoadSnapshot ("SNAPSHOT1"); 622 | } 623 | 624 | // Snapshot 2 625 | if(Input.GetKeyDown(KeyCode.F4)) 626 | { 627 | if(Input.GetKey(KeyCode.Tab)) 628 | StoreSnapshot ("SNAPSHOT2"); 629 | else 630 | LoadSnapshot ("SNAPSHOT2"); 631 | } 632 | 633 | // Snapshot 3 634 | if(Input.GetKeyDown(KeyCode.F5)) 635 | { 636 | if(Input.GetKey(KeyCode.Tab)) 637 | StoreSnapshot ("SNAPSHOT3"); 638 | else 639 | LoadSnapshot ("SNAPSHOT3"); 640 | } 641 | 642 | } 643 | 644 | // StoreSnapshot 645 | bool StoreSnapshot(string snapshotName) 646 | { 647 | float f = 0; 648 | 649 | OVRPresetManager.SetCurrentPreset(snapshotName); 650 | 651 | CameraController.GetIPD(ref f); 652 | OVRPresetManager.SetPropertyFloat("IPD", ref f); 653 | 654 | f = OVRDevice.GetPredictionTime(0); 655 | OVRPresetManager.SetPropertyFloat("PREDICTION", ref f); 656 | 657 | CameraController.GetVerticalFOV(ref f); 658 | OVRPresetManager.SetPropertyFloat("FOV", ref f); 659 | 660 | Vector3 neckPosition = Vector3.zero; 661 | CameraController.GetNeckPosition(ref neckPosition); 662 | OVRPresetManager.SetPropertyFloat("HEIGHT", ref neckPosition.y); 663 | 664 | OVRPresetManager.SetPropertyFloat("SPEEDMULT", 665 | ref OVRPlayerController.MoveScaleMultiplier); 666 | 667 | OVRPresetManager.SetPropertyFloat("ROTMULT", 668 | ref OVRPlayerController.RotationScaleMultiplier); 669 | 670 | float Dk0 = 0.0f; 671 | float Dk1 = 0.0f; 672 | float Dk2 = 0.0f; 673 | float Dk3 = 0.0f; 674 | CameraController.GetDistortionCoefs(ref Dk0, ref Dk1, ref Dk2, ref Dk3); 675 | 676 | OVRPresetManager.SetPropertyFloat("DISTORTIONK0", ref Dk0); 677 | OVRPresetManager.SetPropertyFloat("DISTORTIONK1", ref Dk1); 678 | OVRPresetManager.SetPropertyFloat("DISTORTIONK2", ref Dk2); 679 | OVRPresetManager.SetPropertyFloat("DISTORTIONK3", ref Dk3); 680 | 681 | return true; 682 | } 683 | 684 | // LoadSnapshot 685 | bool LoadSnapshot(string snapshotName) 686 | { 687 | float f = 0; 688 | 689 | OVRPresetManager.SetCurrentPreset(snapshotName); 690 | 691 | if(OVRPresetManager.GetPropertyFloat("IPD", ref f) == true) 692 | CameraController.SetIPD(f); 693 | 694 | if(OVRPresetManager.GetPropertyFloat("PREDICTION", ref f) == true) 695 | OVRDevice.SetPredictionTime(0, f); 696 | 697 | if(OVRPresetManager.GetPropertyFloat("FOV", ref f) == true) 698 | CameraController.SetVerticalFOV(f); 699 | 700 | if(OVRPresetManager.GetPropertyFloat("HEIGHT", ref f) == true) 701 | { 702 | Vector3 neckPosition = Vector3.zero; 703 | CameraController.GetNeckPosition(ref neckPosition); 704 | neckPosition.y = f; 705 | CameraController.SetNeckPosition(neckPosition); 706 | } 707 | 708 | if(OVRPresetManager.GetPropertyFloat("SPEEDMULT", ref f) == true) 709 | OVRPlayerController.MoveScaleMultiplier = f; 710 | 711 | if(OVRPresetManager.GetPropertyFloat("ROTMULT", ref f) == true) 712 | OVRPlayerController.RotationScaleMultiplier = f; 713 | 714 | float Dk0 = 0.0f; 715 | float Dk1 = 0.0f; 716 | float Dk2 = 0.0f; 717 | float Dk3 = 0.0f; 718 | CameraController.GetDistortionCoefs(ref Dk0, ref Dk1, ref Dk2, ref Dk3); 719 | 720 | if(OVRPresetManager.GetPropertyFloat("DISTORTIONK0", ref f) == true) 721 | Dk0 = f; 722 | if(OVRPresetManager.GetPropertyFloat("DISTORTIONK1", ref f) == true) 723 | Dk1 = f; 724 | if(OVRPresetManager.GetPropertyFloat("DISTORTIONK2", ref f) == true) 725 | Dk2 = f; 726 | if(OVRPresetManager.GetPropertyFloat("DISTORTIONK3", ref f) == true) 727 | Dk3 = f; 728 | 729 | CameraController.SetDistortionCoefs(Dk0, Dk1, Dk2, Dk3); 730 | 731 | return true; 732 | } 733 | 734 | // RIFT DETECTION 735 | 736 | // CheckIfRiftPresent 737 | // Checks to see if HMD and / or sensor is available, and displays a 738 | // message if it is not 739 | void CheckIfRiftPresent() 740 | { 741 | HMDPresent = OVRDevice.IsHMDPresent(); 742 | SensorPresent = OVRDevice.IsSensorPresent(0); // 0 is the main head sensor 743 | 744 | if((HMDPresent == false) || (SensorPresent == false)) 745 | { 746 | RiftPresentTimeout = 10.0f; // Keep message up for 10 seconds 747 | 748 | if((HMDPresent == false) && (SensorPresent == false)) 749 | strRiftPresent = "NO HMD AND SENSOR DETECTED"; 750 | else if (HMDPresent == false) 751 | strRiftPresent = "NO HMD DETECTED"; 752 | else if (SensorPresent == false) 753 | strRiftPresent = "NO SENSOR DETECTED"; 754 | } 755 | } 756 | 757 | // ShowRiftDetected 758 | bool ShowRiftDetected() 759 | { 760 | if(RiftPresentTimeout > 0.0f) 761 | { 762 | RiftPresentTimeout -= Time.deltaTime; 763 | if(RiftPresentTimeout < 0.0f) 764 | RiftPresentTimeout = 0.0f; 765 | 766 | GUIStereoBox (StartX, StartY, WidthX, WidthY, ref strRiftPresent, Color.white); 767 | 768 | return true; 769 | } 770 | 771 | return false; 772 | } 773 | 774 | // RIFT RESET ORIENTATION 775 | 776 | // UpdateResetOrientation 777 | void UpdateResetOrientation() 778 | { 779 | if( ((sShowLevels == false) && (OVRGamepadController.GetDPadDown () == true)) || 780 | (Input.GetKeyDown(KeyCode.B) == true) ) 781 | { 782 | OVRDevice.ResetOrientation(0); 783 | } 784 | } 785 | 786 | } 787 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRMainMenu.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e437ffb7de5844b42b64f89763daf449 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRPlayerController.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRPlayerController.cs 4 | Content : Player controller interface. 5 | This script drives OVR camera as well as controls the locomotion 6 | of the player, and handles physical contact in the world. 7 | Created : January 8, 2013 8 | Authors : Peter Giokaris 9 | 10 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 11 | 12 | Use of this software is subject to the terms of the Oculus LLC license 13 | agreement provided at the time of installation or download, or which 14 | otherwise accompanies this software in either electronic or hard copy form. 15 | 16 | ************************************************************************************/ 17 | 18 | using UnityEngine; 19 | using System.Collections.Generic; 20 | 21 | using XInputDotNetPure; 22 | 23 | [RequireComponent(typeof(CharacterController))] 24 | 25 | //------------------------------------------------------------------------------------- 26 | // ***** OVRPlayerController 27 | // 28 | // OVRPlayerController implements a basic first person controller for the Rift. It is 29 | // attached to the OVRPlayerController prefab, which has an OVRCameraController attached 30 | // to it. 31 | // 32 | // The controller will interact properly with a Unity scene, provided that the scene has 33 | // collision assigned to it. 34 | // 35 | // The OVRPlayerController prefab has an empty GameObject attached to it called 36 | // ForwardDirection. This game object contains the matrix which motor control bases it 37 | // direction on. This game object should also house the body geometry which will be seen 38 | // by the player. 39 | // 40 | public class OVRPlayerController : OVRComponent 41 | { 42 | protected CharacterController Controller = null; 43 | protected OVRCameraController CameraController = null; 44 | 45 | public float Acceleration = 0.1f; 46 | public float Damping = 0.15f; 47 | public float JumpForce = 0.3f; 48 | public float RotationAmount = 1.5f; 49 | 50 | public float GravityModifier = 0.379f; 51 | 52 | private float MoveScale = 1.0f; 53 | private Vector3 MoveThrottle = Vector3.zero; 54 | private float FallSpeed = 0.0f; 55 | 56 | // Initial direction of controller (passed down into CameraController) 57 | private Quaternion OrientationOffset = Quaternion.identity; 58 | 59 | // Rotation amount from inputs (passed down into CameraController) 60 | private float YRotation = 0.0f; 61 | 62 | // Transfom used to point player in a given direction; 63 | // We should attach objects to this if we want them to rotate 64 | // separately from the head (i.e. the body) 65 | protected Transform DirXform = null; 66 | 67 | // We can adjust this outside of player controller 68 | public static float MoveScaleMultiplier = 1.0f; 69 | public static float RotationScaleMultiplier = 1.0f; 70 | 71 | public static bool AllowMouseRotation = true; 72 | 73 | // * * * * * * * * * * * * * 74 | 75 | // Awake 76 | new public virtual void Awake() 77 | { 78 | base.Awake(); 79 | 80 | // We use Controller to move player around 81 | Controller = gameObject.GetComponent(); 82 | 83 | // We use OVRCameraController to set rotations to cameras, 84 | // and to be influenced by rotation 85 | OVRCameraController[] CameraControllers; 86 | CameraControllers = gameObject.GetComponentsInChildren(); 87 | 88 | if(CameraControllers.Length == 0) 89 | Debug.LogWarning("No OVRCameraController attached to OVRPlayerController."); 90 | else if (CameraControllers.Length > 1) 91 | Debug.LogWarning("More then 1 OVRCameraController attached to OVRPlayerController."); 92 | else 93 | CameraController = CameraControllers[0]; 94 | 95 | // Instantiate a Transform from the main game object (will be used to 96 | // direct the motion of the PlayerController, as well as used to rotate 97 | // a visible body attached to the controller) 98 | DirXform = null; 99 | Transform[] Xforms = gameObject.GetComponentsInChildren(); 100 | 101 | for(int i = 0; i < Xforms.Length; i++) 102 | { 103 | if(Xforms[i].name == "ForwardDirection") 104 | { 105 | DirXform = Xforms[i]; 106 | break; 107 | } 108 | } 109 | 110 | if(DirXform == null) 111 | Debug.LogWarning("ForwardDirection game object not found. Do not use"); 112 | } 113 | 114 | // Start 115 | new public virtual void Start() 116 | { 117 | base.Start(); 118 | 119 | InitializeInputs(); 120 | SetCameras(); 121 | } 122 | 123 | // Update 124 | new public virtual void Update() 125 | { 126 | base.Update(); 127 | 128 | UpdateMovement(); 129 | 130 | Vector3 moveDirection = Vector3.zero; 131 | 132 | float motorDamp = (1.0f + (Damping * DeltaTime)); 133 | MoveThrottle.x /= motorDamp; 134 | MoveThrottle.y = (MoveThrottle.y > 0.0f) ? (MoveThrottle.y / motorDamp) : MoveThrottle.y; 135 | MoveThrottle.z /= motorDamp; 136 | 137 | moveDirection += MoveThrottle * DeltaTime; 138 | 139 | // Gravity 140 | if (Controller.isGrounded && FallSpeed <= 0) 141 | FallSpeed = ((Physics.gravity.y * (GravityModifier * 0.002f))); 142 | else 143 | FallSpeed += ((Physics.gravity.y * (GravityModifier * 0.002f)) * DeltaTime); 144 | 145 | moveDirection.y += FallSpeed * DeltaTime; 146 | 147 | // Offset correction for uneven ground 148 | float bumpUpOffset = 0.0f; 149 | 150 | if (Controller.isGrounded && MoveThrottle.y <= 0.001f) 151 | { 152 | bumpUpOffset = Mathf.Max(Controller.stepOffset, 153 | new Vector3(moveDirection.x, 0, moveDirection.z).magnitude); 154 | moveDirection -= bumpUpOffset * Vector3.up; 155 | } 156 | 157 | Vector3 predictedXZ = Vector3.Scale((Controller.transform.localPosition + moveDirection), 158 | new Vector3(1, 0, 1)); 159 | 160 | // Move contoller 161 | Controller.Move(moveDirection); 162 | 163 | Vector3 actualXZ = Vector3.Scale(Controller.transform.localPosition, new Vector3(1, 0, 1)); 164 | 165 | if (predictedXZ != actualXZ) 166 | MoveThrottle += (actualXZ - predictedXZ) / DeltaTime; 167 | 168 | // Update rotation using CameraController transform, possibly proving some rules for 169 | // sliding the rotation for a more natural movement and body visual 170 | UpdatePlayerForwardDirTransform(); 171 | } 172 | 173 | // UpdateMovement 174 | // 175 | // COnsolidate all movement code here 176 | // 177 | static float sDeltaRotationOld = 0.0f; 178 | public virtual void UpdateMovement() 179 | { 180 | // Do not apply input if we are showing a level selection display 181 | if(OVRMainMenu.sShowLevels == false) 182 | { 183 | bool moveForward = false; 184 | bool moveLeft = false; 185 | bool moveRight = false; 186 | bool moveBack = false; 187 | 188 | MoveScale = 1.0f; 189 | 190 | // * * * * * * * * * * * 191 | // Keyboard input 192 | 193 | // Move 194 | 195 | // WASD 196 | if (Input.GetKey(KeyCode.W)) moveForward = true; 197 | if (Input.GetKey(KeyCode.A)) moveLeft = true; 198 | if (Input.GetKey(KeyCode.S)) moveBack = true; 199 | if (Input.GetKey(KeyCode.D)) moveRight = true; 200 | // Arrow keys 201 | if (Input.GetKey(KeyCode.UpArrow)) moveForward = true; 202 | if (Input.GetKey(KeyCode.LeftArrow)) moveLeft = true; 203 | if (Input.GetKey(KeyCode.DownArrow)) moveBack = true; 204 | if (Input.GetKey(KeyCode.RightArrow)) moveRight = true; 205 | 206 | if ( (moveForward && moveLeft) || (moveForward && moveRight) || 207 | (moveBack && moveLeft) || (moveBack && moveRight) ) 208 | MoveScale = 0.70710678f; 209 | 210 | // No positional movement if we are in the air 211 | if (!Controller.isGrounded) 212 | MoveScale = 0.0f; 213 | 214 | MoveScale *= DeltaTime; 215 | 216 | // Compute this for key movement 217 | float moveInfluence = Acceleration * 0.1f * MoveScale * MoveScaleMultiplier; 218 | 219 | // Run! 220 | if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 221 | moveInfluence *= 2.0f; 222 | 223 | if (moveForward) 224 | MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence); 225 | 226 | if (moveBack) 227 | MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence); 228 | 229 | if (moveLeft) 230 | MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence); 231 | 232 | if (moveRight) 233 | MoveThrottle += DirXform.TransformDirection(Vector3.right * moveInfluence); 234 | 235 | // Rotate 236 | 237 | // compute for key rotation 238 | float rotateInfluence = DeltaTime * RotationAmount * RotationScaleMultiplier; 239 | 240 | //reduce by half to avoid getting ill 241 | if (Input.GetKey(KeyCode.Q)) 242 | YRotation -= rotateInfluence * 0.5f; 243 | if (Input.GetKey(KeyCode.E)) 244 | YRotation += rotateInfluence * 0.5f; 245 | 246 | // * * * * * * * * * * * 247 | // Mouse input 248 | 249 | // Move 250 | 251 | // Rotate 252 | float deltaRotation = 0.0f; 253 | if(AllowMouseRotation == false) 254 | deltaRotation = Input.GetAxis("Mouse X") * rotateInfluence * 3.25f; 255 | 256 | float filteredDeltaRotation = (sDeltaRotationOld * 0.2f) + (deltaRotation * 0.8f); 257 | YRotation += filteredDeltaRotation; 258 | sDeltaRotationOld = filteredDeltaRotation; 259 | 260 | // * * * * * * * * * * * 261 | // XBox controller input 262 | 263 | // Compute this for xinput movement 264 | moveInfluence = Acceleration * 0.1f * MoveScale * MoveScaleMultiplier; 265 | 266 | // Run! 267 | moveInfluence *= 1.0f + OVRGamepadController.GetTriggerLeft(); 268 | 269 | // Move 270 | if(OVRGamepadController.GetAxisLeftY() > 0.0f) 271 | MoveThrottle += OVRGamepadController.GetAxisLeftY() * 272 | DirXform.TransformDirection(Vector3.forward * moveInfluence); 273 | 274 | if(OVRGamepadController.GetAxisLeftY() < 0.0f) 275 | MoveThrottle += Mathf.Abs(OVRGamepadController.GetAxisLeftY()) * 276 | DirXform.TransformDirection(Vector3.back * moveInfluence); 277 | 278 | if(OVRGamepadController.GetAxisLeftX() < 0.0f) 279 | MoveThrottle += Mathf.Abs(OVRGamepadController.GetAxisLeftX()) * 280 | DirXform.TransformDirection(Vector3.left * moveInfluence); 281 | 282 | if(OVRGamepadController.GetAxisLeftX() > 0.0f) 283 | MoveThrottle += OVRGamepadController.GetAxisLeftX() * 284 | DirXform.TransformDirection(Vector3.right * moveInfluence); 285 | 286 | // Rotate 287 | YRotation += OVRGamepadController.GetAxisRightX() * rotateInfluence; 288 | } 289 | 290 | // Update cameras direction and rotation 291 | SetCameras(); 292 | 293 | } 294 | 295 | // UpdatePlayerControllerRotation 296 | // This function will be used to 'slide' PlayerController rotation around based on 297 | // CameraController. For now, we are simply copying the CameraController rotation into 298 | // PlayerController, so that the PlayerController always faces the direction of the 299 | // CameraController. When we add a body, this will change a bit.. 300 | public virtual void UpdatePlayerForwardDirTransform() 301 | { 302 | if(CameraController != null) 303 | DirXform.rotation = CameraController.transform.rotation; 304 | } 305 | 306 | /////////////////////////////////////////////////////////// 307 | // PUBLIC FUNCTIONS 308 | /////////////////////////////////////////////////////////// 309 | 310 | // Jump 311 | public bool Jump() 312 | { 313 | if (!Controller.isGrounded) 314 | return false; 315 | 316 | MoveThrottle += new Vector3(0, JumpForce, 0); 317 | 318 | return true; 319 | } 320 | 321 | // Stop 322 | public void Stop() 323 | { 324 | Controller.Move(Vector3.zero); 325 | MoveThrottle = Vector3.zero; 326 | FallSpeed = 0.0f; 327 | } 328 | 329 | // InitializeInputs 330 | public void InitializeInputs() 331 | { 332 | // Get our start direction 333 | OrientationOffset = transform.rotation; 334 | // Make sure to set y rotation to 0 degrees 335 | YRotation = 0.0f; 336 | } 337 | 338 | // SetCameras 339 | public void SetCameras() 340 | { 341 | if(CameraController != null) 342 | { 343 | // Make sure to set the initial direction of the camera 344 | // to match the game player direction 345 | CameraController.SetOrientationOffset(OrientationOffset); 346 | CameraController.SetYRotation(YRotation); 347 | } 348 | } 349 | 350 | /////////////////////////////////////////////////////////// 351 | // STATIC PUBLIC FUNCTIONS 352 | /////////////////////////////////////////////////////////// 353 | 354 | } 355 | 356 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRPlayerController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0950df82e7936c84983497630bde5b54 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRPresetManager.cs: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | Filename : OVRPresetManager.cs 4 | Content : Save or load a collection of variables 5 | Created : March 7, 2013 6 | Authors : Peter Giokaris 7 | 8 | Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 9 | 10 | Use of this software is subject to the terms of the Oculus LLC license 11 | agreement provided at the time of installation or download, or which 12 | otherwise accompanies this software in either electronic or hard copy form. 13 | 14 | ************************************************************************************/ 15 | using UnityEngine; 16 | using System.Collections.Generic; 17 | 18 | //------------------------------------------------------------------------------------- 19 | // ***** OVRPresetManager 20 | // 21 | // OVRPresetManager is a helper class to allow for a set of variables to be saved and 22 | // recalled using the Unity PlayerPrefs class. 23 | // 24 | // OVRPresetManager is currently being used by the OVRMainMenu component. 25 | // 26 | public static class OVRPresetManager 27 | { 28 | static string PresetName = ""; 29 | 30 | // SetCurrentPreset 31 | public static bool SetCurrentPreset(string presetName) 32 | { 33 | PresetName = presetName; 34 | return true; 35 | } 36 | 37 | // SetPropertyInt 38 | public static bool SetPropertyInt(string name, ref int v) 39 | { 40 | string key = PresetName + name; 41 | PlayerPrefs.SetInt (key, v); 42 | return true; 43 | } 44 | 45 | // GetPropertyInt 46 | public static bool GetPropertyInt(string name, ref int v) 47 | { 48 | string key = PresetName + name; 49 | if(PlayerPrefs.HasKey(key) == false) 50 | return false; 51 | 52 | v = PlayerPrefs.GetInt (key); 53 | return true; 54 | } 55 | 56 | // SetPropertyFloat 57 | public static bool SetPropertyFloat(string name, ref float v) 58 | { 59 | string key = PresetName + name; 60 | PlayerPrefs.SetFloat (key, v); 61 | return true; 62 | } 63 | 64 | // GetPropertyFloat 65 | public static bool GetPropertyFloat(string name, ref float v) 66 | { 67 | string key = PresetName + name; 68 | if(PlayerPrefs.HasKey(key) == false) 69 | return false; 70 | 71 | v = PlayerPrefs.GetFloat (key); 72 | return true; 73 | } 74 | 75 | // SetPropertyString 76 | public static bool SetPropertyString(string name, ref string v) 77 | { 78 | string key = PresetName + name; 79 | PlayerPrefs.SetString (key, v); 80 | return true; 81 | } 82 | 83 | // GetPropertyString 84 | public static bool GetPropertyString(string name, ref string v) 85 | { 86 | string key = PresetName + name; 87 | if(PlayerPrefs.HasKey(key) == false) 88 | return false; 89 | 90 | v = PlayerPrefs.GetString(key); 91 | return true; 92 | } 93 | 94 | // DeleteProperty 95 | public static bool DeleteProperty(string name) 96 | { 97 | string key = PresetName + name; 98 | PlayerPrefs.DeleteKey(key); 99 | return true; 100 | } 101 | 102 | // SaveAll 103 | public static bool SaveAll() 104 | { 105 | PlayerPrefs.Save(); 106 | return true; 107 | } 108 | 109 | // DeleteAll 110 | public static bool DeleteAll() 111 | { 112 | PlayerPrefs.DeleteAll(); 113 | return true; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Assets/OVR/Scripts/OVRPresetManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e804c45cbfefca841b88e4d9f23ada01 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/OVR/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1938789817fe3974ea20e502ed4bcd76 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Textures/Black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Textures/Black.png -------------------------------------------------------------------------------- /Assets/OVR/Textures/Black.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a025c23fceb7d4966b603419f551ce99 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 2 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: 1 27 | aniso: 1 28 | mipBias: 0 29 | wrapMode: 0 30 | nPOTScale: 0 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/OVR/Textures/CircleCrossHair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Textures/CircleCrossHair.png -------------------------------------------------------------------------------- /Assets/OVR/Textures/CircleCrossHair.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 64ce76f827a71b44da62c30c0b506fd2 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/OVR/Textures/Crosshair.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Textures/Crosshair.tga -------------------------------------------------------------------------------- /Assets/OVR/Textures/Crosshair.tga.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0565332d540dbcf4d9a01224a867cee8 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/OVR/Textures/Icons.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e081f465852cec14380da085e957c52b 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/OVR/Textures/Icons/OculusIcon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/OVR/Textures/Icons/OculusIcon.png -------------------------------------------------------------------------------- /Assets/OVR/Textures/Icons/OculusIcon.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee6b6c047f060074596445bbb9ebbfe7 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 1 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 128 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | textureType: -1 34 | buildTargetSettings: [] 35 | userData: 36 | -------------------------------------------------------------------------------- /Assets/Plugins.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8619015675705434a8d74b228676b99c 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Plugins/MITLicense_XInputDotNetPureDLL.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2009 Remi Gillig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /Assets/Plugins/MITLicense_XInputDotNetPureDLL.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2ca2c18395f82f544aef931f4e831ea2 3 | TextScriptImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Plugins/MITLicense_XInputInterfaceDLL.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2009 Remi Gillig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /Assets/Plugins/MITLicense_XInputInterfaceDLL.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 122cc9bfc045e3846847a1f31c4e0a3a 3 | TextScriptImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Plugins/OculusPlugin.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/Plugins/OculusPlugin.dll -------------------------------------------------------------------------------- /Assets/Plugins/OculusPlugin.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e7cf82acc6d235140a2f1f689a17b15a 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Plugins/XInputDotNetPure.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/Plugins/XInputDotNetPure.dll -------------------------------------------------------------------------------- /Assets/Plugins/XInputDotNetPure.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 19188cd40571b8941be5ff15bfb4ce77 3 | MonoAssemblyImporter: 4 | serializedVersion: 1 5 | iconMap: {} 6 | executionOrder: {} 7 | userData: 8 | -------------------------------------------------------------------------------- /Assets/Plugins/XInputInterface.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/Plugins/XInputInterface.dll -------------------------------------------------------------------------------- /Assets/Plugins/XInputInterface.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f6e479147d9a74441b49651ee4945666 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 38e6f7c16fe0dc34a8d10dc72f965c25 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/CreateVolumetricAssets.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | using System.Collections; 4 | 5 | public class CreateVolumetricAssets : MonoBehaviour 6 | { 7 | public Texture3D tex3D; 8 | public int n; 9 | public Light _light; 10 | public float absorption; 11 | public float lightIntensityScale; 12 | //public bool onlyGenerateTexture3D; 13 | public string texture3DName = "pyroclasticNoise"; 14 | private Mesh m; 15 | 16 | // Use this for initialization 17 | public void Start() { 18 | Debug.Log(SystemInfo.graphicsDeviceVersion); 19 | if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") == false) { 20 | Debug.LogError("The Volumetric Renderer Only Supports OpenGL!!!!"); 21 | } 22 | else { 23 | Debug.Log("OpenGL Renderer Detected"); 24 | } 25 | 26 | CreateMesh(); 27 | //tex3D = Generate3DTexture(); 28 | 29 | Vector4 localEyePos = transform.worldToLocalMatrix.MultiplyPoint( 30 | Camera.main.transform.position); 31 | Vector4 localLightPos = transform.worldToLocalMatrix.MultiplyPoint( 32 | _light.transform.position); 33 | renderer.material.SetVector("g_eyePos", localEyePos); 34 | renderer.material.SetVector("g_lightPos", localLightPos); 35 | renderer.material.SetFloat("g_lightIntensity", _light.intensity); 36 | renderer.material.SetFloat("g_absorption", absorption); 37 | } 38 | 39 | void OnWillRenderObject() { 40 | Vector4 localEyePos = transform.worldToLocalMatrix.MultiplyPoint( 41 | Camera.main.transform.position); 42 | localEyePos += new Vector4(0.5f, 0.5f, 0.5f, 0.0f); 43 | 44 | ///localEyePos = new Vector3(0.0f, 0.0f, -1.3f); 45 | 46 | renderer.material.SetVector("g_eyePos", localEyePos); 47 | 48 | 49 | Vector4 localLightPos = transform.worldToLocalMatrix.MultiplyPoint( 50 | _light.transform.position); 51 | renderer.material.SetVector("g_lightPos", localLightPos); 52 | renderer.material.SetFloat("g_lightIntensity", _light.intensity * 53 | lightIntensityScale); 54 | } 55 | 56 | 57 | 58 | private Mesh CreateMesh() { 59 | m = new Mesh(); 60 | CreateCube(m); 61 | m.RecalculateBounds(); 62 | MeshFilter mf = (MeshFilter)transform.GetComponent(typeof(MeshFilter)); 63 | mf.mesh = m; 64 | return m; 65 | } 66 | 67 | void CreateCube(Mesh m) { 68 | Vector3[] vertices = new Vector3[24]; 69 | Vector2[] uv = new Vector2[24]; 70 | Color[] colors = new Color[24]; 71 | Vector3[] normals = new Vector3[24]; 72 | int[] triangles = new int[36]; 73 | 74 | int i = 0; 75 | int ti = 0; 76 | //Front 77 | vertices[i] = new Vector3(-0.5f, -0.5f, -0.5f); 78 | normals[i] = new Vector3(0, 0, -1); 79 | colors[i] = new Color(0, 0, 0); 80 | 81 | i++; 82 | vertices[i] = new Vector3(0.5f, -0.5f, -0.5f); 83 | normals[i] = new Vector3(0, 0, -1); 84 | colors[i] = new Color(1, 0, 0); 85 | 86 | i++; 87 | vertices[i] = new Vector3(0.5f, 0.5f, -0.5f); 88 | normals[i] = new Vector3(0, 0, -1); 89 | colors[i] = new Color(1, 1, 0); 90 | i++; 91 | 92 | vertices[i] = new Vector3(-0.5f, 0.5f, -0.5f); 93 | normals[i] = new Vector3(0, 0, -1); 94 | colors[i] = new Color(0, 1, 0); 95 | 96 | i++; 97 | 98 | triangles[ti++] = i - 4; 99 | triangles[ti++] = i - 2; 100 | triangles[ti++] = i - 3; 101 | triangles[ti++] = i - 4; 102 | triangles[ti++] = i - 1; 103 | triangles[ti++] = i - 2; 104 | 105 | //Back 106 | vertices[i] = new Vector3(-0.5f, -0.5f, 0.5f); 107 | normals[i] = new Vector3(0, 0, -1); 108 | colors[i] = new Color(0, 0, 1); 109 | 110 | i++; 111 | vertices[i] = new Vector3(0.5f, -0.5f, 0.5f); 112 | normals[i] = new Vector3(0, 0, -1); 113 | colors[i] = new Color(1, 0, 1); 114 | 115 | i++; 116 | vertices[i] = new Vector3(0.5f, 0.5f, 0.5f); 117 | normals[i] = new Vector3(0, 0, -1); 118 | colors[i] = new Color(1, 1, 1); 119 | i++; 120 | 121 | vertices[i] = new Vector3(-0.5f, 0.5f, 0.5f); 122 | normals[i] = new Vector3(0, 0, -1); 123 | colors[i] = new Color(0, 1, 1); 124 | 125 | i++; 126 | 127 | triangles[ti++] = i - 4; 128 | triangles[ti++] = i - 3; 129 | triangles[ti++] = i - 2; 130 | triangles[ti++] = i - 4; 131 | triangles[ti++] = i - 2; 132 | triangles[ti++] = i - 1; 133 | 134 | //Top 135 | vertices[i] = new Vector3(-0.5f, 0.5f, -0.5f); 136 | normals[i] = new Vector3(0, 1, 0); 137 | colors[i] = new Color(0, 1, 0); 138 | 139 | i++; 140 | vertices[i] = new Vector3(0.5f, 0.5f, -0.5f); 141 | normals[i] = new Vector3(0, 1, 0); 142 | colors[i] = new Color(1, 1, 0); 143 | 144 | i++; 145 | vertices[i] = new Vector3(0.5f, 0.5f, 0.5f); 146 | normals[i] = new Vector3(0, 1, 0); 147 | colors[i] = new Color(1, 1, 1); 148 | 149 | i++; 150 | 151 | vertices[i] = new Vector3(-0.5f, 0.5f, 0.5f); 152 | normals[i] = new Vector3(0, 1, 0); 153 | colors[i] = new Color(0, 1, 1); 154 | 155 | i++; 156 | 157 | triangles[ti++] = i - 4; 158 | triangles[ti++] = i - 2; 159 | triangles[ti++] = i - 3; 160 | triangles[ti++] = i - 4; 161 | triangles[ti++] = i - 1; 162 | triangles[ti++] = i - 2; 163 | 164 | //Bottom 165 | vertices[i] = new Vector3(-0.5f, -0.5f, -0.5f); 166 | normals[i] = new Vector3(0, 1, 0); 167 | colors[i] = new Color(0, 0, 0); 168 | 169 | i++; 170 | vertices[i] = new Vector3(0.5f, -0.5f, -0.5f); 171 | normals[i] = new Vector3(0, 1, 0); 172 | colors[i] = new Color(1, 0, 0); 173 | 174 | i++; 175 | vertices[i] = new Vector3(0.5f, -0.5f, 0.5f); 176 | normals[i] = new Vector3(0, 1, 0); 177 | colors[i] = new Color(1, 0, 1); 178 | 179 | i++; 180 | 181 | vertices[i] = new Vector3(-0.5f, -0.5f, 0.5f); 182 | normals[i] = new Vector3(0, 1, 0); 183 | colors[i] = new Color(0, 0, 1); 184 | 185 | i++; 186 | 187 | triangles[ti++] = i - 4; 188 | triangles[ti++] = i - 3; 189 | triangles[ti++] = i - 2; 190 | triangles[ti++] = i - 4; 191 | triangles[ti++] = i - 2; 192 | triangles[ti++] = i - 1; 193 | 194 | //Right 195 | vertices[i] = new Vector3(0.5f, -0.5f, -0.5f); 196 | normals[i] = new Vector3(0, 1, 0); 197 | colors[i] = new Color(1, 0, 0); 198 | 199 | i++; 200 | 201 | vertices[i] = new Vector3(0.5f, 0.5f, -0.5f); 202 | normals[i] = new Vector3(0, 1, 0); 203 | colors[i] = new Color(1, 1, 0); 204 | 205 | i++; 206 | 207 | vertices[i] = new Vector3(0.5f, 0.5f, 0.5f); 208 | normals[i] = new Vector3(0, 1, 0); 209 | colors[i] = new Color(1, 1, 1); 210 | 211 | i++; 212 | 213 | vertices[i] = new Vector3(0.5f, -0.5f, 0.5f); 214 | normals[i] = new Vector3(0, 1, 0); 215 | colors[i] = new Color(1, 0, 1); 216 | 217 | i++; 218 | 219 | triangles[ti++] = i - 4; 220 | triangles[ti++] = i - 3; 221 | triangles[ti++] = i - 2; 222 | triangles[ti++] = i - 4; 223 | triangles[ti++] = i - 2; 224 | triangles[ti++] = i - 1; 225 | 226 | //Left 227 | vertices[i] = new Vector3(-0.5f, -0.5f, -0.5f); 228 | normals[i] = new Vector3(0, 1, 0); 229 | colors[i] = new Color(0, 0, 0); 230 | 231 | i++; 232 | 233 | vertices[i] = new Vector3(-0.5f, 0.5f, -0.5f); 234 | normals[i] = new Vector3(0, 1, 0); 235 | colors[i] = new Color(0, 1, 0); 236 | 237 | i++; 238 | 239 | vertices[i] = new Vector3(-0.5f, 0.5f, 0.5f); 240 | normals[i] = new Vector3(0, 1, 0); 241 | colors[i] = new Color(0, 1, 1); 242 | 243 | i++; 244 | 245 | vertices[i] = new Vector3(-0.5f, -0.5f, 0.5f); 246 | normals[i] = new Vector3(0, 1, 0); 247 | colors[i] = new Color(0, 0, 1); 248 | 249 | i++; 250 | 251 | triangles[ti++] = i - 4; 252 | triangles[ti++] = i - 2; 253 | triangles[ti++] = i - 3; 254 | triangles[ti++] = i - 4; 255 | triangles[ti++] = i - 1; 256 | triangles[ti++] = i - 2; 257 | 258 | m.vertices = vertices; 259 | m.colors = colors; //Putting uv's into the normal channel to get the 260 | //Vector3 type 261 | m.uv = uv; 262 | m.normals = normals; 263 | m.triangles = triangles; 264 | } 265 | 266 | public Texture3D Generate3DTexture() { 267 | float r = 0.3f; 268 | Texture3D texture3D = new Texture3D(n, n, n, TextureFormat.ARGB32, true); 269 | int size = n * n * n; 270 | Color[] cols = new Color[size]; 271 | int idx = 0; 272 | 273 | Color c = Color.white; 274 | float frequency = 0.01f / n; 275 | float center = n / 2.0f + 0.5f; 276 | 277 | for (int i = 0; i < n; i++) { 278 | for (int j = 0; j < n; j++) { 279 | for (int k = 0; k < n; k++, ++idx) { 280 | float dx = center - i; 281 | float dy = center - j; 282 | float dz = center - k; 283 | 284 | float off = Mathf.Abs(Perlin.Turbulence(i * frequency, 285 | j * frequency, 286 | k * frequency, 287 | 6)); 288 | 289 | float d = Mathf.Sqrt(dx * dx + dy * dy + dz * dz) / (n); 290 | //c.r = c.g = c.b = c.a = ((d-off) < r)?1.0f:0.0f; 291 | float p = d - off; 292 | c.r = c.g = c.b = c.a = Mathf.Clamp01(r - p); 293 | cols[idx] = c; 294 | } 295 | } 296 | } 297 | 298 | //for(int i = 0; i < size; i++) 299 | // Debug.Log (newC[i]); 300 | texture3D.SetPixels(cols); 301 | texture3D.Apply(); 302 | renderer.material.SetTexture("g_densityTex", texture3D); 303 | texture3D.filterMode = FilterMode.Trilinear; 304 | texture3D.wrapMode = TextureWrapMode.Clamp; 305 | texture3D.anisoLevel = 1; 306 | 307 | //Color[] cs = texture3D.GetPixels(); 308 | //for(int i = 0; i < 10; i++) 309 | // Debug.Log (cs[i]); 310 | 311 | return texture3D; 312 | } 313 | 314 | } 315 | -------------------------------------------------------------------------------- /Assets/Scripts/CreateVolumetricAssets.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 57911d69acd1cf64fb267ac9f311b77b 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/FlowField.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class FlowField : MonoBehaviour { 5 | public int dimInCells; 6 | public float dimInMeters; 7 | public Vector3 origin; 8 | 9 | public 10 | 11 | // Use this for initialization 12 | void Start () { 13 | 14 | } 15 | 16 | // Update is called once per frame 17 | void Update () { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Assets/Scripts/FlowField.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ca7ba9cb04f5a1e42887cd1b70f59f56 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/MouseOrbit.js: -------------------------------------------------------------------------------- 1 | var target : Transform; 2 | var distance = 10.0; 3 | 4 | var xSpeed = 250.0; 5 | var ySpeed = 120.0; 6 | 7 | var yMinLimit = -20; 8 | var yMaxLimit = 80; 9 | 10 | private var x = 0.0; 11 | private var y = 0.0; 12 | 13 | @script AddComponentMenu("Camera-Control/Mouse Orbit") 14 | 15 | function Start () { 16 | var angles = transform.eulerAngles; 17 | x = angles.y; 18 | y = angles.x; 19 | 20 | // Make the rigid body not change rotation 21 | if (rigidbody) 22 | rigidbody.freezeRotation = true; 23 | } 24 | 25 | function LateUpdate () { 26 | if (target) { 27 | x += Input.GetAxis("Mouse X") * xSpeed * 0.02; 28 | y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; 29 | 30 | y = ClampAngle(y, yMinLimit, yMaxLimit); 31 | 32 | var rotation = Quaternion.Euler(y, x, 0); 33 | var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; 34 | 35 | transform.rotation = rotation; 36 | transform.position = position; 37 | } 38 | } 39 | 40 | static function ClampAngle (angle : float, min : float, max : float) { 41 | if (angle < -360) 42 | angle += 360; 43 | if (angle > 360) 44 | angle -= 360; 45 | return Mathf.Clamp (angle, min, max); 46 | } -------------------------------------------------------------------------------- /Assets/Scripts/MouseOrbit.js.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3927699fdcef3114c9f264e25c013741 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Perlin.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System; 3 | using UnityEngine; 4 | 5 | public class Perlin 6 | { 7 | // Returns noise between 0 - 1 8 | static public float NoiseNormalized(float x, float y) 9 | { 10 | //-0.697 - 0.795 + 0.697 11 | float value = Noise(x, y); 12 | 13 | /* Replacing division with multipling by the inverse as a decimal 14 | 0.793+0.69 = 1.483 15 | 1/1.483 = 0.67430883345 16 | */ 17 | //value = (value + 0.69F) / (0.793F + 0.69F); 18 | value = (value + 0.69F) *0.67430883345F; 19 | return value; 20 | } 21 | 22 | static public float Noise(float x, float y) 23 | { 24 | int X = (int)Mathf.Floor(x) & 255, // FIND UNIT CUBE THAT 25 | Y = (int)Mathf.Floor(y) & 255; // CONTAINS POINT. 26 | x -= Mathf.Floor(x); // FIND RELATIVE X,Y,Z 27 | y -= Mathf.Floor(y); // OF POINT IN CUBE. 28 | float u = fade(x), // COMPUTE FADE CURVES 29 | v = fade(y); // FOR EACH OF X,Y,Z. 30 | int A = p[X ]+Y, AA = p[A], AB = p[A+1], // HASH COORDINATES OF 31 | B = p[X+1]+Y, BA = p[B], BB = p[B+1]; // THE 8 CUBE CORNERS, 32 | 33 | float res = lerp(v, lerp(u, grad2(p[AA ], x , y ), // AND ADD 34 | grad2(p[BA ], x-1, y )), // BLENDED 35 | lerp(u, grad2(p[AB ], x , y-1 ), // RESULTS 36 | grad2(p[BB ], x-1, y-1 )));// FROM 8 37 | return res; 38 | } 39 | 40 | static public float Noise(float x, float y, float z) { 41 | int X = (int)Mathf.Floor(x) & 255, // FIND UNIT CUBE THAT 42 | Y = (int)Mathf.Floor(y) & 255, // CONTAINS POINT. 43 | Z = (int)Mathf.Floor(z) & 255; 44 | x -= Mathf.Floor(x); // FIND RELATIVE X,Y,Z 45 | y -= Mathf.Floor(y); // OF POINT IN CUBE. 46 | z -= Mathf.Floor(z); 47 | float u = fade(x), // COMPUTE FADE CURVES 48 | v = fade(y), // FOR EACH OF X,Y,Z. 49 | w = fade(z); 50 | int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF 51 | B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS, 52 | 53 | return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ), // AND ADD 54 | grad(p[BA ], x-1, y , z )), // BLENDED 55 | lerp(u, grad(p[AB ], x , y-1, z ), // RESULTS 56 | grad(p[BB ], x-1, y-1, z ))),// FROM 8 57 | lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ), // CORNERS 58 | grad(p[BA+1], x-1, y , z-1 )), // OF CUBE 59 | lerp(u, grad(p[AB+1], x , y-1, z-1 ), 60 | grad(p[BB+1], x-1, y-1, z-1 )))); 61 | } 62 | 63 | static public float Turbulence(float posX, float posY, int octaves) { 64 | float x = 0; 65 | float scale = 1; 66 | int octave = 0; 67 | while (++octave <= octaves) { 68 | posX = posX/scale; 69 | posY = posY/scale; 70 | x = x + Math.Abs(Noise(posX, posY))*scale; 71 | scale = scale/2; 72 | } 73 | return x; 74 | } 75 | 76 | static public float Fractal(float posX, float posY, int octaves) 77 | { 78 | float x = 0; 79 | float scale = 1; 80 | int octave = 0; 81 | while (++octave <= octaves) 82 | { 83 | posX = posX / scale; 84 | posY = posY / scale; 85 | x = x + Noise(posX, posY) * scale; 86 | scale = scale / 2; 87 | } 88 | return x; 89 | } 90 | 91 | static public float Marble(float posX, float posY, int octaves) { 92 | //Vector3 rgb = new Vector3(); 93 | float x = Mathf.Sin((posY+3.0f*Turbulence(posX,posY,octaves))*Mathf.PI); 94 | x = Mathf.Sqrt(x+1)*.7071f; 95 | //rgb.z=.3+.8*x; 96 | //x = Mathf.sqrt(x); 97 | //rgb.x = .3 + .6*x; 98 | //rgb.z = .6 + .4*x; 99 | 100 | return x; 101 | } 102 | static public float Turbulence(float posX, float posY, float posZ, int octaves) { 103 | float x = 0; 104 | float scale = 1; 105 | int octave = 0; 106 | while (++octave <= octaves) { 107 | posX = posX/scale; 108 | posY = posY/scale; 109 | posZ = posZ/scale; 110 | x = x + Math.Abs(Noise(posX, posY, posZ))*scale; 111 | scale = scale/2; 112 | } 113 | return x; 114 | } 115 | 116 | static public float Marble(float posX, float posY, float posZ, int octaves) { 117 | //Vector3 rgb = new Vector3(); 118 | float x = Mathf.Sin((posY+3.0f*Turbulence(posX,posY,posZ,octaves))*Mathf.PI); 119 | x = Mathf.Sqrt(x+1)*.7071f; 120 | //rgb.z=.3+.8*x; 121 | //x = Mathf.sqrt(x); 122 | //rgb.x = .3 + .6*x; 123 | //rgb.z = .6 + .4*x; 124 | 125 | return x; 126 | } 127 | 128 | static float fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); } 129 | static float lerp(float t, float a, float b) { return a + t * (b - a); } 130 | static float grad(int hash, float x, float y, float z) { 131 | int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE 132 | float u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. 133 | v = h<4 ? y : h==12||h==14 ? x : z; 134 | return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); 135 | } 136 | 137 | static float grad2(int hash, float x, float y) { 138 | int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE 139 | float u = h < 8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. 140 | v = h < 4 ? y : h==12 || h==14 ? x : 0; 141 | return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); 142 | } 143 | 144 | static int[] p = { 151,160,137,91,90,15, 145 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 146 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 147 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 148 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 149 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 150 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 151 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 152 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 153 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 154 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 155 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 156 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, 157 | 151,160,137,91,90,15, 158 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 159 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 160 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 161 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 162 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 163 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 164 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 165 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 166 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 167 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 168 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 169 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 170 | }; 171 | } 172 | -------------------------------------------------------------------------------- /Assets/Scripts/Perlin.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cece6a11a085767429d9697a7f44cf08 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Rotate.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Rotate : MonoBehaviour { 5 | public float movementScale; 6 | // Use this for initialization 7 | void Start () { 8 | 9 | } 10 | 11 | // Update is called once per frame 12 | void Update () { 13 | Quaternion q = transform.rotation; 14 | Vector3 rot = q.eulerAngles; 15 | rot.x += OVRGamepadController.GetAxisRightY() * movementScale; 16 | rot.y += OVRGamepadController.GetAxisRightX() * movementScale; 17 | rot.x += Input.GetAxis("Vertical") * movementScale; 18 | rot.y += Input.GetAxis("Horizontal") * movementScale; 19 | q.eulerAngles = rot; 20 | transform.rotation = q; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Assets/Scripts/Rotate.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 11f5730b2ee76f245a860b25874a6348 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/VolumeRenderer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class VolumeRenderer : MonoBehaviour { 5 | public Texture3D texture3D; 6 | public int n; 7 | public Light _light; 8 | public float absorption; 9 | public float lightIntensityScale; 10 | public bool selfCreateTexture3D; 11 | private Mesh m; 12 | 13 | // Use this for initialization 14 | public void Start () { 15 | CreateMesh(); 16 | if(selfCreateTexture3D){ 17 | Generate3DTexture(); 18 | } 19 | 20 | Vector4 localEyePos = transform.worldToLocalMatrix.MultiplyPoint( 21 | Camera.main.transform.position); 22 | Vector4 localLightPos = transform.worldToLocalMatrix.MultiplyPoint( 23 | _light.transform.position); 24 | renderer.material.SetVector("g_eyePos", localEyePos); 25 | renderer.material.SetVector("g_lightPos", localLightPos); 26 | renderer.material.SetFloat("g_lightIntensity", _light.intensity); 27 | renderer.material.SetFloat("g_absorption", absorption); 28 | } 29 | 30 | void LateUpdate() 31 | { 32 | Vector4 localEyePos = transform.worldToLocalMatrix.MultiplyPoint( 33 | Camera.main.transform.position); 34 | localEyePos += new Vector4(0.5f,0.5f,0.5f,0.0f); 35 | renderer.material.SetVector("g_eyePos", localEyePos); 36 | 37 | 38 | Vector4 localLightPos = transform.worldToLocalMatrix.MultiplyPoint( 39 | _light.transform.position); 40 | renderer.material.SetVector("g_lightPos", localLightPos); 41 | renderer.material.SetFloat("g_lightIntensity", _light.intensity * 42 | lightIntensityScale); 43 | renderer.material.SetFloat("g_absorption", absorption); 44 | } 45 | 46 | 47 | 48 | private Mesh CreateMesh() 49 | { 50 | m = new Mesh(); 51 | CreateCube(m); 52 | m.RecalculateBounds(); 53 | MeshFilter mf = (MeshFilter)transform.GetComponent(typeof(MeshFilter)); 54 | mf.mesh = m; 55 | return m; 56 | } 57 | 58 | void CreateCube(Mesh m) 59 | { 60 | Vector3[] vertices = new Vector3[24]; 61 | Vector2[] uv = new Vector2[24]; 62 | Color[] colors = new Color[24]; 63 | Vector3[] normals = new Vector3[24]; 64 | int[] triangles = new int[36]; 65 | 66 | int i = 0; 67 | int ti = 0; 68 | //Front 69 | vertices[i] = new Vector3(-0.5f,-0.5f,-0.5f); 70 | normals[i] = new Vector3(0,0,-1); 71 | colors[i] = new Color(0,0,0); 72 | 73 | i++; 74 | vertices[i] = new Vector3(0.5f,-0.5f,-0.5f); 75 | normals[i] = new Vector3(0,0,-1); 76 | colors[i] = new Color(1,0,0); 77 | 78 | i++; 79 | vertices[i] = new Vector3(0.5f,0.5f,-0.5f); 80 | normals[i] = new Vector3(0,0,-1); 81 | colors[i] = new Color(1,1,0); 82 | i++; 83 | 84 | vertices[i] = new Vector3(-0.5f,0.5f,-0.5f); 85 | normals[i] = new Vector3(0,0,-1); 86 | colors[i] = new Color(0,1,0); 87 | 88 | i++; 89 | 90 | triangles[ti++] = i-4; 91 | triangles[ti++] = i-2; 92 | triangles[ti++] = i-3; 93 | triangles[ti++] = i-4; 94 | triangles[ti++] = i-1; 95 | triangles[ti++] = i-2; 96 | 97 | //Back 98 | vertices[i] = new Vector3(-0.5f,-0.5f,0.5f); 99 | normals[i] = new Vector3(0,0,-1); 100 | colors[i] = new Color(0,0,1); 101 | 102 | i++; 103 | vertices[i] = new Vector3(0.5f,-0.5f,0.5f); 104 | normals[i] = new Vector3(0,0,-1); 105 | colors[i] = new Color(1,0,1); 106 | 107 | i++; 108 | vertices[i] = new Vector3(0.5f,0.5f,0.5f); 109 | normals[i] = new Vector3(0,0,-1); 110 | colors[i] = new Color(1,1,1); 111 | i++; 112 | 113 | vertices[i] = new Vector3(-0.5f,0.5f,0.5f); 114 | normals[i] = new Vector3(0,0,-1); 115 | colors[i] = new Color(0,1,1); 116 | 117 | i++; 118 | 119 | triangles[ti++] = i-4; 120 | triangles[ti++] = i-3; 121 | triangles[ti++] = i-2; 122 | triangles[ti++] = i-4; 123 | triangles[ti++] = i-2; 124 | triangles[ti++] = i-1; 125 | 126 | //Top 127 | vertices[i] = new Vector3(-0.5f,0.5f,-0.5f); 128 | normals[i] = new Vector3(0,1,0); 129 | colors[i] = new Color(0,1,0); 130 | 131 | i++; 132 | vertices[i] = new Vector3(0.5f,0.5f,-0.5f); 133 | normals[i] = new Vector3(0,1,0); 134 | colors[i] = new Color(1,1,0); 135 | 136 | i++; 137 | vertices[i] = new Vector3(0.5f,0.5f,0.5f); 138 | normals[i] = new Vector3(0,1,0); 139 | colors[i] = new Color(1,1,1); 140 | 141 | i++; 142 | 143 | vertices[i] = new Vector3(-0.5f,0.5f,0.5f); 144 | normals[i] = new Vector3(0,1,0); 145 | colors[i] = new Color(0,1,1); 146 | 147 | i++; 148 | 149 | triangles[ti++] = i-4; 150 | triangles[ti++] = i-2; 151 | triangles[ti++] = i-3; 152 | triangles[ti++] = i-4; 153 | triangles[ti++] = i-1; 154 | triangles[ti++] = i-2; 155 | 156 | //Bottom 157 | vertices[i] = new Vector3(-0.5f,-0.5f,-0.5f); 158 | normals[i] = new Vector3(0,1,0); 159 | colors[i] = new Color(0,0,0); 160 | 161 | i++; 162 | vertices[i] = new Vector3(0.5f,-0.5f,-0.5f); 163 | normals[i] = new Vector3(0,1,0); 164 | colors[i] = new Color(1,0,0); 165 | 166 | i++; 167 | vertices[i] = new Vector3(0.5f,-0.5f,0.5f); 168 | normals[i] = new Vector3(0,1,0); 169 | colors[i] = new Color(1,0,1); 170 | 171 | i++; 172 | 173 | vertices[i] = new Vector3(-0.5f,-0.5f,0.5f); 174 | normals[i] = new Vector3(0,1,0); 175 | colors[i] = new Color(0,0,1); 176 | 177 | i++; 178 | 179 | triangles[ti++] = i-4; 180 | triangles[ti++] = i-3; 181 | triangles[ti++] = i-2; 182 | triangles[ti++] = i-4; 183 | triangles[ti++] = i-2; 184 | triangles[ti++] = i-1; 185 | 186 | //Right 187 | vertices[i] = new Vector3(0.5f,-0.5f,-0.5f); 188 | normals[i] = new Vector3(0,1,0); 189 | colors[i] = new Color(1,0,0); 190 | 191 | i++; 192 | 193 | vertices[i] = new Vector3(0.5f,0.5f,-0.5f); 194 | normals[i] = new Vector3(0,1,0); 195 | colors[i] = new Color(1,1,0); 196 | 197 | i++; 198 | 199 | vertices[i] = new Vector3(0.5f,0.5f,0.5f); 200 | normals[i] = new Vector3(0,1,0); 201 | colors[i] = new Color(1,1,1); 202 | 203 | i++; 204 | 205 | vertices[i] = new Vector3(0.5f,-0.5f,0.5f); 206 | normals[i] = new Vector3(0,1,0); 207 | colors[i] = new Color(1,0,1); 208 | 209 | i++; 210 | 211 | triangles[ti++] = i-4; 212 | triangles[ti++] = i-3; 213 | triangles[ti++] = i-2; 214 | triangles[ti++] = i-4; 215 | triangles[ti++] = i-2; 216 | triangles[ti++] = i-1; 217 | 218 | //Left 219 | vertices[i] = new Vector3(-0.5f,-0.5f,-0.5f); 220 | normals[i] = new Vector3(0,1,0); 221 | colors[i] = new Color(0,0,0); 222 | 223 | i++; 224 | 225 | vertices[i] = new Vector3(-0.5f,0.5f,-0.5f); 226 | normals[i] = new Vector3(0,1,0); 227 | colors[i] = new Color(0,1,0); 228 | 229 | i++; 230 | 231 | vertices[i] = new Vector3(-0.5f,0.5f,0.5f); 232 | normals[i] = new Vector3(0,1,0); 233 | colors[i] = new Color(0,1,1); 234 | 235 | i++; 236 | 237 | vertices[i] = new Vector3(-0.5f,-0.5f,0.5f); 238 | normals[i] = new Vector3(0,1,0); 239 | colors[i] = new Color(0,0,1); 240 | 241 | i++; 242 | 243 | triangles[ti++] = i-4; 244 | triangles[ti++] = i-2; 245 | triangles[ti++] = i-3; 246 | triangles[ti++] = i-4; 247 | triangles[ti++] = i-1; 248 | triangles[ti++] = i-2; 249 | 250 | m.vertices = vertices; 251 | m.colors = colors; //Putting uv's into the normal channel to get the 252 | //Vector3 type 253 | m.uv = uv; 254 | m.normals = normals; 255 | m.triangles = triangles; 256 | } 257 | 258 | public void set3DTexture(Texture3D t){ 259 | texture3D = t; 260 | renderer.material.SetTexture("g_densityTex", texture3D); 261 | } 262 | 263 | public void Generate3DTexture() 264 | { 265 | float r = 0.3f; 266 | texture3D = new Texture3D(n,n,n,TextureFormat.ARGB32,true); 267 | int size = n * n * n; 268 | Color[] cols = new Color[size]; 269 | int idx = 0; 270 | 271 | Color c = Color.white; 272 | float frequency = 0.01f / n; 273 | float center = n / 2.0f + 0.5f; 274 | 275 | for(int i = 0; i < n; i++) { 276 | for(int j = 0; j < n; j++) { 277 | for(int k = 0; k < n; k++, ++idx) { 278 | float dx = center-i; 279 | float dy = center-j; 280 | float dz = center-k; 281 | 282 | float off = Mathf.Abs(Perlin.Turbulence(i*frequency, 283 | j*frequency, 284 | k*frequency, 285 | 6)); 286 | 287 | float d = Mathf.Sqrt(dx*dx+dy*dy+dz*dz)/(n); 288 | //c.r = c.g = c.b = c.a = ((d-off) < r)?1.0f:0.0f; 289 | float p = d-off; 290 | c.r = c.g = c.b = c.a = Mathf.Clamp01(r - p); 291 | cols[idx] = c; 292 | } 293 | } 294 | } 295 | 296 | //for(int i = 0; i < size; i++) 297 | // Debug.Log (newC[i]); 298 | texture3D.SetPixels(cols); 299 | texture3D.Apply(); 300 | renderer.material.SetTexture("g_densityTex", texture3D); 301 | texture3D.filterMode = FilterMode.Trilinear; 302 | texture3D.wrapMode = TextureWrapMode.Clamp; 303 | texture3D.anisoLevel = 1; 304 | 305 | //Color[] cs = texture3D.GetPixels(); 306 | //for(int i = 0; i < 10; i++) 307 | // Debug.Log (cs[i]); 308 | } 309 | 310 | 311 | } 312 | -------------------------------------------------------------------------------- /Assets/Scripts/VolumeRenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 38b106bfad4d42b4dbcedffabf74f4d4 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/TestScene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/TestScene.unity -------------------------------------------------------------------------------- /Assets/TestScene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b5d4e33dfefaafc4f9b001ad3dedc99f 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Textures3D.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ca2adf1d039b24f4a92f129928b9831d 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Textures3D/pyroclasticNoise.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/Assets/Textures3D/pyroclasticNoise.asset -------------------------------------------------------------------------------- /Assets/Textures3D/pyroclasticNoise.asset.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e42c397cdc10ce1428231f502b830627 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Timer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Timer { 5 | private System.Diagnostics.Stopwatch _stopwatch; 6 | 7 | public Timer(){ 8 | _stopwatch = new System.Diagnostics.Stopwatch(); 9 | } 10 | 11 | public void Start(){ 12 | 13 | _stopwatch.Start (); 14 | } 15 | 16 | public void Stop(){ 17 | _stopwatch.Stop (); 18 | } 19 | 20 | public double Elapsed(){ 21 | return _stopwatch.Elapsed.TotalMilliseconds; 22 | } 23 | } 24 | 25 | public class ScopedTimer { 26 | private System.Diagnostics.Stopwatch _stopwatch; 27 | private string _title; 28 | 29 | public ScopedTimer(string title){ 30 | _title = title; 31 | _stopwatch = new System.Diagnostics.Stopwatch(); 32 | _stopwatch.Start (); 33 | } 34 | 35 | ~ScopedTimer(){ 36 | _stopwatch.Stop (); 37 | System.TimeSpan ts = _stopwatch.Elapsed; 38 | Debug.Log (_title + " Millis: " + ts.Ticks + " Ticks: " + ts.Milliseconds); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /Assets/Timer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 27c680571c0582245a94cf7bd9b6dcd7 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Kristofer Schlachter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kristofe/UnityVolumeOculus/f7045caca02efe69ac458dba1e7e473b13eb478f/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | UnityVolumeOculus 2 | ================= 3 | 4 | Unity + Oculus Rift + Volumetric Rendering 5 | --------------------------------------------------------------------------------