I’ve come up with a solution for 3D text and sprite rendering based on current implementation of DirectX Tool Kit or SpriteBatch/SpriteFont in particular.
The reason why SpriteBatch/SpriteFont is not capable of drawing 3D text at this moment is that it regards the incoming argument ‘position’ of SpriteBatch::Draw and SpriteFont::DrawString function as coordinate in screen space, not world space, and the implementation
code (mainly in SpriteBatch::Impl::RenderSprite) does vertex coordinate transformation in screen space.
Given the above analysis, in order to draw sprite in 3D, we have to do coordinate transformation in world space. Let me explain my implementation.
First of all, I add one more argument ‘sprite3d’ to SpriteBatch::Begin to indicate whether sprites should be drawn in 3D, the declaration is
void XM_CALLCONV Begin(SpriteSortMode sortMode = SpriteSortMode_Deferred, In_opt
ID3D11BlendState* blendState = nullptr,
ID3D11SamplerState* samplerState = nullptr, In_opt
ID3D11DepthStencilState* depthStencilState = nullptr,
ID3D11RasterizerState* rasterizerState = nullptr, In_opt
std::function<void()> setCustomShaders = nullptr, FXMMATRIX transformMatrix = MatrixIdentity, bool sprite3d = false);
Similarly, SpriteBatch::Impl::Begin is changed to
void XM_CALLCONV SpriteBatch::Impl::Begin(SpriteSortMode sortMode, In_opt
ID3D11SamplerState* samplerState, In_opt
ID3D11RasterizerState* rasterizerState, In_opt
std::function<void()> setCustomShaders, FXMMATRIX transformMatrix, bool sprite3d);
Next, in SpriteBatch::Impl::PrepareForRendering(), I remove the viewport transform matrix that is not required in world-view-projection transform.
// Set the viewport transform matrix.
XMMATRIX transformMatrix = mTransformMatrix;
if ( !mSprite3d )
transformMatrix = mTransformMatrix * GetViewportTransform(deviceContext);
Third, I add a private function RenderSprite3d that differs from RenderSprite in that the vertex data transformation is done in world space.
void SpriteBatch::Impl:: RenderSprite3d(In
SpriteInfo const* sprite, _Out_cap_c_(VerticesPerSprite) VertexPositionColorTexture* vertices, FXMVECTOR textureSize, FXMVECTOR inverseTextureSize);
There are still a couple features missing in my implementation, the ‘rotation’, ‘origin’, and ‘effects’ arguments of SpriteBatch::Draw and SpriteFont::DrawString function are not implemented. However, my effort is to show SpriteBatch/SpriteFont can be used
as a 3D text solution with some slight modifications. I hope walbourn may add the 3D text feature to DirectX Took Kit in the future so that I do not have to add my patch every time DirectX Took Kit is updated.
Finally, I’ve uploaded the patch to my website at
The patch is based on DirectXTK simple sample for Windows 8 (http://code.msdn.microsoft.com/DirectXTK-Simple-Sample-608bc274
) and consists of three files. Just replace
the corresponding files with files packed in the patch.