Example usage for javax.media.j3d TransparencyAttributes NICEST

List of usage examples for javax.media.j3d TransparencyAttributes NICEST

Introduction

In this page you can find the example usage for javax.media.j3d TransparencyAttributes NICEST.

Prototype

int NICEST

To view the source code for javax.media.j3d TransparencyAttributes NICEST.

Click Source Link

Document

Use the nicest available method for transparency.

Usage

From source file:InterpolatorTest.java

protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();

    // create a root TG in case we need to scale the scene
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    // create the Appearance for the Shape3D
    Appearance app = new Appearance();

    // create a Material, modified by the ColorInterpolator
    Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Material mat = new Material(objColor, black, objColor, black, 80.0f);
    mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
    app.setMaterial(mat);/*from  w  w w .  ja  va  2  s  .c  om*/

    // create a TransparencyAttributes, modified by the
    // TransparencyInterpolator
    TransparencyAttributes transparency = new TransparencyAttributes();
    transparency.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
    transparency.setTransparencyMode(TransparencyAttributes.NICEST);
    app.setTransparencyAttributes(transparency);

    // create a Switch Node and set capabilities
    Switch switchNode = new Switch();
    switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // create a Alpha object for the Interpolators
    Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 500, 100, 5000, 2000, 1000,
            5000, 2000, 500);

    // add each BG and Interpolator as a child of the Switch Node
    TransformGroup tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ColorInterpolator(alpha, app.getMaterial())));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new PositionInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new RotationInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ScaleInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg,
            new TransparencyInterpolator(alpha, app.getTransparencyAttributes(), 0, 0.8f)));

    // define the data for the RotPosScalePathInterpolator
    float[] knots = { 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f };
    float[] scales = { 0.2f, 0.5f, 0.8f, 2.3f, 5.4f, 0.6f, 0.4f, 0.2f, 0.1f };
    Quat4f[] quats = new Quat4f[9];
    Point3f[] positions = new Point3f[9];

    quats[0] = new Quat4f(0.3f, 1.0f, 1.0f, 0.0f);
    quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.3f);
    quats[2] = new Quat4f(0.2f, 1.0f, 0.0f, 0.0f);
    quats[3] = new Quat4f(0.0f, 0.2f, 1.0f, 0.0f);
    quats[4] = new Quat4f(1.0f, 0.0f, 0.4f, 0.0f);
    quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.2f);
    quats[6] = new Quat4f(0.3f, 0.3f, 0.0f, 0.0f);
    quats[7] = new Quat4f(1.0f, 0.0f, 1.0f, 1.0f);
    quats[8] = quats[0];

    positions[0] = new Point3f(0.0f, 0.0f, -1.0f);
    positions[1] = new Point3f(1.0f, -2.0f, -2.0f);
    positions[2] = new Point3f(-2.0f, 2.0f, -3.0f);
    positions[3] = new Point3f(1.0f, 1.0f, -4.0f);
    positions[4] = new Point3f(-4.0f, -2.0f, -5.0f);
    positions[5] = new Point3f(2.0f, 0.3f, -6.0f);
    positions[6] = new Point3f(-4.0f, 0.5f, -7.0f);
    positions[7] = new Point3f(0.0f, -1.5f, -4.0f);
    positions[8] = positions[0];

    tg = createSharedGroup(app);

    // create the Interpolator
    RotPosScalePathInterpolator rotPosScalePathInterplator = new RotPosScalePathInterpolator(alpha, tg,
            new Transform3D(), knots, quats, positions, scales);

    // add a BG for the Interpolator
    switchNode.addChild(createBranchGroup(tg, rotPosScalePathInterplator));

    // create a RandomAlpha object to control a SwitchInterpolator
    // to set the Switches active child node randomly
    RandomAlpha randomAlpha = new RandomAlpha();

    // create the interpolator
    SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(randomAlpha, switchNode);
    switchInterpolator.setSchedulingBounds(getApplicationBounds());

    // connect the scenegraph
    objTrans.addChild(switchNode);
    objTrans.addChild(switchInterpolator);

    // Set up the global lights
    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(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    // add the lights
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    // connect
    objRoot.addChild(objTrans);

    return objRoot;
}

From source file:BehaviorTest.java

