public void Initialize(NVRHand trackingHand, bool initialState) { Hand = trackingHand; PhysicalController = GameObject.Instantiate(Hand.gameObject); PhysicalController.name = PhysicalController.name.Replace("(Clone)", " [Physical]"); SteamVR_RenderModel renderModel = PhysicalController.GetComponentInChildren<SteamVR_RenderModel>(); ModelParent = renderModel.transform; GameObject.DestroyImmediate(PhysicalController.GetComponent<NVRPhysicalController>()); GameObject.DestroyImmediate(PhysicalController.GetComponent<NVRHand>()); GameObject.DestroyImmediate(PhysicalController.GetComponent<SteamVR_TrackedObject>()); GameObject.DestroyImmediate(renderModel); GameObject.DestroyImmediate(PhysicalController.GetComponent<NVRPhysicalController>()); Collider[] clonedColliders = PhysicalController.GetComponentsInChildren<Collider>(); for (int index = 0; index < clonedColliders.Length; index++) { GameObject.DestroyImmediate(clonedColliders[index]); } PhysicalController.transform.parent = Hand.transform.parent; PhysicalController.transform.position = Hand.transform.position; PhysicalController.transform.rotation = Hand.transform.rotation; PhysicalController.transform.localScale = Hand.transform.localScale; string controllerModel = Hand.GetDeviceName(); switch (controllerModel) { case "vr_controller_05_wireless_b": Transform dk1Trackhat = ModelParent.transform.Find("trackhat"); Collider dk1TrackhatCollider = dk1Trackhat.gameObject.GetComponent<BoxCollider>(); if (dk1TrackhatCollider == null) dk1TrackhatCollider = dk1Trackhat.gameObject.AddComponent<BoxCollider>(); Transform dk1Body = ModelParent.transform.Find("body"); Collider dk1BodyCollider = dk1Body.gameObject.GetComponent<BoxCollider>(); if (dk1BodyCollider == null) dk1BodyCollider = dk1Body.gameObject.AddComponent<BoxCollider>(); Colliders = new Collider[] { dk1TrackhatCollider, dk1BodyCollider }; break; case "vr_controller_vive_1_5": Transform dk2TrackhatColliders = ModelParent.transform.FindChild("VivePreColliders"); if (dk2TrackhatColliders == null) { dk2TrackhatColliders = GameObject.Instantiate(Resources.Load<GameObject>("VivePreColliders")).transform; dk2TrackhatColliders.parent = ModelParent.transform; dk2TrackhatColliders.localPosition = Vector3.zero; dk2TrackhatColliders.localRotation = Quaternion.identity; dk2TrackhatColliders.localScale = Vector3.one; } Colliders = dk2TrackhatColliders.GetComponentsInChildren<Collider>(); break; case "Custom": Transform customCollidersTransform = PhysicalController.transform.FindChild("VivePreColliders"); if (customCollidersTransform == null) { GameObject customColliders = GameObject.Instantiate(Hand.CustomPhysicalColliders); customColliders.name = "CustomColliders"; customCollidersTransform = customColliders.transform; customCollidersTransform.parent = PhysicalController.transform; customCollidersTransform.localPosition = Vector3.zero; customCollidersTransform.localRotation = Quaternion.identity; customCollidersTransform.localScale = Vector3.one; } Colliders = customCollidersTransform.GetComponentsInChildren<Collider>(); break; default: Debug.LogError("Error. Unsupported device type: " + controllerModel); break; } Rigidbody = PhysicalController.GetComponent<Rigidbody>(); Rigidbody.isKinematic = false; Rigidbody.maxAngularVelocity = float.MaxValue; Renderer[] renderers = PhysicalController.GetComponentsInChildren<Renderer>(); for (int index = 0; index < renderers.Length; index++) { NVRHelpers.SetOpaque(renderers[index].material); } if (initialState == false) { Off(); } else { On(); } }
public void Initialize(NVRHand trackingHand, bool initialState) { Hand = trackingHand; PhysicalController = GameObject.Instantiate(Hand.gameObject); PhysicalController.name = PhysicalController.name.Replace("(Clone)", " [Physical]"); GameObject.DestroyImmediate(PhysicalController.GetComponent <NVRPhysicalController>()); GameObject.DestroyImmediate(PhysicalController.GetComponent <NVRHand>()); GameObject.DestroyImmediate(PhysicalController.GetComponent <SteamVR_TrackedObject>()); GameObject.DestroyImmediate(PhysicalController.GetComponent <SteamVR_RenderModel>()); GameObject.DestroyImmediate(PhysicalController.GetComponent <NVRPhysicalController>()); Collider[] clonedColliders = PhysicalController.GetComponentsInChildren <Collider>(); for (int index = 0; index < clonedColliders.Length; index++) { GameObject.DestroyImmediate(clonedColliders[index]); } PhysicalController.transform.parent = Hand.transform.parent; PhysicalController.transform.position = Hand.transform.position; PhysicalController.transform.rotation = Hand.transform.rotation; PhysicalController.transform.localScale = Hand.transform.localScale; Rigidbody = PhysicalController.GetComponent <Rigidbody>(); Rigidbody.isKinematic = false; Rigidbody.useGravity = false; Rigidbody.angularDrag = 0; Rigidbody.maxAngularVelocity = 100f; string controllerModel = Hand.GetDeviceName(); switch (controllerModel) { case "vr_controller_05_wireless_b": Transform dk1Trackhat = PhysicalController.transform.FindChild("trackhat"); Collider dk1TrackhatCollider = dk1Trackhat.gameObject.GetComponent <BoxCollider>(); if (dk1TrackhatCollider == null) { dk1TrackhatCollider = dk1Trackhat.gameObject.AddComponent <BoxCollider>(); } Transform dk1Body = PhysicalController.transform.FindChild("body"); Collider dk1BodyCollider = dk1Body.gameObject.GetComponent <BoxCollider>(); if (dk1BodyCollider == null) { dk1BodyCollider = dk1Body.gameObject.AddComponent <BoxCollider>(); } Colliders = new Collider[] { dk1TrackhatCollider, dk1BodyCollider }; break; case "vr_controller_vive_1_5": Transform dk2TrackhatColliders = PhysicalController.transform.FindChild("VivePreColliders"); if (dk2TrackhatColliders == null) { dk2TrackhatColliders = GameObject.Instantiate(Resources.Load <GameObject>("VivePreColliders")).transform; dk2TrackhatColliders.parent = PhysicalController.transform; dk2TrackhatColliders.localPosition = Vector3.zero; dk2TrackhatColliders.localRotation = Quaternion.identity; dk2TrackhatColliders.localScale = Vector3.one; } Colliders = dk2TrackhatColliders.GetComponentsInChildren <Collider>(); break; default: Debug.LogError("Error. Unsupported device type: " + controllerModel); break; } Renderer[] renderers = PhysicalController.GetComponentsInChildren <Renderer>(); for (int index = 0; index < renderers.Length; index++) { NVRHelpers.SetOpaque(renderers[index].material); } if (initialState == false) { Off(); } else { On(); } }
public void Initialize(NVRHand trackingHand, bool initialState) { Hand = trackingHand; PhysicalController = GameObject.Instantiate(Hand.gameObject); PhysicalController.name = PhysicalController.name.Replace("(Clone)", " [Physical]"); Component[] components = PhysicalController.GetComponentsInChildren <Component>(true); for (int componentIndex = 0; componentIndex < components.Length; componentIndex++) { Type componentType = components[componentIndex].GetType(); if (KeepTypes.Any(keepType => keepType == componentType || componentType.IsSubclassOf(keepType)) == false) { DestroyImmediate(components[componentIndex]); } } PhysicalController.transform.parent = Hand.transform.parent; PhysicalController.transform.position = Hand.transform.position; PhysicalController.transform.rotation = Hand.transform.rotation; PhysicalController.transform.localScale = Hand.transform.localScale; string controllerModel = Hand.GetDeviceName(); switch (controllerModel) { case "Custom": Transform customCollidersTransform = null; if (Hand.CustomPhysicalColliders == null) { GameObject customColliders = GameObject.Instantiate(Hand.CustomModel); customColliders.name = "CustomColliders"; customCollidersTransform = customColliders.transform; customCollidersTransform.parent = PhysicalController.transform; customCollidersTransform.localPosition = Vector3.zero; customCollidersTransform.localRotation = Quaternion.identity; customCollidersTransform.localScale = Vector3.one; foreach (Collider col in customColliders.GetComponentsInChildren <Collider>()) { col.isTrigger = false; } Colliders = customCollidersTransform.GetComponentsInChildren <Collider>(); } else { GameObject customColliders = GameObject.Instantiate(Hand.CustomPhysicalColliders); customColliders.name = "CustomColliders"; customCollidersTransform = customColliders.transform; customCollidersTransform.parent = PhysicalController.transform; customCollidersTransform.localPosition = Vector3.zero; customCollidersTransform.localRotation = Quaternion.identity; customCollidersTransform.localScale = Hand.CustomPhysicalColliders.transform.localScale; } Colliders = customCollidersTransform.GetComponentsInChildren <Collider>(); break; default: Colliders = Hand.SetupDefaultPhysicalColliders(PhysicalController.transform); break; } if (Colliders == null) { Debug.LogError("[NewtonVR] Error: Physical colliders on hand not setup properly."); } Rigidbody = PhysicalController.GetComponent <Rigidbody>(); Rigidbody.isKinematic = false; Rigidbody.maxAngularVelocity = float.MaxValue; if (trackingHand.Player.AutomaticallySetControllerTransparency) { Renderer[] renderers = PhysicalController.GetComponentsInChildren <Renderer>(); for (int index = 0; index < renderers.Length; index++) { NVRHelpers.SetOpaque(renderers[index].material); } } if (initialState == false) { Off(); } else { On(); } }