1

当我这样做时:

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 但只在屏幕的左半边绘制(这意味着所有东西都会在左侧水平缩小,使屏幕的右半边变黑),我该怎么办?

4

4 回答 4

6

您将要使用 ScissorStack。实际上,您定义了一个要在其中绘制的矩形。所有绘图都将在您定义的矩形中。

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();

这会将渲染限制在矩形“clipBounds”的范围内。也可以推送多个矩形。只有在所有矩形内的精灵像素才会被渲染。

来自http://code.google.com/p/libgdx/wiki/GraphicsScissors

于 2013-09-09T15:40:27.613 回答
1

Before rendering the batch, you can set the viewport to draw on a specific screen area. The important line is:

Gdx.gl.glViewport(x, y, w, h);

The viewport usually starts at x = 0 and y = 0 and extends to the full width and height of the screen. If we want to see only a part of that original viewport, we need to change both the size and the starting position. To draw only on the left half of the screen, use:

x = 0;
y = 0;
w = Gdx.graphics.getWidth()/2;
h = Gdx.graphics.getWidth();

I found the solution here and originally answered this question to a slightly more complicated problem, but the technique is the same.

To focus on any different portion of the viewport, simply choose x, y, w, and h accordingly. If you're going to do any more rendering in the normal fashion, make sure to reset the viewport with the original x, y, w, and h values.

于 2014-11-16T19:28:49.627 回答
0

你也可以

  • 视口宽度的两倍SpriteBatch
  • 使用 aSprite并将其宽度比例设置为0.5f(注意原点)并使用它的draw(SpriteBatch)方法来绘制它。
于 2013-09-10T09:06:40.887 回答
0

也许我误解了这个问题,但你能不能把视口宽度加倍,将其设置为 640 而不是 320?

SpriteBatch spriteBatch = new SpriteBatch;
spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, 640, 0, 240, -1, 1));
spriteBatch.begin();
spriteBatch.draw(textureRegion, 0, 0);
spriteBatch.end();
于 2013-09-09T21:14:06.447 回答