図0014a
void CubeObjectGroup::CreateBuffers() { //中略 //インスタンス行列バッファの作成 //Max値で作成する vector<Mat4x4> matrices(m_MaxInstance); for (auto& m : matrices) { m = Mat4x4(); } //インスタンス描画用の行列のメッシュ(内容変更できる) m_InstanceMatrixMesh = MeshResource::CreateMeshResource(matrices, true); }
void CubeObjectGroup::OnCreate() {
CreateBuffers();
//テクスチャの作成
m_TextureResource = TextureResource::CreateTextureResource(m_TextureFileName, L"WIC");
//インスタンス配列の作成
for (UINT count = 0; count < 500; count++) {
CubeObject Data;
Data.Refresh();
m_CubeObjectVec.push_back(Data);
}
//中略
}
void CubeObjectGroup::OnUpdate() {
if (m_CubeObjectVec.size() >= m_MaxInstance) {
throw BaseException(
L"インスタンス上限を超えてます",
L"if(m_CubeObjectVec.size() >= m_MaxInstance)",
L"CubeObjectGroup::OnUpdate()"
);
}
float ElapsedTime = App::GetApp()->GetElapsedTime();
for (auto& v : m_CubeObjectVec) {
v.m_Posision += v.m_Velocity * ElapsedTime;
Quat QtSpan(v.m_QuaternionRot, v.m_QuaternionVelocity * ElapsedTime);
v.m_Quaternion *= QtSpan;
v.m_Quaternion.normalize();
if (v.m_Posision.length() >= 2.0f) {
v.Refresh();
}
}
vector<Mat4x4> MatVec;
for (size_t i = 0; i < m_CubeObjectVec.size(); i++) {
Mat4x4 World;
//ワールド行列の決定
World.affineTransformation(
m_CubeObjectVec[i].m_Scale, //スケーリング
Vec3(0, 0, 0), //回転の中心(重心)
m_CubeObjectVec[i].m_Quaternion, //回転角度
m_CubeObjectVec[i].m_Posision //位置
);
//転置する
World.transpose();
MatVec.push_back(World);
}
//メッシュの頂点の変更
m_InstanceMatrixMesh->UpdateVirtex(MatVec);
}
void CubeObjectGroup::DrawObject() {
CommandList::Reset(m_PipelineState, m_CommandList);
m_CubeMesh->UpdateResources<VertexPositionNormalTexture>(m_CommandList);
m_InstanceMatrixMesh->UpdateResources<Matrix4X4>(m_CommandList);
m_TextureResource->UpdateResources(m_CommandList);
//描画
m_CommandList->SetGraphicsRootSignature(m_RootSignature.Get());
ID3D12DescriptorHeap* ppHeaps[] = { m_CbvSrvUavDescriptorHeap.Get(),
m_SamplerDescriptorHeap.Get() };
m_CommandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
for (size_t i = 0; i < m_GPUDescriptorHandleVec.size(); i++) {
m_CommandList->SetGraphicsRootDescriptorTable(i, m_GPUDescriptorHandleVec[i]);
}
auto Dev = App::GetApp()->GetDeviceResources();
m_CommandList->RSSetViewports(1, &Dev->GetViewport());
m_CommandList->RSSetScissorRects(1, &Dev->GetScissorRect());
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(
Dev->GetRtvHeap()->GetCPUDescriptorHandleForHeapStart(),
Dev->GetFrameIndex(),
Dev->GetRtvDescriptorSize());
CD3DX12_CPU_DESCRIPTOR_HANDLE dsvHandle(
Dev->GetDsvHeap()->GetCPUDescriptorHandleForHeapStart()
);
m_CommandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle);
m_CommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_CommandList->IASetIndexBuffer(&m_CubeMesh->GetIndexBufferView());
//インスタンス描画は、頂点バッファを複数登録する
const D3D12_VERTEX_BUFFER_VIEW Buf[2] = {
m_CubeMesh->GetVertexBufferView(),
m_InstanceMatrixMesh->GetVertexBufferView()
};
m_CommandList->IASetVertexBuffers(0, 2, Buf);
m_CommandList->DrawIndexedInstanced(m_CubeMesh->GetNumIndicis(),
m_CubeObjectVec.size(), 0, 0, 0);
//コマンドリストのクローズ
CommandList::Close(m_CommandList);
//デバイスにコマンドリストを送る
Dev->InsertDrawCommandLists(m_CommandList.Get());
}