libGDX - 通过 SpriteBatch 结果翻转绘制纹理
libGDX - Drawing Texture via SpriteBatch Result Flip
当我尝试使用 SpriteBatch
绘制 Texture
时,结果翻转如下:
这是我的做法:
我创建 MyRect
绘制边界矩形和图像的对象。
此处MyRect
class预览:
public class MyRect {
private Vector2 position;
private int width;
private float height;
private Texture img;
private Sprite sprite;
public MyRect(int x, int y, int width, int height){
img = new Texture("badlogic.jpg");
sprite = new Sprite(img);
position = new Vector2(x,y);
this.width = width;
this.height = height;
}
public void draw(ShapeRenderer shapeRenderer, SpriteBatch batch){
// Gdx.gl.glClearColor(0, 0, 0, 1);
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeType.Line);
// Draw Background color
shapeRenderer.setColor(55 / 255.0f, 80 / 255.0f, 100 / 255.0f, 1);
shapeRenderer.rect(position.x, position.y, width, height);
shapeRenderer.end();
batch.begin();
// batch.disableBlending();
batch.draw(img, position.x, position.y,
width, height);
batch.end();
}
}
参数ShapeRenderer
和SpriteBatch
通过GameScreen
class。
GameScreen
预览:
public class GameScreen implements Screen{
private MyRect myRect;
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameScreen(){
myRect = new MyRect(10,10,50,50);
int gameHeight=100;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
myRect.draw(shapeRenderer, batch);
}
}
为什么会这样?我做错了吗?
显然,我唯一看到的与我的做法不同的是尝试改变:
这个 meybe 如果它有效"for test"
cam.setToOrtho(false, 136, gameHeight);
这就是我使用相机的方式。
不知道要不要用true,反正你得换个方式,画batch,反正。如果相机是假的,它看起来不错,我认为它必须在绘制uv之前翻转图像
你的相机倒置了,因为你在上面调用了 setToOrtho(true, ...)
而不是 setToOrtho(false, ...)
使用倒置相机是有效的(如果您以前使用过 Flash 或其他 Y-down 系统可能会更舒服),但是您需要翻转所有 TextureRegions(又名 Sprites) :sprite.flip(false, true)
。或者,您可以使用 TexturePacker 创建一个 TextureAtlas(在 libgdx 文档中查找)并设置 flipY 选项,以便它提前为您翻转它们。最终,为了提高性能,您无论如何都需要使用 TextureAtlas。
顺便说一句,当您开始绘制 MyRect 的多个实例时,您需要将 spriteBatch.begin()
和 end()
以及 shapeRenderer.begin()
和 end()
移出个别 MyRect 的绘制方法,否则您将 运行 陷入性能问题。因此,将需要两种绘制方法(一种用于精灵批处理,一种用于形状渲染器)。
当我尝试使用 SpriteBatch
绘制 Texture
时,结果翻转如下:
这是我的做法:
我创建 MyRect
绘制边界矩形和图像的对象。
此处MyRect
class预览:
public class MyRect {
private Vector2 position;
private int width;
private float height;
private Texture img;
private Sprite sprite;
public MyRect(int x, int y, int width, int height){
img = new Texture("badlogic.jpg");
sprite = new Sprite(img);
position = new Vector2(x,y);
this.width = width;
this.height = height;
}
public void draw(ShapeRenderer shapeRenderer, SpriteBatch batch){
// Gdx.gl.glClearColor(0, 0, 0, 1);
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeType.Line);
// Draw Background color
shapeRenderer.setColor(55 / 255.0f, 80 / 255.0f, 100 / 255.0f, 1);
shapeRenderer.rect(position.x, position.y, width, height);
shapeRenderer.end();
batch.begin();
// batch.disableBlending();
batch.draw(img, position.x, position.y,
width, height);
batch.end();
}
}
参数ShapeRenderer
和SpriteBatch
通过GameScreen
class。
GameScreen
预览:
public class GameScreen implements Screen{
private MyRect myRect;
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameScreen(){
myRect = new MyRect(10,10,50,50);
int gameHeight=100;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
myRect.draw(shapeRenderer, batch);
}
}
为什么会这样?我做错了吗?
显然,我唯一看到的与我的做法不同的是尝试改变: 这个 meybe 如果它有效"for test"
cam.setToOrtho(false, 136, gameHeight);
这就是我使用相机的方式。
不知道要不要用true,反正你得换个方式,画batch,反正。如果相机是假的,它看起来不错,我认为它必须在绘制uv之前翻转图像
你的相机倒置了,因为你在上面调用了 setToOrtho(true, ...)
而不是 setToOrtho(false, ...)
使用倒置相机是有效的(如果您以前使用过 Flash 或其他 Y-down 系统可能会更舒服),但是您需要翻转所有 TextureRegions(又名 Sprites) :sprite.flip(false, true)
。或者,您可以使用 TexturePacker 创建一个 TextureAtlas(在 libgdx 文档中查找)并设置 flipY 选项,以便它提前为您翻转它们。最终,为了提高性能,您无论如何都需要使用 TextureAtlas。
顺便说一句,当您开始绘制 MyRect 的多个实例时,您需要将 spriteBatch.begin()
和 end()
以及 shapeRenderer.begin()
和 end()
移出个别 MyRect 的绘制方法,否则您将 运行 陷入性能问题。因此,将需要两种绘制方法(一种用于精灵批处理,一种用于形状渲染器)。