public static async Task UpdateKn5(this Kn5 kn5, BaseRenderer renderer = null, CarSkinObject skin = null) { if (kn5.MaterialLoader != DefaultKn5MaterialLoader.Instance || kn5.NodeLoader != DefaultKn5NodeLoader.Instance) { throw new Exception("Can’t save KN5 loaded unusually"); } var backup = kn5.OriginalFilename + ".backup"; try { if (!File.Exists(backup)) { FileUtils.HardLinkOrCopy(kn5.OriginalFilename, backup); } } catch (Exception e) { Logging.Warning(e); } await Task.Run(() => { using (var f = FileUtils.RecycleOriginal(kn5.OriginalFilename)) { try { if (kn5.TextureLoader == DefaultKn5TextureLoader.Instance) { kn5.Save(f.Filename); } else { Logging.Debug("Extra special mode for saving KN5s without textures loaded"); kn5.Save(f.Filename, new ExistingKn5Textures(kn5.OriginalFilename)); } } catch { FileUtils.TryToDelete(f.Filename); throw; } } }); if (renderer != null) { var car = skin == null ? null : CarsManager.Instance.GetById(skin.CarId); var slot = (renderer as ToolsKn5ObjectRenderer)?.MainSlot; if (car != null && slot != null) { slot.SetCar(CarDescription.FromKn5(kn5, car.Location, car.AcdData)); slot.SelectSkin(skin.Id); } } }
private static async Task <IReadOnlyList <UpdatePreviewError> > Run([NotNull] CarObject car, [CanBeNull] string skinId, [CanBeNull] IReadOnlyList <ToUpdatePreview> toUpdate, [CanBeNull] string presetFilename) { var carKn5 = FileUtils.GetMainCarFilename(car.Location, car.AcdData); if (!File.Exists(carKn5)) { ModernDialog.ShowMessage("Model not found"); return(null); } await PrepareAsync(); Kn5 kn5; using (var waiting = new WaitingDialog()) { waiting.Report("Loading model…"); kn5 = await Task.Run(() => Kn5.FromFile(carKn5)); } using (var renderer = new DarkKn5ObjectRenderer(CarDescription.FromKn5(kn5, car.Location, car.AcdData)) { AutoRotate = false, AutoAdjustTarget = false, AsyncTexturesLoading = true, AsyncOverridesLoading = true, AutoloadCarLights = false, AutoloadShowroomLights = false }) { var wrapper = new CmPreviewsFormWrapper(car, renderer, skinId, presetFilename); if (toUpdate != null) { wrapper.SetToUpdate(toUpdate); } wrapper.Form.Icon = AppIconService.GetAppIcon(); wrapper.Run(); return(wrapper.GetErrors()); } }
public static async Task UpdateKn5(this IKn5 kn5, BaseRenderer renderer = null, CarSkinObject skin = null) { if (!kn5.IsEditable) { throw new Exception("Can’t save KN5 loaded unusually"); } var backup = kn5.OriginalFilename + ".backup"; try { if (!File.Exists(backup)) { FileUtils.HardLinkOrCopy(kn5.OriginalFilename, backup); } } catch (Exception e) { Logging.Warning(e); } await Task.Run(() => { using (var f = FileUtils.RecycleOriginal(kn5.OriginalFilename)) { try { kn5.Save(f.Filename); } catch { FileUtils.TryToDelete(f.Filename); throw; } } }); if (renderer != null) { var car = skin == null ? null : CarsManager.Instance.GetById(skin.CarId); var slot = (renderer as ToolsKn5ObjectRenderer)?.MainSlot; if (car != null && slot != null) { slot.SetCar(CarDescription.FromKn5(kn5, car.Location, car.AcdData)); slot.SelectSkin(skin.Id); } } }
protected override void InitializeInner() { base.InitializeInner(); DeviceContextHolder.Set <IMaterialsFactory>(new MaterialsProviderDeferred()); Scene.Add(SkyObject.Create(500f)); foreach (var showroom in _kn5.Skip(1)) { Scene.Add(new Kn5RenderableFile(showroom, Matrix.Identity)); } if (_kn5.Length > 1) { _car = new DeferredRenderableCar(CarDescription.FromKn5(_kn5[0]), Matrix.Identity, shadowsHeight: 0.001f, lights: Lights) { IsReflectable = false }; Scene.Add(_car); } Scene.UpdateBoundingBox(); Camera = new CameraOrbit(32) { Alpha = 0.9f, Beta = 0.1f, Radius = _car?.BoundingBox?.GetSize().Length() * 1.2f ?? 4.8f, Target = (_car?.BoundingBox?.GetCenter() ?? Vector3.Zero) - new Vector3(0f, 0.05f, 0f) }; _resetCamera = (CameraOrbit)Camera.Clone(); Sun = new DirectionalLight { Color = FixLight(new Vector3(1.2f, 1.0f, 0.9f)) * 5f, Direction = Vector3.Normalize(new Vector3(-1.2f, -3.4f, -2.2f)) }; _effect = DeviceContextHolder.GetEffect <EffectDeferredGObject>(); }
private static async Task ShowCarInShowroomAsync(string carId) { if (_showingCarInShowroom) { return; } _showingCarInShowroom = true; try { var temporaryDirectory = FilesStorage.Instance.GetTemporaryDirectory("Workshop", "Showroom", carId); var temporaryFilename = Path.Combine(temporaryDirectory, "data.zip"); byte[] modelData = null; string mainSkidId = null; var carData = new VirtualDataWrapper(); using (var waiting = WaitingDialog.Create("Loading showroom…")) { await WorkshopHolder.Client.DownloadFileAsync($"/cars/{carId}/download-showroom", temporaryFilename, false, waiting, waiting.CancellationToken); waiting.Report(AsyncProgressEntry.FromStringIndetermitate("Loading…")); await Task.Run(() => { using (var stream = File.OpenRead(temporaryFilename)) using (var archive = new ZipArchive(stream)) { foreach (var entry in archive.Entries) { if (entry.Length == 0) { continue; } if (entry.FullName == @"model.kn5") { modelData = entry.Open().ReadAsBytesAndDispose(); } else if (entry.FullName.StartsWith(@"data")) { carData.Data[Path.GetFileName(entry.FullName)] = entry.Open().ReadAsStringAndDispose(); } else { if (mainSkidId == null && entry.FullName.StartsWith(@"skins")) { mainSkidId = Path.GetFileName(Path.GetDirectoryName(entry.FullName)); } var newFilename = Path.Combine(temporaryDirectory, entry.FullName); FileUtils.EnsureFileDirectoryExists(newFilename); entry.ExtractToFile(newFilename, true); } waiting.CancellationToken.ThrowIfCancellationRequested(); } } }); waiting.CancellationToken.ThrowIfCancellationRequested(); } if (modelData == null) { throw new Exception("Model is missing"); } if (mainSkidId == null) { throw new Exception("Skins are missing"); } var description = CarDescription.FromKn5(Kn5.FromBytes(modelData), temporaryDirectory, carData); var renderer = new DarkKn5ObjectRenderer(description) { FlatMirror = true, FlatMirrorReflectiveness = 0.3f, FlatMirrorReflectedLight = true, BackgroundColor = Color.White, BackgroundBrightness = 0.05f, LightBrightness = 2f, AmbientBrightness = 2f, AmbientUp = Color.FromArgb(0xEEEEEE), AmbientDown = Color.FromArgb(0x333333), UseDof = true, UseAccumulationDof = true, AccumulationDofIterations = 40, AccumulationDofApertureSize = 0f, UseSslr = true, VisibleUi = false, UseSprite = false, AnyGround = false, ToneMapping = ToneMappingFn.Uncharted2, ToneExposure = 1.2f, ToneGamma = 1f, ToneWhitePoint = 2.2f, }; renderer.SelectSkin(mainSkidId); await FormWrapperBase.PrepareAsync(); var wrapper = new LiteShowroomFormWrapper(renderer); wrapper.Form.Icon = AppIconService.GetAppIcon(); CustomShowroomWrapper.SetProperties(wrapper, renderer); wrapper.Run(); } catch (Exception e) when(!e.IsCancelled()) { Logging.Warning(e); NonfatalError.Notify("Failed to load showroom", e); } finally { _showingCarInShowroom = false; } }