如何在 libgdx 中使用 SpriteBatch 在屏幕的一部分上绘制? [英] How to draw on just a portion of the screen with SpriteBatch in libgdx?

查看:27
本文介绍了如何在 libgdx 中使用 SpriteBatch 在屏幕的一部分上绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

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

This will limit rendering to within the bounds of the rectangle "clipBounds". It is also possible push multiple rectangles. Only the pixels of the sprites that are within all of the rectangles will be rendered.

From http://code.google.com/p/libgdx/wiki/GraphicsScissors

这篇关于如何在 libgdx 中使用 SpriteBatch 在屏幕的一部分上绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