Repeating Texture on Model/Mesh

夙愿已清 提交于 2020-01-14 04:35:27

问题


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:


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


来源:https://stackoverflow.com/questions/16596087/repeating-texture-on-model-mesh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!