简体   繁体   中英

Repeating Texture on Model/Mesh

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.

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
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM