gpt4 book ai didi

java - Libgdx:SpriteBatch.draw spriteVertecies 奇怪的 UV 坐标行为

转载 作者:行者123 更新时间:2023-12-01 09:32:48 24 4
gpt4 key购买 nike

我正在尝试显示一个简单的纹理梯形(透视中的道路)。为此,我使用 SpriteBatch.draw以顶点数组作为参数。但结果却出人意料。

我的期望: enter image description here

我得到了什么: enter image description here

究竟出了什么问题?或者也许我使用了错误的方法?

这是代码:

@Override
public void create () {
texture = new Texture("road.jpg");

spriteBatch = new SpriteBatch();

float color = Color.toFloatBits(255, 255, 255, 255);

verts = new float[]{
0, 0, color, 0, 0,
Gdx.graphics.getWidth()/2-100, 300, color, 0, 1,
Gdx.graphics.getWidth()/2+100, 300, color, 1, 1,
Gdx.graphics.getWidth(), 0, color, 1, 0,
};

shapeRenderer = new ShapeRenderer();
}

@Override
public void render () {
Gdx.gl.glClearColor(0, 0.5f, 1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rect(0, 0, 640, 300);
shapeRenderer.end();

spriteBatch.begin();
spriteBatch.draw(texture, verts, 0, verts.length);
spriteBatch.end();
}

最佳答案

来自documentation

draw(Texture texture, float[] spriteVertices, int offset, int count)

Draws a rectangle using the given vertices.

LibGDX 将这些矩形绘制为两个三角形,因此它分割了纹理。问题在于,矩形中的这些三角形大小相同,但梯形中的大小不同,因此它们会变形。

解决方案是使用投影或网格。

Image showing real UV mapping Texture UV mapping

关于java - Libgdx:SpriteBatch.draw spriteVertecies 奇怪的 UV 坐标行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com