public ModelRenderSet(IXQueryable data, ModelVisual visual)
     : base(data)
 {
     _lazyGeometry = new Lazy <Geometry>(this.ReadGeometry);
     this.Visual   = visual;
 }
Esempio n. 2
0
        public void Load(ModelType modelType,
                         IFileLocator fileLocator,
                         Device renderDevice,
                         GraphicsSettings graphicsSettings)
        {
            if (_loadCancellationTokenSource != null)
            {
                _loadCancellationTokenSource.Cancel();
            }

            _loadCancellationTokenSource = new CancellationTokenSource();

            var modelPath = this.ModelObject.GetModelPath(modelType);

            this.LoadingTask = Task.Factory.StartNew(() =>
            {
                try
                {
                    using (Diagnostics.PotentialExceptionRegion)
                    {
                        using (var visualStream = OpenVisualFile(modelPath, fileLocator))
                        {
                            this.Visual = ModelVisual.ReadFrom(visualStream);
                            foreach (var rendetSet in Visual.RenderSets)
                            {
                                foreach (var group in rendetSet.Geometry.ModelPrimitiveGroups)
                                {
                                    var material       = group.Value.Material;
                                    material.ShowArmor = modelType == ModelType.Collision;
                                    material.Armor     = this.ArmoredObject.GetArmorGroup(material.Identifier);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    this.LogInfo("exception occurred when loading {0} visual: {1}", modelType, e.Message);
                }
            },
                                                     _loadCancellationTokenSource.Token)
                               .ContinueWith(_ =>
            {
                try
                {
                    using (Diagnostics.PotentialExceptionRegion)
                    {
                        using (var primitivesStream = OpenPrimitiveFile(modelPath, fileLocator))
                        {
                            this.Primitives = ModelPrimitive.ReadFrom(primitivesStream,
                                                                      Visual,
                                                                      modelType == ModelType.Collision);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.LogInfo("exception occurred when loading {0} primitives: {1}",
                                 modelType,
                                 e.Message);
                }
            },
                                             _loadCancellationTokenSource.Token)
                               .ContinueWith(_ =>
            {
                var oldMesh = this.Mesh;
                this.Mesh   = new ModuleMesh(this, fileLocator, renderDevice, graphicsSettings);
                Disposer.RemoveAndDispose(ref oldMesh);
            },
                                             _loadCancellationTokenSource.Token);
        }