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.3 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    ShadowCaster2D.hpp
 * @brief   Marks an entity as a 2D shadow occluder for the Lit lighting path.
 * @details The render collect path turns each enabled caster into a world-space ring — its
 *          own outline, or the four edges of a box — placed by its Transform, and the 2D
 *          shadow pass DRAWS what that ring
 *          hide from each casting light into a screen-space mask. A Lit fragment then reads
 *          its light's channel of that mask. Drawn rather than solved per pixel, which is what
 *          lets a scene hold as many occluders as it has walls.
 *
 * @copyright Copyright (c) 2026 ESEngine Team
 *            Licensed under the Apache License, Version 2.0.
 */
#pragma once

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

#include <vector>

namespace esengine::ecs {

/**
 * @brief A 2D shadow occluder. The box is centred on the entity's world position and
 *        turns with it; its size is its own, which is how a collider is placed too.
 */
ES_COMPONENT(renderable=enabled)
struct ShadowCaster2D {
    /** @brief Occluder box size in world units (full width/height; centered on the Transform).
     *         Ignored by a caster that carries a @ref path. */
    ES_PROPERTY(animatable, min=0, tooltip="Occluder box size in world units (centred on the entity, turning with it). Unused when the caster has an outline.")
    glm::vec2 size{32.0f, 32.0f};

    /**
     * @brief The occluder's outline in local units, or empty for @ref size's box.
     * @details Three points or more, in order round the shape; where it sits and how
     *          it is turned is the Transform's, exactly as the box's is. Traced from
     *          the sprite's own alpha in the editor, which is where a shape that
     *          matches the artwork comes from.
     */
    ES_PROPERTY()
    std::vector<glm::vec2> path;

    /** @brief Disabled casters are skipped during collection. */
    ES_PROPERTY()
    bool enabled{true};

    /// Compared member by member: what the change audit checks a write of this component against (Registry::auditChanges).
    bool operator==(const ShadowCaster2D&) const = default;
};

}  // namespace esengine::ecs