set Java 3D Appearance - Java javax.media.j3d

Java examples for javax.media.j3d:Appearance

Description

set Java 3D Appearance

Demo Code


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Enumeration;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

public class Main{
    public static void setAppearance(BranchGroup bg, Color3f ambDiffColor,
            Color3f specColor, Color3f emissColor, float shininess) {

        //TODO: this needs to keep the current textures etc and just alter the new values
        Material material = new Material();
        material.setAmbientColor(ambDiffColor);
        material.setDiffuseColor(ambDiffColor);
        material.setSpecularColor(specColor);
        material.setEmissiveColor(emissColor);
        material.setShininess(shininess);

        material.setLightingEnable(true);
        material.setColorTarget(Material.SPECULAR);

        Appearance appearance = new Appearance();
        appearance.setMaterial(material);

        PolygonAttributes targetPolyAttr = new PolygonAttributes();
        targetPolyAttr.setPolygonMode(PolygonAttributes.POLYGON_FILL);
        appearance.setPolygonAttributes(targetPolyAttr);

        Enumeration<?> enumeration = bg.getAllChildren();
        while (enumeration.hasMoreElements()) {
            Object o = enumeration.nextElement();
            if (o instanceof Shape3D) {
                Shape3D s3d = (Shape3D) o;
                s3d.setAppearance(appearance);
            }//w  ww.  ja  v  a  2 s  . c  o  m
        }
    }
}

Related Tutorials