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.

1.7 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
#pragma once

#include "../../core/Types.hpp"
#include <glm/glm.hpp>

namespace esengine {

/**
 * @brief One vertex of the shared batch stream.
 *
 * @details `position.z` is world z: what the projection and the depth test consume.
 *          `BatchDrawKey::depth` is that same point measured from the viewer instead —
 *          one is geometry, the other decides draw ORDER. They must agree on which of
 *          two draws is nearer, or painter's order and the depth buffer place them apart.
 */
struct BatchVertex {
    glm::vec3 position;
    u32 color;
    glm::vec2 texCoord;
    f32 texIndex = 0.0f;  // sampler slot within the draw's texture set; assigned at merge time
    // How far to push the SDF text edge outward, in the atlas's own distance units
    // (0.5 = one spread). An outline is the glyph drawn once more with this raised,
    // which is a real dilation of the distance field — the alternative, stamping the
    // glyph around itself, stops looking like the glyph once the ring is wide.
    // Per-vertex rather than per-draw so labels of different styles still batch;
    // everything that is not SDF text leaves it at 0.
    f32 sdfBias = 0.0f;
};

inline u32 packColor(f32 r, f32 g, f32 b, f32 a) {
    auto clamp = [](f32 v) -> u8 {
        return static_cast<u8>(std::min(std::max(v, 0.0f), 1.0f) * 255.0f + 0.5f);
    };
    return static_cast<u32>(clamp(r)) | (static_cast<u32>(clamp(g)) << 8)
         | (static_cast<u32>(clamp(b)) << 16) | (static_cast<u32>(clamp(a)) << 24);
}

inline u32 packColor(const glm::vec4& c) {
    return packColor(c.r, c.g, c.b, c.a);
}

}  // namespace esengine