Example #1
0
    private void UpdateSDKAvatarUnityState()
    {
        //Iterate through all the render components
        UInt64           componentCount    = Oculus.Avatar.CAPI.ovrAvatarComponent_Count(sdkAvatar);
        HashSet <string> componentsThisRun = new HashSet <string>();

        for (UInt64 i = 0; i < componentCount; i++)
        {
            IntPtr             ptr       = Oculus.Avatar.CAPI.ovrAvatarComponent_Get_Native(sdkAvatar, i);
            ovrAvatarComponent component = (ovrAvatarComponent)Marshal.PtrToStructure(ptr, typeof(ovrAvatarComponent));
            componentsThisRun.Add(component.name);
            if (!trackedComponents.ContainsKey(component.name))
            {
                GameObject componentObject = null;
                Type       specificType    = null;
                if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetBaseComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarBase);
                    if (Base != null)
                    {
                        componentObject = Base.gameObject;
                    }
                }
                else if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarBody);
                    if (Body != null)
                    {
                        componentObject = Body.gameObject;
                    }
                }
                else if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetLeftControllerComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarTouchController);
                    if (ControllerLeft != null)
                    {
                        componentObject = ControllerLeft.gameObject;
                    }
                }
                else if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetRightControllerComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarTouchController);
                    if (ControllerRight != null)
                    {
                        componentObject = ControllerRight.gameObject;
                    }
                }
                else if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetLeftHandComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarHand);
                    if (HandLeft != null)
                    {
                        componentObject = HandLeft.gameObject;
                    }
                }
                else if (ptr == Oculus.Avatar.CAPI.ovrAvatarPose_GetRightHandComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarHand);
                    if (HandRight != null)
                    {
                        componentObject = HandRight.gameObject;
                    }
                }

                // If this is an unknown type, just create an object for the rendering
                if (componentObject == null && specificType == null)
                {
                    componentObject      = new GameObject();
                    componentObject.name = component.name;
                    componentObject.transform.SetParent(transform);
                }
                if (componentObject != null)
                {
                    AddAvatarComponent(componentObject, component);
                    if (specificType != null)
                    {
                        IAvatarPart part = componentObject.GetComponent(specificType) as IAvatarPart;
                        if (part != null)
                        {
                            part.OnAssetsLoaded();
                        }
                    }
                }
            }
            UpdateAvatarComponent(component);
        }
        HashSet <string> deletableNames = new HashSet <string>(trackedComponents.Keys);

        deletableNames.ExceptWith(componentsThisRun);
        //deletableNames contains the name of all components which are tracked and were
        //not present in this run
        foreach (var name in deletableNames)
        {
            RemoveAvatarComponent(name);
        }
    }
