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.

2.5 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    patch.ts
 * @brief   The one patch every quadtree node of the ground is drawn with.
 *
 * @details A flat grid of `cells` by `cells` over 0 to 1 in x and z, its uv the same; its box runs 0 to 1 in y as well, because the
 *          Terrain shader lifts it to the node's page and the node's scale.y is its page's height range, so culling sees the ground's true box.
 */
import { MeshChannel, MeshChannelType, packChannels, type MeshData } from '../asset/meshFormat';

export function buildPatchMesh(cells: number, flipped = false): MeshData {
    const n = Math.max(1, Math.floor(cells));
    const { channels, vertexStride } = packChannels([
        { semantic: MeshChannel.Position, components: 3, type: MeshChannelType.Float32 },
        { semantic: MeshChannel.TexCoord0, components: 2, type: MeshChannelType.Float32 },
        { semantic: MeshChannel.Color, components: 4, type: MeshChannelType.UNorm8 },
        { semantic: MeshChannel.Normal, components: 3, type: MeshChannelType.Float32 },
    ]);
    const count = (n + 1) * (n + 1);
    const vertices = new Uint8Array(count * vertexStride);
    const view = new DataView(vertices.buffer);
    const [pos, uv, color, normal] = channels.map((c) => c.offset) as [number, number, number, number];
    for (let j = 0; j <= n; j++) for (let i = 0; i <= n; i++) {
        const base = (j * (n + 1) + i) * vertexStride, x = i / n, z = j / n;
        view.setFloat32(base + pos, x, true);
        view.setFloat32(base + pos + 4, 0, true);
        view.setFloat32(base + pos + 8, z, true);
        view.setFloat32(base + uv, x, true);
        view.setFloat32(base + uv + 4, z, true);
        for (let k = 0; k < 4; k++) view.setUint8(base + color + k, 255);
        view.setFloat32(base + normal + 4, 1, true);
    }
    const indices = new Uint32Array(n * n * 6);
    let at = 0;
    for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) {
        const a = j * (n + 1) + i, b = a + 1, c = a + n + 1, d = c + 1;
        // Facing up: a, then the next row, then the next column, runs counter-clockwise from above.
        // `flipped` is the same grid wound the other way round: a cube face's (u, v, out) is a right-handed frame, where flat ground's (x, z, up) is not.
        indices.set(flipped ? [a, b, c, b, d, c] : [a, c, b, b, c, d], at);
        at += 6;
    }
    return { channels, vertexStride, vertexCount: count, vertices, indices, aabbMin: [0, 0, 0], aabbMax: [1, 1, 1] };
}