Matthew H
Matthew H

Reputation: 5879

XNA — Drawing a 2d sprite in a 3d scene

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?

Upvotes: 0

Views: 1099

Answers (3)

Gene
Gene

Reputation: 46960

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.

Upvotes: 1

Steve H
Steve H

Reputation: 5519

Another option would be to use a textured quad rendered in 3d space and texture it with the sprite's image while billboarding it for alignment.

Upvotes: 0

Eiver
Eiver

Reputation: 2635

Use perspective projection instead of orthogonal projection

Upvotes: 0

Related Questions