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(terrain): a layer with a grain is read without repeat and without hexagons - near the eye every hex cell took its layer turned any way round, so sand in ripples or rock in beds was lit cell by cell and showed the cells; u_antiTileTurn is how far a cell may be turned, and 0 shifts it only

yhh1b63797a01d8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcba14b20..80f5038b8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Added
+- **A layer with a grain is read without repeat and without hexagons.** Near the eye a terrain layer is read in hexagonal cells, each shifted and turned at random, so no tile shows. A layer with a direction to it (sand in ripples, rock in beds) turned cell by cell has its normal map lit cell by cell, and the ground showed a mosaic of light and dark hexagons a metre wide. `u_antiTileTurn` on the Terrain and Terrain Layers materials is how far round a cell may be turned: 1, the default, is any way as before; 0 shifts the cell and does not turn it, which is the setting for a layer with a grain.
- **A game calls its own native code by name.** A packaged Android or iOS game can link an ad SDK, a store or a sign-in library, and had no way to reach it: the native host answered the engine's own calls and nothing else. `NativeChannel.call(method, args)` now runs a handler the app registered (`com.estella.host.NativeChannel.register` in Java, `EstellaNativeChannel` in Objective-C) and resolves with what it answered; `has` says whether a name is answered and `on` hears what the native side raises on its own. Values cross as JSON. `Ads` rides on it: once the app's code answers `ads.show`, `Ads.available` is true on a device and `showRewarded` works as it does in a mini-game. The engine links no ad SDK; which one is the project's choice. An exported Android Studio project keeps your Java under `app/src/game`, which a re-export leaves alone. Elsewhere `has` is false and nothing is called, so a game that asks first runs unchanged on the web.
- **Text has letter spacing.** `Text.letterSpacing` is extra space after every glyph, in the same px as `fontSize`; a negative value draws the glyphs closer. The layout always took a spacing, and `measureText` did, but the component had no field for it and nothing handed one over, so a label could not be spaced. Wrapping, alignment, `Ellipsis` and `Shrink` measure with it, `Shrink` scales it with the type, and it is in the Details panel beside Line Height. The default, 0, is the text as it was.
- **A cliff's beds can wander.** A terrain layer seen from the side is read by height, so the beds of a cliff texture lie level, and ran dead straight along a whole wall like lines on ruled paper. The Terrain Layers material's `u_strataWarp` is how far they wander up and down, by slow noise `u_strataSpan` wide. At 0, the default, they are straight as before.
diff --git a/docs/astro/src/content/docs/graphics/terrain.mdx b/docs/astro/src/content/docs/graphics/terrain.mdx
index f1f2a28a4..07947fa54 100644
--- a/docs/astro/src/content/docs/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/graphics/terrain.mdx
@@ -91,6 +91,8 @@ occlusion and roughness light it. Its material sets how: `u_sharpness`, `u_heigh
`u_macroVariation`, `u_triplanarFrom` (how steep before the sides are read) and `u_wetLevel`/`u_wetBand`, as the Terrain shader's below.
A layer seen from the side is read by height, so a cliff texture's beds lie level, and dead straight along the whole wall:
`u_strataWarp` is how far they wander up and down, by slow noise `u_strataSpan` wide (0, the default, leaves them straight).
+`u_antiTileTurn` is how far round a cell of the no-repeat reading may be turned: a layer with a grain (sand in ripples) turned cell by
+cell is lit cell by cell and shows the hexagons, and 0 shifts the cells without turning them.
What an artist delivers, per layer: the three textures, square, a power of two (1024 is plenty; they are packed at the largest
layer's size, at most 1024, 512 on a minigame), tiling seamlessly, the normal and mask saved as data (not colour-managed). A layer's
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
index 00afd725a..30a9a1a21 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
@@ -66,7 +66,7 @@ description: 一个 Terrain 组件,用高度图或噪声生成地面,可以是
| `normalStrength` | 法线贴图的强度:1 为原样,0 为平坦。 |
| `heightBlend` | 可选。这一层自己的高度在与别的层相交处起多大作用:0 只按涂色混合(草到沙),1 按各层高度混合(石头从草里露出)。多层相遇时,每层按它在此处所占的比例计入。缺省则用材质的 `u_heightBlend`。 |
-地面由 **Terrain Layers** 着色器(`builtin:terrain-layers`)绘制。每一层按世界位置铺设,近处去除重复,远处混入更大的平铺;层与层交界处遮罩高度更高的一层显露(草间露出石头、石缝里填着沙);坡面上同时从侧面投影,悬崖不会被拉伸。法线贴图和遮罩的遮蔽、粗糙度参与光照。材质控制效果:`u_sharpness`、`u_heightBlend`、`u_blendNoise`、`u_antiTile`、`u_farScale`、`u_macroVariation`、`u_triplanarFrom`(多陡开始读侧面)和 `u_wetLevel`/`u_wetBand`,含义同下文 Terrain 着色器。从侧面读的层按高度取样,所以崖壁贴图的岩层是水平的,而且沿整面墙笔直:`u_strataWarp` 是岩层上下起伏的幅度,由宽度为 `u_strataSpan` 的缓慢噪声决定(默认 0,保持笔直)。
+地面由 **Terrain Layers** 着色器(`builtin:terrain-layers`)绘制。每一层按世界位置铺设,近处去除重复,远处混入更大的平铺;层与层交界处遮罩高度更高的一层显露(草间露出石头、石缝里填着沙);坡面上同时从侧面投影,悬崖不会被拉伸。法线贴图和遮罩的遮蔽、粗糙度参与光照。材质控制效果:`u_sharpness`、`u_heightBlend`、`u_blendNoise`、`u_antiTile`、`u_farScale`、`u_macroVariation`、`u_triplanarFrom`(多陡开始读侧面)和 `u_wetLevel`/`u_wetBand`,含义同下文 Terrain 着色器。从侧面读的层按高度取样,所以崖壁贴图的岩层是水平的,而且沿整面墙笔直:`u_strataWarp` 是岩层上下起伏的幅度,由宽度为 `u_strataSpan` 的缓慢噪声决定(默认 0,保持笔直)。`u_antiTileTurn` 是去重复采样时每个格子最多旋转多少:有方向的层(带沙纹的沙)逐格旋转后会逐格受光、露出六边形,设为 0 则只平移不旋转。
美术每层交付:三张贴图,正方形,边长为 2 的幂(1024 足够;打包时取最大一层的尺寸,最大 1024,小游戏上 512),四方连续,法线和遮罩按数据保存(不做色彩管理)。地形构建时各层贴图会打包成纹理数组,地形层文件或其贴图保存后会重新打包。
diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.ts
index 627b033db..d25303dd0 100644
--- a/sdk/src/terrain/terrainShader.ts
+++ b/sdk/src/terrain/terrainShader.ts
@@ -123,6 +123,9 @@ const TERRAIN = `#pragma shader "Terrain"
#pragma param u_farScale float default(4.3) range(1,32) ui(slider)
// How strongly each layer is read without repeat near the eye (hex tiling: every hexagonal cell shifted and turned at random), so no tile is seen.
#pragma param u_antiTile float default(0.6) range(0,1) ui(slider)
+// How far round a cell may be turned (1 any way, 0 shifted only): a layer with a grain, sand in ripples or rock in beds, turned cell by
+// cell is lit cell by cell, and shows the hexagons.
+#pragma param u_antiTileTurn float default(1) range(0,1) ui(slider)
// The wide, slow variation in brightness laid over every layer so a big meadow is not one colour.
#pragma param u_macroVariation float default(0.35) range(0,1) ui(slider)
// How deep, in world units, the layers' relief is drawn into the light (their brightness taken as height); 0 leaves the ground smooth.
@@ -204,7 +207,7 @@ vec2 tnHash2(vec2 p) {
// One reading of a layer in a hex cell: shifted and turned by the cell's own random numbers (the gradients turn with it).
vec3 tnHexTap(sampler2D layer, vec2 uv, vec2 cell, vec2 gx, vec2 gy) {
vec2 r = tnHash2(cell);
- float a = r.x * 6.2831853;
+ float a = r.x * 6.2831853 * u_antiTileTurn;
mat2 turn = mat2(cos(a), -sin(a), sin(a), cos(a));
return textureGrad(layer, turn * uv + r, turn * gx, turn * gy).rgb;
}
@@ -353,7 +356,7 @@ fn tnHash2(p : vec2f) -> vec2f {
}
fn tnHexTap(layer : texture_2d<f32>, smp : sampler, uv : vec2f, cell : vec2f, gx : vec2f, gy : vec2f) -> vec3f {
let r = tnHash2(cell);
- let a = r.x * 6.2831853;
+ let a = r.x * 6.2831853 * mc.u_antiTileTurn;
let turn = mat2x2f(cos(a), -sin(a), sin(a), cos(a));
return textureSampleGrad(layer, smp, turn * uv + r, turn * gx, turn * gy).rgb;
}
@@ -539,6 +542,9 @@ const TERRAIN_LAYERS = `#pragma shader "Terrain Layers"
#pragma param u_farScale float default(4.3) range(1,32) ui(slider)
// How strongly each layer is read without repeat near the eye (hex tiling), so no tile is seen.
#pragma param u_antiTile float default(0.6) range(0,1) ui(slider)
+// How far round a cell may be turned (1 any way, 0 shifted only): a layer with a grain, sand in ripples or rock in beds, turned cell by
+// cell is lit cell by cell, and shows the hexagons.
+#pragma param u_antiTileTurn float default(1) range(0,1) ui(slider)
// The wide, slow variation in brightness laid over every layer so a big meadow is not one colour.
#pragma param u_macroVariation float default(0.25) range(0,1) ui(slider)
// How steep the ground must be (1 minus its normal's up) before the layers are also projected from the sides.
@@ -631,7 +637,7 @@ TnTap tnPlain(vec2 uv, float layer, vec2 gx, vec2 gy) {
// The same in one hex cell: shifted and turned by the cell's random numbers; the normal's slope turned back with it.
TnTap tnHexTap(vec2 uv, float layer, vec2 cell, vec2 gx, vec2 gy) {
vec2 r = tnHash2(cell);
- float a = r.x * 6.2831853;
+ float a = r.x * 6.2831853 * u_antiTileTurn;
mat2 turn = mat2(cos(a), -sin(a), sin(a), cos(a));
TnTap t = tnPlain(turn * uv + r, layer, turn * gx, turn * gy);
t.slope = t.slope * turn;
@@ -828,7 +834,7 @@ fn tnPlain(uv : vec2f, layer : i32, gx : vec2f, gy : vec2f) -> TnTap {
}
fn tnHexTap(uv : vec2f, layer : i32, cell : vec2f, gx : vec2f, gy : vec2f) -> TnTap {
let r = tnHash2(cell);
- let a = r.x * 6.2831853;
+ let a = r.x * 6.2831853 * mc.u_antiTileTurn;
let turn = mat2x2f(cos(a), -sin(a), sin(a), cos(a));
var t = tnPlain(turn * uv + r, layer, turn * gx, turn * gy);
t.slope = t.slope * turn;
diff --git a/sdk/tests/area-map-rows.test.ts b/sdk/tests/area-map-rows.test.ts
index 7da8bb9bd..d9142b3a6 100644
--- a/sdk/tests/area-map-rows.test.ts
+++ b/sdk/tests/area-map-rows.test.ts
@@ -39,4 +39,13 @@ describe('the rows of an image over an area', () => {
expect(source).toContain('if (u_strataWarp > 0.0) bedY +=');
expect(source).toContain('if (mc.u_strataWarp > 0.0) { bedY = bedY +');
});
+
+ it('a hex cell is turned by no more than the material says, in both shaders and both languages', () => {
+ for (const source of [TERRAIN_SHADER_TEMPLATE.source, TERRAIN_LAYERS_SHADER_TEMPLATE.source]) {
+ expect(source).toContain('#pragma param u_antiTileTurn float default(1)');
+ expect(source).toContain('float a = r.x * 6.2831853 * u_antiTileTurn;');
+ expect(source).toContain('let a = r.x * 6.2831853 * mc.u_antiTileTurn;');
+ expect(source).not.toContain('r.x * 6.2831853;');
+ }
+ });
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index fc73f44ad..d1dcd680b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4592,6 +4592,8 @@ export const NOTED = {
{"note": "A waterfall's foot is torn to veils, and light goes through it."},
"fix(webgpu): a view of many materials no longer loses the device - the bind group cache held 256 and was emptied when full, so a view that binds more made every group again each frame and the device ran out of descriptors after some hundreds of frames; it holds 4096 by hash and lets go only of what the last two frames did not bind":
{"note": "A view of many materials no longer loses the WebGPU device."},
+ "feat(terrain): a layer with a grain is read without repeat and without hexagons - near the eye every hex cell took its layer turned any way round, so sand in ripples or rock in beds was lit cell by cell and showed the cells; u_antiTileTurn is how far a cell may be turned, and 0 shifts it only":
+ {"note": "A layer with a grain is read without repeat and without hexagons."},
"fix(terrain): a detail the pages cannot hold no longer cracks the ground - a tree that wanted more nodes than the atlas has pages was drawn in part by coarser pages standing in, which do not meet their finer neighbours' edges, and the pages were fought over every frame; the ground is now drawn at the share of its detail's range that fits, and says so once":
{"note": "A detail the pages cannot hold no longer cracks the ground."},
"fix(foliage): a far plant grows across the ground, not taller - a mesh plant kept in a far version was grown the same every way to cover the ground of those left out, so a flower that stood for ten stood ten times as high and a far meadow was a field of poles; it now grows across the ground only, and Foliage.farGrowUp gives its height a share of the growth back":