Android 非常大的背景纹理
对于我正在开发的游戏,我必须使用大背景图像。这些图像约为5000x3000。当尝试将其显示为单个纹理时,我会在GWT和Android中看到黑匣子。我创建了一个将图像分割为纹理网格的类:Android 非常大的背景纹理,android,opengl-es,libgdx,textures,Android,Opengl Es,Libgdx,Textures,对于我正在开发的游戏,我必须使用大背景图像。这些图像约为5000x3000。当尝试将其显示为单个纹理时,我会在GWT和Android中看到黑匣子。我创建了一个将图像分割为纹理网格的类: public class LargeImage extends LoadableGroup { private boolean smooth=true; public String filename; private int trueWidth,trueHeight; private ArrayList<I
public class LargeImage extends LoadableGroup {
private boolean smooth=true;
public String filename;
private int trueWidth,trueHeight;
private ArrayList<Image> tiles=new ArrayList<Image>();
private ArrayList<Texture> textures=new ArrayList<Texture>();
private final int maxTextureSize = 512;
public LargeImage(String filename, CallbackAssetManager manager)
{
super();
this.filename=filename;
PixmapParameter param=new PixmapParameter();
param.format=Pixmap.Format.RGBA8888;
addAsset(new AssetDescriptor(filename, Pixmap.class,param));
manager.add(this);
}
public LargeImage(Pixmap map, boolean smooth)
{
super();
this.smooth=smooth;
fromPixMap(map);
}
public void loaded(CallbackAssetManager manager)
{
if(!manager.isLoaded(filename))
{
Gdx.app.log("Load Error",filename);
return;
}
Pixmap source = manager.get(filename, Pixmap.class);
fromPixMap(source);
}
private void fromPixMap(Pixmap source)
{
for (int x = 0;x < source.getWidth(); x+=maxTextureSize) {
for (int y = 0;y < source.getHeight(); y+=maxTextureSize) {
Pixmap p = new Pixmap(maxTextureSize, maxTextureSize, Pixmap.Format.RGBA8888);
p.drawPixmap(source, 0, 0, x, y, maxTextureSize, maxTextureSize);
Texture t=new Texture(new PixmapTextureData(p, Pixmap.Format.RGBA8888, true, false, true));
textures.add(t);
if(smooth)
t.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
UncoloredImage tile = new UncoloredImage(t);
tile.setTouchable(Touchable.disabled);
tile.setX(x);
tile.setY(source.getHeight()-y-tile.getHeight());
p.dispose();
this.addActor(tile);
tiles.add(tile);
}
}
super.setWidth(source.getWidth());
super.setHeight(source.getHeight());
trueWidth=source.getWidth();
trueHeight=source.getHeight();
}
@Override
public void setWidth(float width) {
setScaleX(width/trueWidth);
super.setWidth(Math.abs(width));
}
public void setHeight(float height) {
setScaleY(height / (float)trueHeight);
super.setHeight(Math.abs(height));
}
@Override
public void dispose(CallbackAssetManager m) {
super.dispose(m);
for(int i=0;i<textures.size();i++)
textures.get(i).dispose();
}
}
公共类LargeImage扩展了LoadableGroup{
私有布尔平滑=真;
公共字符串文件名;
私有int trueWidth、trueHeight;
私有ArrayList tiles=新ArrayList();
私有ArrayList纹理=新建ArrayList();
私有最终整数maxTextureSize=512;
公共大图像(字符串文件名,CallbackAssetManager)
{
超级();
this.filename=文件名;
PixmapParameter参数=新的PixmapParameter();
param.format=Pixmap.format.rgba888;
addAsset(新的AssetDescriptor(文件名,Pixmap.class,参数));
经理。添加(此);
}
公共大图像(像素贴图,布尔平滑)
{
超级();
这个.平滑=平滑;
来自pixmap(map);
}
已加载公共void(CallbackAssetManager)
{
如果(!manager.isLoaded(文件名))
{
Gdx.app.log(“加载错误”,文件名);
回来
}
Pixmap source=manager.get(文件名,Pixmap.class);
来自PixMap(来源);
}
来自Pixmap的私有void(Pixmap源)
{
对于(int x=0;x 对于(int i=0;i可能具有使该部分成为“源”的属性):
也许对你有帮助。如果你不使用更多,我发现了问题。我忘了处理1x1背景填充。我忘了我使用这个类通过发送Pixmap来创建1x1图像。1x1被转换为512x512,并在各个级别累积,直到达到极限。我在它正在被创建,并修复了整个问题
我还更新了平铺生成器,使其使用所需的最小纹理大小,并提高到GL_MAX_texture_size。这样,1x1图像实际上是1x1,5000x3000平铺更少,具体取决于图形硬件。以下是新代码:
public class LargeImage extends LoadableGroup {
private boolean smooth=true;
public String filename;
public int trueWidth,trueHeight;
private ArrayList<Image> tiles=new ArrayList<Image>();
private ArrayList<Texture> textures=new ArrayList<Texture>();
private int maxTextureSize;
public LargeImage(String filename, CallbackAssetManager manager)
{
super();
this.filename=filename;
PixmapParameter param=new PixmapParameter();
param.format=Pixmap.Format.RGBA8888;
addAsset(new AssetDescriptor(filename, Pixmap.class,param));
manager.add(this);
}
public LargeImage(Pixmap map, boolean smooth)
{
super();
this.smooth=smooth;
fromPixMap(map);
}
public void draw(Batch batch, float parentAlpha)
{
//setCullingArea(new Rectangle(0,0,200,200));
batch.setColor(getColor());
super.draw(batch,parentAlpha);
}
public void loaded(CallbackAssetManager manager)
{
if(!manager.isLoaded(filename))
{
Gdx.app.log("Load Error",filename);
return;
}
Pixmap source = manager.get(filename, Pixmap.class);
fromPixMap(source);
}
private void fromPixMap(Pixmap source)
{
if(Gdx.app.getType()== Application.ApplicationType.WebGL)
maxTextureSize=512;
else
maxTextureSize=Gdx.gl.GL_MAX_TEXTURE_SIZE;
for (int x = 0;x < source.getWidth(); x+=maxTextureSize) {
for (int y = 0;y < source.getHeight(); y+=maxTextureSize) {
int tSize=getTextureSize(source.getWidth()-x,source.getHeight()-y);
Pixmap p = new Pixmap(tSize, tSize, Pixmap.Format.RGBA8888);
p.drawPixmap(source, 0, 0, x, y, tSize, tSize);
Texture t=new Texture(new PixmapTextureData(p, Pixmap.Format.RGBA8888, true, false, true));
textures.add(t);
if(smooth)
t.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
UncoloredImage tile = new UncoloredImage(t);
tile.setTouchable(Touchable.disabled);
tile.setX(x);
tile.setY(source.getHeight()-y-tile.getHeight());
p.dispose();
this.addActor(tile);
tiles.add(tile);
}
}
super.setWidth(source.getWidth());
super.setHeight(source.getHeight());
trueWidth=source.getWidth();
trueHeight=source.getHeight();
}
public int getTextureSize(int width, int height)
{
int n=Math.max(width,height);
if(n>=maxTextureSize)
return maxTextureSize;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
@Override
public void setWidth(float width) {
setScaleX(width/trueWidth);
super.setWidth(Math.abs(width));
}
public void setHeight(float height) {
setScaleY(height / (float)trueHeight);
super.setHeight(Math.abs(height));
}
@Override
public void dispose(CallbackAssetManager m) {
super.dispose(m);
for(int i=0;i<textures.size();i++) {
textures.get(i).dispose();
}
}
public void softDispose(CallbackAssetManager m) {
for(int i=0;i<textures.size();i++) {
textures.get(i).dispose();
}
}
}
公共类LargeImage扩展了LoadableGroup{
私有布尔平滑=真;
公共字符串文件名;
公共int trueWidth、trueHeight;
私有ArrayList tiles=新ArrayList();
私有ArrayList纹理=新建ArrayList();
私有int-maxturesize;
公共大图像(字符串文件名,CallbackAssetManager)
{
超级();
this.filename=文件名;
PixmapParameter参数=新的PixmapParameter();
param.format=Pixmap.format.rgba888;
addAsset(新的AssetDescriptor(文件名,Pixmap.class,参数));
经理。添加(此);
}
公共大图像(像素贴图,布尔平滑)
{
超级();
这个.平滑=平滑;
来自pixmap(map);
}
公共作废提取(批处理、浮动parentAlpha)
{
//setCullingArea(新矩形(0,0200200));
batch.setColor(getColor());
超级绘图(批处理,父alpha);
}
已加载公共void(CallbackAssetManager)
{
如果(!manager.isLoaded(文件名))
{
Gdx.app.log(“加载错误”,文件名);
回来
}
Pixmap source=manager.get(文件名,Pixmap.class);
来自PixMap(来源);
}
来自Pixmap的私有void(Pixmap源)
{
if(Gdx.app.getType()==Application.ApplicationType.WebGL)
maxTextureSize=512;
其他的
maxTextureSize=Gdx.gl.gl_MAX_TEXTURE_SIZE;
对于(int x=0;x=maxTextureSize)
返回maxTextureSize;
n--;
n |=n>>1;
n |=n>>2;
n |=n>>4;
n |=n>>8;
n |=n>>16;
n++;
返回n;
}
@凌驾
公共空白设置宽度(浮动宽度){
设置刻度(宽度/真实宽度);
super.setWidth(Math.abs(width));
}
公共vo
public class LargeImage extends LoadableGroup {
private boolean smooth=true;
public String filename;
public int trueWidth,trueHeight;
private ArrayList<Image> tiles=new ArrayList<Image>();
private ArrayList<Texture> textures=new ArrayList<Texture>();
private int maxTextureSize;
public LargeImage(String filename, CallbackAssetManager manager)
{
super();
this.filename=filename;
PixmapParameter param=new PixmapParameter();
param.format=Pixmap.Format.RGBA8888;
addAsset(new AssetDescriptor(filename, Pixmap.class,param));
manager.add(this);
}
public LargeImage(Pixmap map, boolean smooth)
{
super();
this.smooth=smooth;
fromPixMap(map);
}
public void draw(Batch batch, float parentAlpha)
{
//setCullingArea(new Rectangle(0,0,200,200));
batch.setColor(getColor());
super.draw(batch,parentAlpha);
}
public void loaded(CallbackAssetManager manager)
{
if(!manager.isLoaded(filename))
{
Gdx.app.log("Load Error",filename);
return;
}
Pixmap source = manager.get(filename, Pixmap.class);
fromPixMap(source);
}
private void fromPixMap(Pixmap source)
{
if(Gdx.app.getType()== Application.ApplicationType.WebGL)
maxTextureSize=512;
else
maxTextureSize=Gdx.gl.GL_MAX_TEXTURE_SIZE;
for (int x = 0;x < source.getWidth(); x+=maxTextureSize) {
for (int y = 0;y < source.getHeight(); y+=maxTextureSize) {
int tSize=getTextureSize(source.getWidth()-x,source.getHeight()-y);
Pixmap p = new Pixmap(tSize, tSize, Pixmap.Format.RGBA8888);
p.drawPixmap(source, 0, 0, x, y, tSize, tSize);
Texture t=new Texture(new PixmapTextureData(p, Pixmap.Format.RGBA8888, true, false, true));
textures.add(t);
if(smooth)
t.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
UncoloredImage tile = new UncoloredImage(t);
tile.setTouchable(Touchable.disabled);
tile.setX(x);
tile.setY(source.getHeight()-y-tile.getHeight());
p.dispose();
this.addActor(tile);
tiles.add(tile);
}
}
super.setWidth(source.getWidth());
super.setHeight(source.getHeight());
trueWidth=source.getWidth();
trueHeight=source.getHeight();
}
public int getTextureSize(int width, int height)
{
int n=Math.max(width,height);
if(n>=maxTextureSize)
return maxTextureSize;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
@Override
public void setWidth(float width) {
setScaleX(width/trueWidth);
super.setWidth(Math.abs(width));
}
public void setHeight(float height) {
setScaleY(height / (float)trueHeight);
super.setHeight(Math.abs(height));
}
@Override
public void dispose(CallbackAssetManager m) {
super.dispose(m);
for(int i=0;i<textures.size();i++) {
textures.get(i).dispose();
}
}
public void softDispose(CallbackAssetManager m) {
for(int i=0;i<textures.size();i++) {
textures.get(i).dispose();
}
}
}