Re: Is it possible to give Texture to Furniture?

by alied » Sun Feb 07, 2010 6:14 pm

Well, that was a rough outline. I've changed textures on-the fly, but I've allways tried to have a dummy texture before (to know where to do the change) with a named material.

Code: Select all

private void deepUpdateMaterial(Node node) {
	if (node instanceof Group) {
		Group bg = (Group) node;
		for (int i = 0; i < bg.numChildren(); i++) {
			Node shape = bg.getChild(i);
			deepUpdateMaterial(shape);
		}
	} else if (node instanceof Shape3D) {
		Shape3D shape = (Shape3D) node;
		Appearance material = shape.getAppearance();
		if (material != null) {
			shape.setAppearanceOverrideEnable(true);
			String texture = null;
/*
Here, set the texture to whatever is to set
*/
			if (texture != null) {
				BufferedImage textureImage = null;
				try {
					textureImage = ImageIO.read(new URL(texture));
				} catch (IOException ex) {
					// Ignore images at other format
				}
				if (textureImage != null) {
					TextureLoader textureLoader = new TextureLoader(textureImage, TextureLoader.GENERATE_MIPMAP | TextureLoader.BY_REFERENCE);
					material.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
					material.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
					material.setTexture(textureLoader.getTexture());
				}
			}
		}
	}
}
That is how I do it, and it works. Emmanuel, some of this code would seem familiar to you :-). Anyway, in this case there is already a defined material. If there weren't any, you should have to list all shapes and assign a texture for each, and create materials as needed.

Why not to post this to the developpers forum, in case someone wants to try?