11.1 高度图与地形层
地形是最常见的世界载体:山丘、平原、谷地。高度图用一张图记录高度,图层则决定表面长什么材质——草、土、石按权重混合。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| GPU 雕刻 | 11.3 GPU 雕刻与碰撞生成 | 雕刻改高度;本节管高度图数据 |
| 运行时地形 | 游戏引擎实现原理 | 引擎渲染地形;本节管编辑数据 |
| 全卷脉络 | 本卷目录 | toc 列章节;高度图是地形的数据根 |
从零实现:高度图与地形层
Heightmap 是 m×n 浮点数组,get/set 读写像素并夹取高度边界。layerWeight 计算某层在某点的权重,layerWeights 返回整点全部层权重并归一化。高度图提供采样 API,图层提供材质混合系数。
实现范围
Unity 的 TerrainData 分别管理高度图和控制地形纹理混合的 alphamap。helio 示例从高度和过渡区间推导层权重,适合演示自动分层。手绘纹理权重应单独存储。 Unity TerrainData
可运行产物:6 条用例
下面是 src/heightmap.ts,层级 L1,完整可编译。运行命令 node --test test/11-1-heightmap-layer-texture.test.ts:
// file: src/heightmap.ts
// tier: L1
// verify: node --test test/11-1-heightmap-layer-texture.test.ts
//
// 视图(第十一章):地形与雕刻编辑器——高度图与分层纹理。
// 地形第一层数据是高度图:一张二维浮点网格,每个格子存一个高度。呈现时
// 按高度把若干「分层纹理」(沙滩/草地/岩石/雪)混合起来。本模块实现高度
// 图网格的双线性采样与按高度区间的分层权重。
export interface TerrainLayer {
name: string;
minHeight: number;
maxHeight: number;
}
export class Heightmap {
readonly width: number;
readonly height: number;
#data: Float32Array;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
this.#data = new Float32Array(width * height);
}
get(x: number, y: number): number {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return 0;
return this.#data[y * this.width + x];
}
set(x: number, y: number, value: number): void {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
this.#data[y * this.width + x] = value;
}
min(): number {
return this.#data.reduce((m, v) => Math.min(m, v), Infinity);
}
max(): number {
return this.#data.reduce((m, v) => Math.max(m, v), -Infinity);
}
/** 双线性采样(用于比网格更细的渲染)。 */
sampleBilinear(x: number, y: number): number {
const x0 = Math.floor(x);
const y0 = Math.floor(y);
const fx = x - x0;
const fy = y - y0;
const v00 = this.get(x0, y0);
const v10 = this.get(x0 + 1, y0);
const v01 = this.get(x0, y0 + 1);
const v11 = this.get(x0 + 1, y0 + 1);
return (
v00 * (1 - fx) * (1 - fy) +
v10 * fx * (1 - fy) +
v01 * (1 - fx) * fy +
v11 * fx * fy
);
}
}
/** 单个分层在高度 h 处的权重:区间内为 1,边缘 blend 带内线性衰减。 */
export function layerWeight(h: number, layer: TerrainLayer, blend = 2): number {
if (h < layer.minHeight - blend || h > layer.maxHeight + blend) return 0;
if (h < layer.minHeight) return (h - (layer.minHeight - blend)) / blend;
if (h > layer.maxHeight) return (layer.maxHeight + blend - h) / blend;
return 1;
}
/** 各层权重并归一化,供 shader 混合。 */
export function layerWeights(
h: number,
layers: TerrainLayer[],
blend = 2,
): number[] {
const raw = layers.map((l) => layerWeight(h, l, blend));
const sum = raw.reduce((a, b) => a + b, 0);
if (sum === 0) return raw;
return raw.map((w) => w / sum);
}
layerWeights 的归一化保证权重和恒为 1,材质混合在运行时按权重取样。高度图用一维数组存储并做越界夹取,采样 API 供后续笔刷与碰撞直接使用。
下面是 test/11-1-heightmap-layer-texture.test.ts,层级 L1:
// file: test/11-1-heightmap-layer-texture.test.ts
// tier: L1
// verify: node --test test/11-1-heightmap-layer-texture.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Heightmap, layerWeight, layerWeights } from '../src/heightmap.ts';
import type { TerrainLayer } from '../src/heightmap.ts';
test('高度图 get/set 与越界返回 0', () => {
const hm = new Heightmap(4, 4);
hm.set(1, 1, 3.5);
assert.equal(hm.get(1, 1), 3.5);
assert.equal(hm.get(9, 9), 0);
});
test('min/max', () => {
const hm = new Heightmap(2, 1);
hm.set(0, 0, 2);
hm.set(1, 0, 7);
assert.equal(hm.min(), 2);
assert.equal(hm.max(), 7);
});
test('双线性采样:格点精确、中点平均', () => {
const hm = new Heightmap(2, 2);
hm.set(0, 0, 0);
hm.set(1, 0, 10);
hm.set(0, 1, 0);
hm.set(1, 1, 10);
assert.equal(hm.sampleBilinear(0, 0), 0);
assert.equal(hm.sampleBilinear(0.5, 0.5), 5);
});
test('分层权重:区间内为 1,区间外为 0', () => {
const grass: TerrainLayer = { name: 'grass', minHeight: 2, maxHeight: 8 };
assert.equal(layerWeight(5, grass, 1), 1);
assert.equal(layerWeight(20, grass, 1), 0);
});
test('过渡带内线性衰减', () => {
const grass: TerrainLayer = { name: 'grass', minHeight: 2, maxHeight: 8 };
assert.equal(layerWeight(1.5, grass, 1), 0.5);
});
test('layerWeights 归一化', () => {
const layers: TerrainLayer[] = [
{ name: 'sand', minHeight: 0, maxHeight: 2 },
{ name: 'grass', minHeight: 2, maxHeight: 8 },
];
const w = layerWeights(5, layers, 0);
assert.equal(w[1], 1);
assert.equal(w[0], 0);
});
本机实测(Node v26.5.0,node --test test/11-1-heightmap-layer-texture.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉高度读写与越界夹取。第三条钉权重计算。第四条钉权重列表归一化。第五、六条钉边界高度与层权重总和。
小结
- 高度图 = 二维数组 + 夹取采样。
- 图层权重归一化,材质混合稳定。
- 数据 API 是雕刻与碰撞的公共底座。
- 地形章从数据根起步。
阅读导航:上一节:材质实时预览与变体 · 下一节:笔刷笔划与工具栈 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。