図0002a
#include "INCStructs.hlsli" cbuffer ConstantBuffer : register(b0) { row_major float4x4 MatrixTransform : packoffset(c0); float4 Emissive : packoffset(c4); }; PSPCInput main(VSPCInput input) { PSPCInput result; result.position = mul(input.position, MatrixTransform); result.color = input.color; return result; }
#include "INCStructs.hlsli" cbuffer ConstantBuffer : register(b0) { row_major float4x4 MatrixTransform : packoffset(c0); float4 Emissive : packoffset(c4); }; float4 main(PSPCInput input) : SV_TARGET { return saturate(Emissive + input.color); }
//-------------------------------------------------------------------------------------- /// 三角形スプライト //-------------------------------------------------------------------------------------- class TriangleSprite : public ObjectInterface, public ShapeInterface { ///メッシュ shared_ptr<MeshResource> m_TriangleMesh; Vec3 m_Pos; ///<現在の位置 Vec3 m_MoveSpan; ///<位置変更値 public: //構築と破棄 TriangleSprite(); virtual ~TriangleSprite(); //初期化 virtual void OnCreate()override; void OnUpdate()override; void OnDraw()override; };
void TriangleSprite::OnCreate() { //頂点を作成するための配列 vectorvertices = { { VertexPositionColor(Vec3(0.0f, 0.5f, 0.0f), Col4(1.0f,0.0f,0.0f,1.0f)) }, { VertexPositionColor(Vec3(0.5f, -0.5f, 0.0f), Col4(0.0f, 1.0f, 0.0f, 1.0f)) }, { VertexPositionColor(Vec3(-0.5f, -0.5f, 0.0f), Col4(0.0f, 0.0f, 1.0f, 1.0f)) }, }; m_TriangleMesh = MeshResource::CreateMeshResource(vertices, false); }
void TriangleSprite::OnUpdate() { m_Pos += m_MoveSpan; if (abs(m_Pos.x) >= 0.5f) { m_MoveSpan *= -1.0f; } }
void TriangleSprite::OnDraw() {
auto Dev = App::GetApp()->GetDeviceResources();
auto pD3D11DeviceContext = Dev->GetD3DDeviceContext();
auto RenderState = Dev->GetRenderState();
//コンスタントバッファの準備
SpriteConstantBuffer sb;
sb.Emissive = Col4(0.0f, 0.0f, 0, 1.0f);
Mat4x4 mat;
mat.translation(m_Pos);
sb.World = mat;
//コンスタントバッファの更新
pD3D11DeviceContext->UpdateSubresource(CBSprite::GetPtr()->GetBuffer(),
0, nullptr, &sb, 0, 0);
//ストライドとオフセット
UINT stride = sizeof(VertexPositionColor);
UINT offset = 0;
pD3D11DeviceContext->IASetVertexBuffers(0, 1,
m_TriangleMesh->GetVertexBuffer().GetAddressOf(), &stride, &offset);
//描画方法(3角形)
pD3D11DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//コンスタントバッファの設定
ID3D11Buffer* pConstantBuffer = CBSprite::GetPtr()->GetBuffer();
ID3D11Buffer* pNullConstantBuffer = nullptr;
//頂点シェーダに渡す
pD3D11DeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
//ピクセルシェーダに渡す
pD3D11DeviceContext->PSSetConstantBuffers(0, 1, &pConstantBuffer);
//シェーダの設定
pD3D11DeviceContext->VSSetShader(VSPCSprite::GetPtr()->GetShader(), nullptr, 0);
pD3D11DeviceContext->PSSetShader(PSPCSprite::GetPtr()->GetShader(), nullptr, 0);
//インプットレイアウトの設定
pD3D11DeviceContext->IASetInputLayout(VSPCSprite::GetPtr()->GetInputLayout());
//ブレンドステート
pD3D11DeviceContext->OMSetBlendState(RenderState->GetOpaque(), nullptr, 0xffffffff);
//デプスステンシルステート
pD3D11DeviceContext->OMSetDepthStencilState(RenderState->GetDepthNone(), 0);
//ラスタライザステート
pD3D11DeviceContext->RSSetState(RenderState->GetCullBack());
//描画
pD3D11DeviceContext->Draw(m_TriangleMesh->GetNumVertices(), 0);
//後始末
Dev->InitializeStates();
}
//コンスタントバッファの準備 SpriteConstantBuffer sb; sb.Emissive = Col4(0.0f, 0.0f, 0, 1.0f); Mat4x4 mat; mat.translation(m_Pos); sb.World = mat; //コンスタントバッファの更新 pD3D11DeviceContext->UpdateSubresource(CBSprite::GetPtr()->GetBuffer(), 0, nullptr, &sb, 0, 0);
//コンスタントバッファの設定 ID3D11Buffer* pConstantBuffer = CBSprite::GetPtr()->GetBuffer();
//コンスタントバッファの設定 ID3D11Buffer* pConstantBuffer = CBSprite::GetPtr()->GetBuffer(); //中略 //頂点シェーダに渡す pD3D11DeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer); //ピクセルシェーダに渡す pD3D11DeviceContext->PSSetConstantBuffers(0, 1, &pConstantBuffer);
//スプライト用コンスタントバッファ構造体 struct SpriteConstantBuffer { Mat4x4 World; Col4 Emissive; SpriteConstantBuffer() { memset(this, 0, sizeof(SpriteConstantBuffer)); }; }; DECLARE_DX11_CONSTANT_BUFFER(CBSprite, SpriteConstantBuffer)
IMPLEMENT_DX11_CONSTANT_BUFFER(CBSprite)
CBSprite::GetPtr()->GetBuffer()
DECLARE_DX11_VERTEX_SHADER(VSPCSprite, VertexPositionColor) DECLARE_DX11_PIXEL_SHADER(PSPCSprite)
IMPLEMENT_DX11_VERTEX_SHADER(VSPCSprite, App::GetApp()->m_wstrRelativeShadersPath + L"VSPCSprite.cso") IMPLEMENT_DX11_PIXEL_SHADER(PSPCSprite, App::GetApp()->m_wstrRelativeShadersPath + L"PSPCSprite.cso")
//シェーダの設定 pD3D11DeviceContext->VSSetShader(VSPCSprite::GetPtr()->GetShader(), nullptr, 0); pD3D11DeviceContext->PSSetShader(PSPCSprite::GetPtr()->GetShader(), nullptr, 0);