Libgdx 纹理区域到纹理

问题描述 投票:0回答:2

我想在我的图像上使用

setFilter(TextureFilter.Linear, TextureFilter.Linear);
,取自textureAtlas。当我使用

TextureRegion texReg = textureAtl.findRegion("myImage");
Sprite = new Sprite(texReg);

它工作得很好,但如果我尝试

TextureRegion texReg = textureAtl.findRegion("myImage");
Texture myTexture = new Texture(texReg.getTexture());
myTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
Sprite mySprite = new Sprite(myTexture);

mySprite 包含所有的textureAtlas 图像。如何从textureAtlas设置纹理单个图像?

java textures libgdx
2个回答
4
投票

你的最后一行应该是:

Sprite mySprite = new Sprite(texReg);

纹理可以表示单个图像或多个图像(纹理图集)。当有多个图像时,每个图像都位于其纹理区域中。您只能对整个纹理以及其中的所有图像应用过滤。如果您只想将其应用于单个图像,则它需要位于单独的纹理中。

这就是您对代码所做的事情:

// get the image for your game (or whatever) object
TextureRegion texReg = textureAtl.findRegion("myImage");
// get the texture that is the 'container' of the image you want
Texture myTexture = new Texture(texReg.getTexture());
// apply filtering to the entire texture and all the images
myTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
// set the entire texture as the image for your sprite (instead of only a single region)
Sprite mySprite = new Sprite(myTexture);

0
投票

解决方案 2023:使用 Pixmap [ChatGPT 帮助]


fun TextureRegion.toTexture(): Texture {
    val pixmap = Pixmap(regionWidth, regionHeight, Pixmap.Format.RGBA8888)

    if (texture.textureData.isPrepared.not()) texture.textureData.prepare()
    val texturePixmap = texture.textureData.consumePixmap()

    pixmap.drawPixmap(texturePixmap, 0, 0, regionX, regionY, regionWidth, regionHeight)

    val newTexture = Texture(pixmap)

    texturePixmap.dispose()
    pixmap.dispose()

    return newTexture
}

不要忘记处理纹理|像素图!


小心使用pixmap.dispose()。如果在此转换后使用纹理位图并再次转换该纹理,则可能会遇到内存错误。解决方案是简单地传递标志以使用 pixmap.dispose() 或不使用。


主要工作是由chatGPT编写的,但它并没有正确执行所有操作。在这个例子中,如果没有我,你就会遇到内存问题,因为 chatGPT 忘记调用 dispose


PS。 Vel_daN:热爱你所做的事情💚。

© www.soinside.com 2019 - 2024. All rights reserved.