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.

feat(render): temporal history follows the render origin when it steps

yhhb22e1ded9683
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8fb6c09c..9a3c63a8b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,7 @@ published separately; it ships inside the editor.
### Added
-- **A render origin for large worlds.** `setRenderOriginStep(step)` turns on drawing relative to a point near the camera: the origin is the camera's position rounded to `step` and moves only when the camera is further than `step` from it, the renderer takes every mesh, light, probe and shadow bound from it before the GPU sees them, and the camera's view-projection is built for the same origin, so a world tens of kilometres across stops jittering. It is off by default and changes nothing then. It covers meshes, lights, probes, occluders, planar reflections, sprites, text, particles (GPU too), trails, tilemaps, world-space UI, 2D shadows, and the terrain, planet, water, foliage, height fog and cloud shaders, which read the world's own position back from a new `u_renderOrigin` frame constant. Debug and custom draws stay in the world's coordinates, and the temporal effects settle for a frame when the origin steps.
+- **A render origin for large worlds.** `setRenderOriginStep(step)` turns on drawing relative to a point near the camera: the origin is the camera's position rounded to `step` and moves only when the camera is further than `step` from it, the renderer takes every mesh, light, probe and shadow bound from it before the GPU sees them, and the camera's view-projection is built for the same origin, so a world tens of kilometres across stops jittering. It is off by default and changes nothing then. It covers meshes, lights, probes, occluders, planar reflections, sprites, text, particles (GPU too), trails, tilemaps, world-space UI, 2D shadows, and the terrain, planet, water, foliage, height fog and cloud shaders, which read the world's own position back from a new `u_renderOrigin` frame constant. Debug and custom draws stay in the world's coordinates, and the previous frame's view and meshes are taken to the new origin when it steps, so motion vectors and temporal effects carry on.
- **A planet's ground has the layers' textures.** `Planet.layers` names up to four `.esterrainlayer` files, as on a flat terrain: each layer's albedo and normal map are read on the planes of the world's axes by how much the ground faces them, with a wider read mixed in far off so the repeat is lost. Without layers the ground is still the four colours.
- **A planet's ground collides.** `Planet` takes `collision`, `collisionDistance`, `collisionLayer` and `friction` as flat terrain does: the finest nodes round every moving 3D body and character are built as collision geometry from the same samples the ground is drawn from, one chunk layer to each face of the cube.
- **Height Fog by height above a planet.** `heightFog` takes `u_planetRadius` and the planet's centre (`u_planetX/Y/Z`): with a radius above 0 the fog thins with the height above the ground's sphere and not with world Y, so a round world is hazy at its horizon and clear from orbit.
diff --git a/docs/astro/src/content/docs/graphics/camera.mdx b/docs/astro/src/content/docs/graphics/camera.mdx
index cbe496406..91cc9e3be 100644
--- a/docs/astro/src/content/docs/graphics/camera.mdx
+++ b/docs/astro/src/content/docs/graphics/camera.mdx
@@ -346,7 +346,7 @@ setRenderOriginStep(1024); // metres; 0 (the default) draws relative to the worl
The origin is the camera's position rounded to `step`, and it stays where it is until the camera is further than `step` from it, so shadow maps, the light grid and the other caches laid out on the world's grid change only when it does. The renderer takes every mesh, light, probe, particle, trail, tilemap and world-space UI from the origin before the GPU sees it, and the camera's view-projection is built for the same origin, so the picture is the same and the numbers in it are small. Terrain, planets, water, foliage, height fog and the sky's clouds are laid by the world's own position and read it back from the origin, so they do not slide when it steps.
-Things that stay in the world's own coordinates, and are not affected: transforms, physics, networking, `screenToWorld` and picking. Draw callbacks and debug drawing are handed the world's view-projection and draw in the world's coordinates, so they keep the precision a single float has far from zero. One origin serves a frame: with several cameras it follows the screen's own, and a camera far from it draws at single-float precision. Temporal effects (motion vectors, the global-illumination history) and GPU particles settle for a frame when the origin steps.
+Things that stay in the world's own coordinates, and are not affected: transforms, physics, networking, `screenToWorld` and picking. Draw callbacks and debug drawing are handed the world's view-projection and draw in the world's coordinates, so they keep the precision a single float has far from zero. One origin serves a frame: with several cameras it follows the screen's own, and a camera far from it draws at single-float precision. The previous frame's view and meshes are taken to the new origin when it steps, so motion vectors and temporal effects carry on; the global-illumination voxels settle for a frame.
## See also
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx b/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx
index d4dcc2286..3754234c6 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx
@@ -318,7 +318,7 @@ setRenderOriginStep(1024); // 单位米;0(默认)是相对世界自己的
原点取相机位置按 `step` 取整,相机离开它超过 `step` 才会移动,所以阴影图、光照网格这些按世界网格布置的缓存只在它移动时才变。渲染器在交给 GPU 之前,把每个网格、光、探针、粒子、拖尾、瓦片图和世界空间 UI 都从原点算起,相机的视图投影也为同一个原点构造,所以画面一样,里面的数字却很小。地形、星球、水、植被、高度雾和天空的云按世界位置铺设,会从原点把世界位置读回来,原点移动时不会滑动。
-仍用世界自己坐标、不受影响的有:变换、物理、网络、`screenToWorld` 和拾取。绘制回调和调试绘制拿到的是世界的视图投影,按世界坐标画,所以在远离零点处保持的是单精度浮点原有的精度。一帧只有一个原点:多个相机时它跟随屏幕自己的相机,远离它的相机以单精度浮点的精度绘制。原点移动的那一帧,时间性效果(运动矢量、全局光照历史)和 GPU 粒子会各自缓一帧。
+仍用世界自己坐标、不受影响的有:变换、物理、网络、`screenToWorld` 和拾取。绘制回调和调试绘制拿到的是世界的视图投影,按世界坐标画,所以在远离零点处保持的是单精度浮点原有的精度。一帧只有一个原点:多个相机时它跟随屏幕自己的相机,远离它的相机以单精度浮点的精度绘制。原点移动时,上一帧的视图和网格会被带到新原点,运动矢量和时间性效果照常继续;全局光照的体素缓一帧。
## 参见
diff --git a/src/esengine/renderer/RenderOrigin.hpp b/src/esengine/renderer/RenderOrigin.hpp
index 5b1ab12e7..1788bad1d 100644
--- a/src/esengine/renderer/RenderOrigin.hpp
+++ b/src/esengine/renderer/RenderOrigin.hpp
@@ -12,6 +12,7 @@
#pragma once
#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
namespace esengine {
@@ -28,4 +29,18 @@ inline glm::vec3 rebased(const glm::vec3& world) {
return glm::vec3(glm::dvec3(world) - renderOrigin());
}
+/// A view-projection made for `origin`, for a point given from the origin now: the point from the old origin is the new one's plus the step between them.
+inline glm::mat4 viewProjectionFromOrigin(const glm::mat4& viewProjection, const glm::dvec3& origin) {
+ const glm::dvec3 step = renderOrigin() - origin;
+ if (step == glm::dvec3(0.0)) return viewProjection;
+ return viewProjection * glm::translate(glm::mat4(1.0f), glm::vec3(step));
+}
+
+/// A model placed from `origin`, as it is placed from the origin now.
+inline glm::mat4 modelFromOrigin(const glm::mat4& model, const glm::dvec3& origin) {
+ const glm::dvec3 step = origin - renderOrigin();
+ if (step == glm::dvec3(0.0)) return model;
+ return glm::translate(glm::mat4(1.0f), glm::vec3(step)) * model;
+}
+
} // namespace esengine
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index 196187a25..4da854e1d 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -12,6 +12,7 @@
*/
#include "./PostProcessPipeline.hpp"
+#include "../RenderOrigin.hpp"
#include "./RenderContext.hpp"
#include "./VoxelGI.hpp"
#include "../rhi/GfxDevice.hpp"
@@ -424,8 +425,9 @@ bool PostProcessPipeline::cameraMotionWanted() {
void PostProcessPipeline::trackView(const glm::mat4& viewProjection, u64 key) {
TemporalState& state = temporal_[key];
temporalKey_ = key;
- state.previous = state.frame > 0 ? state.current : viewProjection;
+ state.previous = state.frame > 0 ? viewProjectionFromOrigin(state.current, state.origin) : viewProjection;
state.current = viewProjection;
+ state.origin = renderOrigin();
++state.frame;
}
@@ -468,9 +470,10 @@ constexpr u32 kJitterPhases = 8;
glm::vec2 PostProcessPipeline::beginTemporalView(const glm::mat4& viewProjection, u64 key) {
TemporalState& state = temporal_[key];
temporalKey_ = key;
- if (state.frame > 0) state.previous = state.current;
+ if (state.frame > 0) state.previous = viewProjectionFromOrigin(state.current, state.origin);
else state.previous = viewProjection;
state.current = viewProjection;
+ state.origin = renderOrigin();
++state.frame;
// A chain that did not write last frame leaves history of a picture this one no
// longer follows (a camera that was off, a frame the host skipped).
@@ -488,7 +491,7 @@ PostProcessPipeline::TemporalView PostProcessPipeline::peekTemporalView(const gl
const auto it = temporal_.find(key);
const u32 frame = it == temporal_.end() ? 0u : it->second.frame;
TemporalView view;
- view.previous = frame > 0 ? it->second.current : viewProjection;
+ view.previous = frame > 0 ? viewProjectionFromOrigin(it->second.current, it->second.origin) : viewProjection;
// The step beginTemporalView will take next: one frame on, the same phase of the same sequence.
const u32 phase = ((frame + 1) % kJitterPhases) + 1;
const f32 w = static_cast<f32>(std::max(width_, 1u));
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.hpp b/src/esengine/renderer/frame/PostProcessPipeline.hpp
index 525b4119b..cc1377590 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.hpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.hpp
@@ -677,6 +677,8 @@ private:
struct TemporalState {
glm::mat4 current{1.0f};
glm::mat4 previous{1.0f};
+ /// The render origin `current` was made for: the next frame's `previous` is it, taken to the origin then.
+ glm::dvec3 origin{0.0};
u32 frame = 0;
/// The frame whose chain last wrote the history; a gap means the history
/// is of a picture this one no longer follows.
diff --git a/src/esengine/renderer/frame/VoxelGI.cpp b/src/esengine/renderer/frame/VoxelGI.cpp
index 719707da0..e4830d4c3 100644
--- a/src/esengine/renderer/frame/VoxelGI.cpp
+++ b/src/esengine/renderer/frame/VoxelGI.cpp
@@ -3,6 +3,8 @@
#include "./VoxelGI.hpp"
#include "./FrameConstants.hpp"
#include "./RenderContext.hpp"
+#include "../RenderOrigin.hpp"
+#include <glm/gtc/type_ptr.hpp>
#include "../rhi/GfxDevice.hpp"
#include "../../core/Log.hpp"
@@ -411,7 +413,13 @@ void VoxelGI::writeParams(const VoxelGIParams& p) {
previousVoxelSize_ = vs;
block.view[1] = historyValid_ ? 1.0f : 0.0f;
block.view[2] = std::clamp(p.blend, 0.01f, 1.0f);
- std::copy(previousViewProjection_, previousViewProjection_ + 16, block.prevViewProjection);
+ // A view of a frame drawn from another origin is taken to this one, and what was gathered under it is not history.
+ if (previousOrigin_ != renderOrigin()) historyValid_ = false;
+ {
+ const glm::mat4 previous = viewProjectionFromOrigin(glm::make_mat4(previousViewProjection_), previousOrigin_);
+ std::copy(glm::value_ptr(previous), glm::value_ptr(previous) + 16, block.prevViewProjection);
+ }
+ previousOrigin_ = renderOrigin();
const glm::mat4& vp = context_.viewProjection();
for (int c = 0; c < 4; ++c) for (int r = 0; r < 4; ++r) previousViewProjection_[c * 4 + r] = vp[c][r];
device_.updateBuffer(params_, 0, &block, sizeof(block));
diff --git a/src/esengine/renderer/frame/VoxelGI.hpp b/src/esengine/renderer/frame/VoxelGI.hpp
index 308935302..76ee454de 100644
--- a/src/esengine/renderer/frame/VoxelGI.hpp
+++ b/src/esengine/renderer/frame/VoxelGI.hpp
@@ -27,6 +27,7 @@
#include "../graph/RenderGraph.hpp"
#include "../rhi/GfxEnums.hpp"
+#include <glm/glm.hpp>
#include <string>
#include <unordered_map>
@@ -107,6 +108,8 @@ private:
/// Whether the picture the other one holds is a gather of the view the last frame saw, to blend with.
bool historyValid_ = false;
f32 previousViewProjection_[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
+ /// The render origin that view-projection was made for.
+ glm::dvec3 previousOrigin_{0.0};
f32 previousVoxelSize_ = 0.0f;
/// The window's origin, in cells, as of the frame being drawn.
i32 origin_[3] = {0, 0, 0};
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index 41a519244..f18f83f0d 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -531,8 +531,10 @@ void MeshPlugin::trackPose(const RenderFrameContext& ctx, Entity entity, const g
// standing where it stands says "did not move".
const bool continued = pose.frame != 0 && pose.frame + 1 == ctx.motion_frame;
if (continued) {
- pose.previous = pose.current;
+ // Where it was drawn from the origin of the frame before, as it is placed from the origin now.
+ pose.previous = modelFromOrigin(pose.current, pose.origin);
pose.bonesPrevious = pose.bonesCurrent;
+ for (glm::mat4& bone : pose.bonesPrevious) bone = modelFromOrigin(bone, pose.origin);
} else if (pose.frame != ctx.motion_frame) {
pose.previous = model;
if (bones) pose.bonesPrevious = *bones;
@@ -542,6 +544,7 @@ void MeshPlugin::trackPose(const RenderFrameContext& ctx, Entity entity, const g
if (bones) pose.bonesCurrent = *bones;
else pose.bonesCurrent.clear();
pose.frame = ctx.motion_frame;
+ pose.origin = renderOrigin();
}
namespace {
diff --git a/src/esengine/renderer/plugins/MeshPlugin.hpp b/src/esengine/renderer/plugins/MeshPlugin.hpp
index 684d5b686..7b3e39497 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.hpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.hpp
@@ -66,6 +66,8 @@ private:
std::vector<glm::mat4> bonesPrevious;
std::vector<glm::mat4> bonesCurrent;
u64 frame = 0;
+ /// The render origin `current` was placed from.
+ glm::dvec3 origin{0.0};
};
std::unordered_map<u64, Pose> poses_;
void trackPose(const RenderFrameContext& ctx, Entity entity, const glm::mat4& model,
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 2003c32f1..aabfedacf 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2916,6 +2916,8 @@ export const NOTED = {
{"note":"The editor's own eye shows the scene's post-processing."},
"feat(terrain): the maths of ground on a cube sphere and the nodes drawn round an eye":
{"internal":"The first layer of spherical terrain: pure maths and node selection, not yet drawn."},
+ "feat(render): temporal history follows the render origin when it steps":
+ {"internal":"The render origin of this release, extended."},
"feat(render): the render origin covers every renderer and the terrain, water and sky shaders":
{"internal":"The render origin of this release, extended."},
"feat(render): a render origin keeps a large world's meshes, lights and probes precise":