public void processStimulus(java.util.Enumeration criteria) {
    while (criteria.hasMoreElements()) {
        WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement();

        if (wakeUp instanceof WakeupOnElapsedTime) {
            // we are starting the explosion, apply the
            // appearance changes we require
            PolygonAttributes polyAttribs = new PolygonAttributes(PolygonAttributes.POLYGON_POINT,
                    PolygonAttributes.CULL_NONE, 0);
            m_Shape3D.getAppearance().setPolygonAttributes(polyAttribs);

            PointAttributes pointAttribs = new PointAttributes(3, false);
            m_Shape3D.getAppearance().setPointAttributes(pointAttribs);

            m_Shape3D.getAppearance().setTexture(null);

            m_TransparencyAttributes = new TransparencyAttributes(TransparencyAttributes.NICEST, 0);
            m_TransparencyAttributes.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
            m_Shape3D.getAppearance().setTransparencyAttributes(m_TransparencyAttributes);
        } else {/*from www. j  ava2  s . c o m*/
            // we are mid explosion, modify the GeometryArray
            m_nFrameNumber++;

            m_GeometryArray.getCoordinates(0, m_CoordinateArray);

            m_TransparencyAttributes.setTransparency(((float) m_nFrameNumber) / ((float) m_nNumFrames));
            m_Shape3D.getAppearance().setTransparencyAttributes(m_TransparencyAttributes);

            for (int n = 0; n < m_CoordinateArray.length; n += 3) {
                m_Vector.x = m_CoordinateArray[n];
                m_Vector.y = m_CoordinateArray[n + 1];
                m_Vector.z = m_CoordinateArray[n + 2];

                m_Vector.normalize();

                m_CoordinateArray[n] += m_Vector.x * Math.random() + Math.random();
                m_CoordinateArray[n + 1] += m_Vector.y * Math.random() + Math.random();
                m_CoordinateArray[n + 2] += m_Vector.z * Math.random() + Math.random();
            }

            // assign the new coordinates
            m_GeometryArray.setCoordinates(0, m_CoordinateArray);
        }
    }

    if (m_nFrameNumber < m_nNumFrames) {
        // assign the next WakeUpCondition, so we are notified again
        wakeupOn(m_FrameWakeupCondition);
    } else {
        // we are at the end of the explosion
        // reapply the original appearance and GeometryArray
        // coordinates
        setEnable(false);
        m_Shape3D.setAppearance(m_Appearance);

        m_GeometryArray.setCoordinates(0, m_OriginalCoordinateArray);

        m_OriginalCoordinateArray = null;
        m_GeometryArray = null;
        m_CoordinateArray = null;
        m_TransparencyAttributes = null;

        // if we have a listener notify them that we are done
        if (m_Listener != null)
            wakeupOn(m_Listener.onExplosionFinished(this, m_Shape3D));
    }
}

From source file:AppearanceTest.java

public void onNICEST() {
    getTransparencyAttributes().setTransparencyMode(TransparencyAttributes.NICEST);
}

From source file:AppearanceExplorer.java

TransparencyAttributesEditor(TransparencyAttributes init) {
    transpAttr = init;// w w  w  .j  a  v  a 2 s  .  c  o  m
    transparency = transpAttr.getTransparency();
    mode = transpAttr.getTransparencyMode();
    srcBlendFunction = transpAttr.getSrcBlendFunction();
    dstBlendFunction = transpAttr.getDstBlendFunction();

    setLayout(new GridLayout(4, 1));

    FloatLabelJSlider transparencySlider = new FloatLabelJSlider("Transparency", 0.1f, 0.0f, 1.0f,
            transparency);
    transparencySlider.setMajorTickSpacing(0.1f);
    transparencySlider.setPaintTicks(true);
    transparencySlider.addFloatListener(new FloatListener() {
        public void floatChanged(FloatEvent e) {
            transparency = e.getValue();
            transpAttr.setTransparency(transparency);
        }
    });
    add(transparencySlider);

    String[] modeNames = { "NONE", "SCREEN_DOOR", "BLENDED", "NICEST", "FASTEST" };
    int[] modeValues = { TransparencyAttributes.NONE, TransparencyAttributes.SCREEN_DOOR,
            TransparencyAttributes.BLENDED, TransparencyAttributes.NICEST, TransparencyAttributes.FASTEST };

    IntChooser modeChooser = new IntChooser("Mode:", modeNames, modeValues, mode);
    modeChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            mode = event.getValue();
            transpAttr.setTransparencyMode(mode);
        }
    });
    add(modeChooser);

    String[] blendNames = { "BLEND_ZERO", "BLEND_ONE", "BLEND_SRC_ALPHA", "BLEND_ONE_MINUS_SRC_ALPHA" };
    int[] blendValues = { TransparencyAttributes.BLEND_ZERO, TransparencyAttributes.BLEND_ONE,
            TransparencyAttributes.BLEND_SRC_ALPHA, TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA, };
    IntChooser srcBlendFunctionChooser = new IntChooser("Src Blend Func:", blendNames, blendValues,
            srcBlendFunction);
    srcBlendFunctionChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            srcBlendFunction = event.getValue();
            transpAttr.setSrcBlendFunction(srcBlendFunction);
        }
    });
    add(srcBlendFunctionChooser);
    IntChooser dstBlendFunctionChooser = new IntChooser("Dst Blend Func:", blendNames, blendValues,
            dstBlendFunction);
    dstBlendFunctionChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            dstBlendFunction = event.getValue();
            transpAttr.setDstBlendFunction(dstBlendFunction);
        }
    });
    add(dstBlendFunctionChooser);
}

