Texture from texturepacker in LibGDX

后端 未结 2 1637
慢半拍i
慢半拍i 2021-02-10 23:14

Trying get my head around the texture wrapper in (the awesome) LibGDX framework and I need help.

I would like to bind a texture (according to Mesh, Color & Texture)

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-10 23:31

    Got it to work using the above explanation.

    I can't believe so few are people are running asking this question as it seems like something that others would want to do.

    From the accepted solution I created a function that also does the math for the new UV position.

    Tested, and it works for me but please review as i'm not a java developer.

    public Mesh RebuildMeshUVtoTextureRegion(Mesh ObjectMesh, TextureRegion UVMapPos)
    {
        int numFloats = ObjectMesh.getNumVertices() * ObjectMesh.getVertexSize() / 4;
        float[] vertices = new float[numFloats];
        ObjectMesh.getVertices(vertices);
    
        int numIndices = ObjectMesh.getNumIndices();
        short SourceIndices[] = new short[numIndices];
        ObjectMesh.getIndices(SourceIndices);
    
        final int floatsPerVertex = 5;
        int TimesToLoop = ((vertices.length) /floatsPerVertex); 
    
        float previousU;
        float previousV;
    
        float FullMapHeight = UVMapPos.getTexture().getHeight();
        float FullMapWidth  = UVMapPos.getTexture().getWidth();
        float NewMapWidth = UVMapPos.getRegionWidth();
        float NewMapHeight = UVMapPos.getRegionHeight();
    
        float FullMapUPercent;
        float FullMapVPercent;
    
        for (int i = 0; i < TimesToLoop; i++) 
        {   
            previousU = (vertices[(i * floatsPerVertex) + 3]);
            previousV = (vertices[(i * floatsPerVertex) + 4]);
            FullMapUPercent = previousU / FullMapWidth;
            FullMapVPercent = previousV / FullMapHeight;
            vertices[(i * floatsPerVertex) + 3] = (NewMapWidth * FullMapUPercent) + UVMapPos.getU(); //New U
            vertices[(i * floatsPerVertex) + 4] = (NewMapHeight * FullMapVPercent) + UVMapPos.getV();//New V
        }
    
        ObjectMesh.setVertices(vertices);
        ObjectMesh.setIndices(SourceIndices);
    
        return ObjectMesh;
    }
    

提交回复
热议问题