You have to set TextureCoordinates or let them generate by the engine for you (which doesn’t produce good results most of the time).
Here is a simple method for generating an appearance object with a given texture:
/**
* Generate Apperance with given texture
* @param withTexCoordGeneration
* @param texture
* @param wrapTexture
* @return Appearance
*/
private Appearance generateAppearanceWithTexture(Texture2D texture, boolean withTexCoordGeneration) {
// Create the appearance that will use the texture
Appearance appearance = new Appearance();
appearance.setTexture(texture);
// If TexCoordGeneration is wanted then have it
if (withTexCoordGeneration) {
TexCoordGeneration texCoordGeneration = new TexCoordGeneration(TexCoordGeneration.NORMAL_MAP,
TexCoordGeneration.TEXTURE_COORDINATE_2);
appearance.setTexCoordGeneration(texCoordGeneration);
}
// Define how the texture will be mapped onto the surface
// by creating the appropriate texture attributes
TextureAttributes textAttr = new TextureAttributes();
textAttr.setTextureMode(TextureAttributes.REPLACE);
PolygonAttributes polygonAttributes = new PolygonAttributes();
polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
appearance.setPolygonAttributes(polygonAttributes);
appearance.setTextureAttributes(textAttr);
return appearance;
}
Or another example which generates a quadarray (for a plane) with setting the TexCoodinates manually (see the .setTextureCoordinate() calls):
/**
* Generate a QuadArray (for a plane) by giving the side of a cuboid and the dimension for all 3
* directions
* @param side
* @param x
* @param y
* @param z
* @param texCoordPoint (if null then TexCoords for full size will be set)
* @return QuadArray
*/
public static final QuadArray makePlaneForCuboid(int side, float x, float y, float z, Point2f texCoordPoint) {
QuadArray plane = new QuadArray(24, GeometryArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);
Point3f pa = new Point3f(-1.0f * x, 1.0f * y, 1.0f * z); // = upper corner
Point3f pb = new Point3f(-1.0f * x, -1.0f * y, 1.0f * z);
Point3f pc = new Point3f(1.0f * x, -1.0f * y, 1.0f * z);
Point3f pd = new Point3f(1.0f * x, 1.0f * y, 1.0f * z);
Point3f pe = new Point3f(-1.0f * x, 1.0f * y, -1.0f * z);
Point3f pf = new Point3f(-1.0f * x, -1.0f * y, -1.0f * z);
Point3f pg = new Point3f(1.0f * x, -1.0f * y, -1.0f * z); // = lower corner
Point3f ph = new Point3f(1.0f * x, 1.0f * y, -1.0f * z);
switch (side) {
case 1:
plane.setCoordinate(0, pa);
plane.setCoordinate(1, pb);
plane.setCoordinate(2, pc);
plane.setCoordinate(3, pd);
break;
case 2:
plane.setCoordinate(0, pe);
plane.setCoordinate(1, pf);
plane.setCoordinate(2, pg);
plane.setCoordinate(3, ph);
break;
case 3:
plane.setCoordinate(0, pe);
plane.setCoordinate(1, pf);
plane.setCoordinate(2, pb);
plane.setCoordinate(3, pa);
break;
case 4:
plane.setCoordinate(0, pd);
plane.setCoordinate(1, pc);
plane.setCoordinate(2, pg);
plane.setCoordinate(3, ph);
break;
case 5:
plane.setCoordinate(0, pe);
plane.setCoordinate(1, pa);
plane.setCoordinate(2, pd);
plane.setCoordinate(3, ph);
break;
case 6:
plane.setCoordinate(0, pf);
plane.setCoordinate(1, pb);
plane.setCoordinate(2, pc);
plane.setCoordinate(3, pg);
break;
default:
plane.setCoordinate(0, pa);
plane.setCoordinate(1, pb);
plane.setCoordinate(2, pc);
plane.setCoordinate(3, pd);
break;
}
if (texCoordPoint == null) {
plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, 1f));
plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
plane.setTextureCoordinate(0, 2, new TexCoord2f(1f, 0f));
plane.setTextureCoordinate(0, 3, new TexCoord2f(1f, 1f));
} else {
plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, texCoordPoint.y));
plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
plane.setTextureCoordinate(0, 2, new TexCoord2f(texCoordPoint.x, 0f));
plane.setTextureCoordinate(0, 3, new TexCoord2f(texCoordPoint.x, texCoordPoint.y));
}
return plane;
}