Esempio n. 1
0
        /// <summary>
        /// ImporterContextが所有する UnityEngine.Object を破棄する
        /// </summary>
        public virtual void Dispose()
        {
            foreach (var x in Meshes)
            {
                UnityObjectDestoyer.DestroyRuntimeOrEditor(x.Mesh);
            }
            Meshes.Clear();

            AnimationClipFactory?.Dispose();
            MaterialFactory?.Dispose();
            TextureFactory?.Dispose();
        }
Esempio n. 2
0
        async void LoadModel(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }

            _cancellationTokenSource?.Dispose();
            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;

            try
            {
                Debug.LogFormat("{0}", path);
                var vrm10Instance = await Vrm10.LoadPathAsync(path,
                                                              canLoadVrm0X : true,
                                                              normalizeTransform : m_useNormalization.isOn,
                                                              showMeshes : false,
                                                              awaitCaller : m_useAsync.enabled?(IAwaitCaller) new RuntimeOnlyAwaitCaller() : (IAwaitCaller) new ImmediateCaller(),
                                                                  materialGenerator : GetVrmMaterialDescriptorGenerator(m_useUrpMaterial.isOn),
                                                                  vrmMetaInformationCallback : m_texts.UpdateMeta,
                                                                  ct : cancellationToken);

                if (vrm10Instance != null)
                {
                    // test. error にならなければよい
                    vrm10Instance.Runtime.Expression.SetWeight(ExpressionKey.Aa, 0);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        UnityObjectDestoyer.DestroyRuntimeOrEditor(vrm10Instance.gameObject);
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    SetModel(vrm10Instance.GetComponent <RuntimeGltfInstance>());
                }
                else
                {
                    // NOTE: load as glTF model if failed to load as VRM 1.0.
                    // TODO: Hand over CancellationToken
                    var gltfModel = await GltfUtility.LoadAsync(path,
                                                                awaitCaller : m_useAsync.enabled?(IAwaitCaller) new RuntimeOnlyAwaitCaller() : (IAwaitCaller) new ImmediateCaller());

                    if (gltfModel == null)
                    {
                        throw new Exception("Failed to load the file as glTF format.");
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        gltfModel.Dispose();
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    SetModel(gltfModel);
                }
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException)
                {
                    Debug.LogWarning($"Canceled to Load: {path}");
                }
                else
                {
                    Debug.LogError($"Failed to Load: {path}");
                    Debug.LogException(ex);
                }
            }
        }
Esempio n. 3
0
        private static async Task <Vrm10Instance> LoadAsync(
            string name,
            byte[] bytes,
            bool canLoadVrm0X,
            bool normalizeTransform,
            bool showMeshes,
            IAwaitCaller awaitCaller,
            IMaterialDescriptorGenerator materialGenerator,
            VrmMetaInformationCallback vrmMetaInformationCallback,
            CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();
            if (awaitCaller == null)
            {
                throw new ArgumentNullException();
            }

            using (var gltfData = new GlbLowLevelParser(name, bytes).Parse())
            {
                // 1. Try loading as vrm-1.0
                var instance = await TryLoadingAsVrm10Async(
                    gltfData,
                    normalizeTransform,
                    showMeshes,
                    awaitCaller,
                    materialGenerator,
                    vrmMetaInformationCallback,
                    ct);

                if (instance != null)
                {
                    if (ct.IsCancellationRequested)
                    {
                        UnityObjectDestoyer.DestroyRuntimeOrEditor(instance.gameObject);
                        ct.ThrowIfCancellationRequested();
                    }
                    return(instance);
                }

                // 2. Stop loading if not allowed migration.
                if (!canLoadVrm0X)
                {
                    throw new Exception($"Failed to load as VRM 1.0: {name}");
                }

                // 3. Try migration from vrm-0.x into vrm-1.0
                var migratedInstance = await TryMigratingFromVrm0XAsync(
                    gltfData,
                    normalizeTransform,
                    showMeshes,
                    awaitCaller,
                    materialGenerator,
                    vrmMetaInformationCallback,
                    ct);

                if (migratedInstance != null)
                {
                    if (ct.IsCancellationRequested)
                    {
                        UnityObjectDestoyer.DestroyRuntimeOrEditor(migratedInstance.gameObject);
                        ct.ThrowIfCancellationRequested();
                    }
                    return(migratedInstance);
                }

                // 4. Failed loading.
                throw new Exception($"Failed to load: {name}");
            }
        }