问题描述
当我这样做时:
SpriteBatch spriteBatch = new SpriteBatch();spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, 320, 0, 240, -1, 1));spriteBatch.begin();spriteBatch.draw(textureRegion, 0, 0);spriteBatch.end();
SpriteBatch
会将 textureRegion
绘制到我为整个屏幕指定的坐标系 320-240 上.假设我想使用相同的坐标系 320 240 进行绘制,但只在 屏幕的左半部分(这意味着所有内容都将在左侧水平缩小,使屏幕的右半部分变黑),我该怎么办?
你会想要使用 ScissorStack.实际上,您定义了一个要在其中绘制的矩形.所有绘图都将在您定义的矩形中.
矩形剪刀 = new Rectangle();矩形 clipBounds = new Rectangle(x,y,w,h);ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);ScissorStack.pushScissors(剪刀);spriteBatch.draw(...);spriteBatch.flush();ScissorStack.popScissors();
这会将渲染限制在矩形clipBounds"的范围内.也可以推送多个矩形.只有在所有矩形内的精灵像素才会被渲染.
来自 http://code.google.com/p/libgdx/wiki/GraphicsScissors
When I do this:
SpriteBatch spriteBatch = new SpriteBatch();
spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, 320, 0, 240, -1, 1));
spriteBatch.begin();
spriteBatch.draw(textureRegion, 0, 0);
spriteBatch.end();
SpriteBatch
will draw the textureRegion
onto the coordinate system 320-240 that I have specified to the whole screen. Say I want to draw with the same coordinate system 320 240 but only on the left half of the screen (which means everything will be scaled down horizontally in the left side, leaving the right half of the screen black), how can I do?
You're going to want to use the ScissorStack. Effectively, you define a rectangle that you want to draw in. All drawing will be in the rectangle that you defined.
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(x,y,w,h);
ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
spriteBatch.draw(...);
spriteBatch.flush();
ScissorStack.popScissors();
From http://code.google.com/p/libgdx/wiki/GraphicsScissors
这篇关于如何在 libgdx 中使用 SpriteBatch 在屏幕的一部分上绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!