Move SpriteBatch to 3D-Space Coordinate

Hi,

I want to draw text and an image on a specific position in 3D Space. The scene is rendered in 3D and I want to display a rendered 2D Text and an image on a XYZ-Coordinate.

I have the World, View, Projection Matrices from the scene and the ViewPort. I don’t want to render a real 3D-Font and I also don’t want to display the image with texture vertices.

I’ve tried some matrix multiplications with the transformation matrix and I also tried to use the basic effect as parameter for the begin-method. But non of them worked for me.

    eff.World = Graph3DGame.Current.currentWorld;
    eff.View = Graph3DGame.Current.currentView;
    eff.Projection = Graph3DGame.Current.currentPerspective;

    spriteBatch.Begin(0, null, null, null, null, eff);
    spriteBatch.DrawString(Fonts.Math, "hello, world!", new Vector2(100,100), Color.Blue);
    spriteBatch.End();

Hope anyone could help.

try this,
in the spriteBatch.Begin() parameters turn culling off and flip all draws vertically.

Use the basic effect as in the code you posted to pass WorldViewProj.
The transformationMatrix is for WorldView only, spritebatch uses transformationMatrix in combination with the internal projection matrix.
(in theory you can calculate the same projection as spritebatch, invert it, and then multiply it with your new projection and pass a combined transform matrix (worldviewmyproj*invProj) which in the end will negate the internal projection)

Thanks.

I’ve tried it, but it doesn’t display the sprite batch.
Do you have a code example?

        var cameraPosition = new Vector3(0, 0, 13);
        cameraPosition = Vector3.Transform(
            cameraPosition, Matrix.CreateScale(1));

        var view        = Matrix.CreateLookAt(cameraPosition, new Vector3(0, 0, 0), Vector3.Up);
        var projection  = Matrix.CreatePerspectiveFieldOfView(
                            1, Graph3DGame.Current.GraphicsDevice.Viewport.AspectRatio, 1, 500);

        eff.World       = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
        eff.View        = view;
        eff.Projection = projection;

        spriteBatch.Begin(
            SpriteSortMode.Deferred,
            BlendState.AlphaBlend,
            SamplerState.LinearClamp,
            DepthStencilState.Default,
            RasterizerState.CullNone,
            eff, null);

This article has all the details.
https://blogs.msdn.microsoft.com/shawnhar/2011/01/12/spritebatch-billboards-in-a-3d-world/

I have a sample that might be relevant, look at the 3D Camera Demo,

Actually, all the samples at physics2d are drawn with an Effect and a proj/View/World, although I mostly use an orthographic camera.

1 Like

There is a the built-in Project method for this to get 3D world space coordinates to Screen space coordinates:

Vector3 my3dObjectPos = new Vector3(100,0,100);
Vector3 my2dScreenPos = graphics.GraphicsDevice.Viewport.Project( my3dObjectPos, _YourCamera.Projection, _YourCamera.View, Matrix.Identity);

^_^y