我们只需要为透明的object设置一个新的pipelineStateObject即可。和不透明的object相比,就是额外设置了blend state:
D3D12_RENDER_TARGET_BLEND_DESC blendDesc;
blendDesc.BlendEnable = true;
blendDesc.BlendOp = D3D12_BLEND_OP_ADD;
blendDesc.BlendOpAlpha = D3D12_BLEND_OP_ADD;
blendDesc.DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
blendDesc.DestBlendAlpha = D3D12_BLEND_ZERO;
blendDesc.LogicOp = D3D12_LOGIC_OP_NOOP;
blendDesc.LogicOpEnable = false;
blendDesc.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
blendDesc.SrcBlend = D3D12_BLEND_SRC_ALPHA;
blendDesc.SrcBlendAlpha = D3D12_BLEND_ONE;
psoDesc.BlendState.RenderTarget[0] = blendDesc;
有两点需要注意:
1.调用SetPipelineState
切换不同的pipelineStateObject是有一定开销的,所以可以事先将需要渲染的objects根据不同的pipelineState先分好组,避免频繁切换pipelineState;
2.注意不同objects的渲染顺序,要先渲染不透明的objects,再从后往前地渲染透明的objects。
如果你觉得我的文章有帮助,欢迎关注我的微信公众号(大龄社畜的游戏开发之路)-