Esempio n. 1
0
        private IEnumerator LoadControllerModel(InteractionSource source)
        {
            GameObject controllerModelGameObject;

            if (source.handedness == InteractionSourceHandedness.Left && LeftControllerOverride != null)
            {
                controllerModelGameObject = Instantiate(LeftControllerOverride);
            }
            else if (source.handedness == InteractionSourceHandedness.Right && RightControllerOverride != null)
            {
                controllerModelGameObject = Instantiate(RightControllerOverride);
            }
            else
            {
#if !UNITY_EDITOR
                if (GLTFMaterial == null)
                {
                    Debug.Log("If using glTF, please specify a material on " + name + ".");
                    yield break;
                }

                // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported.
                IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync();

                if (modelTask == null)
                {
                    Debug.Log("Model task is null.");
                    yield break;
                }

                while (modelTask.Status == AsyncStatus.Started)
                {
                    yield return(null);
                }

                IRandomAccessStreamWithContentType modelStream = modelTask.GetResults();

                if (modelStream == null)
                {
                    Debug.Log("Model stream is null.");
                    yield break;
                }

                if (modelStream.Size == 0)
                {
                    Debug.Log("Model stream is empty.");
                    yield break;
                }

                byte[] fileBytes = new byte[modelStream.Size];

                using (DataReader reader = new DataReader(modelStream))
                {
                    DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size);

                    while (loadModelOp.Status == AsyncStatus.Started)
                    {
                        yield return(null);
                    }

                    reader.ReadBytes(fileBytes);
                }

                controllerModelGameObject = new GameObject();
                GLTFComponentStreamingAssets gltfScript = controllerModelGameObject.AddComponent <GLTFComponentStreamingAssets>();
                gltfScript.ColorMaterial   = GLTFMaterial;
                gltfScript.NoColorMaterial = GLTFMaterial;
                gltfScript.GLTFData        = fileBytes;

                yield return(gltfScript.LoadModel());
#else
                yield break;
#endif
            }

            FinishControllerSetup(controllerModelGameObject, source.handedness.ToString(), source.id);
        }
Esempio n. 2
0
        private IEnumerator LoadSourceControllerModel(InteractionSource source)
        {
            byte[]     fileBytes;
            GameObject controllerModelGameObject;

            if (GLTFMaterial == null)
            {
                Debug.Log("If using glTF, please specify a material on " + name + ".");
                yield break;
            }

#if !UNITY_EDITOR
            // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported.
            IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync();

            if (modelTask == null)
            {
                Debug.Log("Model task is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            while (modelTask.Status == AsyncStatus.Started)
            {
                yield return(null);
            }

            IRandomAccessStreamWithContentType modelStream = modelTask.GetResults();

            if (modelStream == null)
            {
                Debug.Log("Model stream is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            if (modelStream.Size == 0)
            {
                Debug.Log("Model stream is empty; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            fileBytes = new byte[modelStream.Size];

            using (DataReader reader = new DataReader(modelStream))
            {
                DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size);

                while (loadModelOp.Status == AsyncStatus.Started)
                {
                    yield return(null);
                }

                reader.ReadBytes(fileBytes);
            }
#else
            IntPtr controllerModel = new IntPtr();
            uint   outputSize      = 0;

            if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel))
            {
                fileBytes = new byte[Convert.ToInt32(outputSize)];

                Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize));
            }
            else
            {
                Debug.Log("Unable to load controller models; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }
#endif

            controllerModelGameObject      = new GameObject();
            controllerModelGameObject.name = "glTFController";
            GLTFComponentStreamingAssets gltfScript = controllerModelGameObject.AddComponent <GLTFComponentStreamingAssets>();
            gltfScript.ColorMaterial   = GLTFMaterial;
            gltfScript.NoColorMaterial = GLTFMaterial;
            gltfScript.GLTFData        = fileBytes;

            yield return(gltfScript.LoadModel());

            FinishControllerSetup(controllerModelGameObject, source.handedness.ToString(), source.id);
        }
Esempio n. 3
0
        private IEnumerator LoadSourceControllerModel(InteractionSource source)
        {
            //throw new NotImplementedException();
            byte[] fileBytes = null;
            UnityEngine.Material GLTFMaterial = new UnityEngine.Material(Shader.Find("Standard"));

#if !UNITY_EDITOR
            // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported.
            IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = source.TryGetRenderableModelAsync();

            if (modelTask == null)
            {
                Debug.Log("Model task is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            while (modelTask.Status == AsyncStatus.Started)
            {
                yield return(null);
            }

            IRandomAccessStreamWithContentType modelStream = modelTask.GetResults();

            if (modelStream == null)
            {
                Debug.Log("Model stream is null; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            if (modelStream.Size == 0)
            {
                Debug.Log("Model stream is empty; loading alternate.");
                LoadAlternateControllerModel(source);
                yield break;
            }

            fileBytes = new byte[modelStream.Size];

            using (DataReader reader = new DataReader(modelStream))
            {
                DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size);

                while (loadModelOp.Status == AsyncStatus.Started)
                {
                    yield return(null);
                }

                reader.ReadBytes(fileBytes);
            }
#else
            IntPtr controllerModel = new IntPtr();
            uint   outputSize      = 0;

            try
            {
                if (TryGetMotionControllerModel(source.id, out outputSize, out controllerModel))
                {
                    fileBytes = new byte[Convert.ToInt32(outputSize)];

                    Marshal.Copy(controllerModel, fileBytes, 0, Convert.ToInt32(outputSize));
                }
                else
                {
                    Debug.Log("Unable to load controller models; loading alternate.");
                    LoadAlternateControllerModel(source);
                    yield break;
                }
            }
            catch (Exception e)
            {
                loading = false;
            }
#endif

            RenderModel = new GameObject {
                name = source.handedness + "-glTFController"
            };
            GLTFComponentStreamingAssets gltfScript = RenderModel.AddComponent <GLTFComponentStreamingAssets>();
            gltfScript.ColorMaterial   = GLTFMaterial;
            gltfScript.NoColorMaterial = GLTFMaterial;
            gltfScript.GLTFData        = fileBytes;

            yield return(gltfScript.LoadModel());

            RenderModelInitialized = true;
            loading = false;

            FinishControllerSetup(RenderModel, source.handedness, source.vendorId + "/" + source.productId + "/" + source.productVersion + "/" + source.handedness);
        }