diff --git a/Definitions/Devices/ControllerMappingLibrary.cs b/Definitions/Devices/ControllerMappingLibrary.cs index 37079cb34..6809b77ff 100644 --- a/Definitions/Devices/ControllerMappingLibrary.cs +++ b/Definitions/Devices/ControllerMappingLibrary.cs @@ -194,39 +194,42 @@ public static class ControllerMappingLibrary #region InputAxisConfig + // Default value for the dead zone. This should match the default used by Unity for the pre-created Horizontal and Vertical axes. + private const float defaultDeadZone = 0.19f; + /// /// Get the InputManagerAxis data needed to configure the Input Mappings for a controller /// /// public static InputManagerAxis[] UnityInputManagerAxes => new[] { - new InputManagerAxis { Name = AXIS_1, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 1 }, - new InputManagerAxis { Name = AXIS_2, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 2 }, - new InputManagerAxis { Name = AXIS_3, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 3 }, - new InputManagerAxis { Name = AXIS_4, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 4 }, - new InputManagerAxis { Name = AXIS_5, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 5 }, - new InputManagerAxis { Name = AXIS_6, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 6 }, - new InputManagerAxis { Name = AXIS_7, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 7 }, - new InputManagerAxis { Name = AXIS_8, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 8 }, - new InputManagerAxis { Name = AXIS_9, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 9 }, - new InputManagerAxis { Name = AXIS_10, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 10 }, - new InputManagerAxis { Name = AXIS_11, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 11 }, - new InputManagerAxis { Name = AXIS_12, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 12 }, - new InputManagerAxis { Name = AXIS_13, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 13 }, - new InputManagerAxis { Name = AXIS_14, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 14 }, - new InputManagerAxis { Name = AXIS_15, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 15 }, - new InputManagerAxis { Name = AXIS_16, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 16 }, - new InputManagerAxis { Name = AXIS_17, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 17 }, - new InputManagerAxis { Name = AXIS_18, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 18 }, - new InputManagerAxis { Name = AXIS_19, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 19 }, - new InputManagerAxis { Name = AXIS_20, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 20 }, - new InputManagerAxis { Name = AXIS_21, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 21 }, - new InputManagerAxis { Name = AXIS_22, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 22 }, - new InputManagerAxis { Name = AXIS_23, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 23 }, - new InputManagerAxis { Name = AXIS_24, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 24 }, - new InputManagerAxis { Name = AXIS_25, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 25 }, - new InputManagerAxis { Name = AXIS_26, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 26 }, - new InputManagerAxis { Name = AXIS_27, Dead = 0.001f, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 27 } + new InputManagerAxis { Name = AXIS_1, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 1 }, + new InputManagerAxis { Name = AXIS_2, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 2 }, + new InputManagerAxis { Name = AXIS_3, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 3 }, + new InputManagerAxis { Name = AXIS_4, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 4 }, + new InputManagerAxis { Name = AXIS_5, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 5 }, + new InputManagerAxis { Name = AXIS_6, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 6 }, + new InputManagerAxis { Name = AXIS_7, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 7 }, + new InputManagerAxis { Name = AXIS_8, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 8 }, + new InputManagerAxis { Name = AXIS_9, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 9 }, + new InputManagerAxis { Name = AXIS_10, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 10 }, + new InputManagerAxis { Name = AXIS_11, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 11 }, + new InputManagerAxis { Name = AXIS_12, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 12 }, + new InputManagerAxis { Name = AXIS_13, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 13 }, + new InputManagerAxis { Name = AXIS_14, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 14 }, + new InputManagerAxis { Name = AXIS_15, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 15 }, + new InputManagerAxis { Name = AXIS_16, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 16 }, + new InputManagerAxis { Name = AXIS_17, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 17 }, + new InputManagerAxis { Name = AXIS_18, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 18 }, + new InputManagerAxis { Name = AXIS_19, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 19 }, + new InputManagerAxis { Name = AXIS_20, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 20 }, + new InputManagerAxis { Name = AXIS_21, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 21 }, + new InputManagerAxis { Name = AXIS_22, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 22 }, + new InputManagerAxis { Name = AXIS_23, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 23 }, + new InputManagerAxis { Name = AXIS_24, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 24 }, + new InputManagerAxis { Name = AXIS_25, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 25 }, + new InputManagerAxis { Name = AXIS_26, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 26 }, + new InputManagerAxis { Name = AXIS_27, Dead = defaultDeadZone, Sensitivity = 1, Invert = false, Type = InputManagerAxisType.JoystickAxis, Axis = 27 } }; #endregion InputAxisConfig diff --git a/Definitions/Utilities/MixedRealityPose.cs b/Definitions/Utilities/MixedRealityPose.cs index 2750a6292..c470adb1f 100644 --- a/Definitions/Utilities/MixedRealityPose.cs +++ b/Definitions/Utilities/MixedRealityPose.cs @@ -122,7 +122,7 @@ bool IEqualityComparer.Equals(object left, object right) public bool Equals(MixedRealityPose other) { return Position == other.Position && - Rotation == other.Rotation; + Rotation.Equals(other.Rotation); } public override bool Equals(object obj) diff --git a/Extensions/BoundsExtensions.cs b/Extensions/BoundsExtensions.cs index 5280fc932..ee7cb0c85 100644 --- a/Extensions/BoundsExtensions.cs +++ b/Extensions/BoundsExtensions.cs @@ -68,7 +68,7 @@ public enum Axis private static Vector3[] corners = null; - private static Vector3[] rectTransformCorners = new Vector3[4]; + private static readonly Vector3[] rectTransformCorners = new Vector3[4]; #region Public Static Functions /// @@ -103,17 +103,18 @@ public static bool IsValid(this Bounds bounds) public static void GetCornerPositions(this Bounds bounds, Transform transform, ref Vector3[] positions) { // Calculate the local points to transform. - Vector3 center = bounds.center; - Vector3 extents = bounds.extents; - float leftEdge = center.x - extents.x; - float rightEdge = center.x + extents.x; - float bottomEdge = center.y - extents.y; - float topEdge = center.y + extents.y; - float frontEdge = center.z - extents.z; - float backEdge = center.z + extents.z; + var center = bounds.center; + var extents = bounds.extents; + var leftEdge = center.x - extents.x; + var rightEdge = center.x + extents.x; + var bottomEdge = center.y - extents.y; + var topEdge = center.y + extents.y; + var frontEdge = center.z - extents.z; + var backEdge = center.z + extents.z; // Allocate the array if needed. const int numPoints = 8; + if (positions == null || positions.Length != numPoints) { positions = new Vector3[numPoints]; @@ -137,16 +138,17 @@ public static void GetCornerPositions(this Bounds bounds, Transform transform, r /// public static void GetCornerPositionsFromRendererBounds(this Bounds bounds, ref Vector3[] positions) { - Vector3 center = bounds.center; - Vector3 extents = bounds.extents; - float leftEdge = center.x - extents.x; - float rightEdge = center.x + extents.x; - float bottomEdge = center.y - extents.y; - float topEdge = center.y + extents.y; - float frontEdge = center.z - extents.z; - float backEdge = center.z + extents.z; + var center = bounds.center; + var extents = bounds.extents; + var leftEdge = center.x - extents.x; + var rightEdge = center.x + extents.x; + var bottomEdge = center.y - extents.y; + var topEdge = center.y + extents.y; + var frontEdge = center.z - extents.z; + var backEdge = center.z + extents.z; const int numPoints = 8; + if (positions == null || positions.Length != numPoints) { positions = new Vector3[numPoints]; @@ -164,10 +166,11 @@ public static void GetCornerPositionsFromRendererBounds(this Bounds bounds, ref public static void GetFacePositions(this Bounds bounds, Transform transform, ref Vector3[] positions) { - Vector3 center = bounds.center; - Vector3 extents = bounds.extents; - const int numPoints = 6; + + var center = bounds.center; + var extents = bounds.extents; + if (positions == null || positions.Length != numPoints) { positions = new Vector3[numPoints]; @@ -190,14 +193,14 @@ public static void GetFacePositions(this Bounds bounds, Transform transform, ref public static void GetCornerAndMidPointPositions(this Bounds bounds, Transform transform, ref Vector3[] positions) { // Calculate the local points to transform. - Vector3 center = bounds.center; - Vector3 extents = bounds.extents; - float leftEdge = center.x - extents.x; - float rightEdge = center.x + extents.x; - float bottomEdge = center.y - extents.y; - float topEdge = center.y + extents.y; - float frontEdge = center.z - extents.z; - float backEdge = center.z + extents.z; + var center = bounds.center; + var extents = bounds.extents; + var leftEdge = center.x - extents.x; + var rightEdge = center.x + extents.x; + var bottomEdge = center.y - extents.y; + var topEdge = center.y + extents.y; + var frontEdge = center.z - extents.z; + var backEdge = center.z + extents.z; // Allocate the array if needed. const int numPoints = LTF_LTB + 1; @@ -242,13 +245,13 @@ public static void GetCornerAndMidPointPositions(this Bounds bounds, Transform t public static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform transform, ref Vector3[] positions, Axis flattenAxis) { // Calculate the local points to transform. - Vector3 center = bounds.center; - Vector3 extents = bounds.extents; + var center = bounds.center; + var extents = bounds.extents; - float leftEdge = 0; - float rightEdge = 0; - float bottomEdge = 0; - float topEdge = 0; + float leftEdge; + float rightEdge; + float bottomEdge; + float topEdge; // Allocate the array if needed. const int numPoints = LB_LT + 1; @@ -259,7 +262,7 @@ public static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform switch (flattenAxis) { - case Axis.X: + // case Axis.X: default: leftEdge = center.z - extents.z; rightEdge = center.z + extents.z; @@ -309,9 +312,14 @@ public static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform /// gameObject that boundingBox bounds. /// array reference that gets filled with points /// layerMask to simplify search - public static void GetColliderBoundsPoints(GameObject target, List boundsPoints, LayerMask ignoreLayers) + /// The colliders for this gameObject and it's children + public static void GetColliderBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers, Collider[] colliders = null) { - Collider[] colliders = target.GetComponentsInChildren(); + if (colliders == null) + { + colliders = target.GetComponentsInChildren(); + } + for (int i = 0; i < colliders.Length; i++) { if (ignoreLayers == (1 << colliders[i].gameObject.layer | ignoreLayers)) @@ -319,50 +327,48 @@ public static void GetColliderBoundsPoints(GameObject target, List boun continue; } - if (colliders[i] is SphereCollider) - { - SphereCollider sc = colliders[i] as SphereCollider; - Bounds sphereBounds = new Bounds(sc.center, Vector3.one * sc.radius * 2); - sphereBounds.GetFacePositions(sc.transform, ref corners); - boundsPoints.AddRange(corners); - } - else if (colliders[i] is BoxCollider) - { - BoxCollider bc = colliders[i] as BoxCollider; - Bounds boxBounds = new Bounds(bc.center, bc.size); - boxBounds.GetCornerPositions(bc.transform, ref corners); - boundsPoints.AddRange(corners); - - } - else if (colliders[i] is MeshCollider) - { - MeshCollider mc = colliders[i] as MeshCollider; - Bounds meshBounds = mc.sharedMesh.bounds; - meshBounds.GetCornerPositions(mc.transform, ref corners); - boundsPoints.AddRange(corners); - } - else if (colliders[i] is CapsuleCollider) + switch (colliders[i]) { - var cc = (CapsuleCollider)colliders[i]; - var capsuleBounds = new Bounds(cc.center, Vector3.zero); - var radius = cc.radius; - - switch (cc.direction) - { - case 0: - capsuleBounds.size = new Vector3(cc.height, cc.radius * 2, radius * 2); - break; - - case 1: - capsuleBounds.size = new Vector3(cc.radius * 2, cc.height, cc.radius * 2); - break; - - case 2: - capsuleBounds.size = new Vector3(cc.radius * 2, radius * 2, cc.height); - break; - } - capsuleBounds.GetFacePositions(cc.transform, ref corners); - boundsPoints.AddRange(corners); + case SphereCollider sphereCollider: + var sphereBounds = new Bounds(sphereCollider.center, Vector3.one * sphereCollider.radius * 2); + sphereBounds.GetFacePositions(sphereCollider.transform, ref corners); + boundsPoints.AddRange(corners); + break; + + case BoxCollider boxCollider: + var boxBounds = new Bounds(boxCollider.center, boxCollider.size); + boxBounds.GetCornerPositions(boxCollider.transform, ref corners); + boundsPoints.AddRange(corners); + break; + + case MeshCollider meshCollider: + var meshBounds = meshCollider.sharedMesh.bounds; + meshBounds.GetCornerPositions(meshCollider.transform, ref corners); + boundsPoints.AddRange(corners); + break; + + case CapsuleCollider capsuleCollider: + var capsuleBounds = new Bounds(capsuleCollider.center, Vector3.zero); + var radius = capsuleCollider.radius; + + switch (capsuleCollider.direction) + { + case 0: + capsuleBounds.size = new Vector3(capsuleCollider.height, capsuleCollider.radius * 2, radius * 2); + break; + + case 1: + capsuleBounds.size = new Vector3(capsuleCollider.radius * 2, capsuleCollider.height, capsuleCollider.radius * 2); + break; + + case 2: + capsuleBounds.size = new Vector3(capsuleCollider.radius * 2, radius * 2, capsuleCollider.height); + break; + } + + capsuleBounds.GetFacePositions(capsuleCollider.transform, ref corners); + boundsPoints.AddRange(corners); + break; } } } @@ -373,12 +379,18 @@ public static void GetColliderBoundsPoints(GameObject target, List boun /// gameObject that bounding box bounds /// array reference that gets filled with points /// layerMask to simplify search - public static void GetRenderBoundsPoints(GameObject target, List boundsPoints, LayerMask ignoreLayers) + /// The renderers for this gameObject and it's children + public static void GetRenderBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers, Renderer[] renderers = null) { - Renderer[] renderers = target.GetComponentsInChildren(); + if (renderers == null) + { + renderers = target.GetComponentsInChildren(); + } + for (int i = 0; i < renderers.Length; ++i) { - Renderer rendererObj = renderers[i]; + var rendererObj = renderers[i]; + if (ignoreLayers == (1 << rendererObj.gameObject.layer | ignoreLayers)) { continue; @@ -392,25 +404,29 @@ public static void GetRenderBoundsPoints(GameObject target, List bounds /// /// GetMeshFilterBoundsPoints - gets bounding box points using MeshFilter method. /// - /// gameObject that boundingbox bounds + /// gameObject that bounding box bounds /// array reference that gets filled with points /// layerMask to simplify search - public static void GetMeshFilterBoundsPoints(GameObject target, List boundsPoints, LayerMask ignoreLayers) + public static void GetMeshFilterBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers) { - MeshFilter[] meshFilters = target.GetComponentsInChildren(); + var meshFilters = target.GetComponentsInChildren(); + for (int i = 0; i < meshFilters.Length; i++) { - MeshFilter meshFilterObj = meshFilters[i]; + var meshFilterObj = meshFilters[i]; + if (ignoreLayers == (1 << meshFilterObj.gameObject.layer | ignoreLayers)) { continue; } - Bounds meshBounds = meshFilterObj.sharedMesh.bounds; + var meshBounds = meshFilterObj.sharedMesh.bounds; meshBounds.GetCornerPositions(meshFilterObj.transform, ref corners); boundsPoints.AddRange(corners); } - RectTransform[] rectTransforms = target.GetComponentsInChildren(); + + var rectTransforms = target.GetComponentsInChildren(); + for (int i = 0; i < rectTransforms.Length; i++) { rectTransforms[i].GetWorldCorners(rectTransformCorners); @@ -448,16 +464,16 @@ public static Bounds Transform(this Bounds bounds, Matrix4x4 transformMatrix) // We will 'imagine' that we want to rotate the bounds' extents vector using the rotation information // stored inside the specified transform matrix. We will need these when calculating the new size if // the transformed bounds. - Vector3 rotatedExtentsRight = rightAxis * bounds.extents.x; - Vector3 rotatedExtentsUp = upAxis * bounds.extents.y; - Vector3 rotatedExtentsLook = lookAxis * bounds.extents.z; + var rotatedExtentsRight = rightAxis * bounds.extents.x; + var rotatedExtentsUp = upAxis * bounds.extents.y; + var rotatedExtentsLook = lookAxis * bounds.extents.z; // Calculate the new bounds size along each axis. The size on each axis is calculated by summing up the // corresponding vector component values of the rotated extents vectors. We multiply by 2 because we want // to get a size and currently we are working with extents which represent half the size. - float newSizeX = (Mathf.Abs(rotatedExtentsRight.x) + Mathf.Abs(rotatedExtentsUp.x) + Mathf.Abs(rotatedExtentsLook.x)) * 2.0f; - float newSizeY = (Mathf.Abs(rotatedExtentsRight.y) + Mathf.Abs(rotatedExtentsUp.y) + Mathf.Abs(rotatedExtentsLook.y)) * 2.0f; - float newSizeZ = (Mathf.Abs(rotatedExtentsRight.z) + Mathf.Abs(rotatedExtentsUp.z) + Mathf.Abs(rotatedExtentsLook.z)) * 2.0f; + var newSizeX = (Mathf.Abs(rotatedExtentsRight.x) + Mathf.Abs(rotatedExtentsUp.x) + Mathf.Abs(rotatedExtentsLook.x)) * 2.0f; + var newSizeY = (Mathf.Abs(rotatedExtentsRight.y) + Mathf.Abs(rotatedExtentsUp.y) + Mathf.Abs(rotatedExtentsLook.y)) * 2.0f; + var newSizeZ = (Mathf.Abs(rotatedExtentsRight.z) + Mathf.Abs(rotatedExtentsUp.z) + Mathf.Abs(rotatedExtentsLook.z)) * 2.0f; // Construct the transformed 'Bounds' instance var transformedBounds = new Bounds(); @@ -478,21 +494,21 @@ public static Bounds Transform(this Bounds bounds, Matrix4x4 transformMatrix) /// public static Vector2[] GetScreenSpaceCornerPoints(this Bounds bounds, Camera camera) { - Vector3 aabbCenter = bounds.center; - Vector3 aabbExtents = bounds.extents; + var aabbCenter = bounds.center; + var aabbExtents = bounds.extents; // Return the screen space point array return new Vector2[] { - camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)), - - camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z)), - camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z)) + camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z - aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z - aabbExtents.z)), + + camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y - aabbExtents.y, aabbCenter.z + aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x + aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z)), + camera.WorldToScreenPoint(new Vector3(aabbCenter.x - aabbExtents.x, aabbCenter.y + aabbExtents.y, aabbCenter.z + aabbExtents.z)) }; } @@ -502,10 +518,11 @@ public static Vector2[] GetScreenSpaceCornerPoints(this Bounds bounds, Camera ca public static Rect GetScreenRectangle(this Bounds bounds, Camera camera) { // Retrieve the bounds' corner points in screen space - Vector2[] screenSpaceCornerPoints = bounds.GetScreenSpaceCornerPoints(camera); + var screenSpaceCornerPoints = bounds.GetScreenSpaceCornerPoints(camera); // Identify the minimum and maximum points in the array Vector3 minScreenPoint = screenSpaceCornerPoints[0], maxScreenPoint = screenSpaceCornerPoints[0]; + for (int screenPointIndex = 1; screenPointIndex < screenSpaceCornerPoints.Length; ++screenPointIndex) { minScreenPoint = Vector3.Min(minScreenPoint, screenSpaceCornerPoints[screenPointIndex]); @@ -534,10 +551,8 @@ public static float Volume(this Bounds bounds) /// public static Bounds ExpandToContain(this Bounds originalBounds, Bounds otherBounds) { - Bounds tmpBounds = originalBounds; - + var tmpBounds = originalBounds; tmpBounds.Encapsulate(otherBounds); - return tmpBounds; } @@ -561,13 +576,13 @@ public static bool ContainsBounds(this Bounds bounds, Bounds otherBounds) /// public static bool CloserToPoint(this Bounds bounds, Vector3 point, Bounds otherBounds) { - Vector3 distToClosestPoint1 = bounds.ClosestPoint(point) - point; - Vector3 distToClosestPoint2 = otherBounds.ClosestPoint(point) - point; + var distToClosestPoint1 = bounds.ClosestPoint(point) - point; + var distToClosestPoint2 = otherBounds.ClosestPoint(point) - point; - if (distToClosestPoint1.magnitude == distToClosestPoint2.magnitude) + if (distToClosestPoint1.magnitude.Equals(distToClosestPoint2.magnitude)) { - Vector3 toCenter1 = point - bounds.center; - Vector3 toCenter2 = point - otherBounds.center; + var toCenter1 = point - bounds.center; + var toCenter2 = point - otherBounds.center; return (toCenter1.magnitude <= toCenter2.magnitude); } @@ -578,6 +593,7 @@ public static bool CloserToPoint(this Bounds bounds, Vector3 point, Bounds other #endregion #region Private Static Functions + /// /// Returns the vector which is used to represent and invalid bounds size. /// @@ -585,6 +601,7 @@ private static Vector3 GetInvalidBoundsSize() { return new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); } + #endregion } } \ No newline at end of file diff --git a/Services/InputSystem/MixedRealityInputSystem.cs b/Services/InputSystem/MixedRealityInputSystem.cs index d6f6ba968..401c3d35c 100644 --- a/Services/InputSystem/MixedRealityInputSystem.cs +++ b/Services/InputSystem/MixedRealityInputSystem.cs @@ -174,6 +174,7 @@ public override void Initialize() CameraCache.Main.gameObject.EnsureComponent(); } + // ReSharper disable once SuspiciousTypeConversion.Global GazeProvider = CameraCache.Main.gameObject.EnsureComponent(MixedRealityToolkit.Instance.ActiveProfile.InputSystemProfile.PointerProfile.GazeProviderType.Type) as IMixedRealityGazeProvider; } @@ -190,6 +191,7 @@ public override void Disable() if (!Application.isPlaying) { + // ReSharper disable once SuspiciousTypeConversion.Global var component = CameraCache.Main.GetComponent() as Component; if (component != null) diff --git a/Utilities/Physics/InterpolationUtilities.cs b/Utilities/Physics/InterpolationUtilities.cs index aaa5a2dbb..e255cd77b 100644 --- a/Utilities/Physics/InterpolationUtilities.cs +++ b/Utilities/Physics/InterpolationUtilities.cs @@ -37,11 +37,9 @@ public static Color ExpDecay(Color from, Color to, float hLife, float dTime) return Color.Lerp(from, to, ExpCoefficient(hLife, dTime)); } - public static float ExpCoefficient(float hLife, float dTime) { - if (hLife == 0) - return 1; + if (hLife.Equals(0)) { return 1; } return 1.0f - Mathf.Pow(0.5f, dTime / hLife); } diff --git a/Utilities/Solvers/RadialView.cs b/Utilities/Solvers/RadialView.cs index 9d39c782a..3c38e396c 100644 --- a/Utilities/Solvers/RadialView.cs +++ b/Utilities/Solvers/RadialView.cs @@ -75,7 +75,7 @@ private Vector3 UpReference { get { - Vector3 upReference = Vector3.up; + var upReference = Vector3.up; if (referenceDirection == ReferenceDirectionEnum.ObjectOriented) { @@ -91,7 +91,7 @@ private Vector3 UpReference /// public override void SolverUpdate() { - Vector3 goalPosition = WorkingPosition; + var goalPosition = WorkingPosition; if (ignoreAngleClamp) { @@ -110,17 +110,11 @@ public override void SolverUpdate() } // Element orientation - Vector3 refDirUp = UpReference; - Quaternion goalRotation; + var refDirUp = UpReference; - if (orientToReferenceDirection) - { - goalRotation = Quaternion.LookRotation(ReferenceDirection, refDirUp); - } - else - { - goalRotation = Quaternion.LookRotation(goalPosition - ReferencePoint, refDirUp); - } + var goalRotation = orientToReferenceDirection + ? Quaternion.LookRotation(ReferenceDirection, refDirUp) + : Quaternion.LookRotation(goalPosition - ReferencePoint, refDirUp); // If gravity aligned then zero out the x and z axes on the rotation if (referenceDirection == ReferenceDirectionEnum.GravityAligned) @@ -143,14 +137,14 @@ private void GetDesiredOrientation_DistanceOnly(ref Vector3 desiredPos) { // TODO: There should be a different solver for distance constraint. // Determine reference locations and directions - Vector3 refPoint = ReferencePoint; - Vector3 elementPoint = transform.position; - Vector3 elementDelta = elementPoint - refPoint; - float elementDist = elementDelta.magnitude; - Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one; + var refPoint = ReferencePoint; + var elementPoint = transform.position; + var elementDelta = elementPoint - refPoint; + var elementDist = elementDelta.magnitude; + var elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one; // Clamp distance too - float clampedDistance = Mathf.Clamp(elementDist, minDistance, maxDistance); + var clampedDistance = Mathf.Clamp(elementDist, minDistance, maxDistance); if (!clampedDistance.Equals(elementDist)) { @@ -161,16 +155,16 @@ private void GetDesiredOrientation_DistanceOnly(ref Vector3 desiredPos) private void GetDesiredOrientation(ref Vector3 desiredPos) { // Determine reference locations and directions - Vector3 direction = ReferenceDirection; - Vector3 upDirection = UpReference; - Vector3 referencePoint = ReferencePoint; - Vector3 elementPoint = transform.position; - Vector3 elementDelta = elementPoint - referencePoint; - float elementDist = elementDelta.magnitude; - Vector3 elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one; + var direction = ReferenceDirection; + var upDirection = UpReference; + var referencePoint = ReferencePoint; + var elementPoint = transform.position; + var elementDelta = elementPoint - referencePoint; + var elementDist = elementDelta.magnitude; + var elementDir = elementDist > 0 ? elementDelta / elementDist : Vector3.one; // Generate basis: First get axis perpendicular to reference direction pointing toward element - Vector3 perpendicularDirection = (elementDir - direction); + var perpendicularDirection = (elementDir - direction); perpendicularDirection -= direction * Vector3.Dot(perpendicularDirection, direction); perpendicularDirection.Normalize(); @@ -186,7 +180,7 @@ private void GetDesiredOrientation(ref Vector3 desiredPos) float clampedDistance = ignoreDistanceClamp ? elementDist : Mathf.Clamp(elementDist, minDistance, maxDistance); // If the angle was clamped, do some special update stuff - if (currentAngle != currentAngleClamped) + if (!currentAngle.Equals(currentAngleClamped)) { float angRad = currentAngleClamped * Mathf.Deg2Rad; diff --git a/Utilities/Solvers/SolverHandler.cs b/Utilities/Solvers/SolverHandler.cs index 605282cc2..5d6dff1d1 100644 --- a/Utilities/Solvers/SolverHandler.cs +++ b/Utilities/Solvers/SolverHandler.cs @@ -1,10 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -using XRTK.Definitions.Utilities; -using XRTK.Utilities; using System.Collections.Generic; using UnityEngine; +using XRTK.Definitions.Utilities; +using XRTK.Utilities; namespace XRTK.SDK.Utilities.Solvers { @@ -118,7 +118,7 @@ public bool UpdateSolvers /// public float DeltaTime { get; set; } - private bool RequiresOffset => AdditionalOffset.sqrMagnitude != 0 || AdditionalRotation.sqrMagnitude != 0; + private bool RequiresOffset => !AdditionalOffset.sqrMagnitude.Equals(0) || !AdditionalRotation.sqrMagnitude.Equals(0); protected readonly List solvers = new List();