Repeating Texture on Model/Mesh

2019-06-01 07:50发布

When I just draw texture on SpriteBatch and set TextureWrap.Repeat everything is OK. But now I have 3D scene and I want have ground and texture must be repeated on model/mesh and this just don' t work.

public static StillModel createPlainMesh(float xs, float zs, Texture texture) {
        final Mesh mesh = new Mesh(true, 4, 6, new VertexAttribute(
                Usage.Position, 3, "a_position"), new VertexAttribute(
                Usage.TextureCoordinates, 2, "a_texCoords")); 
        mesh.setVertices(new float[]
                { xs, 0f, zs, 0, 0,
                xs, 0f, -zs, 0, 1,
                -xs, 0f, zs, 1, 0,
                -xs, 0f, -zs , 1,1
                });
        mesh.setIndices(new short[] { 0, 1, 2, 1, 2, 3 });
        final StillModel model = new StillModel(new StillSubMesh(
                "ground", mesh, GL10.GL_TRIANGLES, new Material()));    

        texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        model.setMaterial(new Material("material", new TextureAttribute(texture, 0, "s_tex")));

        return model;
    }

I have this code. I use setWrap(TextureWrap.Repeat, TextureWrap.Repeat), but textures are still streched. I don't know why, but it looks terrible.

1条回答
虎瘦雄心在
2楼-- · 2019-06-01 08:43

I solved it. If you want repeat texture on model, You must modify this:

mesh.setVertices(new float[]
                { xs, 0f, zs, 0, 0,
                xs, 0f, -zs, 0, 1,
                -xs, 0f, zs, 1, 0,
                -xs, 0f, -zs , 1,1
                });

Modify Texture Coords - change to for example 20 if You want repeat it 20times

Example:

mesh.setVertices(new float[]
                { xs, 0f, zs, 0, 0,
                xs, 0f, -zs, 0, 20,
                -xs, 0f, zs, 20, 0,
                -xs, 0f, -zs , 20,20
                });
查看更多
登录 后发表回答