Example #2
0
    private void UpdateSDKAvatarUnityState()
    {
        //Iterate through all the render components
        UInt64           componentCount    = CAPI.ovrAvatarComponent_Count(sdkAvatar);
        HashSet <string> componentsThisRun = new HashSet <string>();

        for (UInt64 i = 0; i < componentCount; i++)
        {
            IntPtr             ptr       = CAPI.ovrAvatarComponent_Get_Native(sdkAvatar, i);
            ovrAvatarComponent component = (ovrAvatarComponent)Marshal.PtrToStructure(ptr, typeof(ovrAvatarComponent));
            componentsThisRun.Add(component.name);
            if (!trackedComponents.ContainsKey(component.name))
            {
                GameObject componentObject = null;
                Type       specificType    = null;
                if (ptr == CAPI.ovrAvatarPose_GetBaseComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarBase);
                    if (Base != null)
                    {
                        componentObject = Base.gameObject;
                    }
                }
                else if (ptr == CAPI.ovrAvatarPose_GetBodyComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarBody);
                    if (Body != null)
                    {
                        componentObject = Body.gameObject;
                    }
                }
                else if (ptr == CAPI.ovrAvatarPose_GetLeftControllerComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarTouchController);
                    if (ControllerLeft != null)
                    {
                        componentObject = ControllerLeft.gameObject;
                    }
                }
                else if (ptr == CAPI.ovrAvatarPose_GetRightControllerComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarTouchController);
                    if (ControllerRight != null)
                    {
                        componentObject = ControllerRight.gameObject;
                    }
                }
                else if (ptr == CAPI.ovrAvatarPose_GetLeftHandComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarHand);
                    if (HandLeft != null)
                    {
                        componentObject = HandLeft.gameObject;
                    }
                }
                else if (ptr == CAPI.ovrAvatarPose_GetRightHandComponent(sdkAvatar).renderComponent)
                {
                    specificType = typeof(OvrAvatarHand);
                    if (HandRight != null)
                    {
                        componentObject = HandRight.gameObject;
                    }
                }

                // If this is an unknown type, just create an object for the rendering
                if (componentObject == null && specificType == null)
                {
                    componentObject      = new GameObject();
                    componentObject.name = component.name;
                    componentObject.transform.SetParent(transform);
                }
                if (componentObject != null)
                {
                    AddAvatarComponent(componentObject, component);

                    if (ptr == CAPI.ovrAvatarPose_GetLeftHandComponent(sdkAvatar).renderComponent)
                    {
                        //Attach tablet
                        if (tablet)
                        {
                            GameObject tab  = Instantiate(tablet);
                            Transform  temp = tablet.transform;
                            tab.transform.position      = componentObject.transform.position + tabletOffset;
                            tab.transform.parent        = componentObject.transform;
                            tab.transform.localRotation = temp.rotation;
                        }
                    }

                    if (specificType != null)
                    {
                        IAvatarPart part = componentObject.GetComponent(specificType) as IAvatarPart;
                        if (part != null)
                        {
                            part.OnAssetsLoaded();
                        }
                    }
                }
            }
            UpdateAvatarComponent(component);
        }


        if (addCollidersToLeftBones && GameObject.Find("hands:b_l_hand"))
        {
            addCollidersToLeftBones = false;
            Debug.Log(GameObject.Find("hands:b_l_hand"));

            foreach (Transform t in GameObject.Find("hands:b_l_hand").GetComponentsInChildren <Transform>())
            {
                SphereCollider col = t.gameObject.AddComponent <SphereCollider>();
                col.radius           = boneColliderRadius;
                col.isTrigger        = true;
                col.gameObject.layer = LayerMask.NameToLayer("UI");
                Rigidbody rigid = t.gameObject.AddComponent <Rigidbody>();
                rigid.isKinematic = true;
            }
        }
        if (addCollidersToRightBones && GameObject.Find("hands:b_r_hand"))
        {
            addCollidersToRightBones = false;
            //Debug.Log(GameObject.Find("hands:b_r_hand"));

            foreach (Transform t in GameObject.Find("hands:b_r_hand").GetComponentsInChildren <Transform>())
            {
                SphereCollider col = t.gameObject.AddComponent <SphereCollider>();
                col.radius           = boneColliderRadius;
                col.isTrigger        = true;
                col.gameObject.layer = LayerMask.NameToLayer("UI");
                Rigidbody rigid = t.gameObject.AddComponent <Rigidbody>();
                rigid.isKinematic = true;
            }
        }

        if (leftHandPhysicsOnFist)
        {
            leftHandPhysicsOnFist = false;

            leftHandCollider = GameObject.Find("controller_left").AddComponent <SphereCollider>();
            //leftHandRigid = GameObject.Find("controller_left").AddComponent<Rigidbody>();
            leftHandCollider.radius    = grabColliderRadius;
            leftHandCollider.isTrigger = true;
        }
        if (rightHandPhysicsOnFist)
        {
            rightHandPhysicsOnFist = false;

            rightHandCollider = GameObject.Find("controller_right").AddComponent <SphereCollider>();
            //rightHandRigid = GameObject.Find("controller_right").AddComponent<Rigidbody>();
            rightHandCollider.radius    = grabColliderRadius;
            rightHandCollider.isTrigger = true;
        }


        HashSet <string> deletableNames = new HashSet <string>(trackedComponents.Keys);

        deletableNames.ExceptWith(componentsThisRun);
        //deletableNames contains the name of all components which are tracked and were
        //not present in this run
        foreach (var name in deletableNames)
        {
            RemoveAvatarComponent(name);
        }
    }