Bitch of an error in Java3D
This took me like 4 hours to finally complete, so I figured I would write about it for the next unlucky fool to attempt it.
I instantiate a Sphere with a given appearance and after clicking it I would like for that to change. The picking works fine, and I get a reference to the Shape3D. However, I get “Shape3D: no capability to set appearance” exception.
I tried to set TONS of capabilities — for example — here are all the things I hadset:
starS.setCapability(Primitive.ENABLE_APPEARANCE_MODIFY);
starS.setCapability(Sphere.ENABLE_APPEARANCE_MODIFY);
starS.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
starS.setCapability(Primitive.ENABLE_PICK_REPORTING);
starS.setCapability(Primitive.ENABLE_GEOMETRY_PICKING);
starS.setCapability(Primitive.GEOMETRY_NOT_SHARED);
starS was a Sphere, and by default the picking operation was grabbing a Shape3D, which surprisingly does not work properly.
Here’s the fix:
The capabilities must be set every time you define something, not in one lump sum at the end like I was trying to do.
So when you define your first appearance, set it like:
Appearance a = new Appearance();
a.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
a.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
When you define the sphere add
starS.setCapability(Primitive.ENABLE_APPEARANCE_MODIFY);
And before when you want to do changes, do this:
Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);
if (p != null) {
System.out.println(p.getClass().getName());
ColoringAttributes c = new ColoringAttributes();
c.setCapability(ColoringAttributes.ALLOW_COLOR_READ);
c.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
p.getAppearance().setColoringAttributes(c);
c.setColor(new Color3f(0,1,0));
Appearance a = new Appearance();
a.setColoringAttributes(c);
...
This works for me, and I am using http://www.java3d.org/selection.html as a guide to help do the picking.
Good luck!
Leave a Reply