Exemplo n.º 1
0
        private static void LoadModel(GroupNode node, bool renderEnvironmentMap)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = HelixToolkit.SharpDX.Core.Assimp.Importer.SupportedFormatsString;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var path = dialog.FileName;
                exception   = "";
                currentTime = Stopwatch.GetTimestamp();
                loading     = true;
                modelName   = Path.GetFileName(path);
                Task.Run(() =>
                {
                    var importer = new Importer();
                    return(importer.Load(path));
                }).ContinueWith((x) =>
                {
                    loading = false;
                    if (x.IsCompleted && x.Result != null)
                    {
                        node.Clear();
                        foreach (var model in x.Result.Root.Traverse())
                        {
                            if (model is MeshNode mesh)
                            {
                                if (mesh.Material is PBRMaterialCore pbr)
                                {
                                    pbr.RenderEnvironmentMap = renderEnvironmentMap;
                                }
                                else if (mesh.Material is PhongMaterialCore phong)
                                {
                                    phong.RenderEnvironmentMap = renderEnvironmentMap;
                                }
                            }
                        }
                        node.AddChildNode(x.Result.Root);
                        scene = x.Result;
                        if (scene.Animations != null && scene.Animations.Count > 0)
                        {
                            animationUpdaters        = scene.Animations.CreateAnimationUpdaters().Values.ToArray();
                            animationSelection       = new bool[animationUpdaters.Count];
                            animationNames           = animationUpdaters.Select((ani) => ani.Name).ToArray();
                            currentSelectedAnimation = -1;
                        }
                    }
                    else if (x.Exception != null)
                    {
                        exception = x.Exception.Message;
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }