シンプルな雲
#OpenSiv3D #Circle #Quad
https://gyazo.com/8d126a850b4939d4904d5bfc39a0574e
code:cloud.cpp
# include <Siv3D.hpp> // Siv3D v0.6.16
// 雲を表現するためのCircleの配列を作る
void MakeCloudCircles(Array<Circle>& cloudCircles, int n, double baseCenterY, double radiusScale = 1.5)
{
const double r = static_cast<double>(Scene::Width()) / n / 2 * radiusScale;
cloudCircles.clear();
for (int i = 0; i < n + 2; ++i)
{
const Vec2 basePos{ r + (i - 1) * Scene::Width() / n, baseCenterY };
// x,y,rを適当にずらす
cloudCircles << Circle{
basePos + Vec2{ Random() * r * 0.3, Random() * r * 0.5 },
r + Random() * r * 0.5
};
}
}
void MakeCloudCirclesArray(Array<Array<Circle>>& cloudCircles, int n, double radiusScale = 1.5)
{
for (auto index, c : IndexedRef(cloudCircles))
{
const double margin = 250.0;
MakeCloudCircles(c, n, margin + (Scene::Height() - margin) / 3 * index, radiusScale);
}
}
void DrawCloud(Array<Array<Circle>>& cloudCircles, const ColorF& bgColor)
{
for (const auto ic, cloud : Indexed(cloudCircles))
{
const ColorF color = bgColor.lerp(ColorF{ 1.0 }, 0.5 + 0.49 / 2 * ic);
for (const auto& circle : cloud)
{
circle.draw(color);
}
Quad{
Vec2{ cloud.front().x, cloud.front().y },
Vec2{ cloud.back().x, cloud.back().y },
Scene::Rect().br(),
Scene::Rect().bl() }.draw(color);
}
}
void Main()
{
const ColorF bgColor{ 0.6, 0.7, 0.8 };
Scene::SetBackground(bgColor);
const int N = 8;
// 雲 (Array<Circle>)
// 何層か重ねる
Array<Array<Circle>> cloudCircles{ 3 };
MakeCloudCirclesArray(cloudCircles, N);
while (System::Update())
{
// Rキーで作り直す
if (KeyR.down())
{
MakeCloudCirclesArray(cloudCircles, N);
}
// 背景
Scene::Rect().draw(Arg::top = bgColor, Arg::bottom = bgColor.lerp(ColorF{ 1.0 }, 1.0));
// 雲
DrawCloud(cloudCircles, bgColor);
}
}