I’m drawing a 2d sprite onto a 3d scene. At the moment I’m projecting a 3d point onto the viewport, and using that as the position to draw the sprite. The problem is scaling the sprite: obviously, I want the image to look smaller the further away it is in the 3d scene. How can I do this?
Share
The general technique is called billboarding. You create a 2d polygon in 3d world space and arrange for its normal always to point toward the viewer. This is the billboard. The required transform is just a model rotation about the vertical axis piercing the polyon’s “natural” center (e.g. the trunk of a tree or legs of a monster). Then texture the polygon with the sprite image, setting alpha = 0 for transparent areas. Scaling follows naturally.
This is typically used for objects (like trees and monsters) that are quite complex but tend to be in the background so that the inaccuracy introduced by using a fixed 2d image is not very noticeable.