スポットライト
https://scrapbox.io/files/64dd9943a9f898001c19a211.png
code:スポットライト.cpp
# include <Siv3D.hpp>
class Spotlight {
public:
const MSRenderTexture renderTexture;
Spotlight(const Size&size=Scene::Size()):renderTexture{ size }{}
void draw()const {
const ScopedRenderStates2D blend{ BlendState::Multiplicative };
Graphics2D::Flush();
renderTexture.resolve();
renderTexture.draw();
}
};
class ScopedSpotlight {
public:
ScopedSpotlight(const Spotlight& spotlight,const ColorF& color=ColorF{0}) :target{spotlight.renderTexture.clear(color)} {}
private:
const ScopedRenderTarget2D target;
const ScopedRenderStates2D blend{ BlendState::Additive };
};
void Main()
{
Scene::SetBackground(ColorF{ 0.8 });
Spotlight light;
const int32 cellSize = 20;
Texture emoji1{ U"🐦"_emoji };
Texture emoji2{ U"🐣"_emoji };
Texture emoji3{ U"🦜"_emoji };
while (System::Update())
{
for (int32 y = 0; y < (Scene::Height() / cellSize); ++y)
{
for (int32 x = 0; x < (Scene::Width() / cellSize); ++x)
{
if (IsEven(y + x))
{
Rect{ (x * cellSize), (y * cellSize), cellSize }.draw(ColorF{ 0.75 });
}
}
}
emoji1.drawAt(200, 300);
emoji2.drawAt(400, 300);
emoji3.drawAt(600, 300);
{
//ライトを描く
ScopedSpotlight target{ light,ColorF{0.3} };
Circle{ Scene::Size() - Cursor::Pos(),100 }.draw(ColorF{0.5});
Circle{ Cursor::Pos(),100 }.draw(ColorF{0.5});
}
light.draw();
}
}
追記
2023/11/14:コードを変更しました。