A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add ThumbnailGenerator for asset browser
esengine2087e0ca1ef0
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txtindex 77806ad19..84610baa5 100644--- a/src/esengine/editor/CMakeLists.txt+++ b/src/esengine/editor/CMakeLists.txt@@ -4,6 +4,7 @@ set(EDITOR_SOURCESmain.cppEditorApplication.cppAssetDatabase.cpp+ ThumbnailGenerator.cpp# Property editorsproperty/PropertyEditor.cpp@@ -27,6 +28,7 @@ set(EDITOR_SOURCESset(EDITOR_HEADERSEditorApplication.hppAssetDatabase.hpp+ ThumbnailGenerator.hpp# Core subsystemcore/EditorEvents.hppdiff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cppindex 3e971be63..4ce3b2f86 100644--- a/src/esengine/editor/EditorApplication.cpp+++ b/src/esengine/editor/EditorApplication.cpp@@ -97,7 +97,10 @@ void EditorApplication::onInit() {}});- // Initialize asset database+ // Initialize asset database with thumbnail generation callback+ assetDatabase_.setOnAssetAdded([this](const AssetMetadata& asset) {+ thumbnailGenerator_.generateThumbnail(asset.guid, asset.path, asset.type);+ });assetDatabase_.setProjectPath("assets");assetDatabase_.scan();@@ -316,7 +319,7 @@ void EditorApplication::setupEditorLayout() {ES_LOG_INFO("setupEditorLayout: Creating AssetBrowserPanel...");// Create Asset Browser panel (bottom)- auto assetBrowserPanel = makeUnique<AssetBrowserPanel>(assetDatabase_);+ auto assetBrowserPanel = makeUnique<AssetBrowserPanel>(assetDatabase_, thumbnailGenerator_);assetBrowserPanel->setMinSize(glm::vec2(300.0f, 200.0f));ES_LOG_INFO("setupEditorLayout: Adding AssetBrowserPanel to dock...");dockArea_->addPanel(std::move(assetBrowserPanel), ui::DockDropZone::Bottom, nullptr, 0.25f);diff --git a/src/esengine/editor/EditorApplication.hpp b/src/esengine/editor/EditorApplication.hppindex 50df2a48c..c74f75652 100644--- a/src/esengine/editor/EditorApplication.hpp+++ b/src/esengine/editor/EditorApplication.hpp@@ -25,6 +25,7 @@#include "command/CommandHistory.hpp"#include "core/Selection.hpp"#include "AssetDatabase.hpp"+#include "ThumbnailGenerator.hpp"namespace esengine {namespace editor {@@ -105,6 +106,12 @@ public:*/AssetDatabase& getAssetDatabase() { return assetDatabase_; }+ /**+ * @brief Get the thumbnail generator+ * @return Reference to the thumbnail generator+ */+ ThumbnailGenerator& getThumbnailGenerator() { return thumbnailGenerator_; }+protected:// =========================================================================// Application Lifecycle Overrides@@ -199,6 +206,7 @@ private:CommandHistory commandHistory_; ///< Undo/redo historyEntitySelection selection_; ///< Entity selection managerAssetDatabase assetDatabase_; ///< Asset database+ ThumbnailGenerator thumbnailGenerator_; ///< Thumbnail generatorUnique<ui::UIContext> uiContext_; ///< UI system contextui::DockArea* dockArea_ = nullptr; ///< Main docking area (owned by UIContext)diff --git a/src/esengine/editor/ThumbnailGenerator.cpp b/src/esengine/editor/ThumbnailGenerator.cppnew file mode 100644index 000000000..b9407fa43--- /dev/null+++ b/src/esengine/editor/ThumbnailGenerator.cpp@@ -0,0 +1,133 @@+#include "ThumbnailGenerator.hpp"+#include "panels/AssetBrowserTypes.hpp"+#include "../core/Log.hpp"++#include <stb_image.h>++#include <algorithm>+#include <cstring>++namespace esengine::editor {++namespace {++std::vector<u8> resizeImage(const u8* src, u32 srcWidth, u32 srcHeight, u32 dstWidth, u32 dstHeight, u32 channels) {+ std::vector<u8> dst(dstWidth * dstHeight * channels);++ for (u32 y = 0; y < dstHeight; ++y) {+ for (u32 x = 0; x < dstWidth; ++x) {+ u32 srcX = x * srcWidth / dstWidth;+ u32 srcY = y * srcHeight / dstHeight;++ srcX = std::min(srcX, srcWidth - 1);+ srcY = std::min(srcY, srcHeight - 1);++ for (u32 c = 0; c < channels; ++c) {+ dst[(y * dstWidth + x) * channels + c] =+ src[(srcY * srcWidth + srcX) * channels + c];+ }+ }+ }++ return dst;+}++glm::vec4 getColorForType(AssetType type) {+ switch (type) {+ case AssetType::Folder: return {0.9f, 0.8f, 0.3f, 1.0f};+ case AssetType::Texture: return {0.3f, 0.7f, 0.9f, 1.0f};+ case AssetType::Audio: return {0.9f, 0.5f, 0.3f, 1.0f};+ case AssetType::Script: return {0.5f, 0.9f, 0.5f, 1.0f};+ case AssetType::Shader: return {0.8f, 0.5f, 0.9f, 1.0f};+ case AssetType::Scene: return {0.3f, 0.9f, 0.9f, 1.0f};+ case AssetType::Prefab: return {0.9f, 0.3f, 0.6f, 1.0f};+ case AssetType::Font: return {0.7f, 0.7f, 0.7f, 1.0f};+ default: return {0.5f, 0.5f, 0.5f, 1.0f};+ }+}++} // namespace++ThumbnailGenerator::ThumbnailGenerator() = default;++ThumbnailGenerator::~ThumbnailGenerator() = default;++void ThumbnailGenerator::generateThumbnail(const std::string& guid, const std::string& path, AssetType type) {+ if (hasThumbnail(guid)) {+ return;+ }++ if (type == AssetType::Texture) {+ generateTextureThumbnail(guid, path);+ } else {+ generatePlaceholder(guid, type);+ }+}++bool ThumbnailGenerator::hasThumbnail(const std::string& guid) const {+ return thumbnails_.find(guid) != thumbnails_.end();+}++Texture* ThumbnailGenerator::getThumbnail(const std::string& guid) const {+ auto it = thumbnails_.find(guid);+ if (it != thumbnails_.end()) {+ return it->second.get();+ }+ return nullptr;+}++void ThumbnailGenerator::clear() {+ thumbnails_.clear();+}++void ThumbnailGenerator::removeThumbnail(const std::string& guid) {+ thumbnails_.erase(guid);+}++void ThumbnailGenerator::generateTextureThumbnail(const std::string& guid, const std::string& path) {+ int width, height, channels;+ stbi_set_flip_vertically_on_load(false);+ u8* data = stbi_load(path.c_str(), &width, &height, &channels, 4);++ if (!data) {+ ES_LOG_WARN("Failed to load image for thumbnail: {}", path);+ generatePlaceholder(guid, AssetType::Texture);+ return;+ }++ std::vector<u8> resized = resizeImage(data, static_cast<u32>(width), static_cast<u32>(height),+ THUMBNAIL_SIZE, THUMBNAIL_SIZE, 4);+ stbi_image_free(data);++ auto texture = Texture::create(THUMBNAIL_SIZE, THUMBNAIL_SIZE, resized, TextureFormat::RGBA8);+ if (texture) {+ thumbnails_[guid] = std::move(texture);+ }+}++void ThumbnailGenerator::generatePlaceholder(const std::string& guid, AssetType type) {+ glm::vec4 color = getColorForType(type);++ std::vector<u8> pixels(THUMBNAIL_SIZE * THUMBNAIL_SIZE * 4);+ u8 r = static_cast<u8>(color.r * 255.0f);+ u8 g = static_cast<u8>(color.g * 255.0f);+ u8 b = static_cast<u8>(color.b * 255.0f);+ u8 a = static_cast<u8>(color.a * 255.0f);++ for (u32 y = 0; y < THUMBNAIL_SIZE; ++y) {+ for (u32 x = 0; x < THUMBNAIL_SIZE; ++x) {+ usize idx = (y * THUMBNAIL_SIZE + x) * 4;+ pixels[idx + 0] = r;+ pixels[idx + 1] = g;+ pixels[idx + 2] = b;+ pixels[idx + 3] = a;+ }+ }++ auto texture = Texture::create(THUMBNAIL_SIZE, THUMBNAIL_SIZE, pixels, TextureFormat::RGBA8);+ if (texture) {+ thumbnails_[guid] = std::move(texture);+ }+}++} // namespace esengine::editordiff --git a/src/esengine/editor/ThumbnailGenerator.hpp b/src/esengine/editor/ThumbnailGenerator.hppnew file mode 100644index 000000000..60b297673--- /dev/null+++ b/src/esengine/editor/ThumbnailGenerator.hpp@@ -0,0 +1,35 @@+#pragma once++#include "../core/Types.hpp"+#include "../renderer/Texture.hpp"++#include <string>+#include <unordered_map>++namespace esengine::editor {++enum class AssetType : u8;++class ThumbnailGenerator {+public:+ static constexpr u32 THUMBNAIL_SIZE = 64;++ ThumbnailGenerator();+ ~ThumbnailGenerator();++ void generateThumbnail(const std::string& guid, const std::string& path, AssetType type);++ bool hasThumbnail(const std::string& guid) const;+ Texture* getThumbnail(const std::string& guid) const;++ void clear();+ void removeThumbnail(const std::string& guid);++private:+ void generateTextureThumbnail(const std::string& guid, const std::string& path);+ void generatePlaceholder(const std::string& guid, AssetType type);++ std::unordered_map<std::string, Unique<Texture>> thumbnails_;+};++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/AssetBrowserPanel.cpp b/src/esengine/editor/panels/AssetBrowserPanel.cppindex 2926470ba..d2b1c813f 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.cpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.cpp@@ -1,6 +1,7 @@#include "AssetBrowserPanel.hpp"#include "AssetGridItem.hpp"#include "../AssetDatabase.hpp"+#include "../ThumbnailGenerator.hpp"#include "../../ui/UIContext.hpp"#include "../../ui/layout/StackLayout.hpp"#include "../../ui/widgets/Label.hpp"@@ -13,9 +14,10 @@namespace esengine::editor {-AssetBrowserPanel::AssetBrowserPanel(AssetDatabase& assetDB)+AssetBrowserPanel::AssetBrowserPanel(AssetDatabase& assetDB, ThumbnailGenerator& thumbnailGen): DockPanel(ui::WidgetId("asset_browser_panel"), "Assets"),- assetDB_(assetDB) {+ assetDB_(assetDB),+ thumbnailGen_(thumbnailGen) {setPanelType("AssetBrowser");setClosable(true);diff --git a/src/esengine/editor/panels/AssetBrowserPanel.hpp b/src/esengine/editor/panels/AssetBrowserPanel.hppindex 221d35d89..1cf688e6e 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.hpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.hpp@@ -14,10 +14,11 @@namespace esengine::editor {class AssetDatabase;+class ThumbnailGenerator;class AssetBrowserPanel : public ui::DockPanel {public:- explicit AssetBrowserPanel(AssetDatabase& assetDB);+ AssetBrowserPanel(AssetDatabase& assetDB, ThumbnailGenerator& thumbnailGen);~AssetBrowserPanel() override;void refresh();@@ -68,6 +69,7 @@ private:bool needsRebuild_ = false;AssetDatabase& assetDB_;+ ThumbnailGenerator& thumbnailGen_;Connection folderSelectedConnection_;Connection searchChangedConnection_;