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
refactor: Migrate editor shaders to use ResourceManager
esengine03722a2102ca
diff --git a/src/esengine/editor/gizmo/TransformGizmo.cpp b/src/esengine/editor/gizmo/TransformGizmo.cppindex 3b7100488..cf4fdcb6c 100644--- a/src/esengine/editor/gizmo/TransformGizmo.cpp+++ b/src/esengine/editor/gizmo/TransformGizmo.cpp@@ -23,31 +23,6 @@ constexpr f32 AXIS_LENGTH = 1.0f;constexpr f32 ARROW_HEAD_SIZE = 0.15f;constexpr f32 HIT_THRESHOLD = 0.1f;-const char* GIZMO_VERTEX_SHADER = R"(- attribute vec3 a_position;- attribute vec4 a_color;-- uniform mat4 u_viewProj;- uniform mat4 u_model;-- varying vec4 v_color;-- void main() {- gl_Position = u_viewProj * u_model * vec4(a_position, 1.0);- v_color = a_color;- }-)";--const char* GIZMO_FRAGMENT_SHADER = R"(- precision mediump float;-- varying vec4 v_color;-- void main() {- gl_FragColor = v_color;- }-)";-glm::vec4 getAxisColor(GizmoAxis axis, GizmoAxis activeAxis, GizmoAxis hoveredAxis) {bool isActive = (axis == activeAxis);bool isHovered = (axis == hoveredAxis);@@ -180,6 +155,13 @@ void addCircle(std::vector<f32>& vertices, const glm::vec3& center, const glm::v} // namespace+// =============================================================================+// Constructor+// =============================================================================++TransformGizmo::TransformGizmo(resource::ResourceManager& resourceManager)+ : resourceManager_(resourceManager) {}+// =============================================================================// Initialization// =============================================================================@@ -187,7 +169,7 @@ void addCircle(std::vector<f32>& vertices, const glm::vec3& center, const glm::vvoid TransformGizmo::initRenderData() {if (initialized_) return;- shader_ = Shader::create(GIZMO_VERTEX_SHADER, GIZMO_FRAGMENT_SHADER);+ shaderHandle_ = resourceManager_.loadEngineShader("gizmo");std::vector<f32> translateVerts;buildTranslateGeometry(translateVerts);@@ -318,55 +300,58 @@ void TransformGizmo::render(const glm::mat4& view, const glm::mat4& proj,}void TransformGizmo::renderTranslateGizmo(const glm::mat4& viewProj, const glm::mat4& model) {- if (!translateVAO_ || !shader_) return;+ Shader* shader = resourceManager_.getShader(shaderHandle_);+ if (!translateVAO_ || !shader) return;glDisable(GL_DEPTH_TEST);- shader_->bind();- shader_->setUniform("u_viewProj", viewProj);- shader_->setUniform("u_model", model);+ shader->bind();+ shader->setUniform("u_viewProj", viewProj);+ shader->setUniform("u_model", model);translateVAO_->bind();glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(translateVertexCount_));translateVAO_->unbind();- shader_->unbind();+ shader->unbind();glEnable(GL_DEPTH_TEST);}void TransformGizmo::renderRotateGizmo(const glm::mat4& viewProj, const glm::mat4& model) {- if (!rotateVAO_ || !shader_) return;+ Shader* shader = resourceManager_.getShader(shaderHandle_);+ if (!rotateVAO_ || !shader) return;glDisable(GL_DEPTH_TEST);- shader_->bind();- shader_->setUniform("u_viewProj", viewProj);- shader_->setUniform("u_model", model);+ shader->bind();+ shader->setUniform("u_viewProj", viewProj);+ shader->setUniform("u_model", model);rotateVAO_->bind();glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(rotateVertexCount_));rotateVAO_->unbind();- shader_->unbind();+ shader->unbind();glEnable(GL_DEPTH_TEST);}void TransformGizmo::renderScaleGizmo(const glm::mat4& viewProj, const glm::mat4& model) {- if (!scaleVAO_ || !shader_) return;+ Shader* shader = resourceManager_.getShader(shaderHandle_);+ if (!scaleVAO_ || !shader) return;glDisable(GL_DEPTH_TEST);- shader_->bind();- shader_->setUniform("u_viewProj", viewProj);- shader_->setUniform("u_model", model);+ shader->bind();+ shader->setUniform("u_viewProj", viewProj);+ shader->setUniform("u_model", model);scaleVAO_->bind();glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(scaleVertexCount_));scaleVAO_->unbind();- shader_->unbind();+ shader->unbind();glEnable(GL_DEPTH_TEST);}diff --git a/src/esengine/editor/gizmo/TransformGizmo.hpp b/src/esengine/editor/gizmo/TransformGizmo.hppindex 0dcf31c34..cef4245fb 100644--- a/src/esengine/editor/gizmo/TransformGizmo.hpp+++ b/src/esengine/editor/gizmo/TransformGizmo.hpp@@ -20,7 +20,7 @@#include "../../math/Math.hpp"#include "../../ecs/Registry.hpp"#include "../../renderer/Buffer.hpp"-#include "../../renderer/Shader.hpp"+#include "../../resource/ResourceManager.hpp"namespace esengine::editor {@@ -81,7 +81,11 @@ enum class GizmoAxis : u8 {*/class TransformGizmo {public:- TransformGizmo() = default;+ /**+ * @brief Constructs transform gizmo with resource manager+ * @param resourceManager Resource manager for shader loading+ */+ explicit TransformGizmo(resource::ResourceManager& resourceManager);~TransformGizmo() = default;// =========================================================================@@ -205,10 +209,11 @@ private:glm::vec3 dragStartPoint_{0.0f};glm::vec3 gizmoPosition_{0.0f};+ resource::ResourceManager& resourceManager_;Unique<VertexArray> translateVAO_;Unique<VertexArray> rotateVAO_;Unique<VertexArray> scaleVAO_;- Unique<Shader> shader_;+ resource::ShaderHandle shaderHandle_;u32 translateVertexCount_ = 0;u32 rotateVertexCount_ = 0;u32 scaleVertexCount_ = 0;diff --git a/src/esengine/editor/panels/AssetBrowserPanel.cpp b/src/esengine/editor/panels/AssetBrowserPanel.cppindex 099b90ca6..ef0f68ef6 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.cpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.cpp@@ -20,6 +20,7 @@#include "../../ui/widgets/Button.hpp"#include "../../platform/FileSystem.hpp"#include "../../platform/FileDialog.hpp"+#include "../../platform/PathResolver.hpp"#include "../../core/Log.hpp"#include "../../events/Sink.hpp"#include "../../ui/icons/Icons.hpp"@@ -52,7 +53,9 @@ AssetBrowserPanel::AssetBrowserPanel(AssetDatabase& assetDB, ThumbnailGenerator&}currentPath_ = rootPath_;+ engineResourcesPath_ = PathResolver::editorPath("data");ES_LOG_INFO("AssetBrowserPanel: rootPath = {}", rootPath_);+ ES_LOG_INFO("AssetBrowserPanel: engineResourcesPath = {}", engineResourcesPath_);ES_LOG_INFO("AssetBrowserPanel: directoryExists = {}", FileSystem::directoryExists(rootPath_));buildUI();@@ -341,23 +344,33 @@ void AssetBrowserPanel::rebuildFolderTree() {folderTree_->clear();nodeToPath_.clear();pathToNode_.clear();+ engineNodes_.clear();if (!FileSystem::directoryExists(rootPath_)) {ES_LOG_WARN("Asset root path does not exist: {}", rootPath_);return;}- ui::TreeNodeId rootNodeId = folderTree_->addNode(ui::INVALID_TREE_NODE, "assets");- nodeToPath_[rootNodeId] = rootPath_;- pathToNode_[rootPath_] = rootNodeId;+ ui::TreeNodeId projectNodeId = folderTree_->addNode(ui::INVALID_TREE_NODE, "Project");+ nodeToPath_[projectNodeId] = rootPath_;+ pathToNode_[rootPath_] = projectNodeId;- addFolderNodes(rootPath_, rootNodeId);+ addFolderNodes(rootPath_, projectNodeId);- folderTree_->setNodeExpanded(rootNodeId, true);- folderTree_->selectNode(rootNodeId);+ folderTree_->setNodeExpanded(projectNodeId, true);+ folderTree_->selectNode(projectNodeId);++ if (FileSystem::directoryExists(engineResourcesPath_)) {+ ui::TreeNodeId engineNodeId = folderTree_->addNode(ui::INVALID_TREE_NODE, "Engine");+ nodeToPath_[engineNodeId] = engineResourcesPath_;+ pathToNode_[engineResourcesPath_] = engineNodeId;+ engineNodes_.insert(engineNodeId);++ addFolderNodes(engineResourcesPath_, engineNodeId, true);+ }}-void AssetBrowserPanel::addFolderNodes(const std::string& path, ui::TreeNodeId parentNode) {+void AssetBrowserPanel::addFolderNodes(const std::string& path, ui::TreeNodeId parentNode, bool isEngine) {#ifdef ES_PLATFORM_WEBreturn;#endif@@ -374,7 +387,11 @@ void AssetBrowserPanel::addFolderNodes(const std::string& path, ui::TreeNodeId pnodeToPath_[nodeId] = entry;pathToNode_[entry] = nodeId;- addFolderNodes(entry, nodeId);+ if (isEngine) {+ engineNodes_.insert(nodeId);+ }++ addFolderNodes(entry, nodeId, isEngine);}}}@@ -454,6 +471,9 @@ void AssetBrowserPanel::refreshAssetList() {if (statusLabel_) {usize count = currentAssets_.size();std::string text = std::to_string(count) + (count == 1 ? " item" : " items");+ if (browsingEngineResources_) {+ text += " (Read-only)";+ }statusLabel_->setText(text);}}@@ -467,6 +487,7 @@ void AssetBrowserPanel::onFolderSelected(ui::TreeNodeId nodeId) {if (it != nodeToPath_.end()) {if (currentPath_ != it->second) {currentPath_ = it->second;+ browsingEngineResources_ = (engineNodes_.find(nodeId) != engineNodes_.end());refreshAssetList();rebuildBreadcrumb();}@@ -509,16 +530,18 @@ bool AssetBrowserPanel::onMouseDown(const ui::MouseButtonEvent& event) {if (assetScrollView_ && assetScrollView_->getBounds().contains(event.x, event.y)) {contextMenu_->clearItems();- contextMenu_->addItem(ui::MenuItem::action("create_folder", "New Folder", ui::icons::FolderPlus));- contextMenu_->addItem(ui::MenuItem::divider());- contextMenu_->addItem(ui::MenuItem::action("create_scene", "New Scene", ui::icons::Layers));- contextMenu_->addItem(ui::MenuItem::action("create_script", "New Script", ui::icons::File));- contextMenu_->addItem(ui::MenuItem::divider());-- if (!selectedAssetPath_.empty()) {- contextMenu_->addItem(ui::MenuItem::action("rename", "Rename", ui::icons::Edit2, "F2"));- contextMenu_->addItem(ui::MenuItem::action("delete", "Delete", ui::icons::Trash2, "Del"));+ if (!browsingEngineResources_) {+ contextMenu_->addItem(ui::MenuItem::action("create_folder", "New Folder", ui::icons::FolderPlus));+ contextMenu_->addItem(ui::MenuItem::divider());+ contextMenu_->addItem(ui::MenuItem::action("create_scene", "New Scene", ui::icons::Layers));+ contextMenu_->addItem(ui::MenuItem::action("create_script", "New Script", ui::icons::File));contextMenu_->addItem(ui::MenuItem::divider());++ if (!selectedAssetPath_.empty()) {+ contextMenu_->addItem(ui::MenuItem::action("rename", "Rename", ui::icons::Edit2, "F2"));+ contextMenu_->addItem(ui::MenuItem::action("delete", "Delete", ui::icons::Trash2, "Del"));+ contextMenu_->addItem(ui::MenuItem::divider());+ }}contextMenu_->addItem(ui::MenuItem::action("refresh", "Refresh", ui::icons::Refresh));@@ -710,18 +733,16 @@ void AssetBrowserPanel::rebuildBreadcrumb() {breadcrumbConnections_.disconnectAll();breadcrumbPanel_->clearChildren();+ std::string actualRoot = browsingEngineResources_ ? engineResourcesPath_ : rootPath_;+ std::string rootDisplayName = browsingEngineResources_ ? "Engine" : "Project";+std::string normalizedCurrent = currentPath_;- std::string normalizedRoot = rootPath_;+ std::string normalizedRoot = actualRoot;for (char& c : normalizedCurrent) { if (c == '\\') c = '/'; }for (char& c : normalizedRoot) { if (c == '\\') c = '/'; }std::vector<std::pair<std::string, std::string>> segments;-- std::string rootName = normalizedRoot;- if (auto pos = rootName.rfind('/'); pos != std::string::npos) {- rootName = rootName.substr(pos + 1);- }- segments.emplace_back(rootName, rootPath_);+ segments.emplace_back(rootDisplayName, actualRoot);if (normalizedCurrent.size() > normalizedRoot.size() &&normalizedCurrent.substr(0, normalizedRoot.size()) == normalizedRoot) {@@ -730,7 +751,7 @@ void AssetBrowserPanel::rebuildBreadcrumb() {relativePath = relativePath.substr(1);}- std::string accumulated = rootPath_;+ std::string accumulated = actualRoot;std::string segment;for (char c : relativePath) {if (c == '/') {diff --git a/src/esengine/editor/panels/AssetBrowserPanel.hpp b/src/esengine/editor/panels/AssetBrowserPanel.hppindex f3cefef85..89ce5322e 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.hpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.hpp@@ -27,6 +27,7 @@#include "AssetBrowserTypes.hpp"#include <unordered_map>+#include <unordered_set>#include <vector>#include <functional>@@ -67,7 +68,7 @@ protected:private:void buildUI();void rebuildFolderTree();- void addFolderNodes(const std::string& path, ui::TreeNodeId parentNode);+ void addFolderNodes(const std::string& path, ui::TreeNodeId parentNode, bool isEngine = false);void refreshAssetList();void onFolderSelected(ui::TreeNodeId nodeId);@@ -92,7 +93,9 @@ private:std::string rootPath_;std::string currentPath_;+ std::string engineResourcesPath_;AssetViewMode viewMode_ = AssetViewMode::Grid;+ bool browsingEngineResources_ = false;ui::Panel* leftPanel_ = nullptr;ui::Panel* rightPanel_ = nullptr;@@ -107,6 +110,7 @@ private:std::vector<AssetEntry> currentAssets_;std::unordered_map<ui::TreeNodeId, std::string> nodeToPath_;std::unordered_map<std::string, ui::TreeNodeId> pathToNode_;+ std::unordered_set<ui::TreeNodeId> engineNodes_;std::string searchFilter_;std::string selectedAssetPath_;diff --git a/src/esengine/editor/panels/SceneViewPanel.cpp b/src/esengine/editor/panels/SceneViewPanel.cppindex dfbec89a5..8e61b200d 100644--- a/src/esengine/editor/panels/SceneViewPanel.cpp+++ b/src/esengine/editor/panels/SceneViewPanel.cpp@@ -27,54 +27,6 @@#define GL_VIEWPORT 0x0BA2#endif-namespace {--const char* GRID_VERTEX_SHADER = R"(- attribute vec3 a_position;-- uniform mat4 u_viewProj;-- void main() {- gl_Position = u_viewProj * vec4(a_position, 1.0);- }-)";--const char* GRID_FRAGMENT_SHADER = R"(- precision mediump float;-- uniform vec4 u_color;-- void main() {- gl_FragColor = u_color;- }-)";--const char* AXIS_VERTEX_SHADER = R"(- attribute vec3 a_position;- attribute vec4 a_color;-- uniform mat4 u_viewProj;-- varying vec4 v_color;-- void main() {- gl_Position = u_viewProj * vec4(a_position, 1.0);- v_color = a_color;- }-)";--const char* AXIS_FRAGMENT_SHADER = R"(- precision mediump float;-- varying vec4 v_color;-- void main() {- gl_FragColor = v_color;- }-)";--}-namespace esengine::editor {// =============================================================================@@ -112,7 +64,7 @@ SceneViewPanel::SceneViewPanel(ecs::Registry& registry, EntitySelection& selectigizmosVisible_ = visible;}));- transformGizmo_ = makeUnique<TransformGizmo>();+ transformGizmo_ = makeUnique<TransformGizmo>(resourceManager);transformGizmo_->setSize(1.5f);connections_.add(sink(toolbar_->onGizmoModeChanged).connect([this](GizmoMode mode) {@@ -416,18 +368,19 @@ void SceneViewPanel::initGridData() {gridVAO_->addVertexBuffer(Shared<VertexBuffer>(std::move(vbo)));- gridShader_ = Shader::create(GRID_VERTEX_SHADER, GRID_FRAGMENT_SHADER);+ gridShaderHandle_ = resourceManager_.loadEngineShader("grid");gridInitialized_ = true;ES_LOG_DEBUG("Grid initialized with {} vertices", gridVertexCount_);}void SceneViewPanel::renderGrid(const glm::mat4& viewProj) {- if (!gridVAO_ || !gridShader_) return;+ Shader* gridShader = resourceManager_.getShader(gridShaderHandle_);+ if (!gridVAO_ || !gridShader) return;- gridShader_->bind();- gridShader_->setUniform("u_viewProj", viewProj);- gridShader_->setUniform("u_color", glm::vec4(0.3f, 0.3f, 0.3f, 1.0f));+ gridShader->bind();+ gridShader->setUniform("u_viewProj", viewProj);+ gridShader->setUniform("u_color", glm::vec4(0.3f, 0.3f, 0.3f, 1.0f));gridVAO_->bind();glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(gridVertexCount_));@@ -470,8 +423,8 @@ void SceneViewPanel::initGrid2DData() {grid2DVAO_->addVertexBuffer(Shared<VertexBuffer>(std::move(vbo)));- if (!gridShader_) {- gridShader_ = Shader::create(GRID_VERTEX_SHADER, GRID_FRAGMENT_SHADER);+ if (!gridShaderHandle_.isValid()) {+ gridShaderHandle_ = resourceManager_.loadEngineShader("grid");}grid2DInitialized_ = true;@@ -479,11 +432,12 @@ void SceneViewPanel::initGrid2DData() {}void SceneViewPanel::renderGrid2D(const glm::mat4& viewProj) {- if (!grid2DVAO_ || !gridShader_) return;+ Shader* gridShader = resourceManager_.getShader(gridShaderHandle_);+ if (!grid2DVAO_ || !gridShader) return;- gridShader_->bind();- gridShader_->setUniform("u_viewProj", viewProj);- gridShader_->setUniform("u_color", glm::vec4(0.3f, 0.3f, 0.3f, 1.0f));+ gridShader->bind();+ gridShader->setUniform("u_viewProj", viewProj);+ gridShader->setUniform("u_color", glm::vec4(0.3f, 0.3f, 0.3f, 1.0f));grid2DVAO_->bind();glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(grid2DVertexCount_));@@ -671,7 +625,7 @@ void SceneViewPanel::initAxisGizmoData() {axisVAO_->addVertexBuffer(Shared<VertexBuffer>(std::move(vbo)));- axisShader_ = Shader::create(AXIS_VERTEX_SHADER, AXIS_FRAGMENT_SHADER);+ axisShaderHandle_ = resourceManager_.loadEngineShader("axis");axisInitialized_ = true;}@@ -681,7 +635,8 @@ void SceneViewPanel::renderAxisGizmo() {initAxisGizmoData();}- if (!axisVAO_ || !axisShader_) return;+ Shader* axisShader = resourceManager_.getShader(axisShaderHandle_);+ if (!axisVAO_ || !axisShader) return;const ui::Rect& bounds = getBounds();f32 gizmoSize = 60.0f;@@ -709,13 +664,13 @@ void SceneViewPanel::renderAxisGizmo() {glm::mat4 proj = glm::ortho(-1.5f, 1.5f, -1.5f, 1.5f, -10.0f, 10.0f);glm::mat4 viewProj = proj * rotation;- axisShader_->bind();- axisShader_->setUniform("u_viewProj", viewProj);+ axisShader->bind();+ axisShader->setUniform("u_viewProj", viewProj);axisVAO_->bind();glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(axisVertexCount_));axisVAO_->unbind();- axisShader_->unbind();+ axisShader->unbind();glViewport(savedViewport[0], savedViewport[1], savedViewport[2], savedViewport[3]);glDisable(GL_DEPTH_TEST);@@ -866,7 +821,8 @@ void SceneViewPanel::renderAxisGizmo2D() {initAxisGizmo2DData();}- if (!axis2DVAO_ || !axisShader_) return;+ Shader* axisShader = resourceManager_.getShader(axisShaderHandle_);+ if (!axis2DVAO_ || !axisShader) return;const ui::Rect& bounds = getBounds();f32 gizmoSize = 50.0f;@@ -891,13 +847,13 @@ void SceneViewPanel::renderAxisGizmo2D() {glm::mat4 proj = glm::ortho(-1.2f, 1.2f, -1.2f, 1.2f, -1.0f, 1.0f);- axisShader_->bind();- axisShader_->setUniform("u_viewProj", proj);+ axisShader->bind();+ axisShader->setUniform("u_viewProj", proj);axis2DVAO_->bind();glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(axis2DVertexCount_));axis2DVAO_->unbind();- axisShader_->unbind();+ axisShader->unbind();glViewport(savedViewport[0], savedViewport[1], savedViewport[2], savedViewport[3]);}@@ -959,7 +915,8 @@ void SceneViewPanel::renderCanvasGizmo(const glm::mat4& viewProj) {initCanvasGizmoData();}- if (!canvasGizmoVAO_ || !gridShader_) return;+ Shader* gridShader = resourceManager_.getShader(gridShaderHandle_);+ if (!canvasGizmoVAO_ || !gridShader) return;glm::vec2 worldSize = canvas->getWorldSize();f32 halfWidth = worldSize.x * 0.5f;@@ -968,9 +925,9 @@ void SceneViewPanel::renderCanvasGizmo(const glm::mat4& viewProj) {glm::mat4 model = glm::scale(glm::mat4(1.0f), glm::vec3(halfWidth, halfHeight, 1.0f));glm::mat4 mvp = viewProj * model;- gridShader_->bind();- gridShader_->setUniform("u_viewProj", mvp);- gridShader_->setUniform("u_color", glm::vec4(0.4f, 0.8f, 1.0f, 0.8f));+ gridShader->bind();+ gridShader->setUniform("u_viewProj", mvp);+ gridShader->setUniform("u_color", glm::vec4(0.4f, 0.8f, 1.0f, 0.8f));canvasGizmoVAO_->bind();glDrawArrays(GL_LINES, 0, 8);diff --git a/src/esengine/editor/panels/SceneViewPanel.hpp b/src/esengine/editor/panels/SceneViewPanel.hppindex 9dc82bea5..d9e47897d 100644--- a/src/esengine/editor/panels/SceneViewPanel.hpp+++ b/src/esengine/editor/panels/SceneViewPanel.hpp@@ -141,14 +141,14 @@ private:Unique<VertexArray> gridVAO_;Unique<VertexArray> grid2DVAO_;- Unique<Shader> gridShader_;+ resource::ShaderHandle gridShaderHandle_;u32 gridVertexCount_ = 0;u32 grid2DVertexCount_ = 0;bool gridInitialized_ = false;bool grid2DInitialized_ = false;Unique<VertexArray> axisVAO_;- Unique<Shader> axisShader_;+ resource::ShaderHandle axisShaderHandle_;u32 axisVertexCount_ = 0;bool axisInitialized_ = false;glm::vec2 axisGizmoCenter_{0.0f};