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.6 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    ProbeVolume.hpp
 * @brief   A baked environment sampled on a grid, rather than held at one value.
 */
#pragma once

#include "../core/Types.hpp"
#include "../core/Revision.hpp"
#include "../math/Math.hpp"

#include <atomic>
#include <vector>

namespace esengine {

/**
 * @brief Irradiance at a grid of points — an @ref Environment sampled, not held.
 *
 * @details The same nine coefficients per probe, running x fastest then y then z:
 *          the order a bake writes and a trilinear read assumes. Where the grid
 *          STANDS is the component's, so moving a volume moves the light it holds
 *          and one bake can be read at a second placement.
 */
class ProbeVolume {
public:
    /** @brief Probes along each axis. One on an axis is a constant along it —
     *         which is what an Environment already is — so a flat volume is the
     *         same case and not a second one. */
    glm::ivec3 resolution{0};

    /** @brief Nine RGB coefficients per probe, in grid order. */
    std::vector<glm::vec3> irradiance;

    /** @brief How much of the sky each probe sees: nine coefficients per probe, cosine-convolved like the
     *         irradiance (1 everywhere is open sky), in grid order. Empty for a grid baked without sky
     *         occlusion, whose irradiance then holds the sky as it was at the bake; with it, the irradiance
     *         holds the lights and bounces alone and the frame's sky is added through this. */
    std::vector<f32> skyVisibility;

    /** @brief Changes whenever the coefficients do (a new grid, its sky visibility set): what the frame's probe
     *         atlas keys on, so it uploads a grid once and again only when the grid itself changed. Unique across
     *         grids, so a released grid whose memory a new one reuses is never mistaken for it. */
    u64 revision = 0;

    static u64 nextRevision() { return esengine::nextRevision(); }

    bool hasSkyVisibility() const {
        return !skyVisibility.empty() && skyVisibility.size() == static_cast<usize>(probeCount()) * 9;
    }

    u32 probeCount() const {
        if (resolution.x <= 0 || resolution.y <= 0 || resolution.z <= 0) return 0;
        return static_cast<u32>(resolution.x) * static_cast<u32>(resolution.y)
             * static_cast<u32>(resolution.z);
    }

    bool isValid() const {
        const u32 probes = probeCount();
        return probes > 0 && irradiance.size() == static_cast<usize>(probes) * 9;
    }
};

}  // namespace esengine