I modified the Java3D demo program "AppearanceTest" to mimic the behavior that was failing for my application. I attached the source and bytecode if you want to check it out...
Hi, I tryed and have the same problem. I tryed to move the update code inside a behavior but didn't help.
The only workaround I've found is to create a new Appearance, set the the material on this and call a setAppearance on the sphere.
Bye
Lorenzo
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA3D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
/* * @(#)AppearanceTest.java 1.28 02/10/21 13:36:45 * * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed,licensed or intended * for use in the design, construction, operation or maintenance of * any nuclear facility. */
/*
The spheres are initially all given their own Appearance with a Red material.
Toggleing between "Vertical" and "Horizontal" calls the setMaterial method for each Sphere's Appearance, producing RGB stripes that *should* all be the same color...
Interestingly - the first toggle works, the rest fail. Also, the Spheres that are colored incorrectly seem random and change between different invocations of the test program...
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import com.sun.j3d.utils.geometry.*;
public class AppearanceTest extends Applet implements ActionListener {
UpdateMaterial updateMat = new UpdateMaterial();
private SimpleUniverse u = null;
private int num = 25;
Appearance[][] apps;
Sphere[][] shapes;
Color3f red = new Color3f(Color.RED);
Color3f blue = new Color3f(Color.BLUE);
Color3f green = new Color3f(Color.GREEN);
Color3f black = new Color3f(Color.BLACK);
Color3f white = new Color3f(Color.WHITE);
Material redMat = new Material(red, black, red, white, 75.0f);
Material blueMat = new Material(blue, black, blue, white, 75.0f);
Material greenMat = new Material(green, black, green, white, 75.0f);
public void actionPerformed(ActionEvent evt){
String command = evt.getActionCommand();
/*
if (command.equals("Vertical")){
for (int i=0;i<num;i++){
for (int j=0; j<num; j++) {
if (j % 3 == 0) apps[i][j].setMaterial(redMat);
if (j % 3 == 1) apps[i][j].setMaterial(blueMat);
if (j % 3 == 2) apps[i][j].setMaterial(greenMat);
}
}} else {
for (int i=0;i<num;i++){
for (int j=0; j<num; j++) {
if (j % 3 == 0) apps[j][i].setMaterial(redMat);
if (j % 3 == 1) apps[j][i].setMaterial(blueMat);
if (j % 3 == 2) apps[j][i].setMaterial(greenMat);
}
}}
*/
updateMat.setCommand(command);
}
private BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
Background bg = new Background();
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);
int row, col;
apps = new Appearance[num][num];
for (row = 0; row < num; row++)
for (col = 0; col < num; col++)
apps[row][col] = createAppearance();
shapes = new Sphere[num][num];
for (int i = 0; i < num; i++) {
double ypos = (double)(i - num/2) * 0.06;
for (int j = 0; j < num; j++) {
double xpos = (double)(j - num/2) * 0.06;
Group group = createObject(apps[i][j], 0.05, xpos, ypos, i, j);
objRoot.addChild(group);
}
}
updateMat.setSchedulingBounds(bounds);
objRoot.addChild(updateMat);
objRoot.compile();
return objRoot;
}
private Appearance createAppearance() {
Appearance app = new Appearance();
app.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
app.setMaterial(redMat);
return app;
}
private Group createObject(Appearance app, double scale,
double xpos, double ypos, int i, int j) {
Transform3D t = new Transform3D();
t.set(scale, new Vector3d(xpos, ypos, 0.0));
TransformGroup objTrans = new TransformGroup(t);
TransformGroup spinTg = new TransformGroup();
Sphere shape = new Sphere();
shape.setAppearance(app);
shapes[i][j] = shape;
shape.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_READ);
shape.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
spinTg.addChild(shape);
Transform3D yAxis = new Transform3D();
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 10000.0);
objTrans.addChild(spinTg);
return objTrans;
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
JPanel menu = new JPanel(new FlowLayout());
JButton one = new JButton("Vertical");
JButton two = new JButton("Horizontal");
one.addActionListener(this);
two.addActionListener(this);
menu.add(one);
menu.add(two);
add("North", menu);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void destroy() {
u.cleanup();
}
//
// The following allows AppearanceTest to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new AppearanceTest(), 700, 700);
}
class UpdateMaterial extends Behavior {
private String command;
String getCommand() {
return command;
}
void setCommand(String command) {
this.command = command;
}
WakeupCondition wc = new WakeupOnElapsedFrames(0);
/* (non-Javadoc)
* @see javax.media.j3d.Behavior#initialize()
*/
public void initialize() {
wakeupOn(wc);
}
/* (non-Javadoc)
* @see javax.media.j3d.Behavior#processStimulus(java.util.Enumeration)
*/
public void processStimulus(Enumeration enumeration) {
String lCommand = getCommand();
if (lCommand == null) {
wakeupOn(wc);
return;
}
if (lCommand.equals("Vertical")){
for (int i=0;i<num;i++){
for (int j=0; j<num; j++) {
//Appearance app = shapes[i][j].getAppearance();
Appearance app = new Appearance();
if (j % 3 == 0) app.setMaterial(redMat);
if (j % 3 == 1) app.setMaterial(blueMat);
if (j % 3 == 2) app.setMaterial(greenMat);
shapes[i][j].setAppearance(app);
}
}} else {
for (int i=0;i<num;i++){
for (int j=0; j<num; j++) {
//Appearance app = shapes[j][i].getAppearance();
Appearance app = new Appearance();
if (j % 3 == 0) app.setMaterial(redMat);
if (j % 3 == 1) app.setMaterial(blueMat);
if (j % 3 == 2) app.setMaterial(greenMat);
shapes[j][i].setAppearance(app);
}
}}
setCommand(null);
wakeupOn(wc);
}
}
}
