xna 中 spritebatch.draw 中的源矩形是什么 [英] what is source rectangle in spritebatch.draw in xna
问题描述
SpriteBatch.Draw() 方法中源矩形参数的用途是什么?
MSDN 说:一个矩形,它指定(以纹素为单位)来自纹理的源纹素.使用null绘制整个纹理.
这是什么意思?
sourceRectangle
的想法是允许您通过安排多个 sprites 变成单个 纹理.这被称为纹理图集".或精灵表".
(来源:
(来源:andrewrussell.net)子>
texel
或多或少与纹理中的像素(纹理像素",如果您愿意的话)相同.因此,当您绘制精灵时,您可以在 纹理 内指定 精灵 的左上角,以及其宽度和高度.(就像您在图像编辑器中选择它一样.)
如果为源矩形传入 null
,XNA 将假定源矩形覆盖整个纹理.
您指定给 Draw
的 origin
也以源矩形左上角的纹素为单位进行测量.
What is the purpose of the source rectangle parameter in the SpriteBatch.Draw() method?
MSDN says: A rectangle that specifies (in texels) the source texels from a texture. Use null to draw the entire texture.
What does that mean?
The idea of the sourceRectangle
is to allow you to implement what is both a performance optimisation and an artist convenience by arranging multiple sprites into a single texture. This is known as a "Texture Atlas" or a "Sprite Sheet".
(source: andrewrussell.net)
I explain why it is a performance optimisation in this answer. Basically it lets you reduce the number of texture-swaps. (So in the case of my illustration, if you're only drawing an animated character once, using a sprite-sheet will not improve performance.)
It also lets you implement tacky 2D special effects, like having a sprite "wipe" in:
(source: andrewrussell.net)
A texel
is more-or-less the same thing as a pixel in the texture (a "texture pixel", if you will). So, when you draw your sprite, you specify the top-left corner of your sprite within the texture, along with its width and height. (The same as if you selected it in an image editor.)
If you pass in null
for your source rectangle, XNA will assume a source rectangle that covers the entire texture.
The origin
you specify to Draw
is also measured in texels from the upper-left corner of the source rectangle.
这篇关于xna 中 spritebatch.draw 中的源矩形是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!