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
fix(render): an opaque mesh on a material records its surface, and water and glass own theirs
yhh47ce8d5707c7
diff --git a/src/esengine/renderer/draw/DrawList.cpp b/src/esengine/renderer/draw/DrawList.cppindex 4288547fc..58bc31059 100644--- a/src/esengine/renderer/draw/DrawList.cpp+++ b/src/esengine/renderer/draw/DrawList.cpp@@ -388,6 +388,8 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,FrameCapture* capture, const PerDrawBlockSet& blocks, Phase phase) {PipelineDesc lastDesc{};PipelineHandle lastHandle = PipelineHandle::Invalid;+ // The draws of this phase read the scene's colour and compose it under themselves.+ const bool composes = phase == Phase::SceneColorOnly;// Once for the pass, not once per draw: every resident mesh reads the same// texture. Rebound each pass because growing it mints a new handle.@@ -440,9 +442,11 @@ void DrawList::execute(GfxDevice& device, TransientBufferPool& buffers,desc.depthBias = cmd.depth_bias;desc.topology = cmd.topology;desc.alphaToCoverage = !desc.blendEnabled && materials.alphaIsCoverage(cmd.material_id);- // What a surface is is recorded only for what replaces what was behind it: a blended- // surface is no one thing, and a normal averaged with the one under it is neither's.- desc.gbuffer = gbuffer_pass_ && !desc.blendEnabled && materials.programWritesGBuffer(cmd.shader_id);+ // Only what replaces what was behind it records a surface (a blended normal is no one's), except a+ // draw that composes the scene behind it (water, refracting glass): it IS the pixel, so it owns its+ // record, written unblended where the device can say so per target.+ desc.gbuffer = gbuffer_pass_ && materials.programWritesGBuffer(cmd.shader_id)+ && (!desc.blendEnabled || (composes && device.supportsUnblendedSurfaceTarget()));if (lastHandle == PipelineHandle::Invalid || !(desc == lastDesc)) {lastHandle = device.createPipeline(desc);diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cppindex dcd669a21..ec14c22aa 100644--- a/src/esengine/renderer/plugins/MeshPlugin.cpp+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp@@ -757,7 +757,10 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {if (mesh.material != 0) {if (const MaterialRecord* m = material) {key.shaderId = (m->shader != 0) ? m->shader : ctx.batch_shader_id;- key.blend = m->blend;+ // An opaque mesh is drawn unblended whatever its material's blend says: the material's 0 is Normal, which+ // every material file names by default, and it left every opaque material surface blending — so it wrote+ // no surface record (screen-space reflections and rain read nothing) and a cut-out lost its alpha-to-coverage.+ key.blend = mesh.opaque ? BlendMode::None : m->blend;key.materialId = mesh.material;key.depthTest = m->depthTest;key.depthWrite = m->depthWrite;diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cppindex 40b0abcf6..c98c3e186 100644--- a/src/esengine/renderer/rhi/GLDevice.cpp+++ b/src/esengine/renderer/rhi/GLDevice.cpp@@ -227,6 +227,16 @@ GLenum toGLCompressedFormat(GfxCompressedFormat fmt) {// GLES3-correct extension probe (glGetString(GL_EXTENSIONS) returns null on a// core profile). Emscripten exposes WebGL extensions here under GL-style names.+#ifdef __EMSCRIPTEN__+// OES_draw_buffers_indexed is not in Emscripten's GL layer: the one call it is needed for goes to the context directly.+EM_JS(void, es_gl_surface_blend, (int off), {+ var ctx = GLctx;+ var ext = ctx && (ctx.esIndexedBlend || (ctx.esIndexedBlend = ctx.getExtension('OES_draw_buffers_indexed')));+ if (!ext) return;+ if (off) ext.disableiOES(ctx.BLEND, 1); else ext.enableiOES(ctx.BLEND, 1);+});+#endif+bool glExtensionPresent(const char* name) {GLint count = 0;glGetIntegerv(GL_NUM_EXTENSIONS, &count);@@ -303,6 +313,8 @@ void GLDevice::captureDeviceIdentity() {// extension that must be enabled before its enums resolve.constexpr GLenum UNMASKED_VENDOR = 0x9245;constexpr GLenum UNMASKED_RENDERER = 0x9246;+ // A composing draw (water, glass) writes the surface record unblended while it blends its colour: see applySurfaceBlend.+ indexed_blend_ = emscripten_webgl_enable_extension(emscripten_webgl_get_current_context(), "OES_draw_buffers_indexed") ? 1 : 0;if (emscripten_webgl_enable_extension(emscripten_webgl_get_current_context(),"WEBGL_debug_renderer_info")) {const char* v = reinterpret_cast<const char*>(glGetString(UNMASKED_VENDOR));@@ -403,6 +415,7 @@ void GLDevice::resetStateCache() {scissor_test_ = -1;current_depth_bias_ = 0;alpha_to_coverage_ = -1;+ surface_blend_off_ = false;current_framebuffer_ = 0;timer_query_state_ = 0;}@@ -485,6 +498,8 @@ void GLDevice::clear(bool color, bool depth, bool stencil) {void GLDevice::setBlendEnabled(bool enabled) {setCapability(GL_BLEND, enabled);+ // Enable/Disable without an index sets every draw buffer: the surface record's own state is gone with it.+ surface_blend_off_ = false;}void GLDevice::setBlendMode(BlendMode mode) {@@ -1004,6 +1019,7 @@ void GLDevice::backendSetPipeline(u32 id, const PipelineDesc& desc) {}if (current_fbo_has_gbuffer_) applyDrawBuffers(desc.gbuffer);+ applySurfaceBlend(desc.blendEnabled && desc.gbuffer && current_fbo_has_gbuffer_);current_pipeline_id_ = id;current_topology_ = desc.topology;@@ -1441,6 +1457,14 @@ void GLDevice::beginRenderPass(const RenderPassDesc& desc) {if (scoped) setScissorTest(false);}+void GLDevice::applySurfaceBlend(bool off) {+ if (off == surface_blend_off_ || indexed_blend_ != 1) return;+#ifdef __EMSCRIPTEN__+ es_gl_surface_blend(off ? 1 : 0);+#endif+ surface_blend_off_ = off;+}+void GLDevice::applyDrawBuffers(bool gbuffer) {const i8 want = gbuffer ? 1 : 0;if (draw_buffers_ == want) return;diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hppindex f8930c272..41110d4c2 100644--- a/src/esengine/renderer/rhi/GLDevice.hpp+++ b/src/esengine/renderer/rhi/GLDevice.hpp@@ -57,6 +57,7 @@ public:void bindTexture(u32 slot, TextureHandle texture) override;bool supportsCompressedFormat(GfxCompressedFormat format) override;bool supportsFloatTargets() override;+ bool supportsUnblendedSurfaceTarget() const override { return indexed_blend_ == 1; }u32 maxSamples() override;/** A GL blit resolves depth as well as colour. */bool resolvesDepth() const override { return true; }@@ -233,6 +234,11 @@ private:bool current_fbo_has_gbuffer_ = false;i8 draw_buffers_ = -1;void applyDrawBuffers(bool gbuffer);+ /// OES_draw_buffers_indexed: 1 enabled, 0 absent (asked once at init).+ i8 indexed_blend_ = 0;+ /// Blending is off on the second draw buffer while the first blends (a composing draw writing the surface record).+ bool surface_blend_off_ = false;+ void applySurfaceBlend(bool off);int scissor_test_ = -1; // tri-state: -1 unknown, 0 disabled, 1 enabledstruct VaoCache {diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hppindex 9261a24bf..cb96ff799 100644--- a/src/esengine/renderer/rhi/GfxDevice.hpp+++ b/src/esengine/renderer/rhi/GfxDevice.hpp@@ -438,6 +438,15 @@ public:*/virtual bool supportsFloatTargets() = 0;+ /**+ * @brief Whether a blended draw can write the surface record's second attachment unblended.+ * @details The surface record is never blended: a normal averaged with the one under it is neither's. A draw that+ * composes the scene behind itself (water, glass reading the scene's colour) is still blended into the+ * first attachment, and owns the pixel's record all the same. WebGPU sets blend per target; WebGL2 needs+ * OES_draw_buffers_indexed. Where this is false such a draw leaves the record as the opaque scene wrote it.+ */+ virtual bool supportsUnblendedSurfaceTarget() const { return false; }+/*** @brief Most samples an offscreen colour attachment may carry; 1 = none.* @details The engine owns its own multisampling: a target the scene drawsdiff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex f144a9876..fb70e38fd 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -120,6 +120,8 @@ public:bool supportsCompressedFormat(GfxCompressedFormat format) override;// Float-target rendering is a WebGPU core capability.bool supportsFloatTargets() override { return true; }+ /** The surface record's target carries no blend state of its own (see createPipeline). */+ bool supportsUnblendedSurfaceTarget() const override { return true; }u32 maxSamples() override;/** Colour resolves in the pass; depth is resolved by a pass of our own* ({@link resolveDepthAttachment}), since WebGPU's depth attachment has no