I am programming using ColorInterpolator, but I couldn't get the right
result from it.
I want to change a shape's color from color a to color b.
Whatever I set, I could only get result of color change from emissive
color to
(emissive color +diffuse color).
Here is my programs:
--------------Color.java------
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Color extends Applet {
private BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
objRoot.addChild(createObject());
Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f);
BoundingSphere bounds=new BoundingSphere(new
Point3d(0.0,0.0,0.0),100.0);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
objRoot.compile();
return objRoot;
}
private Group createObject() {
Transform3D t = new Transform3D();
TransformGroup objTrans = new TransformGroup(t);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance ap=new Appearance();
ap.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
Material mat = new Material( );
mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
Color3f emissive = new Color3f(0.0f, 1.0f, 0.0f);
Color3f diffuse = new Color3f(0.0f, 0.0f, 1.0f);
mat.setEmissiveColor(emissive);
mat.setDiffuseColor(diffuse);
ap.setMaterial(mat);
ColoringAttributes ca = new ColoringAttributes( );
ca.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
ap.setColoringAttributes( ca);
Alpha alpha = new Alpha(-1,
Alpha.INCREASING_ENABLE|Alpha.DECREASING_ENABLE,
0, 0, 8000, 0, 0, 8000, 0, 0);
ColorInterpolator ci = new ColorInterpolator(alpha, mat,
new Color3f(1.0f,0.0f, 1.0f), new Color3f(0.0f,
0.0f, 1.0f));
BoundingSphere bounds=new BoundingSphere(new
Point3d(0.0,0.0,0.0),100.0);
ci.setSchedulingBounds(bounds);
Shape3D shape = new myShape();
shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
shape.setAppearance(ap);
objTrans.addChild(shape);
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0, 4000, 0, 0, 0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
return objTrans;
}
public Color() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Color(), 540,361);
}
}
---------------end of Color.java----------
---------------myShape.java-------
import javax.media.j3d.*;
import javax.vecmath.*;
public class myShape extends Shape3D {
private float vert[] = {
-.3f,-.3f,0.0f,
-.3f,.3f,0.0f,
-.1f,-.3f,0.0f,
-.1f,.3f,0.0f,
.1f,-.3f,0.0f,
.1f,.3f,0.0f,
.3f,-.3f,0.0f,
.3f,.3f,0.0f,
.4f,.4f,0.0f, };
public myShape() {
int i, face;
TriangleArray shape = new TriangleArray(9,
TriangleArray.COORDINATES|TriangleArray.NORMALS);
shape.setCoordinates(0,vert);
Vector3f normal = new Vector3f();
Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f();
Point3f [] pts = new Point3f[3];
for (i=0;i<3;i++) pts[i] = new Point3f();
for(face =0; face<3; face++) {
shape.getCoordinates(face*3,pts);
v1.sub(pts[1],pts[0]);
v2.sub(pts[2],pts[0]);
normal.cross(v1,v2);
normal.normalize();
for (i=0;i<3;i++) {
shape.setNormal((face*3+i), normal);
}
}
this.setGeometry(shape);
}
}
-----------end of myShape.java-------
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/