Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShadowChannels2D.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#pragma once
14
15#include "../../core/Types.hpp"
16
17#include <glm/glm.hpp>
18
19namespace esengine {
20
23 glm::vec2 pos{0.0f};
25 f32 read = 0.0f;
27 f32 write = 0.0f;
29 bool everywhere = false;
30};
31
33inline bool shadowMasksCollide(const ShadowMaskLight& a, const ShadowMaskLight& b) {
34 if (a.everywhere || b.everywhere) return true;
35 const f32 d = glm::distance(a.pos, b.pos);
36 return d < a.read + b.write || d < b.read + a.write;
37}
38
45inline u32 assignShadowChannels(const ShadowMaskLight* lights, u32 count, u32 channels, i32* out) {
46 u32 dropped = 0;
47 for (u32 i = 0; i < count; ++i) {
48 out[i] = -1;
49 for (u32 c = 0; c < channels && out[i] < 0; ++c) {
50 bool free = true;
51 for (u32 j = 0; j < i && free; ++j) {
52 if (out[j] == static_cast<i32>(c) && shadowMasksCollide(lights[i], lights[j])) free = false;
53 }
54 if (free) out[i] = static_cast<i32>(c);
55 }
56 if (out[i] < 0) ++dropped;
57 }
58 return dropped;
59}
60
61} // namespace esengine
Core type definitions and utilities for ESEngine.
bool shadowMasksCollide(const ShadowMaskLight &a, const ShadowMaskLight &b)
Whether two lights cannot share a channel: one reads where the other writes.
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
u32 assignShadowChannels(const ShadowMaskLight *lights, u32 count, u32 channels, i32 *out)
Gives each light the first channel none of its collisions hold, in the order the lights are given.
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
One light as the channel assignment sees it: where its shadows are read and where they can land.
f32 write
Where its geometry can be written: within this distance of pos.
bool everywhere
A directional light is read and written everywhere, so it shares a channel with nothing.
f32 read
Where a fragment reads this light's channel: within this distance of pos.