#include "MeshGenController.h" #include "GamificationManager.h" #include "MeshGenPredictor.h" #include "GameReadyPresets.h" #include "TripoSGPredictor.h" #include "TripoSplatPredictor.h" #include "Trellis2Predictor.h" #include "ShapeGuide.h" #include "ShapeGuideScene.h" #include "SelectionSet.h" #include #include #include "MeshGenBuilder.h" #include "BackgroundRemover.h" #include "MeshImporterExporter.h" #include "SentryReporter.h" #include "AIAssistManager.h" // ensureUpscaleModel (main-thread model fetch) #include "TextureUpscaler.h" // worker-side Real-ESRGAN 2x on the baked diffuse #include "ImageCaptioner.h" // background SmolVLM caption of the picked image #include "SDManager.h" // prompt-to-3D: text → source image (FLUX.2) #include "SDWorker.h" // Flux2Set detection #include #include #include #include #include #include #include #include #include #include #include #include // organizationName() — test-harness guard #include #include #include #include #include #include namespace { // The selected mesh entity, resolved like the rest of the editor resolves a // selection (an entity, the parent of a selected submesh, or a mesh on a // selected node or one of its descendants). Ogre::Entity* selectedMeshEntity() { auto* sel = SelectionSet::getSingletonPtr(); if (!sel) return nullptr; const QList ents = sel->getResolvedEntities(); return ents.isEmpty() ? nullptr : ents.first(); } } // namespace #include #include "AppStorage.h" // Holds the worker's output for the main-thread build step. struct MeshGenController::Pending { MeshGenPredictor::Result result; QString imageName; std::thread worker; ~Pending() { if (worker.joinable()) worker.join(); } }; MeshGenController* MeshGenController::s_instance = nullptr; MeshGenController::MeshGenController(QObject* parent) : QObject(parent) {} MeshGenController* MeshGenController::instance() { // Unparented process-wide singleton — matches every other PropertiesPanel // controller (IsometricSpritesController, UvUnwrapController, …). Its lifetime // is bounded by kill(), called from the MainWindow teardown, so each // MainWindow/MCPServer test (which builds a fresh MainWindow + QQmlEngine // loading PropertiesPanel.qml, then tears it down) starts and ends clean. An // earlier attempt parented this to qApp and never killed it — the surviving // singleton was then referenced by the NEXT test's engine after the previous // engine had been destroyed, crashing MainWindowTest/MCPServerTest (signal 11). if (!s_instance) s_instance = new MeshGenController(); return s_instance; } MeshGenController* MeshGenController::create(QQmlEngine* engine, QJSEngine*) { // Match the IsometricSpritesController/other-controller pattern: only pin // CppOwnership when an engine is actually provided (guards the singleton from a // stale/second engine trying to take ownership). if (engine) QQmlEngine::setObjectOwnership(instance(), QQmlEngine::CppOwnership); return instance(); } void MeshGenController::kill() { delete s_instance; s_instance = nullptr; } bool MeshGenController::available() const { // In the unit-test harness, report unavailable so PropertiesPanel.qml's // "AI: Image → 3D" section stays collapsed and never instantiates its heavy // component tree (image preview, comboboxes, buttons). MainWindowTest builds // and destroys a real MainWindow — hence a QQmlEngine loading PropertiesPanel // — dozens of times under Mesa/Xvfb; adding this branch's extra QML surface to // every construct/destruct cycle perturbed the already-fragile GL teardown into // a SIGSEGV (the same class of failure the HDR first-run defaults hit — see // HdrBundledLibrary::applyFirstRunDefaultsIfNeeded's identical org-name guard). // The feature itself is unchanged for the real app; only the test harness (which // sets this org name) skips the surface. Pure-data pieces are covered directly. if (QCoreApplication::organizationName() == QLatin1String("QtMeshEditorTests")) return false; // The TRELLIS.2 sidecar backend needs no ONNX build — a machine with its // runtime installed gets the section even on a non-ONNX build. return MeshGenPredictor::isAvailable() || Trellis2Predictor::runtimeAvailable(); } bool MeshGenController::trellis2Available() const { return Trellis2Predictor::runtimeAvailable(); } QString MeshGenController::trellis2RuntimeHint() const { return Trellis2Predictor::runtimeDescription(); } void MeshGenController::setBusy(bool b) { if (m_busy == b) return; m_busy = b; emit busyChanged(); } void MeshGenController::cancel() { if (m_busy) { m_cancel = true; SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("MeshGenController cancel")); emit statusMessage(tr("Cancelling…")); } } bool MeshGenController::modelsPresent(int quality) const { return MeshGenPredictor::modelsPresent(qualityFromInt(quality)); } void MeshGenController::downloadModels(int quality) { if (m_busy) return; if (!available()) { emit modelDownloadFinished(false); emit error(tr("Image-to-3D needs an ONNX build (rebuild with -DENABLE_ONNX).")); return; } const MeshGenPredictor::Quality q = qualityFromInt(quality); if (MeshGenPredictor::modelsPresent(q)) { emit modelDownloadFinished(true); return; } setBusy(true); emit statusMessage(tr("Downloading model…")); // Blocks on this (GUI) thread's event loop; ModelDownloader drives the shared // progress bar in the AI Settings dialog. Also fetch the bg-removal model. const QString enc = MeshGenPredictor::ensureModelBlocking(q); BackgroundRemover::ensureModelBlocking(); const bool ok = !enc.isEmpty() && MeshGenPredictor::modelsPresent(q); setBusy(false); emit statusMessage(ok ? tr("Model ready.") : tr("Model download failed (not hosted yet?).")); emit modelDownloadFinished(ok); } void MeshGenController::selectImage() { if (m_busy) return; const QString path = QFileDialog::getOpenFileName( nullptr, tr("Select an image to reconstruct in 3D"), QString(), tr("Images (*.png *.jpg *.jpeg *.bmp *.webp)"), nullptr, QFileDialog::DontUseNativeDialog); if (path.isEmpty()) return; applySelectedImage(path); } void MeshGenController::applySelectedImage(const QString& path) { m_selectedImage = path; // Build a small preview thumbnail as a data:image/png;base64 URL (same idiom // as the texture-packer previews). Fit within 220px so it's a light payload. m_previewSource.clear(); QImage img(path); if (!img.isNull()) { const QImage thumb = img.scaled(220, 220, Qt::KeepAspectRatio, Qt::SmoothTransformation); QByteArray png; QBuffer buf(&png); buf.open(QIODevice::WriteOnly); if (thumb.save(&buf, "PNG")) m_previewSource = QStringLiteral("data:image/png;base64,") + QString::fromLatin1(png.toBase64()); } SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("MeshGenController selectImage %1").arg(QFileInfo(path).fileName())); emit selectedImageChanged(); emit statusMessage(tr("Selected: %1").arg(QFileInfo(path).fileName())); // Caption the image in the BACKGROUND now, so it's ready by the time the // (slow) mesh generation finishes and the AI texture pass needs it — no // blocking the UI to caption. Shown under the thumbnail as it lands. m_caption.clear(); startCaptioning(path); } QString MeshGenController::selectedImageUrl() const { return m_selectedImage.isEmpty() ? QString() : QUrl::fromLocalFile(m_selectedImage).toString(); } void MeshGenController::clearSelectedImage() { if (m_busy || m_imageGenActive) return; m_selectedImage.clear(); m_previewSource.clear(); m_caption.clear(); m_captioning = false; // Drop any in-flight caption for the cleared image — its late result // must not repopulate the caption of a selection that no longer exists. m_captionForPath.clear(); emit selectedImageChanged(); emit captionChanged(); emit statusMessage(tr("Image cleared.")); } int MeshGenController::imageGenSizeForModel(const QString& model) { if (SDManager::isFluxKleinModelName(model)) return 1024; // SDXL checkpoints train at 1024²; SD 1.x at 512² (bigger makes SD 1.5 // duplicate the subject). Matches SDManager's own auto-detect naming. const QString lower = model.toLower(); if (lower.contains(QLatin1String("sdxl")) || lower.contains(QLatin1String("sd_xl"))) return 1024; return 512; } QString MeshGenController::normalisedSavePath(const QString& destPath) { QString dest = destPath.trimmed(); if (dest.startsWith(QLatin1String("file://"))) dest = QUrl(dest).toLocalFile(); if (!dest.isEmpty() && QFileInfo(dest).suffix().isEmpty()) dest += QStringLiteral(".png"); return dest; } QString MeshGenController::suggestedImageFileName() const { // Prefer the caption/prompt so a folder of saved images is readable; // fall back to the source file's own name. QString stem = m_caption.trimmed(); if (stem.isEmpty()) stem = QFileInfo(m_selectedImage).completeBaseName(); if (stem.isEmpty()) return QStringLiteral("generated_image.png"); // Keep it filesystem-safe and short: captions are sentences, not names. stem.replace(QRegularExpression(QStringLiteral("[^A-Za-z0-9 _-]")), QString()); stem = stem.simplified(); stem.replace(' ', '_'); if (stem.size() > 60) stem = stem.left(60); if (stem.isEmpty()) return QStringLiteral("generated_image.png"); return stem + QStringLiteral(".png"); } bool MeshGenController::saveSelectedImageAs(const QString& destPath) { SentryReporter::addBreadcrumb(QStringLiteral("ui.action"), QStringLiteral("Save source image")); if (m_selectedImage.isEmpty() || !QFileInfo::exists(m_selectedImage)) { emit error(tr("There is no source image to save.")); return false; } // QML's FileDialog hands back a file:// URL; a caller may pass a plain // path. One normaliser, shared with the interactive variant so the path // it RETURNS is the path this writes. const QString dest = normalisedSavePath(destPath); if (dest.isEmpty()) { emit error(tr("No save location chosen.")); return false; } // Saving onto the source would delete it via the overwrite below. if (QFileInfo(dest).absoluteFilePath() == QFileInfo(m_selectedImage).absoluteFilePath()) { emit statusMessage(tr("Image is already saved there.")); return true; } QDir().mkpath(QFileInfo(dest).absolutePath()); // Write through QSaveFile: it stages into a temporary and commits on // commit(), so a failure part-way leaves an EXISTING destination intact. // Removing the target first (QFile::copy refuses to overwrite) would // destroy the user's previous file if the copy then failed. QFile src(m_selectedImage); if (!src.open(QIODevice::ReadOnly)) { emit error(tr("Could not read the source image.")); return false; } QSaveFile out(dest); if (!out.open(QIODevice::WriteOnly)) { emit error(tr("Could not save the image to %1.").arg(dest)); return false; } char buf[64 * 1024]; while (!src.atEnd()) { const qint64 n = src.read(buf, sizeof(buf)); if (n < 0 || out.write(buf, n) != n) { out.cancelWriting(); // existing destination untouched emit error(tr("Could not save the image to %1.").arg(dest)); return false; } } if (!out.commit()) { emit error(tr("Could not save the image to %1.").arg(dest)); return false; } emit statusMessage(tr("Saved %1").arg(QFileInfo(dest).fileName())); return true; } QString MeshGenController::saveSelectedImageInteractive() { if (m_selectedImage.isEmpty() || !QFileInfo::exists(m_selectedImage)) { emit error(tr("There is no source image to save.")); return {}; } const QString startDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); const QString suggested = QDir(startDir.isEmpty() ? QDir::homePath() : startDir) .filePath(suggestedImageFileName()); // Same recipe as MaterialEditorQML::openFileDialog — a dialog opened // from a QQuickWidget context with a null parent and the NATIVE backend // silently fails to appear on macOS (the button looks dead). Pump the // queue, raise/activate the real window, parent to it, and use Qt's own // dialog. QApplication::processEvents(); if (QWidget* activeWin = QApplication::activeWindow()) { activeWin->raise(); activeWin->activateWindow(); } QApplication::processEvents(); const QString dest = QFileDialog::getSaveFileName( QApplication::activeWindow(), tr("Save Source Image"), suggested, tr("PNG image (*.png);;All files (*)"), nullptr, QFileDialog::DontUseNativeDialog | QFileDialog::DontUseCustomDirectoryIcons); if (dest.isEmpty()) return {}; // cancelled — not an error // Return the path actually WRITTEN: saveSelectedImageAs appends .png to a // suffix-less name, so returning the raw dialog result would hand callers // a path that does not exist. const QString written = normalisedSavePath(dest); return saveSelectedImageAs(written) ? written : QString(); } // ── Prompt-to-3D: generate the SOURCE image from text (FLUX.2-klein via // stable-diffusion.cpp) instead of importing one ───────────────────────────── bool MeshGenController::imageGenAvailable() const { #ifdef ENABLE_STABLE_DIFFUSION SDManager* sd = SDManager::instance(); if (!sd) return false; return SDWorker::detectFlux2Set(SDManager::flux2KleinDirectory()).valid() || sd->isModelLoaded() || !sd->availableModels().isEmpty(); #else return false; #endif } QString MeshGenController::imageGenModelName() const { #ifdef ENABLE_STABLE_DIFFUSION SDManager* sd = SDManager::instance(); // An EXPLICITLY LOADED model wins. Klein used to short-circuit here, so // downloading and loading another checkpoint (e.g. SDXL Base, the whole // point of offering a non-distilled option) silently still generated // with klein — the alternative was unusable unless klein was deleted. if (sd && sd->isModelLoaded() && !sd->currentModelName().isEmpty()) return sd->currentModelName(); if (SDWorker::detectFlux2Set(SDManager::flux2KleinDirectory()).valid()) return SDManager::flux2KleinModelName(); if (sd && !sd->availableModels().isEmpty()) return sd->availableModels().first(); #endif return {}; } void MeshGenController::generateSourceImage(const QString& prompt) { #ifdef ENABLE_STABLE_DIFFUSION if (m_busy || m_imageGenActive) { // The QML button flips its local busy flag BEFORE calling in — a // silent return would leave it stuck. Report the rejection. emit imageGenStatus( tr("Busy — wait for the current generation to finish."), true); return; } const QString trimmed = prompt.trimmed(); if (trimmed.isEmpty()) { emit imageGenStatus(tr("Enter a prompt first."), true); return; } SDManager* sd = SDManager::instance(); const QString model = imageGenModelName(); if (!sd || model.isEmpty()) { emit imageGenStatus( tr("No image model — download FLUX.2-klein-4B in AI Model " "Settings (or install a Stable Diffusion model)."), true); return; } // EDIT mode: an image is already selected → the prompt describes a // CHANGE to it (FLUX.2 kontext-style reference editing). Only klein can // do that; on an SD checkpoint tell the user instead of silently // generating something unrelated. m_imageGenRef.clear(); if (!m_selectedImage.isEmpty()) { if (!SDManager::isFluxKleinModelName(model)) { emit imageGenStatus( tr("Editing the loaded image needs FLUX.2-klein-4B or FLUX.3 (AI Model " "Settings) — or remove the image (🗑) to generate from " "scratch."), true); return; } m_imageGenRef = m_selectedImage; } // Fresh generation: steer toward what the 3D reconstruction wants — one // isolated subject on a plain backdrop. Edits keep the reference image's // composition, so no suffix there. // NB "full body" and "single subject" caption HUMAN figure photography // in the training data, and on a short prompt that association can beat // the subject itself: "capybara" reproducibly generated a person in // plain clothes on a grey backdrop — the suffix, rendered literally, // with the animal absent. Steering must describe the FRAMING without // implying a person. m_imageGenPrompt = m_imageGenRef.isEmpty() ? trimmed + QStringLiteral(", the entire subject fully visible, centered, " "isolated on a plain light gray background") : trimmed; // Explicit output name: SDManager's generationCompleted is GLOBAL, so a // texture generation finishing while we wait must not be mistaken for our // image — onImageGenCompleted correlates on this exact filename. m_imageGenFileName = QStringLiteral("prompt3d_%1.png") .arg(QDateTime::currentMSecsSinceEpoch()); m_imageGenActive = true; // One-time signal wiring. NB: Qt::UniqueConnection only works with // member-function-pointer connections — on a LAMBDA it silently fails to // connect at all (which is how the progress bar stayed "indeterminate" // forever: the forwarder below never fired). Guard with a flag instead. if (!m_imageGenWired) { m_imageGenWired = true; connect(sd, &SDManager::modelLoadCompleted, this, &MeshGenController::onImageGenModelLoaded); connect(sd, &SDManager::modelLoadError, this, &MeshGenController::onImageGenError); connect(sd, &SDManager::generationCompleted, this, &MeshGenController::onImageGenCompleted); connect(sd, &SDManager::generationError, this, &MeshGenController::onImageGenError); connect(sd, &SDManager::generationProgressChanged, this, [this, sd]() { if (!m_imageGenActive) return; emit imageGenProgress(sd->generationStep(), sd->generationTotalSteps()); }); } SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("prompt-to-3d image gen (%1)").arg(model)); const bool loadedIsTarget = sd->isModelLoaded() && sd->currentModelName() == model; // Native training resolution: klein and SDXL are 1024 models; SD 1.x is // 512 (and duplicates the subject when pushed higher). Running SDXL at // 512 would quarter the pixel count of the very option added for quality. m_imageGenSize = imageGenSizeForModel(model); if (loadedIsTarget) { emit imageGenStatus(m_imageGenRef.isEmpty() ? tr("Generating image…") : tr("Editing the loaded image…"), false); sd->generateImage(m_imageGenPrompt, m_imageGenSize, m_imageGenSize, m_imageGenFileName, m_imageGenRef); } else { emit imageGenStatus(tr("Loading %1…").arg(model), false); sd->loadModel(model); // continues in onImageGenModelLoaded } #else Q_UNUSED(prompt); emit imageGenStatus( tr("This build has no Stable Diffusion support."), true); #endif } void MeshGenController::onImageGenModelLoaded() { #ifdef ENABLE_STABLE_DIFFUSION if (!m_imageGenActive) return; emit imageGenStatus(m_imageGenRef.isEmpty() ? tr("Generating image…") : tr("Editing the loaded image…"), false); SDManager::instance()->generateImage(m_imageGenPrompt, m_imageGenSize, m_imageGenSize, m_imageGenFileName, m_imageGenRef); #endif } void MeshGenController::onImageGenCompleted(const QString& outputPath) { if (!m_imageGenActive) return; // someone else's SD generation // Correlate: only OUR file ends the run (a texture generation finishing // in parallel emits the same global signal with a different path). if (QFileInfo(outputPath).fileName() != m_imageGenFileName) return; m_imageGenActive = false; applySelectedImage(outputPath); emit imageGenStatus( tr("Image ready — press Generate to build the 3D model."), false); } void MeshGenController::onImageGenError(const QString& message) { if (!m_imageGenActive) return; m_imageGenActive = false; emit imageGenStatus(message, true); } void MeshGenController::startCaptioning(const QString& path) { if (!ImageCaptioner::isAvailable() || path.isEmpty()) { m_captioning = false; emit captionChanged(); return; } m_captionForPath = path; m_captioning = true; emit captionChanged(); // Detached worker: ensure the model (first-use download) + caption, then // marshal the result back to the main thread via a queued invocation. QPointer self(this); std::thread([self, path]() { QString cap; const QString model = ImageCaptioner::ensureModelBlocking(); if (!model.isEmpty()) { QImage img(path); if (!img.isNull()) cap = ImageCaptioner::caption(img); } if (!self) return; QMetaObject::invokeMethod(self, "setCaptionResult", Qt::QueuedConnection, Q_ARG(QString, cap), Q_ARG(QString, path)); }).detach(); } void MeshGenController::setCaptionResult(const QString& caption, const QString& forPath) { // Drop a stale result if the user picked a different image meanwhile. if (forPath != m_captionForPath) return; m_caption = caption; m_captioning = false; emit captionChanged(); if (!caption.isEmpty()) emit statusMessage(tr("Image described: \"%1\"").arg(caption)); } void MeshGenController::generateSelected(int resolution, bool removeBackground, int quality, const QVariantMap& options) { if (m_selectedImage.isEmpty()) { emit error(tr("Select an image first.")); return; } generate(m_selectedImage, resolution, removeBackground, quality, options); } MeshGenPredictor::Quality MeshGenController::qualityFromInt(int q) { // Dropdown/CLI index: 0 = fp32 (best), 1 = int8 (smallest). return (q == 1) ? MeshGenPredictor::Quality::Int8 : MeshGenPredictor::Quality::Fp32; } void MeshGenController::pickImageAndGenerate(int resolution, bool removeBackground, int quality, const QVariantMap& options) { if (m_busy) return; const QString path = QFileDialog::getOpenFileName( nullptr, tr("Select an image to reconstruct in 3D"), QString(), tr("Images (*.png *.jpg *.jpeg *.bmp *.webp)"), nullptr, QFileDialog::DontUseNativeDialog); if (path.isEmpty()) return; generate(path, resolution, removeBackground, quality, options); } // Ensure the matting model(s) on the main thread (the worker cannot download). // Best-effort: a failed BiRefNet fetch degrades to U²-Net. When Best is wanted, // U²-Net is cached too so a BiRefNet INFERENCE failure on the worker can still // fall back (removeBackgroundWithFallback). Stop is polled during the ~930 MB // transfer. Returns false (after resetting busy) only when the user cancelled. bool MeshGenController::ensureMatteModels(bool wantBest) { const auto cancelPoll = [this]() { return m_cancel.load(); }; BackgroundRemover::Quality ensuredQ = BackgroundRemover::Quality::Fast; BackgroundRemover::resolveModelBlocking( wantBest ? BackgroundRemover::Quality::Best : BackgroundRemover::Quality::Fast, &ensuredQ, cancelPoll); if (wantBest && !m_cancel.load()) BackgroundRemover::resolveModelBlocking(BackgroundRemover::Quality::Fast, nullptr, cancelPoll); if (m_cancel.load()) { setBusy(false); emit statusMessage(tr("Cancelled.")); return false; } return true; } void MeshGenController::generate(const QString& imagePath, int resolution, bool removeBackground, int quality, const QVariantMap& options) { if (m_busy) return; if (!available()) { emit error(tr("Image-to-3D needs an ONNX build (rebuild with -DENABLE_ONNX).")); return; } QFileInfo fi(imagePath); if (!fi.exists()) { emit error(tr("Image not found: %1").arg(imagePath)); return; } QImage image(fi.absoluteFilePath()); if (image.isNull()) { emit error(tr("Could not load image: %1").arg(imagePath)); return; } m_quality = qualityFromInt(quality); // Parse the user-selectable pipeline stages (defaults match the predictor's // — see generateSelected()'s doc). Missing keys keep the defaults. auto optBool = [&options](const char* key, bool def) { return options.contains(QLatin1String(key)) ? options.value(QLatin1String(key)).toBool() : def; }; // #1016: matting tier. BiRefNet Best is the default; only an explicit // "fast" opts out to U²-Net. Only meaningful when background removal is on. const bool wantBestMatte = options.value(QLatin1String("matting")).toString().toLower() != QLatin1String("fast"); const bool wantSmooth = optBool("smooth", true); const bool wantRefine = optBool("refine", true); const bool wantBake = optBool("bake_texture", true); const bool wantPartUv = options.value(QLatin1String("uv_layout")).toString().toLower() != QLatin1String("xatlas"); const bool wantClean = optBool("remove_degenerates", true); m_upscaleTexture = optBool("upscale_texture", false); m_generatePbr = optBool("generate_pbr", true) && wantBake; int textureSize = options.contains(QLatin1String("texture_size")) ? options.value(QLatin1String("texture_size")).toInt() : 1024; // Backend: "trellis2" (the default whenever its sidecar runtime is // installed), "triposr" (fast + textured) or "triposg" (rectified flow — // higher-fidelity geometry, geometry-only, slower). Empty/unknown values // resolve through defaultBackend(). const QString backendStr = options.value(QLatin1String("backend")).toString().toLower(); MeshGenPredictor::Backend backend = MeshGenPredictor::defaultBackend(); if (backendStr == QLatin1String("triposr")) backend = MeshGenPredictor::Backend::TripoSR; else if (backendStr == QLatin1String("triposg")) backend = MeshGenPredictor::Backend::TripoSG; else if (backendStr.startsWith(QLatin1String("trellis"))) backend = MeshGenPredictor::Backend::Trellis2; else if (backendStr.startsWith(QLatin1String("pixal"))) backend = MeshGenPredictor::Backend::Pixal3D; else if (backendStr == QLatin1String("triposplat")) backend = MeshGenPredictor::Backend::TripoSplat; const bool useSG = (backend == MeshGenPredictor::Backend::TripoSG); const bool useSplat = (backend == MeshGenPredictor::Backend::TripoSplat); // Pixal3D shares the trellis-cli runtime, so every useT2 gate applies. const bool useT2 = MeshGenPredictor::isTrellisRuntime(backend); const int flowSteps = options.contains(QLatin1String("flow_steps")) ? options.value(QLatin1String("flow_steps")).toInt() : 25; const unsigned t2Seed = options.contains(QLatin1String("seed")) ? options.value(QLatin1String("seed")).toUInt() : 42u; const QString t2Preset = options.contains(QLatin1String("preset")) ? options.value(QLatin1String("preset")).toString().toLower() : QStringLiteral("balanced"); int t2TargetTris = options.contains(QLatin1String("target_tris")) ? options.value(QLatin1String("target_tris")).toInt() : 0; bool t2TargetTrisStrict = false; int t2MaxTexture = 0; // platform cap on the FINAL images (0 = none) // A named preset (the picker's id) wins over a raw target_tris: it is // the same table the CLI/MCP resolve, and the Roblox presets carry the // platform's upload limits — a hard triangle ceiling plus a 1024 px // texture cap that is applied HERE, not only in QML, so a caller that // bypasses the picker's snap still gets a Roblox-legal bake. if (options.contains(QLatin1String("game_preset"))) { const QString id = options.value(QLatin1String("game_preset")).toString(); if (const GameReady::Preset* gp = GameReady::find(id)) { t2TargetTris = gp->targetTriangles; t2TargetTrisStrict = gp->strictTriangles; t2MaxTexture = gp->maxTextureSize; if (gp->maxTextureSize > 0 && textureSize > gp->maxTextureSize) textureSize = gp->maxTextureSize; SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("game_preset=%1 tris=%2 strict=%3 tex=%4") .arg(gp->id).arg(t2TargetTris).arg(t2TargetTrisStrict) .arg(textureSize)); } else if (!id.isEmpty()) { qWarning() << "MeshGenController: unknown game_preset" << id << "- using target_tris" << t2TargetTris; } } // Subsamples per baked texel (TRELLIS.2). 1 = one sample at the texel // centre (default); 2 = 2x2, which averages four positions and softens // the speckle a single centre sample leaves where a texel straddles a // colour boundary — at roughly 4x the bake cost. Anything else is // ignored rather than trusted, since it comes in as a loose QVariant. const int t2Supersample = options.value(QLatin1String("texture_supersample")).toInt() == 2 ? 2 : 1; GamificationManager::noteFeature(QStringLiteral("image_to_3d")); // Mark busy BEFORE ensureModelBlocking() — it spins a nested QEventLoop for the // first-use download, during which the QML button would otherwise stay enabled // and could re-enter generate(), racing over m_pending. setBusy disables it. m_cancel = false; setBusy(true); // #1016: set once the matte model has been ensured, so the generic // pre-ensure below does not repeat a failed (and slow) Best download. bool matteEnsured = false; emit progress(QStringLiteral("prep"), 0, 1); // Ensure the chosen backend's models on the MAIN thread first — // ensureModelBlocking() spins a local QEventLoop for the download, which // must not run on the worker thread. Once present, the worker only reads // the files (no event loop needed). emit statusMessage(tr("Checking model…")); if (useT2) { if (!Trellis2Predictor::runtimeAvailable()) { setBusy(false); emit error(Trellis2Predictor::runtimeDescription()); return; } // The alpha-matte model must be ensured HERE (main thread — nested // event loop); the worker-side predictor only reads it. #1016: ensure // the REQUESTED tier — pre-ensuring Fast while the predictor then asks // for Best leaves BiRefNet uncached and silently degrades the matte. // resolveModelBlocking (not ensureModelBlocking) so a failed ~930 MB // BiRefNet fetch still leaves U²-Net cached: the worker cannot download // (no nested event loop off the main thread), so without the Fast // fallback HERE it would find nothing and skip matting entirely. matteEnsured = true; if (!ensureMatteModels(wantBestMatte)) return; } else if (useSplat) { QString dlErr; emit statusMessage(tr("Checking TripoSplat models (~6 GB on first use)…")); // Forward Stop into ModelFetch's 200 ms cancel poll — without this the // ~6 GB first-use download ignores m_cancel until every graph finishes. if (!TripoSplatPredictor::ensureModelsBlocking( &dlErr, [this]() { return m_cancel.load(); })) { setBusy(false); if (m_cancel.load() || dlErr == QLatin1String("cancelled")) emit statusMessage(tr("Cancelled.")); else emit error(tr("TripoSplat models unavailable: %1").arg(dlErr)); return; } if (m_cancel.load()) { setBusy(false); emit statusMessage(tr("Cancelled.")); return; } // TripoSplat mattes on the WORKER — which can only read models already // on disk, so ensure the matte here. matteEnsured = true; if (!ensureMatteModels(wantBestMatte)) return; } else if (useSG) { // TripoSG always runs the fp32 DiT — the int8 tier is dropped // (quantized geometry degrades to blobs; no ARM speed win). const QString enc = TripoSGPredictor::ensureModelBlocking(false); if (enc.isEmpty()) { setBusy(false); emit error(tr("TripoSG models unavailable — they download on first " "use; if not hosted yet, set " "QTMESH_TRIPOSG_MODEL_BASE_URL or drop the files in " "the ai_models/triposg/ cache.")); return; } // TripoSG's colour bake queries TripoSR's image-conditioned colour // field — ensure those models too (best-effort: if unavailable the // predictor falls back to the clay look with a warning). if (wantBake) { emit statusMessage(tr("Checking colour model…")); MeshGenPredictor::ensureModelBlocking(m_quality); } } else { const QString enc = MeshGenPredictor::ensureModelBlocking(m_quality); if (enc.isEmpty() || !MeshGenPredictor::modelsPresent(m_quality)) { setBusy(false); emit error(tr("TripoSR model unavailable — it downloads on first use; if it " "is not hosted yet, set QTMESH_TRIPOSR_MODEL_BASE_URL or drop " "the files in the ai_models/triposr/ cache.")); return; } } // Skip when the TRELLIS.2 branch above already ensured it: repeating a // failed Best fetch would burn a SECOND ~930 MB / 40-minute timeout before // generation even starts. if (removeBackground && !matteEnsured && !ensureMatteModels(wantBestMatte)) return; // The optional post-bake upscale runs on the WORKER, so its model must be // ensured here on the main thread (event loop) first. Best-effort: an empty // path just skips the upscale later. m_upscaleModelPath.clear(); if (m_upscaleTexture) { emit statusMessage(tr("Checking upscale model…")); m_upscaleModelPath = AIAssistManager::instance()->ensureUpscaleModel(2); } // Shape guide: resolved HERE (main thread — scene access, and the encoder // download needs the downloader's event loop); the worker only reads it. auto guide = std::make_shared(); const QString guideSpec = options.value(QLatin1String("guide")).toString().trimmed(); const float guideStrength = options.contains(QLatin1String("guide_strength")) ? std::clamp(options.value(QLatin1String("guide_strength")).toFloat(), 0.0f, 1.0f) : 0.33f; const bool bakeOntoGuide = options.value(QLatin1String("bake_onto_guide")).toBool(); const float guideReach = options.contains(QLatin1String("guide_reach")) ? std::clamp(options.value(QLatin1String("guide_reach")).toFloat(), 0.0f, 1.0f) : 0.08f; QString guideEncoder; if (useT2 && !guideSpec.isEmpty() && guideSpec != QLatin1String("none")) { QString gerr; bool okGuide = false; if (guideSpec == QLatin1String("selected")) { Ogre::Entity* e = selectedMeshEntity(); okGuide = e ? ShapeGuideScene::fromEntity(e, *guide, &gerr) : (gerr = tr("select a mesh to use as the shape guide"), false); } else { okGuide = ShapeGuideScene::resolve(guideSpec, *guide, &gerr); } if (!okGuide) { setBusy(false); emit error(tr("Shape guide: %1").arg(gerr)); return; } if (guideStrength > 0.0f) { emit statusMessage(tr("Checking shape-guide encoder…")); guideEncoder = ShapeGuide::ensureEncoderBlocking( &gerr, [this]() { return m_cancel.load(); }); if (m_cancel.load()) { setBusy(false); emit statusMessage(tr("Cancelled.")); return; } if (guideEncoder.isEmpty()) emit statusMessage(tr("Shape-guide encoder unavailable (%1) — the guide " "will only be used for the bake.").arg(gerr)); } SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.shape_guide"), QStringLiteral("gui guide=%1 strength=%2 bake=%3 tris=%4") .arg(guideSpec.startsWith('/') ? QStringLiteral("file") : guideSpec) .arg(guideStrength).arg(bakeOntoGuide).arg(guide->indices.size() / 3)); } SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("MeshGenController generate %1 res=%2 rembg=%3 smooth=%4 " "refine=%5 bake=%6 upscale=%7 pbr=%8") .arg(fi.fileName()).arg(resolution).arg(removeBackground) .arg(wantSmooth).arg(wantRefine).arg(wantBake) .arg(m_upscaleTexture).arg(m_generatePbr)); emit statusMessage(tr("Preparing…")); delete m_pending; m_pending = new Pending(); m_pending->imageName = fi.fileName(); const int res = resolution; const bool rembg = removeBackground; // --- Worker thread: model download + background removal + inference -------- // Everything here is pure data (no Ogre). Progress is emitted via a queued // connection so the GUI thread updates the bar. m_pending->worker = std::thread([this, image, res, rembg, wantBestMatte, wantSmooth, wantRefine, wantBake, textureSize, useSG, useT2, useSplat, flowStepsSet = options.contains(QLatin1String("flow_steps")), flowSteps, backend, t2Seed, t2Preset, t2TargetTris, t2TargetTrisStrict, t2MaxTexture, t2Supersample, wantClean, wantPartUv, guide, guideStrength, bakeOntoGuide, guideReach, guideEncoder, imageStem = fi.completeBaseName()]() { auto post = [this](const QString& stage, int done, int total) { QMetaObject::invokeMethod(this, "progress", Qt::QueuedConnection, Q_ARG(QString, stage), Q_ARG(int, done), Q_ARG(int, total)); }; // Models are guaranteed present (ensured on the main thread before this // worker started), so this thread only reads files — no event loop // needed. (The encode stage is reported by the predictor itself.) QImage subject = image; if (rembg && !useSG && !useT2 && !useSplat) { // TripoSR path: composite over gray-128 (its training background). // TripoSG / TripoSplat / TRELLIS.2 leave removal to the predictor // dispatch (white / keep-alpha-over-black / keep-alpha respectively). post(QStringLiteral("background"), 0, 1); QMetaObject::invokeMethod(this, "statusMessage", Qt::QueuedConnection, Q_ARG(QString, tr("Removing background…"))); // #1016: pick the model for the requested tier, degrading to Fast // when Best is not cached. Passing a Best tier with a Fast model // feeds a 1024² tensor into U²-Net's fixed 320² graph — inference // throws and the matte is silently lost. BackgroundRemover::Quality mq = wantBestMatte ? BackgroundRemover::Quality::Best : BackgroundRemover::Quality::Fast; if (!BackgroundRemover::modelPresent(mq)) mq = BackgroundRemover::Quality::Fast; BackgroundRemover::Options bgo; bgo.quality = mq; const QString bgModel = BackgroundRemover::modelPath(mq); const auto br = BackgroundRemover::removeBackgroundWithFallback(image, bgModel, bgo); subject = br.image; post(QStringLiteral("background"), 1, 1); } MeshGenPredictor::Options opts; opts.sdfResolution = res; opts.vertexColor = true; // TripoSR removal already ran above; TripoSG (white), TripoSplat // (keep-alpha → black via prepareImage) and TRELLIS.2 (keep-alpha) // matte inside their predictor dispatch. TRELLIS.2 ALWAYS gets the // matte (the CLI forces it too): its preprocess needs an alpha // channel to keep the non-commercial rembg model unused. The GUI // checkbox governs TripoSR / TripoSG / TripoSplat. opts.removeBackground = useT2 || (rembg && (useSG || useSplat)); opts.mattingQuality = wantBestMatte ? BackgroundRemover::Quality::Best : BackgroundRemover::Quality::Fast; opts.smoothMesh = wantSmooth; opts.refineSurface = wantRefine; opts.bakeTexture = wantBake; opts.partUvIslands = wantPartUv; opts.removeDegenerates = wantClean; opts.textureSize = textureSize; opts.backend = backend; opts.flowSteps = flowSteps; opts.quality = m_quality; // Game-ready simplification target — ALL backends (TripoSR/TripoSG // run the weld/debris/simplify + detail-normal-bake pass in the // predictor; TRELLIS.2 does it natively in its own pipeline). opts.targetTriangles = t2TargetTris; opts.targetTrianglesStrict = t2TargetTrisStrict; opts.maxTextureSize = t2MaxTexture; opts.bakeNormalMap = m_generatePbr; if (useSplat) { // TripoSplat defaults (TripoSplatPredictor::kDefault*): a 150k // game-ready target unless the picker set one, and the raw // Gaussians kept as a .ply. The splat→mesh grid resolution comes // from the shared Resolution picker (GUI) / --resolution (CLI) — // same control as TripoSR/TripoSG; 1024 is the recommended default // (matches splat spacing) and is what the QML combo snaps to when // this backend is selected. if (opts.targetTriangles <= 0) opts.targetTriangles = TripoSplatPredictor::kDefaultTargetTriangles; opts.seed = t2Seed; if (flowStepsSet) opts.splatFlowSteps = std::clamp(flowSteps, 1, 100); const QString dir = QDir(AppStorage::persistentRoot()) .filePath(QStringLiteral("generated_splats")); QDir().mkpath(dir); // Unique per run (each costs ~25 min): a re-run with another seed, // or a same-named photo from another folder, must not replace it. opts.splatPlyPath = QDir(dir).filePath( QStringLiteral("%1_%2_splats.ply") .arg(imageStem) .arg(QDateTime::currentMSecsSinceEpoch())); } if (useT2) { opts.seed = t2Seed; opts.trellis2Preset = t2Preset; opts.textureSupersample = t2Supersample; // Phase 9: keep the raw full-res generation in AppData so // textures/LODs can be re-baked without re-running inference. opts.trellis2SourceKeepDir = QDir(AppStorage::persistentRoot()) .filePath(QStringLiteral("generated_sources")); opts.trellis2SourceKeepBaseName = imageStem; if (!guide->empty()) { opts.guidePositions = guide->positions; opts.guideIndices = guide->indices; opts.guideStrength = guideStrength; opts.bakeOntoGuide = bakeOntoGuide; opts.guideRayReach = guideReach; opts.guideEncoderPath = guideEncoder; } } QMetaObject::invokeMethod(this, "statusMessage", Qt::QueuedConnection, Q_ARG(QString, tr("Reconstructing…"))); // Map the predictor's typed stages onto the string stages the QML // step list keys on. total <= 0 = pure cancellation check (no post). auto progressFn = [this, &post](MeshGenPredictor::Stage st, int done, int total) -> bool { if (total > 0) { const char* name = nullptr; switch (st) { case MeshGenPredictor::Stage::Encode: name = "encode"; break; case MeshGenPredictor::Stage::Denoise: name = "denoise"; break; case MeshGenPredictor::Stage::Decode: name = "decode"; break; case MeshGenPredictor::Stage::Refine: name = "refine"; break; case MeshGenPredictor::Stage::Bake: name = "bake"; break; case MeshGenPredictor::Stage::Color: name = "color"; break; } if (name) post(QString::fromLatin1(name), done, total); } return !m_cancel.load(); }; MeshGenPredictor::Result r = MeshGenPredictor::predict( subject, MeshGenPredictor::encoderModelPath(m_quality), MeshGenPredictor::decoderModelPath(), opts, progressFn); // Optional Real-ESRGAN 2x on the baked diffuse — pure CPU, so it stays // on this worker. Model was ensured on the main thread; best-effort. // A platform preset caps the FINAL image (Roblox: 1024 px): a 2x // that would cross it is skipped, and the result says so. // Without a preset the 8192 px bake ceiling still applies: a 2x of // an 8192 bake would be a 16384² image (1 GB of RGBA) that most GPUs // cannot even sample. constexpr int kMaxTexturePx = 8192; const int upscaleCap = t2MaxTexture > 0 ? t2MaxTexture : kMaxTexturePx; const bool upscaleBlockedByCap = !r.texture.isNull() && (r.texture.width() * 2 > upscaleCap || r.texture.height() * 2 > upscaleCap); if (r.ok && m_upscaleTexture && upscaleBlockedByCap) { if (!r.warning.isEmpty()) r.warning += QStringLiteral(" "); r.warning += t2MaxTexture > 0 ? tr("Upscale skipped: the preset caps textures at %1 px.") .arg(t2MaxTexture) : tr("Upscale skipped: the texture is already at the %1 px maximum.") .arg(kMaxTexturePx); } if (r.ok && m_upscaleTexture && !upscaleBlockedByCap && !m_upscaleModelPath.isEmpty() && !r.uvs.empty() && !r.texture.isNull() && !m_cancel.load()) { QMetaObject::invokeMethod(this, "statusMessage", Qt::QueuedConnection, Q_ARG(QString, tr("Upscaling texture…"))); const TextureUpscaler::Result ur = TextureUpscaler::upscale( r.texture, m_upscaleModelPath, {}, [this, &post](int done, int total) { post(QStringLiteral("upscale"), done, total); return !m_cancel.load(); }); if (ur.ok && !ur.image.isNull()) r.texture = ur.image; } m_pending->result = std::move(r); // Hand back to the main thread to build/attach the Ogre mesh. QMetaObject::invokeMethod(this, "buildOnMainThread", Qt::QueuedConnection); }); } void MeshGenController::buildOnMainThread() { // Runs on the GUI/main thread (queued from the worker). Join the worker first. if (m_pending && m_pending->worker.joinable()) m_pending->worker.join(); auto finish = [this]() { setBusy(false); m_cancel = false; }; if (!m_pending) { finish(); return; } const MeshGenPredictor::Result& r = m_pending->result; if (!r.ok) { finish(); emit error(r.error.isEmpty() ? tr("Image-to-3D failed.") : r.error); return; } emit progress(QStringLiteral("build"), 0, 1); emit statusMessage(m_generatePbr && !r.uvs.empty() ? tr("Building mesh + PBR maps…") : tr("Building mesh…")); // Give the QML a chance to repaint the step list before buildSceneNode // blocks this (main) thread on mesh construction + PBR synthesis. QCoreApplication::processEvents(); // PBR synthesis (when enabled) runs inside buildSceneNode on this (main) // thread — same as the Material Editor's button; the PBRify models are // small and download on first use via the main-thread event loop. MeshGenBuilder::BuildOptions buildOpts; buildOpts.generatePbrMaps = m_generatePbr; Ogre::SceneNode* node = MeshGenBuilder::buildSceneNode(r, QStringLiteral("qtmesh_gen3d"), buildOpts); emit progress(QStringLiteral("build"), 1, 1); finish(); if (!node) { emit error(tr("Failed to build mesh from prediction.")); return; } QVariantMap out{ {"ok", true}, {"vertexCount", r.vertexCount}, {"triangleCount", r.triangleCount}, }; // Expose the built entity's name so QML can run a follow-up AI texture bake // on it (the "Generate texture (AI)" option for the geometry-only TripoSG // backend). The node has exactly one attached Entity. if (node->numAttachedObjects() > 0) { if (auto* obj = node->getAttachedObject(0)) out["entityName"] = QString::fromStdString(obj->getName()); } if (!r.warning.isEmpty()) out["warning"] = r.warning; if (!r.guideNote.isEmpty()) out["guideNote"] = r.guideNote; if (!r.splatPlyPath.isEmpty()) out["splatPath"] = r.splatPlyPath; if (r.uvCharts > 0) { out["uvCharts"] = r.uvCharts; SentryReporter::addBreadcrumb(QStringLiteral("ai.assist.image_to_3d"), QStringLiteral("uv layout %1, %2 islands") .arg(r.partUvIslands ? QStringLiteral("parts") : QStringLiteral("xatlas")) .arg(r.uvCharts)); } emit statusMessage(!r.warning.isEmpty() ? tr("Generated %1 verts, %2 tris (%3)") .arg(r.vertexCount).arg(r.triangleCount).arg(r.warning) : r.uvs.empty() ? tr("Generated %1 verts, %2 tris") .arg(r.vertexCount).arg(r.triangleCount) : tr("Generated %1 verts, %2 tris + %3px texture") .arg(r.vertexCount).arg(r.triangleCount) .arg(r.texture.width())); emit completed(out); } // ── Game-ready presets (GameReadyPresets.h) ───────────────────────────────── QVariantList MeshGenController::gameReadyPresets() const { QVariantList out; for (const GameReady::Preset& p : GameReady::presets()) { QVariantMap m; m.insert(QStringLiteral("id"), p.id); m.insert(QStringLiteral("label"), p.label); m.insert(QStringLiteral("tris"), p.targetTriangles); m.insert(QStringLiteral("strict"), p.strictTriangles); m.insert(QStringLiteral("maxTexture"), p.maxTextureSize); m.insert(QStringLiteral("note"), p.note); out.append(m); } return out; } int MeshGenController::gameReadyDefaultIndex() const { const int i = GameReady::indexOf(GameReady::defaultId()); return i < 0 ? 0 : i; } QVariantList MeshGenController::guidePresets() const { QVariantList out; for (const ShapeGuide::PresetInfo& p : ShapeGuide::presets()) out.append(QVariantMap{{QStringLiteral("id"), p.id}, {QStringLiteral("label"), p.label}, {QStringLiteral("category"), p.category}, {QStringLiteral("hint"), p.hint}}); return out; } QString MeshGenController::insertGuidePreset(const QString& id) { if (!ShapeGuide::isPreset(id)) return {}; Ogre::SceneNode* node = ShapeGuideScene::insert(ShapeGuide::preset(id), QStringLiteral("blockout_") + id); if (!node) return {}; SentryReporter::addBreadcrumb(QStringLiteral("ui.action"), QStringLiteral("insert shape guide %1").arg(id)); if (auto* sel = SelectionSet::getSingleton()) { sel->clear(); sel->append(node); } return QString::fromStdString(node->getName()); } double MeshGenController::bakeOnGuideMinStrength() const { return ShapeGuide::kBakeOnGuideMinStrength; } QString MeshGenController::selectedMeshName() const { Ogre::Entity* e = selectedMeshEntity(); return e ? QString::fromStdString(e->getName()) : QString(); } QString MeshGenController::pickGuideFile() { return QFileDialog::getOpenFileName( nullptr, tr("Choose a shape-guide mesh"), QString(), tr("Meshes (*.obj *.glb *.gltf *.fbx *.dae *.mesh *.ply *.stl);;All files (*)")); }