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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(2d): lights that cannot meet share a channel of the 2D shadow mask, so a camp of torches all cast
yhh6ae220d0f909
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 443343686..36cbb1825 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -32,7 +32,7 @@ env:test_hierarchy_links test_entity_activity test_transform_traversal test_ui_tree test_tween_activity test_springbonetest_shader_device test_draw_params test_per_draw_blocks test_texture_fb test_buffer_devicetest_compressed_format test_batch_builder test_skeletal_batch_color test_boundary_span test_webgpu_device- test_clip_depth test_planar_reflection test_sort_key test_retained_text test_sprite_mask_range test_sprite_draw_mode test_shadow_atlas test_particle_shapes test_lod_selection test_view_shading test_mesh_edges test_hdr_format test_probe_store test_occlusion_view test_reflection_store test_tiled_lights test_light_layers+ test_clip_depth test_planar_reflection test_sort_key test_retained_text test_sprite_mask_range test_sprite_draw_mode test_shadow_atlas test_particle_shapes test_lod_selection test_view_shading test_mesh_edges test_hdr_format test_probe_store test_occlusion_view test_reflection_store test_tiled_lights test_light_layers test_shadow_channelstest_text_edit test_render_graph test_device_loss test_physics_interpolationtest_bitmap_font test_texture_cache test_material_shader_lifetime test_mesh_recovery test_aot_host test_aot_conformancetest_touch_streamdiff --git a/docs/astro/src/content/docs/graphics/lighting.mdx b/docs/astro/src/content/docs/graphics/lighting.mdxindex b1a3c6537..3a281a841 100644--- a/docs/astro/src/content/docs/graphics/lighting.mdx+++ b/docs/astro/src/content/docs/graphics/lighting.mdx@@ -375,13 +375,15 @@ mask, and a lit surface reads one texel of it. So the number of occluders is a nof triangles rather than a shader constant — a room may have as many walls as it needs,and adding one costs a few triangles rather than a test on every pixel of the screen.-What IS capped is how many lights cast at once: the mask has **four channels**, so the-first four casting lights get one each. A light past that still lights the scene and-stops shadowing — the room stays lit, and what is missing is a shadow rather than the-light. A scene with more than four shadow-casting lights in view at once is usually-saying something about its art direction; the fix is to turn shadows off (`shadowDistance`-of 0 on a sun, or a smaller `radius` that takes a light out of view) on the ones whose-shadows nobody looks at.+What IS capped is how many lights that **meet** cast at once. The mask has **four channels**, and a channel is only wrong where two lights could both be: a pixel reads the+channel of each light that reaches it, so two lights may share one whenever neither's reach touches the other's shadow. A camp of torches spread round a clearing is as many+casting lights as it has torches; only lights whose reach and shadows overlap each need a channel of their own, so four lights in one place fill the mask and a fifth among them+does not. The sixteen lights the lighting loop reads are the ones that can cast (the brightest, once there are more than sixteen).++A light left without a channel still lights the scene and stops shadowing — the room stays lit, and what is missing is a shadow rather than the light. The first lights in+brightness order are placed first, so it is the dimmest of a crowd that goes without. A sun (a directional light with `shadowDistance`) is everywhere, so it takes a channel to+itself and leaves three for the rest. The profiler's `render.shadow2d.unplaced` counts the lights that went without. To make room, give the ones whose shadows nobody looks+at no shadow (`shadowDistance` of 0 on a sun), or a smaller `radius` so they stop overlapping their neighbours.## Blob shadows: a disc instead of a shadow mapdiff --git a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdxindex 22f7eb38a..a052a33ed 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx@@ -297,10 +297,9 @@ cmds.spawn()着色器常量——一个房间需要多少面墙就能有多少面,多加一面的代价是几个三角形,而不是整屏每个像素多一次测试。-**有上限的是同时投影的光数**:遮蔽图有**四个通道**,所以前四个投射光各占一个。超出的光仍然照亮场景,-只是不再投影——房间还是亮的,少掉的是影子而不是光。同屏超过四个投影光通常说明美术方向本身有问题;-处理办法是把没人会看那道影子的光关掉阴影(太阳的 `shadowDistance` 设 0,或者用更小的 `radius`-让某个光离开视野)。+**有上限的是同时投影、而且互相会碰到的光数**:遮蔽图有**四个通道**,而一个通道只有在两盏灯可能同时出现的地方才会出错——像素读的是每盏照到它的灯的通道,所以只要两盏灯的照射范围碰不到对方的影子,它们就能共用一个通道。围着空地分散摆放的一圈火把,有几个火把就有几盏投影灯;只有照射范围和影子互相重叠的灯才各占一个通道,所以同一处的四盏灯占满遮蔽图,挤进来的第五盏就没有。光照循环读取的十六盏灯是能投影的那些(超过十六盏时取最亮的)。++没分到通道的灯仍然照亮场景,只是不再投影——房间还是亮的,少掉的是影子而不是光。按亮度从高到低依次分配,所以一堆灯里最暗的那盏没有影子。太阳(带 `shadowDistance` 的方向光)无处不在,独占一个通道,给其余的灯留三个。性能分析器的 `render.shadow2d.unplaced` 统计了没分到通道的灯。要腾出位置,可以把没人看的那道影子关掉(太阳的 `shadowDistance` 设 0),或者用更小的 `radius` 让灯不再和邻居重叠。## 假阴影:用一个圆片代替阴影图diff --git a/examples/lighting-2d/README.md b/examples/lighting-2d/README.mdindex 31f2042b8..5239ed5a4 100644--- a/examples/lighting-2d/README.md+++ b/examples/lighting-2d/README.md@@ -18,8 +18,9 @@ Placed lights are capped at 10 and obstacles at 40; when you exceed a cap theoldest one fades out and is removed. The light cap is the frame's (16 fit in thelighting block); the obstacle one is only to keep the demo tidy — occluders aredrawn as geometry, so a scene may hold as many as it has walls. What IS capped-is how many lights cast at once: the first four get a channel of the 2D shadow-mask, and any beyond that light the room without shadowing it.+is how many lights that meet cast at once: the 2D shadow mask has four channels+and lights whose reach and shadows cannot touch share one, so only a fifth light+among four that overlap it lights the room without shadowing it.## How it worksdiff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hppindex aacc095f5..92e0f2f03 100644--- a/src/esengine/renderer/frame/RenderFrame.hpp+++ b/src/esengine/renderer/frame/RenderFrame.hpp@@ -899,6 +899,9 @@ private:struct ShadowRing2D {u32 first = 0;u32 count = 0;+ /// The middle of its points and how far the furthest is from it: what a light tests its reach against.+ glm::vec2 centre{0.0f};+ f32 radius = 0.0f;};/** @brief A light whose 2D shadows the frame draws, and the mask channel it owns. */@@ -914,12 +917,16 @@ private:f32 softness = 0.0f;/// Its slot in the light array, which is what the channel is recorded against.u32 slot = 0;+ /// The mask channel it draws into and is read from. Lights that cannot meet share one (see ShadowChannels2D.hpp).+ u32 channel = 0;bool directional = false;};std::vector<glm::vec2> shadow_points_2d_;std::vector<ShadowRing2D> shadow_rings_2d_;std::vector<Shadow2DLight> shadow_2d_lights_;+ /// How many casting lights the last collect could give no channel.+ u32 shadow_2d_unplaced_ = 0;/// The mask as a graph resource: the 2D shadow pass writes it, the scene reads it/// through a pinned sampler unit rather than a graph-bound one.rg::ResourceId shadow_2d_resource_ = rg::kNoResource;diff --git a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp b/src/esengine/renderer/frame/RenderFrameShadow2D.cppindex 05b58a1de..8b6cdbf9d 100644--- a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp+++ b/src/esengine/renderer/frame/RenderFrameShadow2D.cpp@@ -17,6 +17,7 @@#include "../../resource/ShaderParser.hpp"#include "../rhi/ShaderEmbeds.generated.hpp"#include "../../core/FrameProfiler.hpp"+#include "../store/ShadowChannels2D.hpp"#include <algorithm>#include <cmath>@@ -49,6 +50,7 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {shadow_points_2d_.clear();shadow_rings_2d_.clear();shadow_2d_lights_.clear();+ shadow_2d_unplaced_ = 0;shadow_2d_resource_ = rg::kNoResource;shadow_2d_texture_id_ = 0;context_.lights().setMask2DRect(glm::vec4(0.0f));@@ -88,11 +90,23 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {}if (shadow_rings_2d_.empty()) return;- // Which lights the mask can carry. The collect already ordered the light array by- // what the cap sorted on, so taking them in slot order takes the brightest first.+ for (ShadowRing2D& ring : shadow_rings_2d_) {+ const glm::vec2* pts = shadow_points_2d_.data() + ring.first;+ glm::vec2 centre{0.0f};+ for (u32 i = 0; i < ring.count; ++i) centre += pts[i];+ centre /= static_cast<f32>(ring.count);+ f32 radius = 0.0f;+ for (u32 i = 0; i < ring.count; ++i) radius = std::max(radius, glm::distance(pts[i], centre));+ ring.centre = centre;+ ring.radius = radius;+ }++ // The lights that cast. The collect already ordered the light array by what the cap+ // sorted on, so taking them in slot order takes the brightest first, and the mask's+ // four channels are shared by lights that cannot meet (see ShadowChannels2D.hpp).const LightConstants& lights = context_.lights().data();+ std::vector<ShadowMaskLight> masks;for (u32 slot = 0; slot < context_.lights().count(); ++slot) {- if (shadow_2d_lights_.size() >= MAX_SHADOW_2D_LIGHTS) break;const GpuLight& light = lights.lights[slot];if (light.color.a <= 0.0f) continue;const f32 type = light.posDir.z;@@ -113,7 +127,26 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {entry.reach = light.shadow.y;}if (entry.reach <= 0.0f) continue;++ ShadowMaskLight mask;+ mask.pos = entry.pos;+ if (entry.directional) {+ mask.everywhere = true;+ } else {+ // Where its geometry can land: the furthest of the occluders it sees, plus how far a shadow is carried past them. A light+ // that sees none draws nothing and has no shadow to read, so it takes no channel.+ f32 furthest = -1.0f;+ for (const ShadowRing2D& ring : shadow_rings_2d_) {+ const f32 d = glm::distance(ring.centre, entry.pos);+ if (d > entry.reach + entry.softness + ring.radius) continue;+ furthest = std::max(furthest, d + ring.radius + entry.softness);+ }+ if (furthest < 0.0f) continue;+ mask.read = entry.reach;+ mask.write = furthest + entry.reach * kShadow2DOvershoot;+ }shadow_2d_lights_.push_back(entry);+ masks.push_back(mask);}if (shadow_2d_lights_.empty()) {shadow_rings_2d_.clear();@@ -121,8 +154,26 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {return;}- for (u32 i = 0; i < shadow_2d_lights_.size(); ++i) {- context_.lights().setLightShadow2DChannel(shadow_2d_lights_[i].slot, i);+ std::vector<i32> channels(masks.size(), -1);+ const u32 dropped = assignShadowChannels(masks.data(), static_cast<u32>(masks.size()), MAX_SHADOW_2D_LIGHTS, channels.data());+ // A light that found no free channel still lights the scene; it only casts no 2D shadow. Kept in order, so what stays is the brightest.+ usize kept = 0;+ for (usize i = 0; i < shadow_2d_lights_.size(); ++i) {+ if (channels[i] < 0) continue;+ shadow_2d_lights_[kept] = shadow_2d_lights_[i];+ shadow_2d_lights_[kept].channel = static_cast<u32>(channels[i]);+ ++kept;+ }+ shadow_2d_lights_.resize(kept);+ shadow_2d_unplaced_ = dropped;+ if (shadow_2d_lights_.empty()) {+ shadow_rings_2d_.clear();+ shadow_points_2d_.clear();+ return;+ }++ for (const Shadow2DLight& light : shadow_2d_lights_) {+ context_.lights().setLightShadow2DChannel(light.slot, light.channel);}rg::TargetDesc desc;@@ -216,8 +267,8 @@ void RenderFrame::buildShadow2DGeometry() {vertex(c, sc, channel);};- for (u32 channel = 0; channel < shadow_2d_lights_.size(); ++channel) {- const Shadow2DLight& light = shadow_2d_lights_[channel];+ for (const Shadow2DLight& light : shadow_2d_lights_) {+ const u32 channel = light.channel;// A source with width is sampled across, each sample throwing the silhouette// it sees at its own share. The shares add, which is the soft edge: hidden from// every sample collects all of them, from half collects half.@@ -231,21 +282,13 @@ void RenderFrame::buildShadow2DGeometry() {for (const ShadowRing2D& ring : shadow_rings_2d_) {const glm::vec2* pts = shadow_points_2d_.data() + ring.first;- glm::vec2 centre{0.0f};- for (u32 i = 0; i < ring.count; ++i) centre += pts[i];- centre /= static_cast<f32>(ring.count);-- // What a light cannot reach it cannot be hidden from: past its own reach it- // contributes nothing, so an occluder out there has no shadow to draw. Costs- // one distance per ring and saves every triangle behind it.- if (!light.directional) {- f32 radius = 0.0f;- for (u32 i = 0; i < ring.count; ++i) {- radius = std::max(radius, glm::distance(pts[i], centre));- }- if (glm::distance(centre, light.pos) > light.reach + light.softness + radius) {- continue;- }+ const glm::vec2 centre = ring.centre;++ // Past a light's own reach it contributes nothing, so an occluder out there has no shadow+ // to draw. The same test bounds where this light's channel is written (collectShadow2D).+ if (!light.directional+ && glm::distance(centre, light.pos) > light.reach + light.softness + ring.radius) {+ continue;}for (u32 s = 0; s < samples; ++s) {@@ -325,6 +368,8 @@ void RenderFrame::executeShadow2DPass() {static_cast<u32>(shadow_2d_vertices_.size() / (kShadow2DFloats * 3)));ES_PROFILE_COUNTER("render.shadow2d.occluders", static_cast<u32>(shadow_rings_2d_.size()));ES_PROFILE_COUNTER("render.shadow2d.lights", static_cast<u32>(shadow_2d_lights_.size()));+ // Casting lights that found every channel held by one they overlap, and so cast no 2D shadow this frame.+ ES_PROFILE_COUNTER("render.shadow2d.unplaced", shadow_2d_unplaced_);if (shadow_2d_vertices_.empty()) return;const u32 bytes = static_cast<u32>(shadow_2d_vertices_.size() * sizeof(f32));diff --git a/src/esengine/renderer/store/LightConstants.hpp b/src/esengine/renderer/store/LightConstants.hppindex f561758fb..3186b309c 100644--- a/src/esengine/renderer/store/LightConstants.hpp+++ b/src/esengine/renderer/store/LightConstants.hpp@@ -80,11 +80,9 @@ inline constexpr u32 SHAPE_2D_TEXTURE_UNIT = 6;inline constexpr u32 MAX_SHAPE_2D_LIGHTS = 4;/**- * @brief Lights whose 2D shadows one frame can carry — the mask's four channels.- * @details A cap on CASTERS, not on lights: past it a light still lights the scene and- * stops shadowing, which is the failure a 2D scene can look at and understand.- * Four because an RGBA8 target has four channels and a fifth would be a second- * target, a second bind and a second sample for a case 2D art rarely reaches.+ * @brief Channels of the 2D shadow mask: the lights that MEET one another and cast at once (see ShadowChannels2D.hpp).+ * @details Four because an RGBA8 target has four, and a fifth would be a second target, bind and sample. A light past them still+ * lights the scene and stops shadowing, which is the failure a 2D scene can look at and understand.*/inline constexpr u32 MAX_SHADOW_2D_LIGHTS = 4;diff --git a/src/esengine/renderer/store/ShadowChannels2D.hpp b/src/esengine/renderer/store/ShadowChannels2D.hppnew file mode 100644index 000000000..a222e2b4b--- /dev/null+++ b/src/esengine/renderer/store/ShadowChannels2D.hpp@@ -0,0 +1,61 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file ShadowChannels2D.hpp+ * @brief Which channel of the 2D shadow mask each casting light draws into, so lights that never meet can share one.+ * @details The mask is one screen-sized RGBA8 target, four channels, and a Lit fragment reads the channel of each light that reaches it.+ * A channel is only wrong where two lights could both be there: a fragment that a light reaches reads that light's channel, and+ * what ELSE was drawn into it, by a light whose shadow lands on the same fragment, would be read as this light's. So two lights+ * may share a channel when neither's reach (where it is read) touches the other's shadow (where it is written). A scene of+ * torches spread round a camp is then as many casting lights as it has torches, and only lights that overlap pay a channel each.+ * Header-only and device-free, so the answer is pinned by a test and not by a frame.+ */+#pragma once++#include "../../core/Types.hpp"++#include <glm/glm.hpp>++namespace esengine {++/** @brief One light as the channel assignment sees it: where its shadows are read and where they can land. */+struct ShadowMaskLight {+ glm::vec2 pos{0.0f};+ /// Where a fragment reads this light's channel: within this distance of @ref pos.+ f32 read = 0.0f;+ /// Where its geometry can be written: within this distance of @ref pos.+ f32 write = 0.0f;+ /// A directional light is read and written everywhere, so it shares a channel with nothing.+ bool everywhere = false;+};++/** @brief Whether two lights cannot share a channel: one reads where the other writes. */+inline bool shadowMasksCollide(const ShadowMaskLight& a, const ShadowMaskLight& b) {+ if (a.everywhere || b.everywhere) return true;+ const f32 d = glm::distance(a.pos, b.pos);+ return d < a.read + b.write || d < b.read + a.write;+}++/**+ * @brief Gives each light the first channel none of its collisions hold, in the order the lights are given.+ * @param channels How many channels the mask has.+ * @param out One entry per light: its channel, or -1 where every channel holds a light it collides with.+ * @return How many lights got none. A light with no channel still lights; it only casts no 2D shadow.+ */+inline u32 assignShadowChannels(const ShadowMaskLight* lights, u32 count, u32 channels, i32* out) {+ u32 dropped = 0;+ for (u32 i = 0; i < count; ++i) {+ out[i] = -1;+ for (u32 c = 0; c < channels && out[i] < 0; ++c) {+ bool free = true;+ for (u32 j = 0; j < i && free; ++j) {+ if (out[j] == static_cast<i32>(c) && shadowMasksCollide(lights[i], lights[j])) free = false;+ }+ if (free) out[i] = static_cast<i32>(c);+ }+ if (out[i] < 0) ++dropped;+ }+ return dropped;+}++} // namespace esenginediff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txtindex 15f489eef..75b3c0f6a 100644--- a/tests/CMakeLists.txt+++ b/tests/CMakeLists.txt@@ -414,6 +414,14 @@ target_include_directories(test_light_layers PRIVATE)add_test(NAME test_light_layers COMMAND test_light_layers)+# Which channel of the 2D shadow mask each casting light draws into. Header-only, so it runs without a device.+add_executable(test_shadow_channels renderer/test_shadow_channels.cpp)+target_include_directories(test_shadow_channels PRIVATE+ ${CMAKE_SOURCE_DIR}/src+ ${CMAKE_SOURCE_DIR}/third_party/glm+)+add_test(NAME test_shadow_channels COMMAND test_shadow_channels)+# Which baked column a point reflects. Header-only, so it runs without a device —# the pixel gate says a surface came out the wrong colour, this says whether the# lookup took the wrong box or let two bakes into one frame.diff --git a/tests/renderer/test_shadow_channels.cpp b/tests/renderer/test_shadow_channels.cppnew file mode 100644index 000000000..a8c2b89ab--- /dev/null+++ b/tests/renderer/test_shadow_channels.cpp@@ -0,0 +1,106 @@+// Which channel of the 2D shadow mask each casting light draws into: lights that cannot meet share one.+// The contract: two lights share a channel only when neither's read reach touches the other's written shadow,+// a directional light shares with nothing, the first lights given get channels first, and a fifth overlapping light gets none.++#include "esengine/renderer/store/ShadowChannels2D.hpp"++#include <cstdio>+#include <vector>++using namespace esengine;++static int g_failures = 0;+#define CHECK(cond, msg) \+ do { \+ if (!(cond)) { std::printf("FAIL: %s\n", msg); ++g_failures; } \+ else { std::printf("ok: %s\n", msg); } \+ } while (0)++namespace {++ShadowMaskLight lamp(f32 x, f32 y, f32 reach = 100.0f, f32 write = 230.0f) {+ ShadowMaskLight l;+ l.pos = {x, y};+ l.read = reach;+ l.write = write;+ return l;+}++ShadowMaskLight sun() {+ ShadowMaskLight l;+ l.everywhere = true;+ return l;+}++std::vector<i32> assign(const std::vector<ShadowMaskLight>& lights, u32* dropped = nullptr) {+ std::vector<i32> out(lights.size(), 0);+ const u32 d = assignShadowChannels(lights.data(), static_cast<u32>(lights.size()), 4, out.data());+ if (dropped) *dropped = d;+ return out;+}++} // namespace++int main() {+ // --- lights that meet each take their own channel+ {+ const auto c = assign({lamp(0, 0), lamp(50, 0), lamp(0, 50), lamp(50, 50)});+ CHECK(c[0] == 0 && c[1] == 1 && c[2] == 2 && c[3] == 3, "four lamps in one place take the four channels, in the order they were given");+ }++ // --- lights that cannot meet share the first one+ {+ const auto c = assign({lamp(0, 0), lamp(5000, 0), lamp(-5000, 0), lamp(0, 5000)});+ CHECK(c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == 0, "four lamps far apart all share channel 0");+ }++ // --- more than four, as long as no more than four meet anywhere+ {+ std::vector<ShadowMaskLight> camp;+ for (int i = 0; i < 12; ++i) camp.push_back(lamp(i * 1000.0f, 0));+ u32 dropped = 9;+ const auto c = assign(camp, &dropped);+ bool all = true;+ for (i32 v : c) if (v < 0) all = false;+ CHECK(all && dropped == 0, "twelve torches in a row, a thousand apart, all cast: the cap was on lights that meet, not on lights");+ }++ // --- the test is the read against the write: one lamp's reach touching the other's shadow, either way round+ {+ // Each reads 100 out and can write 150 out, so two collide while they are less than 100 + 150 apart.+ const auto apart = assign({lamp(0, 0, 100, 150), lamp(260, 0, 100, 150)});+ CHECK(apart[0] == 0 && apart[1] == 0, "260 apart, past 100 + 150: they cannot meet and share a channel");+ const auto close = assign({lamp(0, 0, 100, 150), lamp(240, 0, 100, 150)});+ CHECK(close[0] == 0 && close[1] == 1, "240 apart, inside it: one reads where the other writes, so they take a channel each");+ const auto meet = assign({lamp(0, 0, 100, 150), lamp(260, 0, 100, 150), lamp(130, 0, 100, 150)});+ CHECK(meet[2] == 1, "a lamp between two that share one meets both and takes the next");+ // A long shadow and a short reach: the reading light is the one that is near, the writing light's shadow is what reaches it.+ const auto lopsided = assign({lamp(0, 0, 10, 500), lamp(400, 0, 10, 20)});+ CHECK(lopsided[1] == 1, "a lamp whose reach is tiny still collides with one whose shadow lands on it");+ }++ // --- a directional light shares with nothing, wherever the others are+ {+ const auto c = assign({sun(), lamp(0, 0), lamp(9000, 0)});+ CHECK(c[0] == 0 && c[1] != 0 && c[2] != 0, "the sun holds channel 0 alone and every lamp goes round it");+ CHECK(c[1] == 1 && c[2] == 1, "and the two far lamps share the next");+ }++ // --- a fifth that meets four has none+ {+ u32 dropped = 0;+ const auto c = assign({lamp(0, 0), lamp(10, 0), lamp(0, 10), lamp(10, 10), lamp(5, 5)}, &dropped);+ CHECK(c[4] == -1 && dropped == 1, "a fifth lamp that meets four gets no channel and is counted");+ CHECK(c[0] == 0 && c[3] == 3, "and the first four keep theirs: what is dropped is the last given, the dimmest");+ }++ // --- nothing at all+ {+ u32 dropped = 7;+ const auto c = assign({}, &dropped);+ CHECK(c.empty() && dropped == 0, "no lights is no channels and nothing dropped");+ }++ std::printf("%s\n", g_failures == 0 ? "all passed" : "FAILED");+ return g_failures == 0 ? 0 : 1;+}