Skip to content

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.

fix(render): a material mesh reads its own tangents on WebGL again

yhhde60c54e0304
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index d1996617c..04c119dbc 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -402,8 +402,8 @@ u32 MeshPlugin::meshProgram(RenderFrameContext& ctx, u32 variant) {
if (normalMapped) features.emplace_back("NORMAL_MAP");
if (skinned) features.emplace_back("SKINNED");
if (lightmapped) features.emplace_back("MESH_LIGHTMAP");
- // Not in a depth pass: its vertex stage writes no tangent varying, and a fragment that declares one fails to link.
- if (tangents && normals && !depthOnly) features.emplace_back("MESH_TANGENTS");
+ // Only where its vertex stage writes the tangent varying (lit, and not a depth pass): a fragment that declares one it lacks fails to link.
+ if (tangents && normals && lit && !depthOnly) features.emplace_back("MESH_TANGENTS");
if (weightHeat) features.emplace_back("WEIGHT_HEAT");
if (depthOnly) features.emplace_back("SHADOW_DEPTH");
if (motion) features.emplace_back("ES_VELOCITY");
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 7566cbb1a..d4db00432 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -3002,14 +3002,14 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// Tangent frame from screen-space derivatives, so a tangent-space normal
// lands in world space without the geometry carrying a tangent channel —
// which most exported models do not. Degenerate UVs return N unchanged.
- // Only where the vertex stage writes it: that is LIT with MESH_TANGENTS. A fragment stage that names it elsewhere (an unlit variant of
- // a mesh that carries tangents) fails to link, though nothing calls the function.
- "#if defined(MESH_TANGENTS) && defined(LIT)\n"
+ // MESH_TANGENTS is asked for only where the vertex stage writes the varying (the canonical stage under MESH_NORMALS, the built-in
+ // mesh shader when lit): a fragment that declares one its vertex stage lacks fails to link.
+ "#ifdef MESH_TANGENTS\n"
"in highp vec4 v_worldTangent;\n"
"#endif\n"
"highp vec3 perturbNormal(in highp vec3 N, in highp vec3 worldPos,\n"
" in highp vec2 uv, in highp vec3 tangentNormal) {\n"
- "#if defined(MESH_TANGENTS) && defined(LIT)\n"
+ "#ifdef MESH_TANGENTS\n"
// The frame the normal map was baked in: the tangent made square to the interpolated normal.
" highp vec3 Tg = normalize(v_worldTangent.xyz - N * dot(N, v_worldTangent.xyz));\n"
" highp vec3 Bg = cross(N, Tg) * v_worldTangent.w;\n"