Transparency support for GIFs/PNGs?

Hi

Is there a built in way in java3d to to texture a shape3d using this transparency of a GIF?

I was looking over some code that read in an image and then specified a colour for transparency manually - is the neccesary, this is the code I was looking at


TextureLoader loader = new TextureLoader(fileurl,GameFrame.frame);
    ImageComponent2D image = loader.getImage();

    BufferedImage bim = image.getImage();

    WritableRaster wr = bim.getRaster();
    int[] pix = new int[4];
    for (int i=0; i<wr.getHeight(); i++)
      for (int j=0; j<wr.getWidth(); j++)
      {
        wr.getPixel(i,j,pix);
        if (pix[0] == 0 // pure blue pixels are changed to pure transparent pixels
         && pix[1] == 0
         && pix[2] == 255)
        {
          pix[2] = 0;
          pix[3] = 0;
        }
        else // all other pixels have the transparency and tint applied
        {
          if (tint != null)
          {
            pix[0] *= tint.getRed()/255f;
            pix[1] *= tint.getGreen()/255f;
            pix[2] *= tint.getBlue()/255f;
          }
          pix[3] = transparency;
        }
        wr.setPixel(i,j,pix);
      }

    bim.setData(wr);
    image.set(bim);

    WritableRaster testrast = image.getImage().getRaster();
    int[] testpixel = new int[4];
    testrast.getPixel(0,0,testpixel);

    if(image == null)
    {
      System.out.println("load failed for texture " + fileurl);
      System.exit(-1);
    }
    else
    {
      Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight());

      TransparencyAttributes tranattrib = new TransparencyAttributes();
      tranattrib.setTransparencyMode(tranattrib.NICEST);
      tranattrib.setTransparency(1f);

      texture.setImage(0, image);
      texture.setMagFilter(texture.BASE_LEVEL_LINEAR);
      texture.setBoundaryModeS(texture.CLAMP);
      texture.setBoundaryModeT(texture.CLAMP);
      appearance.setTexture(texture);
      appearance.setTransparencyAttributes(tranattrib);
    }

I refer here to Arabian Flights, the open-source Java3D game by Mike Prosser. The appearance is applied to an OrientedShape3D so any blue pixels (0, 0, 255) are transparent.

Any suggestions? ;D

Hi
An easier way would be to use Paint shop pro or some other package like it and convert all your gifs to a format that supports alpha chanels, like PNG, then just use those images in java3d. My AC3D loader (under the resouces section) supports textures with transparency, there is a java3d and a xith3d version and both work, you can check them out if you need to see the source.

Cheers

Endolf

hey, thanks, I’ll take a look, so is it only possible to do this with a extra classes and not within the native java3d classes?

Hi
It’s all done with java3d and ImageIO, the two crucial things are the texture format, and the transparency attributes you use when applying the texture, they should both be in the java3d part of the loader.

HTH

Endolf

Hi
Check line 155 of java3dscene/src/org/newdawn/j3d/loaders/ac3d/tree/ShapeTree.java for where I set the TransparencyAttributes and line 46 in java3dscene/src/org/newdawn/j3d/loaders/resourcepools/TextureResourcePool.java for where I load the textures, it really is quite simple, when you know how :slight_smile:

HTH

Endolf

yeah that’s great, thanks :slight_smile: