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 shadow is cast by what the camera's level of detail draws, and the terrain slides its vertices by the eye in every pass

yhhe7be47f8f3ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 613997275..fa93d29d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,7 @@ published separately; it ships inside the editor.
### Fixed
+- **Shadows follow what the camera draws.** A mesh the camera's level of detail culls (grass past its draw distance) no longer casts a shadow, and one drawn coarse casts coarse; seen from high up, a field of grass no longer laid an unseen square of shadow on the ground. The terrain's vertices slide between levels by the eye the ground was chosen for in every pass, so a shadow's ground is the ground that is seen, under an orthographic camera too.
- **Ridged ground without end has no steps.** Infinite noise ground that was both ridged and worn broke into terraces with sheer walls where a ridge's fold turned; the crest is now rounded over a few hundredths, so the ground is continuous.
- **The running game's resolution can be chosen.** A picker beside Play shows the target screen and stays usable while the game runs (it used to sit in a menu that was disabled in Play), with Free Aspect, the project's design resolution, the device presets and portrait or landscape; the choice is kept per project. ([#100](https://github.com/esengine/estella/issues/100))
- **Terrain skirts and surf films no longer show as streaks.** The skirt under a terrain chunk's edge, seen where two levels of detail part, took the relief bump meant for the ground and lit as a bright line; it now draws without it. The surf running up dry ground no longer stretches into a wall between two waters' heights.
diff --git a/docs/WORLD_GENERATION_PLAN.md b/docs/WORLD_GENERATION_PLAN.md
index 6700eb17a..c6ce843b7 100644
--- a/docs/WORLD_GENERATION_PLAN.md
+++ b/docs/WORLD_GENERATION_PLAN.md
@@ -83,7 +83,7 @@
## 引擎自身待办(同日会话记录)
-- 俯视(正下方)相机时视点下方地形出现一块轴对齐暗方块,疑为第一级阴影级联自阴影;平视无此问题(`examples/worldgen` 用 `WORLDGEN_TOP=1` 复现)。
+- ~~俯视时地形上的暗方块~~:已修复。原因是被相机 LOD 剔除的草仍在阴影通道里按最高精度投影;现在阴影投射跟随相机的 LOD 决定。另修地形形变在阴影通道与正交相机下用错视点。
- 植被改为真正的 GPU 实例化(与 T-03/T-04 的放置输出一起设计)。
- 雨天/阴天时 CPU 渲染提交阻塞约 45 ms(GPU 计时或同步读回导致),需定位。
diff --git a/sdk/src/terrain/terrainPlugin.ts b/sdk/src/terrain/terrainPlugin.ts
index 76f53e2be..f0d79b763 100644
--- a/sdk/src/terrain/terrainPlugin.ts
+++ b/sdk/src/terrain/terrainPlugin.ts
@@ -635,6 +635,8 @@ export class TerrainPlugin implements Plugin {
set('u_heightRange', { x: source.minHeight, y: Math.max(source.maxHeight - source.minHeight, 1e-6) });
set('u_lodRange0', tree.range0);
set('u_terrainOrigin', { x: t.worldPosition.x, y: t.worldPosition.y, z: t.worldPosition.z, w: 0 });
+ // Not the pass's camera: a shadow is drawn from the light, and its ground must be the one the eye sees.
+ set('u_terrainEye', { x: eyeAt[0] + t.worldPosition.x, y: eyeAt[1] + t.worldPosition.y, z: eyeAt[2] + t.worldPosition.z, w: 1 });
const b = source.bounds;
set('u_terrainBounds', b ? { x: b[0], y: b[1], z: b[2], w: b[3] } : { x: -1e9, y: -1e9, z: 1e9, w: 1e9 });
if (!built.data.material) {
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
index c3aa120cb..7ed33b3ed 100644
--- a/sdk/src/terrain/terrainShader.ts
+++ b/sdk/src/terrain/terrainShader.ts
@@ -49,6 +49,7 @@ const TERRAIN = `#pragma shader "Terrain"
#pragma param u_roughness float default(0.92) range(0,1) ui(slider)
// The ground in pages (pages.ts; the Terrain component writes these): heights and normals, and the paint, in two atlases; heights over
// u_heightRange above u_terrainOrigin; level 0 drawn out to u_lodRange0, each level twice as far; nothing off u_terrainBounds.
+// u_terrainEye is the eye the nodes were chosen for: every pass (a shadow's too) slides the vertices by it, so all draw one ground.
#pragma param u_terrainPages float default(0) range(0,1) ui(slider)
#pragma param u_heightPages texture default(white) texel(data)
#pragma param u_paintPages texture default(white) texel(data)
@@ -58,6 +59,7 @@ const TERRAIN = `#pragma shader "Terrain"
#pragma param u_heightRange vec2 default(0,1)
#pragma param u_lodRange0 float default(100) range(0,1000000) ui(slider)
#pragma param u_terrainOrigin vec4 default(0,0,0,0)
+#pragma param u_terrainEye vec4 default(0,0,0,0)
#pragma param u_terrainBounds vec4 default(-1000000000,-1000000000,1000000000,1000000000)
#pragma vertexoffset
@@ -86,7 +88,7 @@ void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
vec2 g = uv * u_patchCells;
float h0 = tnPageHeight(page + uv * u_pageTexels + 0.5);
float range = u_lodRange0 * exp2(level);
- float dist = distance(u_camera.xyz, vec3(corner.x + uv.x * size, h0, corner.z + uv.y * size));
+ float dist = distance(u_terrainEye.xyz, vec3(corner.x + uv.x * size, h0, corner.z + uv.y * size));
float k = clamp((dist - range * 0.7) / (range * 0.3), 0.0, 1.0);
g -= fract(g * 0.5) * 2.0 * k;
vec2 at = g / u_patchCells;
@@ -126,7 +128,7 @@ fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, ve
var g = uv * mc.u_patchCells;
let h0 = tnPageHeight(page + uv * mc.u_pageTexels + vec2f(0.5));
let range = mc.u_lodRange0 * exp2(level);
- let dist = distance(frame.camera.xyz, vec3f(corner.x + uv.x * size, h0, corner.z + uv.y * size));
+ let dist = distance(mc.u_terrainEye.xyz, vec3f(corner.x + uv.x * size, h0, corner.z + uv.y * size));
let k = clamp((dist - range * 0.7) / (range * 0.3), 0.0, 1.0);
g = g - fract(g * 0.5) * 2.0 * k;
let at = g / mc.u_patchCells;
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hpp
index e07b9eb8b..515de93bf 100644
--- a/src/esengine/renderer/RenderTypePlugin.hpp
+++ b/src/esengine/renderer/RenderTypePlugin.hpp
@@ -339,6 +339,9 @@ struct RenderCollectContext {
u32 view = 0;
lod::LodViewState* state = nullptr;
lod::SelectionCounts* counts = nullptr;
+ /** @brief Read the level @ref view settled on instead of choosing one: what a shadow cast for that view's picture is
+ * cast by. What the view culled casts nothing; what it has not looked at lately casts at its finest. */
+ bool follow = false;
};
LodView lod;
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index cc4d8353d..2edb70a88 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -2268,6 +2268,9 @@ void RenderFrame::executeShadowPass(ecs::Registry& registry) {
list, ctx,
computeCameraView(view.view_projection)};
collectCtx.screen_ui = screen_domain_;
+ // A shadow is part of the camera's picture: what the camera culled by its level of detail (grass past its distance)
+ // does not cast, and what it draws coarse casts coarse.
+ collectCtx.lod = {view_id_, &lod_view_state_, nullptr, true};
for (auto& plugin : plugins_) {
if (plugin->castsShadows()) plugin->collect(collectCtx);
}
diff --git a/src/esengine/renderer/lod/LodViewState.hpp b/src/esengine/renderer/lod/LodViewState.hpp
index 902d37a5d..80c8dcf9c 100644
--- a/src/esengine/renderer/lod/LodViewState.hpp
+++ b/src/esengine/renderer/lod/LodViewState.hpp
@@ -52,6 +52,15 @@ public:
return it == entries_.end() ? 0 : it->second.level;
}
+ /** @brief The level @p view drew @p entity at within the last @p frames frames, or false when it has not drawn it
+ * that lately (it is off that view's screen, or new). What a view culled reads as @ref kCulled. */
+ bool recent(u32 view, Entity entity, u64 frames, u8& outLevel) const {
+ const auto it = entries_.find(key(view, entity));
+ if (it == entries_.end() || frame_ - it->second.stamp > frames) return false;
+ outLevel = it->second.level;
+ return true;
+ }
+
/**
* @brief Record a decision: what was chosen, what the bare thresholds asked
* for, how many stand-ins were reachable, and the size all of it read.
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index ec14c22aa..2d4826143 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -668,7 +668,16 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
// Nothing visible is worth measuring precisely, so the cull runs first; what
// survives it is measured on the sphere the whole group shares.
- if (group) {
+ if (group && collect_ctx.lod.follow) {
+ // Cast by what the view draws: the level it settled on last frame (this frame's camera collect comes after the
+ // shadows), nothing where it culled, the finest where it has not looked lately.
+ u8 settled = 0;
+ const auto& state = *collect_ctx.lod.state;
+ if (!state.recent(collect_ctx.lod.view, entity, 2, settled)) settled = 0;
+ const u8 shown = settled == lod::kCulled ? settled : state.drawn(collect_ctx.lod.view, entity, settled, levels.set.count);
+ if (shown == lod::kCulled) { ++collect_ctx.culled; tick(2); continue; }
+ resident = levels.mesh[shown];
+ } else if (group) {
glm::vec3 centre(0.0f);
f32 radius = 0.0f;
lod::boundingSphere(position, rotation, scale, localMin, localMax, centre, radius);
diff --git a/tests/renderer/test_lod_selection.cpp b/tests/renderer/test_lod_selection.cpp
index 5776d67b8..d7a6c0280 100644
--- a/tests/renderer/test_lod_selection.cpp
+++ b/tests/renderer/test_lod_selection.cpp
@@ -226,6 +226,22 @@ static void testPreviewOwnership() {
"a preview past the last stand-in is ignored, not clamped to the last one");
}
+// A shadow cast for a view's picture reads what that view settled on: culled casts nothing, and what it has not drawn lately is
+// not taken from an old answer.
+static void testRecentForShadows() {
+ lod::LodViewState state;
+ const Entity grass = Entity::make(9, 1);
+ const u32 camera = 3;
+ state.beginFrame();
+ u8 level = 7;
+ CHECK(!state.recent(camera, grass, 2, level), "a view that never drew it has nothing recent to say");
+ state.remember(camera, grass, lod::kCulled, lod::kCulled, 1, 0.001f);
+ state.beginFrame();
+ CHECK(state.recent(camera, grass, 2, level) && level == lod::kCulled, "the view culled it last frame: it reads as culled");
+ for (int i = 0; i < 3; ++i) state.beginFrame();
+ CHECK(!state.recent(camera, grass, 2, level), "three frames on, the answer is no longer this view's");
+}
+
int main() {
testProjection();
testBoundingSphere();
@@ -234,6 +250,7 @@ int main() {
testViewOwnership();
testDecisionRecorded();
testPreviewOwnership();
+ testRecentForShadows();
std::printf(g_failures ? "\n%d failure(s)\n" : "\nall LOD selection claims hold\n",
g_failures);
return g_failures ? 1 : 0;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 1d2eddfe0..809603fa2 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2780,4 +2780,6 @@ export const NOTED = {
{"note":"Generated worlds."},
"feat(water): the sea fills only what joins it, a body is water only over its ground, bodies carry stable ids, and basins show where water gathers":
{"note":"Which water a place is."},
+ "fix(render): a shadow is cast by what the camera's level of detail draws, and the terrain slides its vertices by the eye in every pass":
+ {"note":"Shadows follow what the camera draws."},
};