3Dの光源
Reputeless 2016/05/31(Tue) 18:36
Siv3D の基本光源にスポッライトは無いので、
3D 描画を Forward Rendering にして、カスタムシェーダでスポットライト光源を処理してください。
[SpotLight.hlsl]
https://gist.github.com/Reputeless/840382fde6ba5a18e216a18dfef22f75
[Main]
# include <Siv3D.hpp>
struct SpotLight
{
Float3 position;
float attenuation;
Float3 diffuseColor;
float exponent;
Float3 direction;
float cutoff;
};
void Main()
{
const Texture textureGround(L"Example/Ground.jpg", TextureDesc::For3D);
const Texture textureBrick(L"Example/Brick.jpg", TextureDesc::For3D);
Graphics::SetBackground(ColorF(0.02, 0.02, 0.05));
Graphics3D::SetLightForward(0, Light::None());
Graphics3D::SetAmbientLightForward(ColorF(0.05));
// SpotLight
const VertexShader vs(L"SpotLight.hlsl");
const PixelShader ps(L"SpotLight.hlsl");
ConstantBuffer<SpotLight> cb;
if (!vs || !ps)
return;
GUI gui(GUIStyle::Default);
gui.add(GUIText::Create(L"attenuation"));
gui.addln(L"attenuation", GUISlider::Create(0.0, 0.1, 0.01));
gui.add(GUIText::Create(L"exponent"));
gui.addln(L"exponent", GUISlider::Create(0.0, 256.0, 32.0));
gui.add(GUIText::Create(L"cutoff"));
gui.addln(L"cutoff", GUISlider::Create(0_deg, 90_deg, 30_deg));
while (System::Update())
{
Graphics3D::FreeCamera();
cb->position = Graphics3D::GetCamera().pos + Vec3(1, 0, 0);// Vec3(20, 20, -20);
cb->attenuation = gui.slider(L"attenuation").value;
cb->diffuseColor = ColorF(1, 0.9, 0.3).rgb();
cb->exponent = gui.slider(L"exponent").value;
cb->direction = Mouse::Ray().direction;
cb->cutoff = gui.slider(L"cutoff").value;
Graphics3D::SetConstantForward(ShaderStage::Pixel, 1, cb);
Graphics3D::BeginVSForward(vs);
Graphics3D::BeginPSForward(ps);
Plane(1000).drawForward(textureGround);
for (int32 x = -5; x <= 5; ++x)
{
Box(x * 8, 5, 10, 4, 10, 2).drawForward(textureBrick);
}
Graphics3D::EndVSForward();
Graphics3D::EndPSForward();
}
}