如何拉伸纹理以填充Libgdx中的2d多边形?

时间:2015-03-18 16:54:06

标签: java libgdx

我已经看过很多关于texture.setWrap(TextureWrap.Repeat,....)的内容,但这并不是我在播放atm之后的原因。我正在使用Rube和Libgdx,以及Tescott的rube loader。 理想情况下,我想使用如下图像,顶部有一条尖锐的暗线,用于汽车接触的地面。有没有人有任何资源或提示指出我正确的方向?我相信我需要UV映射,但我确切地使用它我不确定。谢谢。

编辑:我也见过Clamp_to_Edge,但我认为我不需要这个。

http://i.imgur.com/wRQuB3r.png

 Array<Body> bodies = scene.getBodies();

  EarClippingTriangulator ect = new EarClippingTriangulator();

  if ((bodies != null) && (bodies.size > 0))
  {
     mPolySpatials = new Array<PolySpatial>();
     Vector2 bodyPos = new Vector2();
     // for each body in the scene...
     for (int i = 0; i < bodies.size; i++)
     {
        Body body = bodies.get(i);
        bodyPos.set(body.getPosition());

        float bodyAngle = body.getAngle()*MathUtils.radiansToDegrees;

        Array<Fixture> fixtures = body.getFixtureList();

        if ((fixtures != null) && (fixtures.size > 0))
        {
           // for each fixture on the body...
           for (int j = 0; j < fixtures.size; j++)
           {
              Fixture fixture = fixtures.get(j);

              String textureName = (String)scene.getCustom(fixture, "TextureMask", null);
              if (textureName != null)
              {
                 String textureFileName = "data/" + textureName;
                 Texture texture = mTextureMap.get(textureFileName);
                 TextureRegion textureRegion = null;
                 if (texture == null)
                 {
                    texture = new Texture(textureFileName);
                    texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                    mTextureMap.put(textureFileName, texture);
                    textureRegion = new TextureRegion(texture);
                    mTextureRegionMap.put(texture, textureRegion);
                 }
                 else
                 {
                    textureRegion = mTextureRegionMap.get(texture);
                 }

                 // only handle polygons at this point -- no chain, edge, or circle fixtures.
                 if (fixture.getType() == Shape.Type.Polygon)
                 {
                    PolygonShape shape = (PolygonShape) fixture.getShape();
                    int vertexCount = shape.getVertexCount();
                    float[] vertices = new float[vertexCount * 2];

                    // static bodies are texture aligned and do not get drawn based off of the related body.
                    if (body.getType() == BodyType.StaticBody)
                    {
                       for (int k = 0; k < vertexCount; k++)
                       {

                          shape.getVertex(k, mTmp);
                          mTmp.rotate(bodyAngle);
                          mTmp.add(bodyPos); // convert local coordinates to world coordinates to that textures are
                                             // aligned
                          vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                          vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                       }

                       short [] triangleIndices = ect.computeTriangles(vertices).toArray();
                       PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                       PolySpatial spatial = new PolySpatial(region, Color.WHITE);
                       mPolySpatials.add(spatial);
                    }
                    else
                    {
                       // all other fixtures are aligned based on their associated body.
                       for (int k = 0; k < vertexCount; k++)
                       {
                          shape.getVertex(k, mTmp);
                          vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                          vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                       }
                       short [] triangleIndices = ect.computeTriangles(vertices).toArray();
                       PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                       PolySpatial spatial = new PolySpatial(region, body, Color.WHITE);
                       mPolySpatials.add(spatial);
                    }
                 }

0 个答案:

没有答案