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); }
public Model(ModelViewModel modelViewerViewModel, ITankObject tankObject, ModuleViewModel moduleViewModel) { ModuleViewModel = moduleViewModel; _modelViewerViewModel = modelViewerViewModel; _tankObject = tankObject; DispatchUniqueProperty(); var model = this.ModelObject.Model.Undamaged; LoadingTask = Task.Factory.StartNew(() => { try { using (Diagnostics.PotentialExceptionRegion) { using (var visualStream = this.OpenVisualFile(model)) { this.Visual = ModelVisual.ReadFrom(visualStream); } } } catch (Exception e) { Log.Info("exception occurred when load visual", e); } }).ContinueWith((_) => { try { using (Diagnostics.PotentialExceptionRegion) { using (var modelStream = this.OpenPrimitiveFile(model)) { this.Primitive = ModelPrimitive.ReadFrom(modelStream, this.Visual, false); } } } catch (Exception e) { Log.Info("exception occurred when load primitive", e); } }); var collisionModel = this.ModelObject.Model.Collision; LoadingTask = LoadingTask.ContinueWith((_) => { try { using (Diagnostics.PotentialExceptionRegion) { using (var visualStream = this.OpenVisualFile(collisionModel)) { this.CollisionVisual = ModelVisual.ReadFrom(visualStream); foreach (var rendetSet in CollisionVisual.RenderSets) { foreach (var groupKv in rendetSet.Geometry.ModelPrimitiveGroups) { var material = groupKv.Value.Material; material.ShowArmor = true; if (ArmorObject.Armor.TryGetArmorValue(material.Identifier, out ArmorGroup armor)) { material.Armor = armor; } else { material.Armor = new ArmorGroup(); } } } } } } catch (Exception e) { Log.Info("exception occurred when load collision visual", e); } }).ContinueWith((_) => { try { using (Diagnostics.PotentialExceptionRegion) { using (var modelStream = OpenPrimitiveFile(collisionModel)) { this.CollisionPrimitive = ModelPrimitive.ReadFrom(modelStream, CollisionVisual, true); } } } catch (Exception e) { Log.Info("exception occurred when load collision primitive", e); } }); }