1
\$\begingroup\$

I have this code

textureAtlas = TextureAtlas("atlas.atlas")

val box = textureAtlas.findRegion("box")

I want to create a texture with "box". Is it possible? box.texture return the original texture, not the regioned. Oh and I don't want to use Sprite and SpriteBatch. I need this in 3D, not 2D.

Thanks

\$\endgroup\$
1
\$\begingroup\$

TextureAtlas actually not separating pieces. When you get region from atlas its just saying that this is the area you gonna use (u,v,u2,v2) and this is original reference to whole texture.

This is why batch.draw(Texture) and batch.draw(TextureRegion) are not same in use.

However taking part of picture as texture is possible.

  • You can use pixmap to do it.

    First generate pixmap from atlas texture. Then create new empty pixmap in size of "box" area you want. Then assign pixel arrays and generate texture from your new pixmap.

It may be quite expensive due to your Textureatlas size.

  • You can use framebuffer. Create FBbuilder and build new frame buffer.Draw texture region to this buffer and get texture from it.

Problem here is the sizes of texture will be same as viewport/screen sizes.I guess you can create new camera to change it to sizes you want.

GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(widthofBox, heightofBox);
    frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
    frameBuffer = frameBufferBuilder.build();
    OrthographicCamera c = new OrthographicCamera(widthofBox, heightofBox);
    c.up.set(0, 1, 0);
    c.direction.set(0, 0, -1);
    c.position.set(widthofBox / 2, heightofBox / 2, 0f);
    c.update();
    batch.setProjectionMatrix(c.combined);
    frameBuffer.begin();
    batch.begin();
    batch.draw(boxregion...)
    batch.end();
    frameBuffer.end();
    Texture texturefbo = frameBuffer.getColorBufferTexture();

Texturefbo will be y flipped. You can fix this with texture draw method by setting scaleY to -1 or You can scale scaleY to -1 while drawing on framebuffer or can change camera like this

up.set(0, -1, 0);
direction.set(0, 0, 1);

to flip to camera on y axis.

Last thing came to my mind is mipmapping this texture.Its also not so hard.

    texturefbo.bind();
    Gdx.gl.glGenerateMipmap(GL20.GL_TEXTURE_2D);
    texturefbo.setFilter(Texture.TextureFilter.MipMapLinearLinear, 
    Texture.TextureFilter.MipMapLinearLinear);
\$\endgroup\$
1
\$\begingroup\$

Inside an TextureAtlas you can have many pointers to textures, so you can add one that just goes to the box.png and retrieve it's region like so:

box.png
size: 128,128
format: RGBA8888
filter: Nearest,Nearest
repeat: none
box
  rotate: false
  xy: 0, 0
  size: 128, 128
  orig: 128, 128
  offset: 0, 0
  index: -1

test.png
size: 1024,1024
format: RGBA8888
filter: Nearest,Nearest
repeat: none
testA
  rotate: false
  xy: 974, 974
  size: 48, 48
  orig: 48, 48
  offset: 0, 0
  index: -1
testB
  rotate: false
  xy: 974, 974
  size: 48, 48
  orig: 48, 48
  offset: 0, 0
  index: -1

test2.png
size: 1024,1024
format: RGBA8888
filter: Nearest,Nearest
repeat: none
testA
  rotate: false
  xy: 2, 358
  size: 576, 324
  orig: 576, 324
  offset: 0, 0
  index: -1
testB
  rotate: false
  xy: 928, 438
  size: 48, 48
  orig: 48, 48
  offset: 0, 0
  index: -1

Then just do:

TextureAtlas at = new TextureAtlas(<somefile_path);
Texture t = at.findRegion("box).getTexture();

You mention original texture, so the texture must be just the box. If the texture has many images, then it's an atlas and you will need to get that region and make an individual texture from that using Pixmap.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.