简体   繁体   English

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

[英]libGDX - Drawing Texture via SpriteBatch Result Flip

When I tried draw a Texture using SpriteBatch it was result flip like this: 当我尝试使用SpriteBatch绘制Texture时,结果翻转如下:

使用SpriteBatch结果Flip绘制纹理

Here what I do: I create MyRect object which draw bounding rectangle and an image. 在这里我做的是:我创建了绘制边界矩形和图像的MyRect对象。

Here MyRect class preview: 这里是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();
}
}

Parameters ShapeRenderer and SpriteBatch pass by GameScreen class. 参数ShapeRendererSpriteBatch通过GameScreen类传递。

GameScreen preview: 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);
}
}

Why is this happen? 为什么会这样? Am I doing it wrong? 我做错了吗?

Your camera is upside down because you called setToOrtho(true, ...) on it instead of setToOrtho(false, ...) 你的相机是颠倒的,因为你在它上面调用setToOrtho(true, ...)而不是setToOrtho(false, ...)

It is valid to use an upside-down camera (which might be more comfortable if you've used Flash or some other Y-down system before), but then you need to flip all your TextureRegions (aka Sprites): sprite.flip(false, true) . 使用颠倒的相机是有效的(如果您之前使用过Flash或其他Y-down系统可能会更舒服),但是您需要翻转所有的TextureRegions(也就是Sprite): sprite.flip(false, true) Alternatively, you can create a TextureAtlas using TexturePacker (look it up in libgdx documentation) and set the flipY option, so it flips them ahead of time for you. 或者,您可以使用TexturePacker创建TextureAtlas(在libgdx文档中查找)并设置flipY选项,以便它提前翻转它们。 Eventually, for performance you will need to use a TextureAtlas anyway. 最终,为了性能,您还需要使用TextureAtlas。

By the way, when you start drawing multiple instances of MyRect, you are going to need to move the spriteBatch.begin() and end() and shapeRenderer.begin() and end() out of the individual MyRect's draw method or you will run into a performance problem. 顺便说一句,当你开始绘制MyRect的多个实例时,你将需要将spriteBatch.begin()end()以及shapeRenderer.begin()end()移出单独的MyRect绘制方法,否则你将需要遇到性能问题。 And as such, there will need to be two draw methods (one for sprite batch and one for shape renderer). 因此,需要有两种绘制方法(一种用于精灵批处理,一种用于形状渲染器)。

apparently, the only thing I see different than how I would do, is try to change: this meybe if it works"for test" 显然,我唯一看到的不同之处就是尝试改变:这个meybe如果它可以“用于测试”

cam.setToOrtho(false, 136, gameHeight);

this is how I use the camera. 这就是我使用相机的方式。

I do not know whether to use true, you have to do some different way, to draw the batch, in anyway. 我不知道是否使用true,你必须做一些不同的方式来绘制批量,无论如何。 if the camera false, it looks good, I think it has to flip the image before to draw uv 如果相机假,它看起来不错,我认为它必须翻转图像才能画出紫外线

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

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