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): the render origin covers every renderer and the terrain, water and sky shaders

yhh46bb3a0a6eed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1d0d1564c..b8fb6c09c 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. The first slice covers meshes, lights, probes, occluders and planar reflections; sprites, particles, trails, UI in the world, post-process height effects and terrain, water and foliage follow.
+- **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 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 cc78df980..cbe496406 100644
--- a/docs/astro/src/content/docs/graphics/camera.mdx
+++ b/docs/astro/src/content/docs/graphics/camera.mdx
@@ -334,6 +334,20 @@ split-screen, left and right halves:
- **Prefer blending** (`setViewTarget` with `time`) over teleporting the camera for
scene and cutscene transitions.
+## Large worlds
+
+A position held in a single float is precise only near zero: 50 km out a step is 4 mm, and a mesh there jitters as the camera moves. `setRenderOriginStep(step)` draws the world relative to a point near the camera instead:
+
+```ts
+import { setRenderOriginStep } from 'esengine';
+
+setRenderOriginStep(1024); // metres; 0 (the default) draws relative to the world's own zero
+```
+
+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.
+
## See also
- [Screen & Design Resolution](/docs/core-concepts/screen/) — the design-resolution
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 1fceeb634..d4dcc2286 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/camera.mdx
@@ -306,6 +306,20 @@ updateCameraAspectRatio(app.world, canvas.width / canvas.height);
- **像素画开 `pixelPerfect`** 消除亚像素抖动。
- 场景与过场切换**优先用混合**(带 `time` 的 `setViewTarget`),而不是瞬移相机。
+## 大世界
+
+一个单精度浮点只在零点附近精确:离零点 50 公里时一步是 4 毫米,那里的网格会随相机移动而抖动。`setRenderOriginStep(step)` 改为相对相机附近的一个点来绘制世界:
+
+```ts
+import { setRenderOriginStep } from 'esengine';
+
+setRenderOriginStep(1024); // 单位米;0(默认)是相对世界自己的零点绘制
+```
+
+原点取相机位置按 `step` 取整,相机离开它超过 `step` 才会移动,所以阴影图、光照网格这些按世界网格布置的缓存只在它移动时才变。渲染器在交给 GPU 之前,把每个网格、光、探针、粒子、拖尾、瓦片图和世界空间 UI 都从原点算起,相机的视图投影也为同一个原点构造,所以画面一样,里面的数字却很小。地形、星球、水、植被、高度雾和天空的云按世界位置铺设,会从原点把世界位置读回来,原点移动时不会滑动。
+
+仍用世界自己坐标、不受影响的有:变换、物理、网络、`screenToWorld` 和拾取。绘制回调和调试绘制拿到的是世界的视图投影,按世界坐标画,所以在远离零点处保持的是单精度浮点原有的精度。一帧只有一个原点:多个相机时它跟随屏幕自己的相机,远离它的相机以单精度浮点的精度绘制。原点移动的那一帧,时间性效果(运动矢量、全局光照历史)和 GPU 粒子会各自缓一帧。
+
## 参见
- [屏幕与设计分辨率](/docs/zh-cn/core-concepts/screen/) —— 设计分辨率适配、缩放模式、
diff --git a/sdk/src/foliage/foliageShader.ts b/sdk/src/foliage/foliageShader.ts
index fbad1f43e..d25118de2 100644
--- a/sdk/src/foliage/foliageShader.ts
+++ b/sdk/src/foliage/foliageShader.ts
@@ -39,7 +39,8 @@ const FOLIAGE = `#pragma shader "Foliage"
void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
float weight = es_vertexColor.r;
float phase = es_vertexColor.g * 6.2831853;
- vec3 anchor = (esInstanceModel() * vec4(local, 1.0)).xyz;
+ // The wind is in the world, so its phase is by the world's own position, not the render origin's.
+ vec3 anchor = (esInstanceModel() * vec4(local, 1.0)).xyz + u_renderOrigin.xyz;
float angle = radians(u_windDirection);
vec2 dir = vec2(sin(angle), cos(angle));
float t = u_time.x * u_windSpeed;
@@ -64,7 +65,7 @@ void esMaterialVertex(inout vec3 local, inout vec3 localNormal, in vec2 uv) {
fn esMaterialVertex(local : ptr<function, vec3f>, localNormal : ptr<function, vec3f>, uv : vec2f) {
let weight = es_vertexColor.r;
let phase = es_vertexColor.g * 6.2831853;
- let anchor = (esInstanceModel() * vec4f(*local, 1.0)).xyz;
+ let anchor = (esInstanceModel() * vec4f(*local, 1.0)).xyz + frame.renderOrigin.xyz;
let angle = radians(mc.u_windDirection);
let dir = vec2f(sin(angle), cos(angle));
let t = tc.u_time.x * mc.u_windSpeed;
diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.ts
index 6b73cf20e..7c6e7b94f 100644
--- a/sdk/src/postprocess/sources/atmosphere.ts
+++ b/sdk/src/postprocess/sources/atmosphere.ts
@@ -213,11 +213,12 @@ void main() {
// Clouds that cast shadows are in the world, not on the camera: the pattern is anchored to where the camera stands.
bool shadowed = u_cloudShadow > 0.001;
- vec3 anchor = shadowed ? vec3(entry.x, 0.0, entry.z) / max(u_unitsPerKm, 1e-3) : vec3(0.0);
+ vec3 entryWorld = entry + u_renderOrigin.xyz;
+ vec3 anchor = shadowed ? vec3(entryWorld.x, 0.0, entryWorld.z) / max(u_unitsPerKm, 1e-3) : vec3(0.0);
float windAngle = radians(u_windDirection);
vec3 windDrift = vec3(sin(windAngle), 0.0, cos(windAngle)) * (u_windSpeed * u_time.x) + anchor;
if (geometry) {
- float lit = cloudShadowAt(worldFromDepth(v_texCoord, depth), entry, sun, max(u_clouds, overcast * 0.95), overcast, windDrift - anchor);
+ float lit = cloudShadowAt(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, entryWorld, sun, max(u_clouds, overcast * 0.95), overcast, windDrift - anchor);
fragColor = vec4(src.rgb * lit, src.a);
return;
}
@@ -472,12 +473,13 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
let overcast = clamp(mc.u_overcast, 0.0, 1.0);
let haze = mc.u_haze * (1.0 + overcast * 2.0);
+ let entryWorld = entry + frame.renderOrigin.xyz;
var anchor = vec3f(0.0);
- if (mc.u_cloudShadow > 0.001) { anchor = vec3f(entry.x, 0.0, entry.z) / max(mc.u_unitsPerKm, 1e-3); }
+ if (mc.u_cloudShadow > 0.001) { anchor = vec3f(entryWorld.x, 0.0, entryWorld.z) / max(mc.u_unitsPerKm, 1e-3); }
let windAngle = radians(mc.u_windDirection);
let windDrift = vec3f(sin(windAngle), 0.0, cos(windAngle)) * (mc.u_windSpeed * tc.u_time.x) + anchor;
if (geometry) {
- let lit = cloudShadowAt(worldFromDepth(v.v_texCoord, depth), entry, sun, max(mc.u_clouds, overcast * 0.95), overcast, windDrift - anchor, v.pos.xy);
+ let lit = cloudShadowAt(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, entryWorld, sun, max(mc.u_clouds, overcast * 0.95), overcast, windDrift - anchor, v.pos.xy);
return vec4f(src.rgb * lit, src.a);
}
@@ -691,7 +693,8 @@ float cloudLight(vec3 world, float camY, vec3 sun, vec3 drift) {
/// The height of a point above the ground the fog lies on: world Y on a flat world, the distance above the sphere's surface on a planet (never below it: inside the planet the fog is the ground's, not an exponential of a depth).
float fogAltitude(vec3 p) {
- return u_planetRadius > 0.0 ? max(length(p - vec3(u_planetX, u_planetY, u_planetZ)) - u_planetRadius, 0.0) : p.y;
+ vec3 a = p + u_renderOrigin.xyz;
+ return u_planetRadius > 0.0 ? max(length(a - vec3(u_planetX, u_planetY, u_planetZ)) - u_planetRadius, 0.0) : a.y;
}
/// What the exponential density integrates to along a ray: the eye's density, the length,
/// and the term for how fast the ray leaves the fog. Written so a level ray (no climb)
@@ -723,7 +726,7 @@ void main() {
float climb = dir.y;
if (u_planetRadius > 0.0) {
// The ground curves away: the climb is the rise between the ray's ends, and a sky ray climbs as it points.
- vec3 up = normalize(entry + dir * u_startDistance - vec3(u_planetX, u_planetY, u_planetZ));
+ vec3 up = normalize(entry + dir * u_startDistance + u_renderOrigin.xyz - vec3(u_planetX, u_planetY, u_planetZ));
climb = sky ? dot(dir, up) : (fogAltitude(target) - eyeAlt) / effective;
}
float tau = opticalDepth(eyeAlt, climb, effective);
@@ -766,7 +769,7 @@ void main() {
for (int i = 0; i < VOLUME_STEPS; ++i) {
float t = u_startDistance + (float(i) + jitter) * ds;
vec3 p = entry + dir * t;
- if (i % 3 == 0) shade = cloudLight(p, entry.y, sun, drift);
+ if (i % 3 == 0) shade = cloudLight(p + u_renderOrigin.xyz, entry.y + u_renderOrigin.y, sun, drift);
float dens = u_density * exp(clamp(-u_heightFalloff * (fogAltitude(p) - u_baseHeight), -60.0, 60.0));
lit += transmit * dens * ds * sunVisibility(p, tiles.x, tiles.y) * shade;
transmit *= exp(-dens * ds);
@@ -838,8 +841,9 @@ fn cloudLight(world : vec3f, camY : f32, sun : vec3f, drift : vec3f) -> f32 {
}
fn fogAltitude(p : vec3f) -> f32 {
- if (mc.u_planetRadius > 0.0) { return max(length(p - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ)) - mc.u_planetRadius, 0.0); }
- return p.y;
+ let a = p + frame.renderOrigin.xyz;
+ if (mc.u_planetRadius > 0.0) { return max(length(a - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ)) - mc.u_planetRadius, 0.0); }
+ return a.y;
}
fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
let k = mc.u_heightFalloff;
@@ -869,7 +873,7 @@ fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
let eyeAlt = fogAltitude(entry + dir * mc.u_startDistance);
var climb = dir.y;
if (mc.u_planetRadius > 0.0) {
- let up = normalize(entry + dir * mc.u_startDistance - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ));
+ let up = normalize(entry + dir * mc.u_startDistance + frame.renderOrigin.xyz - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ));
if (sky) { climb = dot(dir, up); } else { climb = (fogAltitude(tgt) - eyeAlt) / effective; }
}
let tau = opticalDepth(eyeAlt, climb, effective);
@@ -909,7 +913,7 @@ fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {
let t = mc.u_startDistance + (f32(i) + jitter) * ds;
let p = entry + dir * t;
- if (i % 3 == 0) { shade = cloudLight(p, entry.y, sun, drift); }
+ if (i % 3 == 0) { shade = cloudLight(p + frame.renderOrigin.xyz, entry.y + frame.renderOrigin.y, sun, drift); }
let dens = mc.u_density * exp(clamp(-mc.u_heightFalloff * (fogAltitude(p) - mc.u_baseHeight), -60.0, 60.0));
lit = lit + transmit * dens * ds * sunVisibility(p, found.tiles.x, found.tiles.y) * shade;
transmit = transmit * exp(-dens * ds);
diff --git a/sdk/src/terrain/planetPlugin.ts b/sdk/src/terrain/planetPlugin.ts
index af83ca0dc..b574e3d8b 100644
--- a/sdk/src/terrain/planetPlugin.ts
+++ b/sdk/src/terrain/planetPlugin.ts
@@ -27,6 +27,7 @@ import { getResourceManager, provideTextureContent, withdrawTextureContent } fro
import { TextureContent } from '../wasm';
import { EditorView, editorViewEye } from '../camera/EditorView';
import { log } from '../util/logger';
+import { renderOrigin } from '../render/renderOrigin';
import { bakesOf } from '../runtime/bakes';
import type { Color, Entity } from '../types';
import { PageTable, PAGE_SIDE, PAGE_TEXELS, PATCH_CELLS, type PageImage } from './pages';
@@ -333,7 +334,9 @@ export class PlanetPlugin implements Plugin {
this.pack(app, built);
const set = (name: string, value: Parameters<typeof Material.setUniform>[2]): void => { Material.setUniform(built.instance, name, value); };
- set('u_planetCenter', { x: t.worldPosition.x, y: t.worldPosition.y, z: t.worldPosition.z, w: 0 });
+ // From the render origin, as the nodes' boxes are: the vertex stage puts its points in them.
+ const o = renderOrigin();
+ set('u_planetCenter', { x: t.worldPosition.x - o.x, y: t.worldPosition.y - o.y, z: t.worldPosition.z - o.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: eye[0], y: eye[1], z: eye[2], w: 1 });
const d = built.data;
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
index 312729475..4f5b70fd0 100644
--- a/sdk/src/terrain/terrainShader.ts
+++ b/sdk/src/terrain/terrainShader.ts
@@ -230,10 +230,12 @@ void main() {
highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
highp vec3 N = vec3(0.0, 1.0, 0.0);
#endif
+ // Where the ground is in the world, for what is laid on it by position (the layers' tiles, the noise, the bounds): the draw's own P is from the render origin.
+ highp vec3 Pw = P + u_renderOrigin.xyz;
// A paged ground reads its paint and its normal where the vertex said it lies in the page, filtered; off its area there is none.
vec4 paint = v_color;
if (u_terrainPages > 0.5) {
- vec2 lp = P.xz - u_terrainOrigin.xz;
+ vec2 lp = Pw.xz - u_terrainOrigin.xz;
if (lp.x < u_terrainBounds.x || lp.y < u_terrainBounds.y || lp.x > u_terrainBounds.z || lp.y > u_terrainBounds.w) discard;
paint = texture(u_paintPages, v_color.xy);
vec2 nxz = texture(u_heightPages, v_color.xy).ba * 2.0 - 1.0;
@@ -242,8 +244,8 @@ void main() {
vec4 w = max(paint, vec4(0.0));
w /= max(dot(w, vec4(1.0)), 1e-4);
// Each layer's say wanders by slow noise of its own, so where two meet the edge is ragged, not the paint's gradient.
- vec4 wander = vec4(tnNoise(P.xz / (u_tile0 * 2.3) + 3.0), tnNoise(P.xz / (u_tile1 * 2.3) + 11.0),
- tnNoise(P.xz / (u_tile2 * 2.3) + 23.0), tnNoise(P.xz / (u_tile3 * 2.3) + 37.0));
+ vec4 wander = vec4(tnNoise(Pw.xz / (u_tile0 * 2.3) + 3.0), tnNoise(Pw.xz / (u_tile1 * 2.3) + 11.0),
+ tnNoise(Pw.xz / (u_tile2 * 2.3) + 23.0), tnNoise(Pw.xz / (u_tile3 * 2.3) + 37.0));
w *= mix(vec4(1.0), 0.35 + 1.3 * wander, u_blendNoise);
w /= max(dot(w, vec4(1.0)), 1e-4);
// The strongest layer wins its seam more cleanly the higher the sharpness.
@@ -257,17 +259,17 @@ void main() {
float near = (1.0 - smoothstep(u_tile0 * 20.0, u_tile0 * 45.0, eye)) * u_antiTile;
float far = smoothstep(u_tile0 * 6.0, u_tile0 * 40.0, eye) * 0.65;
vec3 s0 = vec3(0.0), s1 = vec3(0.0), s2 = vec3(0.0), s3 = vec3(0.0);
- if (w.x > 1e-3) s0 = tnRead(u_layer0, P.xz, u_tile0, near, far, dpx.xz, dpy.xz);
- if (w.y > 1e-3) s1 = tnRead(u_layer1, P.xz, u_tile1, near, far, dpx.xz, dpy.xz);
- if (w.z > 1e-3) s2 = tnRead(u_layer2, P.xz, u_tile2, near, far, dpx.xz, dpy.xz);
+ if (w.x > 1e-3) s0 = tnRead(u_layer0, Pw.xz, u_tile0, near, far, dpx.xz, dpy.xz);
+ if (w.y > 1e-3) s1 = tnRead(u_layer1, Pw.xz, u_tile1, near, far, dpx.xz, dpy.xz);
+ if (w.z > 1e-3) s2 = tnRead(u_layer2, Pw.xz, u_tile2, near, far, dpx.xz, dpy.xz);
if (w.w > 1e-3) {
// Rock is seen from three sides, in proportion to how the ground faces them, so a sheer face is not stretched; a side that
// carries almost none of it is not read.
vec3 axis = pow(abs(N), vec3(4.0));
axis /= dot(axis, vec3(1.0));
- s3 = tnRead(u_layer3, P.xz, u_tile3, near, far, dpx.xz, dpy.xz) * axis.y;
- if (axis.x > 0.02) s3 += textureGrad(u_layer3, P.zy / u_tile3, dpx.zy / u_tile3, dpy.zy / u_tile3).rgb * axis.x;
- if (axis.z > 0.02) s3 += textureGrad(u_layer3, P.xy / u_tile3, dpx.xy / u_tile3, dpy.xy / u_tile3).rgb * axis.z;
+ s3 = tnRead(u_layer3, Pw.xz, u_tile3, near, far, dpx.xz, dpy.xz) * axis.y;
+ if (axis.x > 0.02) s3 += textureGrad(u_layer3, Pw.zy / u_tile3, dpx.zy / u_tile3, dpy.zy / u_tile3).rgb * axis.x;
+ if (axis.z > 0.02) s3 += textureGrad(u_layer3, Pw.xy / u_tile3, dpx.xy / u_tile3, dpy.xy / u_tile3).rgb * axis.z;
}
// Then by relief: each layer stands as high as it is bright, and where two meet the higher one shows.
vec4 heights = vec4(tnLuma(s0), tnLuma(s1), tnLuma(s2), tnLuma(s3));
@@ -278,7 +280,7 @@ void main() {
w = mix(w, hw, u_heightBlend);
vec3 albedo = s0 * u_tint0.rgb * w.x + s1 * u_tint1.rgb * w.y + s2 * u_tint2.rgb * w.z + s3 * u_tint3.rgb * w.w;
- float broad = tnNoise(P.xz / (u_tile0 * 7.3)) * 0.6 + tnNoise(P.xz / (u_tile0 * 29.0) + 5.0) * 0.4;
+ float broad = tnNoise(Pw.xz / (u_tile0 * 7.3)) * 0.6 + tnNoise(Pw.xz / (u_tile0 * 29.0) + 5.0) * 0.4;
albedo *= mix(1.0, 0.7 + 0.6 * broad, u_macroVariation);
// The relief, as a bump from the screen-space change of its height, faded out before it is finer than a pixel.
@@ -387,19 +389,20 @@ fn packNormal(n : vec3f) -> vec2f {
let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
var N = vec3f(0.0, 1.0, 0.0);
#endif
+ let Pw = P + frame.renderOrigin.xyz;
let pagePaint = textureSample(u_paintPages, u_paintPages_s, v.v_color.xy);
let pageNormal = textureSample(u_heightPages, u_heightPages_s, v.v_color.xy).ba * 2.0 - vec2f(1.0);
var paint = v.v_color;
if (mc.u_terrainPages > 0.5) {
- let lp = P.xz - mc.u_terrainOrigin.xz;
+ let lp = Pw.xz - mc.u_terrainOrigin.xz;
if (lp.x < mc.u_terrainBounds.x || lp.y < mc.u_terrainBounds.y || lp.x > mc.u_terrainBounds.z || lp.y > mc.u_terrainBounds.w) { discard; }
paint = pagePaint;
N = normalize(vec3f(pageNormal.x, sqrt(max(1.0 - dot(pageNormal, pageNormal), 0.0)), pageNormal.y));
}
var w = max(paint, vec4f(0.0));
w = w / max(dot(w, vec4f(1.0)), 1e-4);
- let wander = vec4f(tnNoise(P.xz / (mc.u_tile0 * 2.3) + 3.0), tnNoise(P.xz / (mc.u_tile1 * 2.3) + 11.0),
- tnNoise(P.xz / (mc.u_tile2 * 2.3) + 23.0), tnNoise(P.xz / (mc.u_tile3 * 2.3) + 37.0));
+ let wander = vec4f(tnNoise(Pw.xz / (mc.u_tile0 * 2.3) + 3.0), tnNoise(Pw.xz / (mc.u_tile1 * 2.3) + 11.0),
+ tnNoise(Pw.xz / (mc.u_tile2 * 2.3) + 23.0), tnNoise(Pw.xz / (mc.u_tile3 * 2.3) + 37.0));
w = w * mix(vec4f(1.0), vec4f(0.35) + 1.3 * wander, vec4f(mc.u_blendNoise));
w = w / max(dot(w, vec4f(1.0)), 1e-4);
w = pow(w, vec4f(mc.u_sharpness));
@@ -416,15 +419,15 @@ fn packNormal(n : vec3f) -> vec2f {
var s1 = vec3f(0.0);
var s2 = vec3f(0.0);
var s3 = vec3f(0.0);
- if (w.x > 1e-3) { s0 = tnRead(u_layer0, u_layer0_s, P.xz, mc.u_tile0, near, far, dpx.xz, dpy.xz); }
- if (w.y > 1e-3) { s1 = tnRead(u_layer1, u_layer1_s, P.xz, mc.u_tile1, near, far, dpx.xz, dpy.xz); }
- if (w.z > 1e-3) { s2 = tnRead(u_layer2, u_layer2_s, P.xz, mc.u_tile2, near, far, dpx.xz, dpy.xz); }
+ if (w.x > 1e-3) { s0 = tnRead(u_layer0, u_layer0_s, Pw.xz, mc.u_tile0, near, far, dpx.xz, dpy.xz); }
+ if (w.y > 1e-3) { s1 = tnRead(u_layer1, u_layer1_s, Pw.xz, mc.u_tile1, near, far, dpx.xz, dpy.xz); }
+ if (w.z > 1e-3) { s2 = tnRead(u_layer2, u_layer2_s, Pw.xz, mc.u_tile2, near, far, dpx.xz, dpy.xz); }
if (w.w > 1e-3) {
var axis = pow(abs(N), vec3f(4.0));
axis = axis / dot(axis, vec3f(1.0));
- s3 = tnRead(u_layer3, u_layer3_s, P.xz, mc.u_tile3, near, far, dpx.xz, dpy.xz) * axis.y;
- if (axis.x > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, P.zy / mc.u_tile3, dpx.zy / mc.u_tile3, dpy.zy / mc.u_tile3).rgb * axis.x; }
- if (axis.z > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, P.xy / mc.u_tile3, dpx.xy / mc.u_tile3, dpy.xy / mc.u_tile3).rgb * axis.z; }
+ s3 = tnRead(u_layer3, u_layer3_s, Pw.xz, mc.u_tile3, near, far, dpx.xz, dpy.xz) * axis.y;
+ if (axis.x > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, Pw.zy / mc.u_tile3, dpx.zy / mc.u_tile3, dpy.zy / mc.u_tile3).rgb * axis.x; }
+ if (axis.z > 0.02) { s3 = s3 + textureSampleGrad(u_layer3, u_layer3_s, Pw.xy / mc.u_tile3, dpx.xy / mc.u_tile3, dpy.xy / mc.u_tile3).rgb * axis.z; }
}
let heights = vec4f(tnLuma(s0), tnLuma(s1), tnLuma(s2), tnLuma(s3));
let stand = heights + w * 1.5;
@@ -434,7 +437,7 @@ fn packNormal(n : vec3f) -> vec2f {
w = mix(w, hw, vec4f(mc.u_heightBlend));
var albedo = s0 * mc.u_tint0.rgb * w.x + s1 * mc.u_tint1.rgb * w.y + s2 * mc.u_tint2.rgb * w.z + s3 * mc.u_tint3.rgb * w.w;
- let broad = tnNoise(P.xz / (mc.u_tile0 * 7.3)) * 0.6 + tnNoise(P.xz / (mc.u_tile0 * 29.0) + 5.0) * 0.4;
+ let broad = tnNoise(Pw.xz / (mc.u_tile0 * 7.3)) * 0.6 + tnNoise(Pw.xz / (mc.u_tile0 * 29.0) + 5.0) * 0.4;
albedo = albedo * mix(1.0, 0.7 + 0.6 * broad, mc.u_macroVariation);
let relief = dot(heights, w) * mc.u_relief;
@@ -617,9 +620,11 @@ void main() {
highp vec3 P = vec3(v_worldPos.x, 0.0, v_worldPos.y);
highp vec3 N = vec3(0.0, 1.0, 0.0);
#endif
+ // Where the ground is in the world, for what is laid on it by position (the layers' tiles, the noise, the bounds): the draw's own P is from the render origin.
+ highp vec3 Pw = P + u_renderOrigin.xyz;
vec4 paintA = v_color, paintB = vec4(0.0);
if (u_terrainPages > 0.5) {
- vec2 lp = P.xz - u_terrainOrigin.xz;
+ vec2 lp = Pw.xz - u_terrainOrigin.xz;
if (lp.x < u_terrainBounds.x || lp.y < u_terrainBounds.y || lp.x > u_terrainBounds.z || lp.y > u_terrainBounds.w) discard;
paintA = texture(u_paintPages, v_color.xy);
paintB = texture(u_paintPages2, v_color.xy);
@@ -635,9 +640,9 @@ void main() {
// moves where the edge runs, the two finer break it up at the size of a paint texel and below, where a sharpened bilinear paint
// would otherwise follow the texel grid in steps.
float tileSize = tnTile(i);
- float wander = tnNoise(P.xz / (tileSize * 2.3) + float(i) * 11.0 + 3.0) * 0.5
- + tnNoise(P.xz / (tileSize * 0.45) + float(i) * 7.0 + 17.0) * 0.3
- + tnNoise(P.xz / (tileSize * 0.11) + float(i) * 5.0 + 29.0) * 0.2;
+ float wander = tnNoise(Pw.xz / (tileSize * 2.3) + float(i) * 11.0 + 3.0) * 0.5
+ + tnNoise(Pw.xz / (tileSize * 0.45) + float(i) * 7.0 + 17.0) * 0.3
+ + tnNoise(Pw.xz / (tileSize * 0.11) + float(i) * 5.0 + 29.0) * 0.2;
w[i] = i < count ? max(raw, 0.0) * mix(1.0, 0.35 + 1.3 * wander, u_blendNoise) : 0.0;
sum += w[i];
}
@@ -666,11 +671,11 @@ void main() {
float layer = float(i), tile = tnTile(i);
// WebGL takes no ?: between structures.
TnTap top;
- if (near > 0.01) top = tnHex(P.xz / tile, layer, dpx.xz / tile, dpy.xz / tile, near);
- else top = tnPlain(P.xz / tile, layer, dpx.xz / tile, dpy.xz / tile);
+ if (near > 0.01) top = tnHex(Pw.xz / tile, layer, dpx.xz / tile, dpy.xz / tile, near);
+ else top = tnPlain(Pw.xz / tile, layer, dpx.xz / tile, dpy.xz / tile);
if (far > 0.01) {
float big = tile * u_farScale;
- TnTap wide = tnPlain(P.xz / big + 0.21, layer, dpx.xz / big, dpy.xz / big);
+ TnTap wide = tnPlain(Pw.xz / big + 0.21, layer, dpx.xz / big, dpy.xz / big);
top.albedo = mix(top.albedo, wide.albedo, far);
top.mask = mix(top.mask, wide.mask, far);
}
@@ -678,12 +683,12 @@ void main() {
mask[i] = top.mask * planes.y;
bend[i] = tnBend(top.slope, vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), N) * planes.y;
if (planes.x > 0.02) {
- TnTap x = tnPlain(P.zy / tile, layer, dpx.zy / tile, dpy.zy / tile);
+ TnTap x = tnPlain(Pw.zy / tile, layer, dpx.zy / tile, dpy.zy / tile);
albedo[i] += x.albedo * planes.x; mask[i] += x.mask * planes.x;
bend[i] += tnBend(x.slope, vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 0.0), N) * planes.x;
}
if (planes.z > 0.02) {
- TnTap z = tnPlain(P.xy / tile, layer, dpx.xy / tile, dpy.xy / tile);
+ TnTap z = tnPlain(Pw.xy / tile, layer, dpx.xy / tile, dpy.xy / tile);
albedo[i] += z.albedo * planes.z; mask[i] += z.mask * planes.z;
bend[i] += tnBend(z.slope, vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), N) * planes.z;
}
@@ -703,7 +708,7 @@ void main() {
float k = mix(w[i], hw[i] / max(hsum, 1e-4), byHeight);
colour += albedo[i] * k; slope += bend[i] * k; surface += mask[i] * k;
}
- float broad = tnNoise(P.xz / (scale * 7.3)) * 0.6 + tnNoise(P.xz / (scale * 29.0) + 5.0) * 0.4;
+ float broad = tnNoise(Pw.xz / (scale * 7.3)) * 0.6 + tnNoise(Pw.xz / (scale * 29.0) + 5.0) * 0.4;
colour *= mix(1.0, 0.7 + 0.6 * broad, u_macroVariation);
N = normalize(N + slope);
@@ -818,13 +823,14 @@ fn packNormal(n : vec3f) -> vec2f {
let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
var N = vec3f(0.0, 1.0, 0.0);
#endif
+ let Pw = P + frame.renderOrigin.xyz;
let pageA = textureSample(u_paintPages, u_paintPages_s, v.v_color.xy);
let pageB = textureSample(u_paintPages2, u_paintPages2_s, v.v_color.xy);
let pageNormal = textureSample(u_heightPages, u_heightPages_s, v.v_color.xy).ba * 2.0 - vec2f(1.0);
var paintA = v.v_color;
var paintB = vec4f(0.0);
if (mc.u_terrainPages > 0.5) {
- let lp = P.xz - mc.u_terrainOrigin.xz;
+ let lp = Pw.xz - mc.u_terrainOrigin.xz;
if (lp.x < mc.u_terrainBounds.x || lp.y < mc.u_terrainBounds.y || lp.x > mc.u_terrainBounds.z || lp.y > mc.u_terrainBounds.w) { discard; }
paintA = pageA;
paintB = pageB;
@@ -838,9 +844,9 @@ fn packNormal(n : vec3f) -> vec2f {
if (i < 4) { raw = paintA[i]; } else { raw = paintB[i - 4]; }
// Three octaves: the slow one moves where the edge runs, the two finer break it up at the size of a paint texel and below.
let tileSize = tnTile(i);
- let wander = tnNoise(P.xz / (tileSize * 2.3) + vec2f(f32(i) * 11.0 + 3.0)) * 0.5
- + tnNoise(P.xz / (tileSize * 0.45) + vec2f(f32(i) * 7.0 + 17.0)) * 0.3
- + tnNoise(P.xz / (tileSize * 0.11) + vec2f(f32(i) * 5.0 + 29.0)) * 0.2;
+ let wander = tnNoise(Pw.xz / (tileSize * 2.3) + vec2f(f32(i) * 11.0 + 3.0)) * 0.5
+ + tnNoise(Pw.xz / (tileSize * 0.45) + vec2f(f32(i) * 7.0 + 17.0)) * 0.3
+ + tnNoise(Pw.xz / (tileSize * 0.11) + vec2f(f32(i) * 5.0 + 29.0)) * 0.2;
w[i] = 0.0;
if (i < count) { w[i] = max(raw, 0.0) * mix(1.0, 0.35 + 1.3 * wander, mc.u_blendNoise); }
sum = sum + w[i];
@@ -871,11 +877,11 @@ fn packNormal(n : vec3f) -> vec2f {
if (w[i] <= 1e-3) { continue; }
let tile = tnTile(i);
var top : TnTap;
- if (near > 0.01) { top = tnHex(P.xz / tile, i, dpx.xz / tile, dpy.xz / tile, near); }
- else { top = tnPlain(P.xz / tile, i, dpx.xz / tile, dpy.xz / tile); }
+ if (near > 0.01) { top = tnHex(Pw.xz / tile, i, dpx.xz / tile, dpy.xz / tile, near); }
+ else { top = tnPlain(Pw.xz / tile, i, dpx.xz / tile, dpy.xz / tile); }
if (far > 0.01) {
let big = tile * mc.u_farScale;
- let wide = tnPlain(P.xz / big + vec2f(0.21), i, dpx.xz / big, dpy.xz / big);
+ let wide = tnPlain(Pw.xz / big + vec2f(0.21), i, dpx.xz / big, dpy.xz / big);
top.albedo = mix(top.albedo, wide.albedo, vec3f(far));
top.mask = mix(top.mask, wide.mask, vec3f(far));
}
@@ -883,13 +889,13 @@ fn packNormal(n : vec3f) -> vec2f {
mask[i] = top.mask * planes.y;
bend[i] = tnBend(top.slope, vec3f(1.0, 0.0, 0.0), vec3f(0.0, 0.0, 1.0), N) * planes.y;
if (planes.x > 0.02) {
- let x = tnPlain(P.zy / tile, i, dpx.zy / tile, dpy.zy / tile);
+ let x = tnPlain(Pw.zy / tile, i, dpx.zy / tile, dpy.zy / tile);
albedo[i] = albedo[i] + x.albedo * planes.x;
mask[i] = mask[i] + x.mask * planes.x;
bend[i] = bend[i] + tnBend(x.slope, vec3f(0.0, 0.0, 1.0), vec3f(0.0, 1.0, 0.0), N) * planes.x;
}
if (planes.z > 0.02) {
- let z = tnPlain(P.xy / tile, i, dpx.xy / tile, dpy.xy / tile);
+ let z = tnPlain(Pw.xy / tile, i, dpx.xy / tile, dpy.xy / tile);
albedo[i] = albedo[i] + z.albedo * planes.z;
mask[i] = mask[i] + z.mask * planes.z;
bend[i] = bend[i] + tnBend(z.slope, vec3f(1.0, 0.0, 0.0), vec3f(0.0, 1.0, 0.0), N) * planes.z;
@@ -916,7 +922,7 @@ fn packNormal(n : vec3f) -> vec2f {
slope = slope + bend[i] * k;
surface = surface + mask[i] * k;
}
- let broad = tnNoise(P.xz / (scale * 7.3)) * 0.6 + tnNoise(P.xz / (scale * 29.0) + vec2f(5.0)) * 0.4;
+ let broad = tnNoise(Pw.xz / (scale * 7.3)) * 0.6 + tnNoise(Pw.xz / (scale * 29.0) + vec2f(5.0)) * 0.4;
colour = colour * mix(1.0, 0.7 + 0.6 * broad, mc.u_macroVariation);
N = normalize(N + slope);
diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.ts
index d211d8e4a..f60159be5 100644
--- a/sdk/src/water/waterShader.ts
+++ b/sdk/src/water/waterShader.ts
@@ -772,7 +772,7 @@ void main() {
}
// Past the shore only the surf's film is water: over ground no lower than the water, within its height of the rest level (further off
// it is the mesh stretched between two waters' heights); the depth test cuts it where the ground rises above it.
- bool film = abs(P.y - zone.x) <= surf.x + u_waveHeight * zone.z + 1e-3;
+ bool film = abs(P.y + u_renderOrigin.y - zone.x) <= surf.x + u_waveHeight * zone.z + 1e-3;
// Ground lower than the water there (a dry basin) takes no film. The picture's depth says it of this very pixel; the baked depth only
// of whole texels (dry is 0 and any wet one beside it raises the blend), which would cut the film in the texels' steps.
float under = haveDepth ? column0 * abs(viewDir.y) - 2.0 * (surf.x + u_waveHeight * zone.z) - 1.0 : shoreDepth - 1e-3;
@@ -1230,7 +1230,7 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, amp : f32, calm : f32) -> vec
}
var alpha = 0.85;
if (haveColor) { alpha = 1.0; }
- let film = abs(P.y - zone.x) <= surf.x + mc.u_waveHeight * zone.z + 1e-3;
+ let film = abs(P.y + frame.renderOrigin.y - zone.x) <= surf.x + mc.u_waveHeight * zone.z + 1e-3;
// Ground lower than the water there (a dry basin) takes no film. The picture's depth says it of this very pixel; the baked depth only
// of whole texels (dry is 0 and any wet one beside it raises the blend), which would cut the film in the texels' steps.
var under = shoreDepth - 1e-3;
diff --git a/src/esengine/renderer/frame/FrameConstants.hpp b/src/esengine/renderer/frame/FrameConstants.hpp
index 75990e347..42b5cb459 100644
--- a/src/esengine/renderer/frame/FrameConstants.hpp
+++ b/src/esengine/renderer/frame/FrameConstants.hpp
@@ -44,6 +44,8 @@ struct FrameConstants {
* depth is a number with no length in it.
*/
glm::mat4 inverseViewProjection{1.0f};
+ /// The render origin (RenderOrigin.hpp): a world position is the one the shader holds plus this, for what depends on the world's own coordinates.
+ glm::vec4 renderOrigin{0.0f};
};
/**
diff --git a/src/esengine/renderer/frame/GpuParticles.cpp b/src/esengine/renderer/frame/GpuParticles.cpp
index ec8041198..acdf3aee0 100644
--- a/src/esengine/renderer/frame/GpuParticles.cpp
+++ b/src/esengine/renderer/frame/GpuParticles.cpp
@@ -5,6 +5,7 @@
#include "../../core/Log.hpp"
#include "../../ecs/components/ParticleEmitter.hpp"
#include "../../ecs/components/Transform.hpp"
+#include "../RenderOrigin.hpp"
#include "../../particle/ParticleSystem.hpp"
#include <algorithm>
@@ -54,12 +55,13 @@ struct ParamsBlock {
f32 scale[4]; // emitter scale
f32 rot0[4], rot1[4], rot2[4]; // emitter rotation, one column each
f32 local[4]; // cos, sin of the emitter's turn about Z; scale.x, scale.y
+ f32 shift[4]; // the render origin, taken from each particle's world position as it is written for drawing
f32 fields[kMaxFields * 3][4];
f32 colorLut[kLutSize][4];
f32 sizeLut[kLutSize / 4][4];
f32 speedLut[kLutSize / 4][4];
};
-static_assert(sizeof(ParamsBlock) == 92 * 16, "the kernel's Params is 92 vec4s");
+static_assert(sizeof(ParamsBlock) == 93 * 16, "the kernel's Params is 93 vec4s");
constexpr u32 kJobStride = 2048; // a uniform binding starts on a multiple of 256
static_assert(sizeof(ParamsBlock) <= kJobStride && kJobStride % 256 == 0);
constexpr u32 kMaxJobs = 64;
@@ -76,6 +78,7 @@ struct Params {
origin: vec4<f32>, scale: vec4<f32>,
rot0: vec4<f32>, rot1: vec4<f32>, rot2: vec4<f32>,
local: vec4<f32>,
+ shift: vec4<f32>,
fields: array<vec4<f32>, 24>,
colorLut: array<vec4<f32>, 32>,
sizeLut: array<vec4<f32>, 8>,
@@ -392,6 +395,7 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
worldPos = P.origin.xyz + vec3<f32>(rel3.x * P.local.x - rel3.y * P.local.y, rel3.x * P.local.y + rel3.y * P.local.x, rel3.z);
worldSize = worldSize * vec2<f32>(P.local.z, P.local.w);
}
+ worldPos = worldPos - P.shift.xyz;
insts[i] = Inst(worldPos.x, worldPos.y, worldPos.z, worldSize.x, worldSize.y, rotation, pack4x8unorm(color), u0, v0, us, vs);
}
)WGSL";
@@ -556,6 +560,8 @@ bool GpuParticles::submit(Entity entity, const ecs::ParticleEmitter& emitter, co
f32 angle = 2.0f * std::atan2(transform.worldRotation.z, transform.worldRotation.w);
if (std::abs(angle) <= 0.001f) angle = 0.0f;
set4(b.local, std::cos(angle), std::sin(angle), scale.x, scale.y);
+ const glm::dvec3 origin = renderOrigin();
+ set4(b.shift, static_cast<f32>(origin.x), static_cast<f32>(origin.y), static_cast<f32>(origin.z), 0.0f);
for (u32 k = 0; k < fieldCount; ++k) {
const particle::ForceFieldInstance& f = fields[k];
diff --git a/src/esengine/renderer/frame/RenderContext.cpp b/src/esengine/renderer/frame/RenderContext.cpp
index 3ba058c1d..e19b928a8 100644
--- a/src/esengine/renderer/frame/RenderContext.cpp
+++ b/src/esengine/renderer/frame/RenderContext.cpp
@@ -14,6 +14,7 @@
#include "./RenderContext.hpp"
#include "../rhi/GfxDevice.hpp"
#include "./FrameConstants.hpp"
+#include "../RenderOrigin.hpp"
#include "../draw/DrawParams.hpp"
#include "../store/SkinConstants.hpp"
#include "../store/MorphConstants.hpp"
@@ -188,7 +189,7 @@ u32 RenderContext::frameConstantsSize() {
void RenderContext::useFrameConstants(BufferHandle ubo, const glm::mat4& viewProjection) {
if (ubo == BufferHandle::Invalid) return;
const FrameConstants frame{viewProjection, cameraFromViewProjection(viewProjection),
- glm::inverse(viewProjection)};
+ glm::inverse(viewProjection), glm::vec4(glm::vec3(renderOrigin()), 0.0f)};
device_.updateBuffer(ubo, 0, &frame, sizeof(FrameConstants));
device_.setUniformBuffer(FRAME_CONSTANTS_BINDING, ubo);
// Same re-arm the frame's own upload does: a previous pass may have left a
@@ -204,7 +205,7 @@ void RenderContext::uploadFrameConstants(const glm::mat4& upload, const glm::mat
// The viewpoint from `engine` and the un-projection from `upload`: the two
// agree about where the eye is, but only the uploaded one describes the clip
// space a shader will hand back.
- const FrameConstants frame{upload, cameraFromViewProjection(engine), glm::inverse(upload)};
+ const FrameConstants frame{upload, cameraFromViewProjection(engine), glm::inverse(upload), glm::vec4(glm::vec3(renderOrigin()), 0.0f)};
device_.updateBuffer(frameUbo_, 0, &frame, sizeof(FrameConstants));
// Take the binding back. A pass that draws several views points this slot at
// one buffer per view; without this the scene would keep reading whichever
diff --git a/src/esengine/renderer/frame/RenderFrameMask.cpp b/src/esengine/renderer/frame/RenderFrameMask.cpp
index af5110494..84683f185 100644
--- a/src/esengine/renderer/frame/RenderFrameMask.cpp
+++ b/src/esengine/renderer/frame/RenderFrameMask.cpp
@@ -40,6 +40,9 @@ glm::vec2 worldToScreen(const glm::vec3& world, const glm::mat4& vp,
return {sx, sy};
}
+/// Whether the masks being laid out stand in the world (and so are taken from the render origin) or on the screen, whose units have none.
+thread_local bool g_masksInWorld = false;
+
ScreenRect computeMaskScreenRect(
ecs::Registry& registry, Entity entity,
const glm::mat4& vp, i32 vpX, i32 vpY, i32 vpW, i32 vpH
@@ -67,7 +70,7 @@ ScreenRect computeMaskScreenRect(
}
const glm::vec3 hx = axisX * node.computed_size_.x * .5f;
const glm::vec3 hy = axisY * node.computed_size_.y * .5f;
- const glm::vec3 center = transform.worldPosition;
+ const glm::vec3 center = g_masksInWorld ? rebased(transform.worldPosition) : glm::vec3(transform.worldPosition);
const glm::vec3 corners[] = {center - hx - hy, center + hx - hy,
center + hx + hy, center - hx + hy};
@@ -220,7 +223,9 @@ void applyStencilHierarchy(ecs::Registry& registry, RenderFrame& frame,
} // anonymous namespace
void RenderFrame::processMasks(ecs::Registry& registry, i32 vpX, i32 vpY, i32 vpW, i32 vpH) {
+ g_masksInWorld = true;
processMasks(registry, view_projection_, vpX, vpY, vpW, vpH);
+ g_masksInWorld = false;
}
void RenderFrame::processMasks(ecs::Registry& registry, const glm::mat4& projection,
diff --git a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp b/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
index 385fd5057..05b58a1de 100644
--- a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
+++ b/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
@@ -59,7 +59,7 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {
if (!caster.enabled) continue;
auto& transform = casters.get<ecs::Transform>(entity);
transform.ensureDecomposed();
- const glm::vec3 p = transform.worldPosition;
+ const glm::vec3 p = rebased(transform.worldPosition);
// The shape turns with the entity, the way a collider on it does — a wall laid
// at an angle casts the shadow of a wall at that angle. Its SIZE is its own,
// also like a collider's: scaling the entity does not resize the occluder.
@@ -388,7 +388,7 @@ void RenderFrame::collectShape2D(ecs::Registry& registry) {
const glm::vec3 p = [&] {
auto& transform = view.get<ecs::Transform>(entity);
transform.ensureDecomposed();
- return transform.worldPosition;
+ return rebased(transform.worldPosition);
}();
u32 slot = MAX_LIGHTS;
for (u32 i = 0; i < context_.lights().count(); ++i) {
diff --git a/src/esengine/renderer/frame/VoxelGI.cpp b/src/esengine/renderer/frame/VoxelGI.cpp
index 1f74f1dbe..719707da0 100644
--- a/src/esengine/renderer/frame/VoxelGI.cpp
+++ b/src/esengine/renderer/frame/VoxelGI.cpp
@@ -20,7 +20,7 @@ constexpr u32 kVoxelWords = 8;
#ifdef ES_ENABLE_WEBGPU
/// What every kernel sees at group 0: the frame, the cache's parameters and the cache itself.
const char* kCommon = R"WGSL(
-struct Frame { projection : mat4x4f, camera : vec4f, inverseViewProjection : mat4x4f };
+struct Frame { projection : mat4x4f, camera : vec4f, inverseViewProjection : mat4x4f, renderOrigin : vec4f };
struct GI {
origin : vec4i, // xyz: the window's first cell; w: the frame
dims : vec4u, // xyz: voxels per axis; w: rays per point
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cpp
index 9cb38a683..f48bb2334 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp
@@ -131,7 +131,7 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {
bool isLocalSpace = emitter.simulationSpace ==
static_cast<i32>(ecs::SimulationSpace::Local);
- glm::vec3 emitterWorldPos = transform.worldPosition;
+ glm::vec3 emitterWorldPos = rebased(transform.worldPosition);
f32 emitterAngle = 0.0f;
glm::vec2 emitterScale(transform.worldScale);
f32 cosA = 1.0f, sinA = 0.0f;
@@ -171,7 +171,7 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {
}
size = glm::vec2(p.size) * emitterScale;
} else {
- worldPos = p.position;
+ worldPos = rebased(p.position);
size = glm::vec2(p.size);
}
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index c5c41ca49..4b622b3ee 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -229,8 +229,8 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
? layer.origin_entity : entity;
if (auto* transform = registry.tryGet<ecs::Transform>(transformEntity)) {
transform->ensureDecomposed(); // lazily derived; see Transform.hpp
- originX = transform->worldPosition.x;
- originY = transform->worldPosition.y;
+ originX = rebased(transform->worldPosition).x;
+ originY = rebased(transform->worldPosition).y;
const auto& q = transform->worldRotation;
angle = std::atan2(2.0f * (q.w * q.z + q.x * q.y),
1.0f - 2.0f * (q.y * q.y + q.z * q.z));
diff --git a/src/esengine/renderer/plugins/TrailPlugin.cpp b/src/esengine/renderer/plugins/TrailPlugin.cpp
index 28f968232..919494b9f 100644
--- a/src/esengine/renderer/plugins/TrailPlugin.cpp
+++ b/src/esengine/renderer/plugins/TrailPlugin.cpp
@@ -37,7 +37,7 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {
auto& transform = view.get<ecs::Transform>(entity);
transform.ensureDecomposed();
- const glm::vec3 headWorld = transform.worldPosition;
+ const glm::vec3 headWorld = rebased(transform.worldPosition);
// --- Build the centerline: anchors (oldest→newest), then the live head. -----
const f32 invTime = trail.time > 1e-6f ? 1.0f / trail.time : 0.0f;
@@ -48,8 +48,9 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {
for (const auto& pt : state->points) {
f32 age01 = std::min(std::max((now - pt.birth_time) * invTime, 0.0f), 1.0f);
- scratch_center_.push_back({pt.position, age01});
- extend(pt.position);
+ const glm::vec3 at = rebased(pt.position);
+ scratch_center_.push_back({at, age01});
+ extend(at);
}
// The live head glues the ribbon to the moving entity between recorded
// anchors. Skip it when frozen (the streak fades where it was left) or when it
diff --git a/src/esengine/renderer/plugins/UIElementPlugin.cpp b/src/esengine/renderer/plugins/UIElementPlugin.cpp
index 6ee578a79..2a4701165 100644
--- a/src/esengine/renderer/plugins/UIElementPlugin.cpp
+++ b/src/esengine/renderer/plugins/UIElementPlugin.cpp
@@ -77,7 +77,8 @@ void UIElementPlugin::collect(RenderCollectContext& collect_ctx) {
auto& transform = uiView.get<ecs::Transform>(entity);
transform.ensureDecomposed();
- glm::vec3 position = transform.worldPosition;
+ // Screen-space UI is laid out in screen units, which have no origin to take; UI standing in the world has.
+ glm::vec3 position = collect_ctx.isOverlay() ? glm::vec3(transform.worldPosition) : rebased(transform.worldPosition);
const auto& rotation = transform.worldRotation;
const auto& scale = transform.worldScale;
const bool parented = registry.has<ecs::Parent>(entity);
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index d0abe5fff..d59121afc 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1049,6 +1049,7 @@ const char* kFrameHeaderWGSL =
" projection : mat4x4f,\n"
" camera : vec4f,\n"
" inverseViewProjection : mat4x4f,\n"
+ " renderOrigin : vec4f,\n"
"};\n"
"@group(0) @binding(0) var<uniform> frame : FrameConstants;\n"
"fn viewDirection(worldPos : vec3f) -> vec3f {\n"
@@ -2308,6 +2309,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" highp mat4 u_projection;\n"
" highp vec4 u_camera;\n" // xyz = eye (w=1) or direction toward it (w=0)
" highp mat4 u_inverseViewProjection;\n"
+ " highp vec4 u_renderOrigin;\n" // xyz: what a position here is taken from, to be the world's own
"};\n"
"highp vec3 viewDirection(in highp vec3 worldPos) {\n"
" return u_camera.w > 0.5 ? normalize(u_camera.xyz - worldPos) : u_camera.xyz;\n"
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index d94fe6ceb..2003c32f1 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): 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":
{"note":"A render origin for large worlds."},
"feat(terrain): a planet's ground is drawn with the layers' textures":