private void EnableRenderer(bool cameraHigher) { try { if (_imageEx != null) { return; } var renderer = new DarkKn5ObjectRenderer(Model.Car); renderer.SelectSkin(null); renderer.Initialize(); renderer.SetCameraHigher = cameraHigher; SetRendererSize(renderer); renderer.Draw(); Model.Renderer = renderer; _imageEx = new D3DImageEx(); Scene.Source = _imageEx; _setCount = 0; _lastTarget = IntPtr.Zero; CompositionTarget.Rendering += OnRendering; VisibilityAnimation.SetVisible(Scene, true); } catch (Exception e) { MessageBox.Show(e.ToString()); } }
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; } }