From source file:Demo3D.java

/**
 * Construction of the desired tetrahedron.
 * /* w w w  . java2s. c  o  m*/
 * @return javax.media.j3d.Shape3D myTetrahedron - the constructed
 *         tetrahedron
 */
public Shape3D myTetrahedron() {

    ////////////////////// Geometric part ///////////////////////////

    // The 4 vertices p0, p1, p2 and p3 of the tetrahedron.
    vertices = new Point3f[lengthVertices]; // 4
    vertices[0] = new Point3f(0.0f, 0.0f, 0.0f);
    vertices[1] = new Point3f(1.0f, 0.0f, 0.0f);
    vertices[2] = new Point3f(0.0f, 1.0f, 0.0f);
    vertices[3] = new Point3f(0.0f, 0.0f, 1.0f);

    // Scaling of vertices
    for (int i = 0; i < lengthVertices; i++)
        // lengthVertices = 4
        vertices[i].scale(scale_XYZ);

    // Set the face's indices for the tetrahedron (referenced to the array
    // of vertices
    // by setCoordinates(vertices) and
    // setCoordinateIndices(tetraFaceIndices)).
    tetraFaceIndices = new int[lengthTetraFaceIndices]; // 12
    // From the camera in the view coordinate system
    // bottom
    tetraFaceIndices[0] = 0;
    tetraFaceIndices[1] = 1;
    tetraFaceIndices[2] = 3;
    // back-left face
    tetraFaceIndices[3] = 0;
    tetraFaceIndices[4] = 3;
    tetraFaceIndices[5] = 2;
    // back face
    tetraFaceIndices[6] = 0;
    tetraFaceIndices[7] = 2;
    tetraFaceIndices[8] = 1;
    // front face
    tetraFaceIndices[9] = 1;
    tetraFaceIndices[10] = 2;
    tetraFaceIndices[11] = 3;

    // Create the GeometryInfo instance and set the vertices
    tetra_GeometryInfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
    tetra_GeometryInfo.setCoordinates(vertices);
    tetra_GeometryInfo.setCoordinateIndices(tetraFaceIndices);

    //      triangulator = new Triangulator(); // only for polygons:
    // POLYGON_ARRAY
    //      triangulator.triangulate(tetra_GeometryInfo); // and with: int
    // stripCounts[]
    //           gi.setStripCounts(...)
    //           int contourCounts[]

    // Set the parameters (1 texture with dimension 2) for the texture's
    // coordinates
    tetra_GeometryInfo.setTextureCoordinateParams(1, 2);

    //    case #1: each face of the tetrahedron has the same texture portion.

    // The coordinates of the 3 points in the 2D texture space.
    textCoord2f = new TexCoord2f[3];
    textCoord2f[0] = new TexCoord2f(0.0f, 0.2f);
    textCoord2f[1] = new TexCoord2f(0.5f, 1.0f);
    textCoord2f[2] = new TexCoord2f(1.0f, 0.5f);

    // Set the texture coordinate's indices (referenced to the array of 2D
    // points
    // in the texture space by setTextureCoordinates(0, textCoord2f) and
    // setTextureCoordinateIndices(0, textCoordIndices)).
    textCoordIndices = new int[lengthTetraFaceIndices]; // 12

    // From the camera in the view coordinate system (inverse of
    // tetraFaceIndices !!!)
    // front face
    textCoordIndices[0] = 0;
    textCoordIndices[1] = 1;
    textCoordIndices[2] = 2;
    // back face
    textCoordIndices[3] = 0;
    textCoordIndices[4] = 1;
    textCoordIndices[5] = 2;
    // back-left face
    textCoordIndices[6] = 2;
    textCoordIndices[7] = 0;
    textCoordIndices[8] = 1;
    // bottom
    textCoordIndices[9] = 0;
    textCoordIndices[10] = 1;
    textCoordIndices[11] = 2;

    /*
     * // case #2: each face of the tetrahedron has a different part of the
     * texture.
     *  // The coordinates of the 4 points in the 2D texture space.
     * textCoord2f = new TexCoord2f[4]; textCoord2f[0] = new
     * TexCoord2f(0.0f, 0.5f); textCoord2f[1] = new TexCoord2f(1.0f, 0.5f);
     * textCoord2f[2] = new TexCoord2f(0.6f, 0.7f); textCoord2f[3] = new
     * TexCoord2f(0.6f, 0.3f);
     * 
     *  // Set the texture coordinate's indices (referenced to the array of
     * 2D points // in the texture space by setTextureCoordinates(0,
     * textCoord2f) and // setTextureCoordinateIndices(0,
     * textCoordIndices)). textCoordIndices = new
     * int[lengthTetraFaceIndices]; // 12
     *  // From the camera in the view coordinate system (inverse of
     * tetraFaceIndices !!!) // front face textCoordIndices[0] = 3;
     * textCoordIndices[1] = 2; textCoordIndices[2] = 0; // back face
     * textCoordIndices[3] = 1; textCoordIndices[4] = 2; textCoordIndices[5] =
     * 3; // back-left face textCoordIndices[6] = 1; textCoordIndices[7] =
     * 0; textCoordIndices[8] = 2; // bottom textCoordIndices[9] = 1;
     * textCoordIndices[10]= 3; textCoordIndices[11]= 0;
     */
    // just one set
    tetra_GeometryInfo.setTextureCoordinates(0, textCoord2f);
    // just one set
    tetra_GeometryInfo.setTextureCoordinateIndices(0, textCoordIndices);

    normalGenerator = new NormalGenerator();
    normalGenerator.generateNormals(tetra_GeometryInfo);

    if (crAngle)
        normalGenerator.setCreaseAngle(0.0f); // with 0 radian ===> creased

    stripifier = new Stripifier();
    stripifier.stripify(tetra_GeometryInfo);

    tetra_GeometryArray = tetra_GeometryInfo.getGeometryArray();

    // The geonometry is passed to the instance this of the tetrahedron.
    this.setGeometry(tetra_GeometryArray);

    ////////////////////// Appearance part ///////////////////////////

    appearance = new Appearance();

    // Optical properties of the tetrahedron.

    // Ambient-diffuse-reflection coefficient
    diffAmb = new Color3f(1.0f, 0.5f, 1.0f);
    // Diffuse-reflection coefficient
    reflDiff = new Color3f(1.0f, 0.5f, 1.0f);
    // Specular-reflection coefficient (reflectance function)
    reflSpec = new Color3f(1.0f, 0.5f, 1.0f);
    // c = shininess: cos^c in the specular reflection
    float c = 15;
    // Emitted light
    emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

    material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
    appearance.setMaterial(material);

    // This instance acts only on the tetrahedron and not on its texture.
    trAttr = new TransparencyAttributes(TransparencyAttributes.NICEST, 0.0f);
    // 0.0 = fully opaque
    // 1.0 = fully transparent
    appearance.setTransparencyAttributes(trAttr);

    // Loading the texture
    newTextureLoader = new NewTextureLoader("Images/Claude.jpg");
    newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());

    texture = newTextureLoader.getTexture();

    appearance.setTexture(texture);

    // Application mode of the texture
    textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.MODULATE); // there still are:
    // BLEND, COMBINE,
    // DECAL, and REPLACE
    appearance.setTextureAttributes(textAttr);

    // The appearance is passed to the instance this of the tetrahedron.
    this.setAppearance(appearance);

    return this;
}