図0004a
void SquareSpriteGroup::OnCreate() { float HelfSize = 0.5f; //頂点配列 m_BackupVertices = { { VertexPositionColor(Vec3(-HelfSize, HelfSize, 0), Col4(1.0f, 0.0f, 0.0f, 1.0f)) }, { VertexPositionColor(Vec3(HelfSize, HelfSize, 0), Col4(0.0f, 1.0f, 0.0f, 1.0f)) }, { VertexPositionColor(Vec3(-HelfSize, -HelfSize, 0), Col4(0.0f, 0.0f, 1.0f, 1.0f)) }, { VertexPositionColor(Vec3(HelfSize, -HelfSize, 0), Col4(1.0f, 0.0f, 1.0f, 1.0f)) }, }; //インデックス配列 vector<uint16_t> indices = { 0, 1, 2, 1, 3, 2 }; //メッシュの作成 //頂点変更できる m_SquareSpriteMesh = MeshResource::CreateMeshResource(m_BackupVertices, indices, true); //グループの配列の作成 m_SquareSpriteVec.assign(100, SquareSprite()); for (auto& v : m_SquareSpriteVec) { v.m_LocalRot = Util::RandZeroToOne(true); v.m_LocalRotVelocity = Util::RandZeroToOne(true) * 20.0f - 10.0f; v.m_LocalPos = Vec2(0, 0); v.m_LocalVelocity = Vec2(Util::RandZeroToOne(true) * 200.0f - 100.0f, 100 + Util::RandZeroToOne(true) * 100.0f); v.m_LocalGravityVelocity = Vec2(0, 0); } }
void SquareSpriteGroup::OnUpdate() { float ElapsedTime = App::GetApp()->GetElapsedTime(); UpdateObjects(ElapsedTime); }
//各オブジェクトの位置等の変更 void SquareSpriteGroup::UpdateObjects(float ElapsedTime) { float h = static_cast<float>(App::GetApp()->GetGameHeight()); h /= 2.0f; for (auto& v : m_SquareSpriteVec) { if (v.m_LocalPos.y < -h) { v.m_LocalRot = Util::RandZeroToOne(true); v.m_LocalRotVelocity = Util::RandZeroToOne(true) * 20.0f - 10.0f; v.m_LocalPos = Vec2(0, 0); v.m_LocalVelocity = Vec2(Util::RandZeroToOne(true) * 200.0f - 100.0f, 100 + Util::RandZeroToOne(true) * 100.0f); v.m_LocalGravityVelocity = Vec2(0, 0); } else { v.m_LocalRot += v.m_LocalRotVelocity * ElapsedTime; v.m_LocalPos += v.m_LocalVelocity * ElapsedTime; v.m_LocalGravityVelocity += Vec2(0, -98.0f) * ElapsedTime; v.m_LocalPos += v.m_LocalGravityVelocity * ElapsedTime; } } }
void SquareSpriteGroup::OnDraw() {
auto Dev = App::GetApp()->GetDeviceResources();
auto pD3D11DeviceContext = Dev->GetD3DDeviceContext();
auto RenderState = Dev->GetRenderState();
//中略
//グループをまとめて描画
for (auto& v : m_SquareSpriteVec) {
//行列の定義
Mat4x4 World, Proj;
//ワールド行列の決定
World.affineTransformation2D(
v.m_LocalScale, //スケーリング
Vec2(0, 0), //回転の中心(重心)
v.m_LocalRot, //回転角度
v.m_LocalPos //位置
);
//射影行列の決定
float w = static_cast<float>(App::GetApp()->GetGameWidth());
float h = static_cast<float>(App::GetApp()->GetGameHeight());
Proj = XMMatrixOrthographicLH(w, h, -1.0, 1.0f);
//行列の合成
World *= Proj;
//コンスタントバッファの準備
SpriteConstantBuffer sb;
//エミッシブ加算は行わない。
sb.Emissive = Col4(0, 0, 0, 0);
//行列の設定
sb.World = World;
//コンスタントバッファの更新
pD3D11DeviceContext->UpdateSubresource(CBSprite::GetPtr()->GetBuffer(), 0, nullptr, &sb, 0, 0);
//コンスタントバッファの設定
ID3D11Buffer* pConstantBuffer = CBSprite::GetPtr()->GetBuffer();
ID3D11Buffer* pNullConstantBuffer = nullptr;
//頂点シェーダに渡す
pD3D11DeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
//ピクセルシェーダに渡す
pD3D11DeviceContext->PSSetConstantBuffers(0, 1, &pConstantBuffer);
//描画
pD3D11DeviceContext->DrawIndexed(m_SquareSpriteMesh->GetNumIndicis(), 0, 0);
}
//後始末
Dev->InitializeStates();
}
//コンスタントバッファの準備 SpriteConstantBuffer sb; //エミッシブ加算は行わない。 sb.Emissive = Col4(0, 0, 0, 0); //行列の設定 sb.World = World; //コンスタントバッファの更新 pD3D11DeviceContext->UpdateSubresource(CBSprite::GetPtr()->GetBuffer(), 0, nullptr, &sb, 0, 0); //コンスタントバッファの設定 ID3D11Buffer* pConstantBuffer = CBSprite::GetPtr()->GetBuffer(); ID3D11Buffer* pNullConstantBuffer = nullptr; //頂点シェーダに渡す pD3D11DeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer); //ピクセルシェーダに渡す pD3D11DeviceContext->PSSetConstantBuffers(0, 1, &pConstantBuffer);