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

查看:82
本文介绍了如何在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进行绘制,但只在屏幕的左半部分 进行绘制(这意味着所有内容将在左侧水平缩小,而屏幕的右半部分则保持黑色),我该怎么办?

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?

推荐答案

您将要使用ScissorStack.实际上,您定义了要绘制的矩形.所有绘制都将在您定义的矩形中.

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

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

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.

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

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

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