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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(terrain): a colour map and an occlusion map lie the way the heightmap does - Terrain.colorMap, Terrain.occlusionMap and Foliage.colorMap were sampled with their rows turned over, so a picture painted to match a heightmap, a splatmap or a density map came out mirrored north to south under them; an image over an area now has its first row along the least z whoever reads it
yhh135ddac45b62
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 30fd0e539..50dfd4c03 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -34,6 +34,7 @@ published separately; it ships inside the editor.### Fixed+- **A colour map and an occlusion map lie the way the heightmap does.** `Terrain.colorMap`, `Terrain.occlusionMap` and `Foliage.colorMap` were sampled with their rows turned over: a picture painted to match the heightmap, the splatmap or a density map came out mirrored north to south under them, flowers coloured for the far side of an island and gullies shaded where the ridges were. An image over an area now has its first row along the least z whoever reads it. A map that was drawn turned over to make up for this needs turning back.- **The ground's lengths follow the project's units.** A `Terrain`'s size, height, cell, draw distance and collision distance were plain numbers, and so were its shaders' layer tiles, relief and wet line and a layer file's default tile: a new terrain in a default project was ten metres of ground on 2 cm cells with collision built 64 cm round a body. They are lengths now (`worldUnitFields`, `unit(length)`), written at 100 units a metre: a kilometre of ground on 2 m cells, collision 64 m round a body, a layer 24 m across, in a centimetre project and a metre one. What a scene, a material or a layer file states is untouched; a scene that left `collisionDistance` or `drawDistance` to the default gets the new one, so state the old (64, 4000) to keep it. Ground with `layers` is also no longer sent the four layer colours of the plain shader, which warned four times a load.- **Water mirrors only what is there.** Over open sea with no ground behind it the water's reflection went black: the ray through the picture took a sky pixel for something it had met, and the sky is painted after that picture is taken. From above, the sea darkened along the edge of the terrain under it, a line across the water. And a ripple steep enough to send the ray down met the sea bed in the picture, undimmed by its water: white flecks all over a shallow sea. The copy of the scene's distances now marks where the picture holds nothing, a ray sent down goes on up as off the next wave. (A first version also widened the highlight by how far the normal turns across a pixel; that made a distant sea rough and took most of its glancing reflection of the sky away, and is not in this release: the ripple map's mips keep far water from flashing.) The ray's step and the thickness of what it meets are shares of the distance to the water, where they were a number of units (a centimetre in one project, a metre in another).- **A mesh shader's eighth texture finds a unit.** A material's textures past the seventh take an engine unit the shader never reads. A shader that named `u_textures` at all was counted as reading all eight batch units, though a mesh shader reads only its base texture, `u_textures[0]`: so anything built on Model had seven textures and no more. Ground Blend has eight, lost the last (the wet-ground map), and did not compile on WebGPU (`unresolved value 'u_wetMask'`); voxel ground had dropped its wet map to stay under. A literal index now counts as that one unit, so a Lit mesh shader has eight, Ground Blend draws on WebGPU, and voxel ground is wet where the weather says, as other ground is.diff --git a/docs/astro/src/content/docs/graphics/terrain.mdx b/docs/astro/src/content/docs/graphics/terrain.mdxindex 22c9aae89..7a0581842 100644--- a/docs/astro/src/content/docs/graphics/terrain.mdx+++ b/docs/astro/src/content/docs/graphics/terrain.mdx@@ -39,7 +39,7 @@ The ground is one of two kinds, chosen by `extent`:| `erosion` | 0 to 4: how much rain wears the ground (below). 0 leaves it as made. || `splatmap` | Area only. An image whose four channels are the weights of layers 1 to 4. Empty paints by height and slope. || `splatmap2` | Area only. The weights of layers 5 to 8, read with the first. |-| `colorMap` | Area only. A picture laid over the whole area and mixed into the ground's colour by its alpha: a meadow's flowers, the regions of a map, scorched ground. One texture for the whole terrain, on top of the layers; its import Use is Colour. |+| `colorMap` | Area only. A picture laid over the whole area and mixed into the ground's colour by its alpha: a meadow's flowers, the regions of a map, scorched ground. One texture for the whole terrain, on top of the layers; its import Use is Colour. Its first row lies along the area's least z, as the heightmap's does. || `colorMapStrength` | How much of the colour map is mixed in where its alpha is whole (1). Read each frame: changing it rebuilds nothing. || `occlusionMap`, `occlusionStrength` | Area only. An image over the whole area whose red says how much of the sky reaches the ground there: a baked occlusion for gullies, hollows and the foot of a cliff, which are too large for a screen-space pass to see. It dims the light that comes from all round and leaves the sun's own to the shadows, so it is quiet in full sun and tells in shade. Its import Use is Data. || `layers` | The [terrain layers](#terrain-layers) the ground is drawn with, up to eight, in the order the paint weighs them. Empty draws the four layer colours or the Terrain material's textures. |diff --git a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdxindex 0163c6c00..170e00f8c 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx@@ -31,7 +31,7 @@ description: 一个 Terrain 组件,用高度图或噪声生成地面,可以是| `erosion` | 0 到 4:雨水把地面侵蚀多少(见下)。0 保持生成时的样子。 || `splatmap` | 仅区域。一张图,四个通道是第 1 到 4 层的权重。为空则按高度和坡度上色。 || `splatmap2` | 仅区域。第 5 到 8 层的权重,与第一张一起读取。 |-| `colorMap` | 仅区域。铺在整块区域上的一张图,按它的 alpha 混进地面颜色:草甸上的花色、地图上的分区、烧焦的地面。整块地形一张,叠在图层之上;导入用途是颜色。 |+| `colorMap` | 仅区域。铺在整块区域上的一张图,按它的 alpha 混进地面颜色:草甸上的花色、地图上的分区、烧焦的地面。整块地形一张,叠在图层之上;导入用途是颜色。图的第一行沿区域 z 最小的一边,和高度图一致。 || `colorMapStrength` | alpha 为满的地方混入多少颜色图(1)。每帧读取:改它不会重建任何东西。 || `occlusionMap`、`occlusionStrength` | 仅区域。铺在整块区域上的一张图,红色通道说明那里的地面能接到多少天空光:给沟壑、洼地、崖脚用的烘焙遮蔽,这些尺度太大,屏幕空间的遮蔽看不到。它只压暗来自四周的光,太阳的直射留给阴影,所以在阳光下很轻,在背阴处明显。导入用途是数据。 || `layers` | 绘制地面所用的[地形层](#地形层),最多八个,按地表图权重的顺序。为空则用四种图层颜色或 Terrain 材质的贴图。 |diff --git a/sdk/src/foliage/foliageShader.ts b/sdk/src/foliage/foliageShader.tsindex 3bc02cca0..ea7bcb93b 100644--- a/sdk/src/foliage/foliageShader.ts+++ b/sdk/src/foliage/foliageShader.ts@@ -29,6 +29,7 @@ const FOLIAGE = `#pragma shader "Foliage"#pragma coverage u_alphaCutoff// A picture over the field that colours the plants by where they stand (the Foliage component writes it): mixed in by its alpha, from// u_colorMapFrom up the plant, over the area whose corner and size u_colorMapArea is.+// Its first row lies along the area's least z, as the density map's does: a texture is stored last row first, so v is turned.#pragma param u_colorMap texture default(white)#pragma param u_colorMapStrength float default(0) range(0,1) ui(slider)#pragma param u_colorMapFrom float default(0) range(0,1) ui(slider)@@ -239,7 +240,7 @@ void main() {#ifdef MESH_NORMALSif (u_colorMapStrength > 0.0) {vec2 over = (P.xz + u_renderOrigin.xz - u_colorMapArea.xy) / max(u_colorMapArea.zw, vec2(1e-4));- vec4 wash = textureLod(u_colorMap, over, 0.0);+ vec4 wash = textureLod(u_colorMap, vec2(over.x, 1.0 - over.y), 0.0);float up = u_colorMapFrom > 0.0 ? smoothstep(u_colorMapFrom * 0.8, u_colorMapFrom, height) : 1.0;albedo = mix(albedo, wash.rgb, wash.a * u_colorMapStrength * up);}@@ -315,7 +316,7 @@ fn packNormal(n : vec3f) -> vec2f {#ifdef MESH_NORMALSif (mc.u_colorMapStrength > 0.0) {let over = (P.xz + frame.renderOrigin.xz - mc.u_colorMapArea.xy) / max(mc.u_colorMapArea.zw, vec2f(1e-4));- let wash = textureSampleLevel(u_colorMap, u_colorMap_s, over, 0.0);+ let wash = textureSampleLevel(u_colorMap, u_colorMap_s, vec2f(over.x, 1.0 - over.y), 0.0);var up = 1.0;if (mc.u_colorMapFrom > 0.0) { up = smoothstep(mc.u_colorMapFrom * 0.8, mc.u_colorMapFrom, height); }albedo = mix(albedo, wash.rgb, vec3f(wash.a * mc.u_colorMapStrength * up));diff --git a/sdk/src/terrain/terrainShader.ts b/sdk/src/terrain/terrainShader.tsindex 2ef722ec6..7f1ebdf72 100644--- a/sdk/src/terrain/terrainShader.ts+++ b/sdk/src/terrain/terrainShader.ts@@ -134,6 +134,7 @@ const TERRAIN = `#pragma shader "Terrain"// terrain's own x and z) and how much of it.#pragma param u_colorMap texture default(white)#pragma param u_colorMapStrength float default(0) range(0,1) ui(slider)+// An image's first row lies along the area's least z, as a heightmap's and a splatmap's do: a texture is stored last row first, so v is turned.#pragma param u_colorMapArea vec4 default(0,0,1,1)// How much of the sky reaches the ground, baked over the same area: its red dims the light that comes from all round (a gully, the foot// of a cliff), and leaves the sun's own to the shadows.@@ -308,7 +309,7 @@ void main() {if (u_colorMapStrength > 0.0) {vec2 over = (Pw.xz - u_terrainOrigin.xz - u_colorMapArea.xy) / max(u_colorMapArea.zw, vec2(1e-4));- vec4 wash = texture(u_colorMap, over);+ vec4 wash = texture(u_colorMap, vec2(over.x, 1.0 - over.y));bool on = all(greaterThanEqual(over, vec2(0.0))) && all(lessThanEqual(over, vec2(1.0)));albedo = mix(albedo, wash.rgb, on ? wash.a * u_colorMapStrength : 0.0);}@@ -322,7 +323,7 @@ void main() {float open = 1.0;if (u_occlusionStrength > 0.0) {vec2 under = (Pw.xz - u_terrainOrigin.xz - u_colorMapArea.xy) / max(u_colorMapArea.zw, vec2(1e-4));- if (all(greaterThanEqual(under, vec2(0.0))) && all(lessThanEqual(under, vec2(1.0)))) open = mix(1.0, textureLod(u_occlusionMap, under, 0.0).r, u_occlusionStrength);+ if (all(greaterThanEqual(under, vec2(0.0))) && all(lessThanEqual(under, vec2(1.0)))) open = mix(1.0, textureLod(u_occlusionMap, vec2(under.x, 1.0 - under.y), 0.0).r, u_occlusionStrength);}vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 0.5, open, ext);fragColor = vec4(lit, 1.0);@@ -479,7 +480,7 @@ fn packNormal(n : vec3f) -> vec2f {if (mc.u_colorMapStrength > 0.0) {let over = (Pw.xz - mc.u_terrainOrigin.xz - mc.u_colorMapArea.xy) / max(mc.u_colorMapArea.zw, vec2f(1e-4));- let wash = textureSampleLevel(u_colorMap, u_colorMap_s, over, 0.0);+ let wash = textureSampleLevel(u_colorMap, u_colorMap_s, vec2f(over.x, 1.0 - over.y), 0.0);if (all(over >= vec2f(0.0)) && all(over <= vec2f(1.0))) { albedo = mix(albedo, wash.rgb, vec3f(wash.a * mc.u_colorMapStrength)); }}var wet = 0.0;@@ -492,7 +493,7 @@ fn packNormal(n : vec3f) -> vec2f {var open = 1.0;if (mc.u_occlusionStrength > 0.0) {let under = (Pw.xz - mc.u_terrainOrigin.xz - mc.u_colorMapArea.xy) / max(mc.u_colorMapArea.zw, vec2f(1e-4));- if (all(under >= vec2f(0.0)) && all(under <= vec2f(1.0))) { open = mix(1.0, textureSampleLevel(u_occlusionMap, u_occlusionMap_s, under, 0.0).r, mc.u_occlusionStrength); }+ if (all(under >= vec2f(0.0)) && all(under <= vec2f(1.0))) { open = mix(1.0, textureSampleLevel(u_occlusionMap, u_occlusionMap_s, vec2f(under.x, 1.0 - under.y), 0.0).r, mc.u_occlusionStrength); }}let lit = applyLightingSurface(albedo, Ns, P, viewDir, 0.0, roughness, 0.5, open, ext);#ifdef ES_GBUFFER@@ -549,6 +550,7 @@ const TERRAIN_LAYERS = `#pragma shader "Terrain Layers"// terrain's own x and z) and how much of it.#pragma param u_colorMap texture default(white)#pragma param u_colorMapStrength float default(0) range(0,1) ui(slider)+// An image's first row lies along the area's least z, as a heightmap's and a splatmap's do: a texture is stored last row first, so v is turned.#pragma param u_colorMapArea vec4 default(0,0,1,1)// How much of the sky reaches the ground, baked over the same area: its red dims the light that comes from all round (a gully, the foot// of a cliff), and leaves the sun's own to the shadows.@@ -760,7 +762,7 @@ void main() {if (u_colorMapStrength > 0.0) {vec2 over = (Pw.xz - u_terrainOrigin.xz - u_colorMapArea.xy) / max(u_colorMapArea.zw, vec2(1e-4));- vec4 wash = texture(u_colorMap, over);+ vec4 wash = texture(u_colorMap, vec2(over.x, 1.0 - over.y));bool on = all(greaterThanEqual(over, vec2(0.0))) && all(lessThanEqual(over, vec2(1.0)));colour = mix(colour, wash.rgb, on ? wash.a * u_colorMapStrength : 0.0);}@@ -774,7 +776,7 @@ void main() {float open = 1.0;if (u_occlusionStrength > 0.0) {vec2 under = (Pw.xz - u_terrainOrigin.xz - u_colorMapArea.xy) / max(u_colorMapArea.zw, vec2(1e-4));- if (all(greaterThanEqual(under, vec2(0.0))) && all(lessThanEqual(under, vec2(1.0)))) open = mix(1.0, textureLod(u_occlusionMap, under, 0.0).r, u_occlusionStrength);+ if (all(greaterThanEqual(under, vec2(0.0))) && all(lessThanEqual(under, vec2(1.0)))) open = mix(1.0, textureLod(u_occlusionMap, vec2(under.x, 1.0 - under.y), 0.0).r, u_occlusionStrength);}vec3 lit = applyLightingSurface(colour, N, P, viewDir, 0.0, roughness, 0.5, surface.r * open, ext);fragColor = vec4(lit, 1.0);@@ -987,7 +989,7 @@ fn packNormal(n : vec3f) -> vec2f {if (mc.u_colorMapStrength > 0.0) {let over = (Pw.xz - mc.u_terrainOrigin.xz - mc.u_colorMapArea.xy) / max(mc.u_colorMapArea.zw, vec2f(1e-4));- let wash = textureSampleLevel(u_colorMap, u_colorMap_s, over, 0.0);+ let wash = textureSampleLevel(u_colorMap, u_colorMap_s, vec2f(over.x, 1.0 - over.y), 0.0);if (all(over >= vec2f(0.0)) && all(over <= vec2f(1.0))) { colour = mix(colour, wash.rgb, vec3f(wash.a * mc.u_colorMapStrength)); }}var wet = 0.0;@@ -1000,7 +1002,7 @@ fn packNormal(n : vec3f) -> vec2f {var open = 1.0;if (mc.u_occlusionStrength > 0.0) {let under = (Pw.xz - mc.u_terrainOrigin.xz - mc.u_colorMapArea.xy) / max(mc.u_colorMapArea.zw, vec2f(1e-4));- if (all(under >= vec2f(0.0)) && all(under <= vec2f(1.0))) { open = mix(1.0, textureSampleLevel(u_occlusionMap, u_occlusionMap_s, under, 0.0).r, mc.u_occlusionStrength); }+ if (all(under >= vec2f(0.0)) && all(under <= vec2f(1.0))) { open = mix(1.0, textureSampleLevel(u_occlusionMap, u_occlusionMap_s, vec2f(under.x, 1.0 - under.y), 0.0).r, mc.u_occlusionStrength); }}let lit = applyLightingSurface(colour, N, P, viewDir, 0.0, roughness, 0.5, surface.r * open, ext);#ifdef ES_GBUFFERdiff --git a/sdk/tests/area-map-rows.test.ts b/sdk/tests/area-map-rows.test.tsnew file mode 100644index 000000000..ddb2db58d--- /dev/null+++ b/sdk/tests/area-map-rows.test.ts@@ -0,0 +1,30 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file area-map-rows.test.ts+ * @brief Every image laid over an area has its first row along the least z, whether a system reads it or a shader samples it.+ */+import { describe, expect, it } from 'vitest';+import { densityAt, type DensityMap } from '../src/foliage/cellBuild';+import { FOLIAGE_SHADER_TEMPLATE } from '../src/foliage/foliageShader';+import { TERRAIN_LAYERS_SHADER_TEMPLATE, TERRAIN_SHADER_TEMPLATE } from '../src/terrain/terrainShader';++describe('the rows of an image over an area', () => {+ it('a map a system reads has its first row at the least z', () => {+ const map: DensityMap = { width: 1, height: 2, values: new Uint8Array([255, 0]), originX: 0, originZ: 0, sizeX: 10, sizeZ: 10 };+ expect(densityAt(map, 5, 1)).toBeGreaterThan(0.8);+ expect(densityAt(map, 5, 9)).toBeLessThan(0.2);+ });++ it('a map a shader samples is read with v turned, in both languages, and never straight', () => {+ for (const [source, coords] of [[TERRAIN_SHADER_TEMPLATE.source, ['over', 'under']], [TERRAIN_LAYERS_SHADER_TEMPLATE.source, ['over', 'under']],+ [FOLIAGE_SHADER_TEMPLATE.source, ['over']]] as const) {+ for (const at of coords) {+ expect(source).toContain(`vec2(${at}.x, 1.0 - ${at}.y)`);+ expect(source).toContain(`vec2f(${at}.x, 1.0 - ${at}.y)`);+ expect(source).not.toContain(`, ${at}, 0.0)`);+ expect(source).not.toContain(`, ${at})`);+ }+ }+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 7c74feb16..46478cf52 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4568,4 +4568,6 @@ export const NOTED = {{"note": "A tilemap layer can be lit."},"feat(sky): a sky of your own shades the ground under its clouds - defineSkyEffect takes groundShadow and a second function, skyShadow(world, sun), what is left of the light at a world position, and the sky pass multiplies every surface it finds by it, as Sky Atmosphere does with its own clouds":{"note": "A sky of your own shades the ground under its clouds."},+ "fix(terrain): a colour map and an occlusion map lie the way the heightmap does - Terrain.colorMap, Terrain.occlusionMap and Foliage.colorMap were sampled with their rows turned over, so a picture painted to match a heightmap, a splatmap or a density map came out mirrored north to south under them; an image over an area now has its first row along the least z whoever reads it":+ {"note": "A colour map and an occlusion map lie the way the heightmap does."},};