繁体   English   中英

libGDX - 通过SpriteBatch结果翻转绘制纹理

[英]libGDX - Drawing Texture via SpriteBatch Result Flip

当我尝试使用SpriteBatch绘制Texture时,结果翻转如下:

使用SpriteBatch结果Flip绘制纹理

在这里我做的是:我创建了绘制边界矩形和图像的MyRect对象。

这里是MyRect类预览:

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

参数ShapeRendererSpriteBatch通过GameScreen类传递。

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

为什么会这样? 我做错了吗?

你的相机是颠倒的,因为你在它上面调用setToOrtho(true, ...)而不是setToOrtho(false, ...)

使用颠倒的相机是有效的(如果您之前使用过Flash或其他Y-down系统可能会更舒服),但是您需要翻转所有的TextureRegions(也就是Sprite): sprite.flip(false, true) 或者,您可以使用TexturePacker创建TextureAtlas(在libgdx文档中查找)并设置flipY选项,以便它提前翻转它们。 最终,为了性能,您还需要使用TextureAtlas。

顺便说一句,当你开始绘制MyRect的多个实例时,你将需要将spriteBatch.begin()end()以及shapeRenderer.begin()end()移出单独的MyRect绘制方法,否则你将需要遇到性能问题。 因此,需要有两种绘制方法(一种用于精灵批处理,一种用于形状渲染器)。

显然,我唯一看到的不同之处就是尝试改变:这个meybe如果它可以“用于测试”

cam.setToOrtho(false, 136, gameHeight);

这就是我使用相机的方式。

我不知道是否使用true,你必须做一些不同的方式来绘制批量,无论如何。 如果相机假,它看起来不错,我认为它必须翻转图像才能画出紫外线

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM