Changing a Shape3D's color

Hi,

I’m running into some problems changing the color of a Shape3D. My Shape3D is a simple rectangle, implemented like:


public class Box extends Shape3D {
     public Box() {
          super();
          // Set capabilities.            
this.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
this.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);

            
         // Draw a rectangle.
   QuadArray array = new QuadArray(4,QuadArray.COORDINATES | QuadArray.COLOR_3);
   array.setCoordinate(0,new Point3d(-1,-1,0));
   array.setCoordinate(1,new Point3d(1,-1,0));
   array.setCoordinate(2,new Point3d(1,1,0));
   array.setCoordinate(3,new Point3d(-1,1,0));

   for (int i = 0; i < 3; i++) {
       array.setColor(i, navyblue);
   }
            
   this.setGeometry(array);

   }

}

With the above code, I’ve created a navy-blue box. I’ve tried changing the color of the box to red by using this method:


private void applyColor(Shape3D s3d, Color3f color) {
     Appearance ap;
     ap = new Appearance();
     ap.setCapability(Appearance.ALLOW_MATERIAL_READ);
     ap.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
     ap.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
     ap.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
     ap.setCapability(Appearance.ALLOW_TEXGEN_WRITE);

     Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
     Material m = new Material(color, black, color, black, 1.0f);
     m.setCapability(Material.ALLOW_COMPONENT_READ);
     ap.setMaterial(m);
     s3d.setAppearance(ap);
}

When I try to use this method by calling “applyColor(anInstanceOfTheBox, red);” however, the box doesn’t change color and stays navy-blue.

Does anyone know what I’m leaving out or doing wrong?

Thanks,
DAT

Hey, there are 2 things that u might want to consider.

1 is lighting. Try using a Point Light and aiming directly at your box. If u just have ambient light, then I don’t think the color will show up correctly.

2 is what properties u want to change. Do you want to change specular, diffuse, ect properites of the color. Below is some code that will change the diffuse part of the color (this will effectly change the color you see on the object).


         Shape3D shape  =  (reference to the shape --- u're box in this case)
                                                                                
         Appearance tempAp = shape.getAppearance();
         Material material = tempAp.getMaterial();
         if(material != null && tempAp != null){
            material.setDiffuseColor(color);
            tempAp.setMaterial(material);
            shape.setAppearance(tempAp);
         }

Hmm, I tried both of your suggestions and it didn’t change the color.

BTW, when I tried using your code, it said my Box didn’t have an appearance and so instead of doing " Appearance tempAp = shape.getAppearance();
Material material = tempAp.getMaterial(); ", which gave a null pointer exception, I just said tempAp = new Apperance and material = new Material(); Not sure if that’s what’s screwing it up.

Instaed of setting the color on each vertex, try setting the material and appeariance when you initialize the box. Then when you modify the color somewhere else, do the method that I provided.

That should work I think.