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: move texture Y-flip from JS to GPU via GL_UNPACK_FLIP_Y_WEBGL
esengine0b9b8bd222ef
diff --git a/editor/src/asset/AssetLoader.ts b/editor/src/asset/AssetLoader.tsindex 24c7af231..ed9c99c89 100644--- a/editor/src/asset/AssetLoader.ts+++ b/editor/src/asset/AssetLoader.ts@@ -74,7 +74,7 @@ export class AssetLoader {const pixelPtr = this.module_._malloc(decoded.data.length);this.module_.HEAPU8.set(decoded.data, pixelPtr);- const handleId = rm.createTexture(decoded.width, decoded.height, pixelPtr, decoded.data.length, 1);+ const handleId = rm.createTexture(decoded.width, decoded.height, pixelPtr, decoded.data.length, 1, false);this.module_._free(pixelPtr);const INVALID_HANDLE = 0xFFFFFFFF;diff --git a/editor/src/builder/WeChatBuilder.ts b/editor/src/builder/WeChatBuilder.tsindex 7ee31c1cf..47b5f7e71 100644--- a/editor/src/builder/WeChatBuilder.ts+++ b/editor/src/builder/WeChatBuilder.ts@@ -313,7 +313,7 @@ async function initPhysicsModule() {var wxfs = wx.getFileSystemManager();var provider = {loadPixels: function(ref) { return SDK.wxLoadImagePixels(resolvePath(ref)); },- loadPixelsRaw: function(ref) { return SDK.wxLoadImagePixels(resolvePath(ref), false); },+ loadPixelsRaw: function(ref) { return SDK.wxLoadImagePixels(resolvePath(ref)); },readText: function(ref) { return wxfs.readFileSync(resolvePath(ref), 'utf-8'); },readBinary: function(ref) { return new Uint8Array(wxfs.readFileSync(resolvePath(ref))); },resolvePath: resolvePathdiff --git a/editor/src/renderer/EditorTextureManager.ts b/editor/src/renderer/EditorTextureManager.tsindex 57e951770..71ad9bdeb 100644--- a/editor/src/renderer/EditorTextureManager.ts+++ b/editor/src/renderer/EditorTextureManager.ts@@ -151,10 +151,6 @@ export class EditorTextureManager {return { handle: 0, width: 0, height: 0 };}- // Flip Y-axis to match OpenGL texture coordinates (bottom-to-top)- // This mirrors stbi_set_flip_vertically_on_load(true) in Desktop C++- ctx.translate(0, bitmap.height);- ctx.scale(1, -1);ctx.drawImage(bitmap, 0, 0);const imageData = ctx.getImageData(0, 0, bitmap.width, bitmap.height);@@ -170,7 +166,8 @@ export class EditorTextureManager {imageData.height,pixelsPtr,pixels.length,- 1 // RGBA8+ 1,+ true);this.module_._free(pixelsPtr);diff --git a/sdk/src/asset/AssetServer.ts b/sdk/src/asset/AssetServer.tsindex 5609c8947..4932c6c4b 100644--- a/sdk/src/asset/AssetServer.ts+++ b/sdk/src/asset/AssetServer.ts@@ -469,13 +469,7 @@ export class AssetServer {}this.ctx_.clearRect(0, 0, this.canvas_.width, this.canvas_.height);- this.ctx_.save();- if (flip) {- this.ctx_.translate(0, height);- this.ctx_.scale(1, -1);- }this.ctx_.drawImage(img, 0, 0);- this.ctx_.restore();const imageData = this.ctx_.getImageData(0, 0, width, height);const pixels = new Uint8Array(imageData.data.buffer);@@ -484,7 +478,7 @@ export class AssetServer {const rm = this.module_.getResourceManager();const ptr = this.module_._malloc(pixels.length);this.module_.HEAPU8.set(pixels, ptr);- const handle = rm.createTexture(width, height, ptr, pixels.length, 1);+ const handle = rm.createTexture(width, height, ptr, pixels.length, 1, flip);this.module_._free(ptr);return { handle, width, height };diff --git a/sdk/src/platform/wechat/image.ts b/sdk/src/platform/wechat/image.tsindex 9904ae1e1..24a16fa1d 100644--- a/sdk/src/platform/wechat/image.ts+++ b/sdk/src/platform/wechat/image.ts@@ -61,20 +61,13 @@ export function wxLoadImage(path: string): Promise<WechatMinigame.Image> {/*** Extract pixel data from an image* @param img - Loaded image object- * @param flipY - Whether to flip the image vertically (default: true for OpenGL)*/-export function wxGetImagePixels(img: WechatMinigame.Image, flipY = true): ImageLoadResult {+export function wxGetImagePixels(img: WechatMinigame.Image): ImageLoadResult {const { width, height } = img;const { ctx } = getOffscreenCanvas(width, height);ctx.clearRect(0, 0, width, height);- ctx.save();- if (flipY) {- ctx.translate(0, height);- ctx.scale(1, -1);- }ctx.drawImage(img as unknown as CanvasImageSource, 0, 0);- ctx.restore();const imageData = ctx.getImageData(0, 0, width, height);const pixels = new Uint8Array(imageData.data.buffer);@@ -84,10 +77,9 @@ export function wxGetImagePixels(img: WechatMinigame.Image, flipY = true): Image/*** Load image and get pixel data in one call* @param path - File path relative to game root- * @param flipY - Whether to flip the image vertically (default: true for OpenGL)*/-export async function wxLoadImagePixels(path: string, flipY = true): Promise<ImageLoadResult> {+export async function wxLoadImagePixels(path: string): Promise<ImageLoadResult> {const img = await wxLoadImage(path);- return wxGetImagePixels(img, flipY);+ return wxGetImagePixels(img);}diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.tsindex 89388700c..5b6c56564 100644--- a/sdk/src/runtimeLoader.ts+++ b/sdk/src/runtimeLoader.ts@@ -32,11 +32,12 @@ export interface RuntimeAssetProvider {function createTextureFromPixels(module: ESEngineModule,result: { width: number; height: number; pixels: Uint8Array },+ flipY: boolean = true,): number {const rm = module.getResourceManager();const ptr = module._malloc(result.pixels.length);module.HEAPU8.set(result.pixels, ptr);- const handle = rm.createTexture(result.width, result.height, ptr, result.pixels.length, 1);+ const handle = rm.createTexture(result.width, result.height, ptr, result.pixels.length, 1, flipY);module._free(ptr);return handle;}@@ -172,7 +173,7 @@ async function loadSpineAssets(const result = provider.loadPixelsRaw? await provider.loadPixelsRaw(texPath): await provider.loadPixels(texPath);- const handle = createTextureFromPixels(module, result);+ const handle = createTextureFromPixels(module, result, false);const rm = module.getResourceManager();const glId = rm.getTextureGLId(handle);spineAPI.setAtlasPageTexture(skelHandle, k, glId, result.width, result.height);diff --git a/sdk/src/ui/TextRenderer.ts b/sdk/src/ui/TextRenderer.tsindex a10415142..39a46e0f4 100644--- a/sdk/src/ui/TextRenderer.ts+++ b/sdk/src/ui/TextRenderer.ts@@ -134,13 +134,12 @@ export class TextRenderer {const imageData = ctx.getImageData(0, 0, width, height);const pixels = new Uint8Array(imageData.data.buffer);- const flipped = this.flipVertically(pixels, width, height);const rm = this.module.getResourceManager();- const ptr = this.module._malloc(flipped.length);- this.module.HEAPU8.set(flipped, ptr);+ const ptr = this.module._malloc(pixels.length);+ this.module.HEAPU8.set(pixels, ptr);- const textureHandle = rm.createTexture(width, height, ptr, flipped.length, 1);+ const textureHandle = rm.createTexture(width, height, ptr, pixels.length, 1, true);this.module._free(ptr);@@ -274,16 +273,4 @@ export class TextRenderer {return p;}- private flipVertically(pixels: Uint8Array, width: number, height: number): Uint8Array {- const rowSize = width * 4;- const flipped = new Uint8Array(pixels.length);-- for (let y = 0; y < height; y++) {- const srcOffset = y * rowSize;- const dstOffset = (height - 1 - y) * rowSize;- flipped.set(pixels.subarray(srcOffset, srcOffset + rowSize), dstOffset);- }-- return flipped;- }}diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.tsindex 60c9d8427..06e72f286 100644--- a/sdk/src/wasm.ts+++ b/sdk/src/wasm.ts@@ -90,7 +90,7 @@ export interface CppRegistry {// =============================================================================export interface CppResourceManager {- createTexture(width: number, height: number, pixels: number, pixelsLen: number, format: number): number;+ createTexture(width: number, height: number, pixels: number, pixelsLen: number, format: number, flipY: boolean): number;createShader(vertSrc: string, fragSrc: string): number;registerExternalTexture(glTextureId: number, width: number, height: number): number;getTextureGLId(handle: number): number;diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 323ec62f8..e37655284 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -323,7 +323,7 @@ resource::ResourceManager* getResourceManager() {}u32 rm_createTexture(resource::ResourceManager& rm, u32 width, u32 height,- uintptr_t pixelsPtr, u32 pixelsLen, i32 format) {+ uintptr_t pixelsPtr, u32 pixelsLen, i32 format, bool flipY) {const u8* pixels = reinterpret_cast<const u8*>(pixelsPtr);ConstSpan<u8> pixelSpan(pixels, pixelsLen);@@ -331,7 +331,7 @@ u32 rm_createTexture(resource::ResourceManager& rm, u32 width, u32 height,if (format == 0) texFormat = TextureFormat::RGB8;else if (format == 1) texFormat = TextureFormat::RGBA8;- auto handle = rm.createTexture(width, height, pixelSpan, texFormat);+ auto handle = rm.createTexture(width, height, pixelSpan, texFormat, flipY);return handle.id();}diff --git a/src/esengine/renderer/Texture.cpp b/src/esengine/renderer/Texture.cppindex fce036c54..335441627 100644--- a/src/esengine/renderer/Texture.cpp+++ b/src/esengine/renderer/Texture.cpp@@ -105,17 +105,20 @@ Unique<Texture> Texture::create(const TextureSpecification& spec) {return texture;}-Unique<Texture> Texture::create(u32 width, u32 height, std::span<const u8> pixels, TextureFormat format) {+Unique<Texture> Texture::create(u32 width, u32 height, std::span<const u8> pixels,+ TextureFormat format, bool flipY) {[[maybe_unused]] u32 expectedSize = width * height * (format == TextureFormat::RGBA8 ? 4 : 3);ES_ASSERT(pixels.size() == expectedSize, "Pixel data size mismatch");- return createRaw(width, height, pixels.data(), format);+ return createRaw(width, height, pixels.data(), format, flipY);}-Unique<Texture> Texture::create(u32 width, u32 height, const std::vector<u8>& pixels, TextureFormat format) {- return create(width, height, std::span<const u8>(pixels), format);+Unique<Texture> Texture::create(u32 width, u32 height, const std::vector<u8>& pixels,+ TextureFormat format, bool flipY) {+ return create(width, height, std::span<const u8>(pixels), format, flipY);}-Unique<Texture> Texture::createRaw(u32 width, u32 height, const void* data, TextureFormat format) {+Unique<Texture> Texture::createRaw(u32 width, u32 height, const void* data,+ TextureFormat format, bool flipY) {TextureSpecification spec;spec.width = width;spec.height = height;@@ -130,7 +133,7 @@ Unique<Texture> Texture::createRaw(u32 width, u32 height, const void* data, Text}if (data) {- texture->setDataRaw(data, width * height * (format == TextureFormat::RGBA8 ? 4 : 3));+ texture->setDataRaw(data, width * height * (format == TextureFormat::RGBA8 ? 4 : 3), flipY);}return texture;@@ -248,13 +251,16 @@ void Texture::setData(const std::vector<u8>& pixels) {setData(std::span<const u8>(pixels));}-void Texture::setDataRaw(const void* data, u32 sizeBytes) {+void Texture::setDataRaw(const void* data, u32 sizeBytes, bool flipY) {#ifdef ES_PLATFORM_WEB[[maybe_unused]] u32 bpp = (format_ == TextureFormat::RGBA8) ? 4 : 3;ES_ASSERT(sizeBytes == width_ * height_ * bpp, "Data size mismatch");- (void)sizeBytes; // Used only for assertion+ (void)sizeBytes;glBindTexture(GL_TEXTURE_2D, textureId_);+ if (flipY) {+ glPixelStorei(0x9240 /* GL_UNPACK_FLIP_Y_WEBGL */, GL_TRUE);+ }glTexSubImage2D(GL_TEXTURE_2D,0,@@ -264,9 +270,13 @@ void Texture::setDataRaw(const void* data, u32 sizeBytes) {GL_UNSIGNED_BYTE,data);+ if (flipY) {+ glPixelStorei(0x9240, GL_FALSE);+ }#else(void)data;(void)sizeBytes;+ (void)flipY;#endif}diff --git a/src/esengine/renderer/Texture.hpp b/src/esengine/renderer/Texture.hppindex 4ba01ce15..4bb6246d4 100644--- a/src/esengine/renderer/Texture.hpp+++ b/src/esengine/renderer/Texture.hpp@@ -156,7 +156,8 @@ public:* @return Unique pointer to the texture*/static Unique<Texture> create(u32 width, u32 height, std::span<const u8> pixels,- TextureFormat format = TextureFormat::RGBA8);+ TextureFormat format = TextureFormat::RGBA8,+ bool flipY = false);/*** @brief Creates a texture from pixel vector@@ -164,10 +165,12 @@ public:* @param height Texture height in pixels* @param pixels Pixel data (size must match width*height*channels)* @param format Pixel format (default RGBA8)+ * @param flipY Flip vertically on upload (for web image data)* @return Unique pointer to the texture*/static Unique<Texture> create(u32 width, u32 height, const std::vector<u8>& pixels,- TextureFormat format = TextureFormat::RGBA8);+ TextureFormat format = TextureFormat::RGBA8,+ bool flipY = false);/*** @brief Creates a texture from an image file@@ -243,7 +246,8 @@ public:* @return Unique pointer to the texture*/static Unique<Texture> createRaw(u32 width, u32 height, const void* data,- TextureFormat format = TextureFormat::RGBA8);+ TextureFormat format = TextureFormat::RGBA8,+ bool flipY = false);/*** @brief Wraps an existing GL texture ID@@ -261,7 +265,7 @@ public:* @param data Pointer to pixel data* @param sizeBytes Size of data in bytes*/- void setDataRaw(const void* data, u32 sizeBytes);+ void setDataRaw(const void* data, u32 sizeBytes, bool flipY = false);private:/**diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cppindex 548ebeeaa..32d6fefd1 100644--- a/src/esengine/resource/ResourceManager.cpp+++ b/src/esengine/resource/ResourceManager.cpp@@ -166,9 +166,9 @@ TextureHandle ResourceManager::createTexture(const TextureSpecification& spec) {}TextureHandle ResourceManager::createTexture(u32 width, u32 height, ConstSpan<u8> pixels,- TextureFormat format) {+ TextureFormat format, bool flipY) {std::vector<u8> pixelVec(pixels.begin(), pixels.end());- auto texture = Texture::create(width, height, pixelVec, format);+ auto texture = Texture::create(width, height, pixelVec, format, flipY);if (!texture) {ES_LOG_ERROR("Failed to create texture from pixels");return TextureHandle();diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hppindex 0bb79a00c..c3b6a8b87 100644--- a/src/esengine/resource/ResourceManager.hpp+++ b/src/esengine/resource/ResourceManager.hpp@@ -184,7 +184,7 @@ public:* @return Handle to the texture, or invalid handle on failure*/TextureHandle createTexture(u32 width, u32 height, ConstSpan<u8> pixels,- TextureFormat format);+ TextureFormat format, bool flipY = false);/*** @brief Loads a texture from file (with caching)