如何在libgdx中使用SpriteBatch在屏幕的一部分上绘图?

Hải*_*ong 1 opengl libgdx

当我这样做:

SpriteBatch spriteBatch = new SpriteBatch();
spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, 320, 0, 240, -1, 1));
spriteBatch.begin();
spriteBatch.draw(textureRegion, 0, 0);
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)

SpriteBatch将绘制textureRegion到我已指定的坐标系320-240到整个屏幕.假设我想使用相同的坐标系320 240进行绘制,但仅在屏幕的左半部分绘制(这意味着所有内容都将在左侧水平缩小,使屏幕的右半部分变黑),我该怎么办?

Jer*_*ins 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();
Run Code Online (Sandbox Code Playgroud)

这将限制渲染到矩形"clipBounds"的范围内.也可以推多个矩形.仅渲染所有矩形内的精灵的像素.

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

  • 你可能想要更新你的问题,以明确你不想剪辑,因为我不清楚. (2认同)