2014-05-10 9 views
0

我试图在Box的两侧放置不同的纹理,但没有任何成功。Java3D 1.5.1 - 如何在Box部件上放置不同的纹理?

这里是我的代码:

BufferedImage texture1 = ...; // brown image 
BufferedImage texture2 = ...; // green image 

Box box = new Box(1f, 1f, 1f, Box.GENERATE_TEXTURE_COORDS, new Appearance()); 

TextureAttributes ta = new TextureAttributes(); 
ta.setTextureMode(TextureAttributes.MODULATE); 

Appearance app = new Appearance(); 
app.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2)); 
app.setTexture(new TextureLoader(texture1).getTexture()); 
app.setTextureAttributes(ta); 
box.setAppearance(Box.TOP, app); 

Appearance app2 = new Appearance(); 
app2.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2)); 
app2.setTexture(new TextureLoader(texture2).getTexture()); 
app2.setTextureAttributes(ta); 
box.setAppearance(Box.RIGHT, app2); 

结果:

Generated box

好了,它把两边的图片,但你可以看到,他们所迷离。

我认为这可能是由错误的TexCoordGeneration应用于边的外观造成的。但我也不确定是否使用正确的参数创建Box实例。

我该如何解决这个问题?

非常感谢您的回答!

+0

Java 3D 1.5.1已过时,您应该切换到Java 3D 1.6:http://tinyurl.com/cf47kcb – gouessej

回答

0

好吧我通过生成我自己的TexCoordGeneration对象与Vector4f对象来解决这个问题。

代码:

app.setTexCoordGeneration(this.generateTexCoord(box.getShape(Box.TOP))); 

和方法generateTexCoord()

private TexCoordGeneration generateTexCoord(Shape3D shape) { 
    BoundingBox bb = new BoundingBox(shape.getBounds()); 
    Point3d lower = new Point3d(); 
    Point3d upper = new Point3d(); 
    bb.getLower(lower); 
    bb.getUpper(upper); 

    double width = upper.x - lower.x; 
    double height = upper.y - lower.y; 
    double deep = upper.z - lower.z; 
    Vector4f planeX = new Vector4f((float)(1.0/width), 0.0f, 0.0f, (float)(-lower.x/width)); 
    Vector4f planeY = new Vector4f(0.0f, (float)(1.0/height), 0.0f, (float)(-lower.y/height)); 
    Vector4f planeZ = new Vector4f(0.0f, 0.0f, (float)(1.0/deep), (float)(-lower.z/deep)); 

    TexCoordGeneration tcg = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2); 
    if(width == 0) { // RIGHT, LEFT: YZ 
     tcg.setPlaneS(planeZ); 
     tcg.setPlaneT(planeY); 
    } else if(height == 0) { // TOP, BOTTOM: XZ 
     tcg.setPlaneS(planeX); 
     tcg.setPlaneT(planeZ); 
    } else { // FRONT, BACK: XY 
     tcg.setPlaneS(planeX); 
     tcg.setPlaneT(planeY); 
    } 
    return tcg; 
} 

。希望帮助别人有同样的问题。 :)

0

我遇到了同样的问题,因为我正在阅读互联网上的页面,以及所有我用不同纹理完成的立方体。我使用了我电脑中的图像。希望它适合每个人,它花了我3天!

protected Node buildShape() { 

    TextureLoader loader; 
    Texture texture; 

    Box caja=new Box(1.5f,1.5f,1.5f,Box.GENERATE_TEXTURE_COORDS,new Appearance()); 
    Appearance ap = new Appearance(); 

    loader = new TextureLoader("blue.jpg",this); 
    texture = loader.getTexture(); 
    ap.setTexture(texture); 
    caja.setAppearance(Box.BACK,ap); 

    Appearance ap2 = new Appearance(); 

    loader = new TextureLoader("white.jpg",this); 
    texture = loader.getTexture(); 
    ap2.setTexture(texture); 
    caja.setAppearance(Box.TOP,ap2); 

    Appearance ap3 = new Appearance(); 

    loader = new TextureLoader("red.jpg",this); 
    texture = loader.getTexture(); 
    ap3.setTexture(texture); 
    caja.setAppearance(Box.BOTTOM,ap3); 

    Appearance ap4 = new Appearance(); 

    loader = new TextureLoader("green.jpg",this); 
    texture = loader.getTexture(); 
    ap4.setTexture(texture); 
    caja.setAppearance(Box.LEFT,ap4); 

    Appearance ap5 = new Appearance(); 

    loader = new TextureLoader("orange.jpg",this); 
    texture = loader.getTexture(); 
    ap5.setTexture(texture); 
    caja.setAppearance(Box.RIGHT,ap5); 

    Appearance ap6 = new Appearance(); 

    loader = new TextureLoader("yellow.jpg",this); 
    texture = loader.getTexture(); 
    ap6.setTexture(texture); 
    caja.setAppearance(Box.FRONT,ap6); 

    return caja; 
} 
相关问题