How to set different images as faces/sides of a 3D

2019-08-09 17:03发布

I've created a small piece of code which draws a 3D cube in my SWT application allowing to rotate it.

Now, I want to change each face/side of the cube and draw a different image on it, but I can't find how to do it (or at least, in an easy way, if that's possible).

I was able to change the complete texture of the cube to an image, but it change all the faces and I want to set a different image to each face. Is this possible? Any code example?

Thanks

2条回答
Summer. ? 凉城
2楼-- · 2019-08-09 17:38

Ok, based on the previous answer and some other forums I reach the following code that allows to set a different texture to each face of a cube:

Basically the line that allows to do that is the following one:

        ((Shape3D) textureCube.getChild(POSITION)).setAppearance(APPEARANCE);

Taking into account that:

textureCube:

    Box textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS,
            defaultAppearance);

(defaultAppearance is just a basic Appearance object: Appearance defaultAppearance = new Appearance();)

The position is given by, as vembutech pointed out, TextureCubeMap class and their values for each face: POSITIVE_X, POSITIVE_Y, POSITIVE_Z, NEGATIVE_X, NEGATIVE_Y, NEGATIVE_Z.

And the appearance object is just an appearance object. I created mine appearance objects with this method:

private Appearance getAppearance(String f) throws Exception {
    Appearance app = new Appearance();
    URL texImage = new java.net.URL("file:" + f);
    Texture tex = new TextureLoader(texImage, this).getTexture();
    app.setTexture(tex);
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    app.setTextureAttributes(texAttr);
    return app;
}

This method creates an appearance based on an input file (f).

Cheers

查看更多
Rolldiameter
3楼-- · 2019-08-09 17:49

Use TextureCubeMap class which is a sub class of Texture. The texture mapping can be used to apply images to faces of the cube.

You can do it by specifying the cube faces using xyz coordinates as positive and negative.

Refer the below link for its complete documentation.

查看更多
登录 后发表回答