【问题标题】:libgdx - Simple repeating texture (setWrap())libgdx - 简单的重复纹理 (setWrap())
【发布时间】:2018-02-08 20:33:34
【问题描述】:

所以我得到了一个非常简单的尖刺纹理,我想在屏幕底部重复多次,因此无论宽度如何,它总是填满屏幕的整个大小。 当然,我现在已经了解了 setWrap() 函数,但我无法得到我正在寻找的结果。

This is the texture

And I'd like to get something like this

更具体地说,我有给定的屏幕宽度,并计算显示的纹理高度占屏幕高度的百分比。 现在我想将纹理放置在 0, 0 并重复它直到它到达屏幕的右侧(当然它可能会被屏幕切断一点但这不是问题我只是不想要纹理得到拉伸)

到目前为止,我“已经”得到了这个代码

Texture spikesTex;

spikesTex = new Texture("spike.png");
spikesTex.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

然后我已经尝试了不同的参数

batch.draw(spikesTex, ...);

但它们都没有真正奏效。

【问题讨论】:

    标签: java libgdx mapping textures repeat


    【解决方案1】:

    我尝试以这种方式完成您的要求。也许它会帮助你。

    public class MAIN extends ApplicationAdapter {
        SpriteBatch batch;
        Texture img;
        TextureRegion textureRegion;
        float drawingWidth,drawingHeight;
    
        @Override
        public void create () {
            batch = new SpriteBatch();
            img = new Texture("spike.png");
    
            img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.ClampToEdge);
            int width=Gdx.graphics.getWidth();
    
            textureRegion=new TextureRegion(img,0,0,width,img.getHeight());
    
            drawingWidth=width;
            drawingHeight=Gdx.graphics.getHeight()*.2f; 
       }
    
        @Override
        public void render () {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.begin();
            batch.draw(textureRegion,0,0,drawingWidth,drawingHeight);
            batch.end();
        }
    
        @Override
        public void dispose () {
            batch.dispose();
            img.dispose();
        }
    }
    

    【讨论】:

    • 您的解决方案并没有完全符合我的要求,因为 drawingHeight 比 img.getHeight() 更小,纹理仍然在 x 轴上拉伸(或者更好:在 y 轴上收缩)。但是,当我将“宽度”乘以除以“drawingHeight”的相同因子时,它会起作用。非常感谢!
    【解决方案2】:

    不需要创建额外的TextureRegion,你可以自己绘制重​​复的纹理,给出想要的宽高参数:

        img = new Texture("spike.png");
        img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
        ...
        int imgPosX = 0;// x position where to draw
        int imgPosY = 0;// y position where to draw 
        batch.draw(img, imgPosX, imgPosY, 0, 0, drawingWidth, drawingHeight);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      相关资源
      最近更新 更多