How to have a flat sprite in a 3D environment?

There you go.

Now you see what Matrix.CreateBillBoard does in One line.

mySpriteEffect.World = Matrix.CreateBillboard(quadPosition, cameraPosition, Vector3.Up, ... );

Don’t forget what i said about the two different kinds of billboards and thier association with the specified up vector.

For instance one type of billboard might be a tree and its up might depend on the terrains up.
Another type of billboard might be 3d text that you write into world space, that needs the Vector3.up.
As you have just done.

Just one little pet peave to point out.

Don’t confuse yourself though by poorly naming your billboard matrix by calling it… lookat.
That is a world matrix, but the word lookat alone typically refers to a Forward vector but is commonly associated to the view matrix or worldCamera.

You should probably call that billboardMatrix or quadToCameraMatrix or worldQuadToLookAtMatrix ect…
With matrix’s you always keep the naming clear so as not to be confused later on when you build on your old code so as to what space and domain these matrices pertain to.

As for using this in a effect in a cool wayyou are and in a awsome way.
you pass the world and it gets multiplied one time it is exactly one matrix by matrix operation.

Your not going to get it much more efficent then that the world will just get multiplied by the pre-multiplied view projection on the basic effect shader which for it is a straight up one shot function.
Even manually splicing in the calculations to build the world matrix cpu side is a trifle difference as createworld is much simpler and safer and costs only a pitance more, so its not worth it.

You could instance this and pass much less and build the world as well as multiply via pre-multiplication on the shader cheaply but that is outside the scope of this topic.

2 Likes