我最近开始学习DirectX,我想为我的项目创建一个漂亮的RGB发光背景。但是我不知道如何实现适当的RGB衰落。这是我的代码,但它不会创建所有颜色,并且其中某些颜色太暗:
r = sin(timer.Peek() + 2.0f) / 2.0f + 0.5f;
g = sin(timer.Peek() + 1.0f) / 2.0f + 0.5f;
b = sin(timer.Peek()) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer(r, g, b);
这是我的Graphics类的代码:
class Graphics
{
public:
Graphics(HWND hWnd);
Graphics(const Graphics&) = delete;
Graphics& operator=(const Graphics&) = delete;
~Graphics();
void EndFrame();
void ClearBuffer(float red, float green, float blue) noexcept;
private:
ID3D11Device* pDevice = nullptr;
IDXGISwapChain* pSwap = nullptr;
ID3D11DeviceContext* pContext = nullptr;
ID3D11RenderTargetView* pTarget = nullptr;
};
void Graphics::ClearBuffer(float red, float green, float blue) noexcept
{
const float color[] = { red,green,blue,1.0f };
pContext->ClearRenderTargetView(pTarget, color);
}
以及我的计时器代码:
class BasicTimer
{
public:
BasicTimer() noexcept;
float Mark() noexcept;
float Peek() const noexcept;
private:
std::chrono::steady_clock::time_point last;
};
float BasicTimer::Peek() const noexcept
{
return duration<float>(steady_clock::now() - last).count();
}
这里是全色混合功能,将两种颜色传递给它,它将在它们之间进行淡入淡出(to_weight
在0到1之间)。它也可以正确处理Alpha通道。
rgb mix(const rgb& from, const rgb& to, float to_weight)
{
to_weight = clamp(to_weight, 0.0f, 1.0f);
float f = (1 - to_weight) * from.alpha();
float t = to_weight * to.alpha();
float a = f + t;
if (a > 0)
{
float r = from.red() * f + to.red() * t;
float g = from.green() * f + to.green() * t;
float b = from.blue() * f + to.blue() * t;
return { r / a, g / a, b / a, a };
}
else
{
return { 0, 0, 0, 0 };
}
}
[rgb
是一个自定义类,clamp
是一个自定义函数,但是您知道了...