Saving an image with alpha channel

Trying to get alpha blending to work, and I have tried in the material file:

AdditionalRenderState {
  Blend Alpha
}

and through JME’s Material

mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);

  1. Which is the same, right?

  2. What’s left is to blame it on the images I use. Anyone know of any easy way to save images with the alpha channel ? Every time I try through photoshop or gimp - it doesn’t work. Even though the image is 32 bit

Am I on the right track? I thought I had it work - and I do in my testcase - which is a simple stand-alone class to test things in. However, in my main project, the assets are in a subproject (gradle) - could that interfere?

I can’t see your image. My photoshop PNGs always have alpha just fine.

…though if you are using jpg then that doesn’t support alpha, obviously.

Basically, we don’t have enough information to help other than to tell you that alpha works fine for the large majority of us.

Ah - well I guess it does work in the image - I can see right here. Ok, so I have to look for something in the code then

So, code to attach spatial:

private Spatial createShip() {
    Quad quad = new Quad(shipSize, shipSize);
    //<-- Move into the material?
    float halfSize = shipSize * 0.5f;
    quad.setBuffer(VertexBuffer.Type.Position, 3, new float[]{-halfSize, -halfSize, 0,
        halfSize, -halfSize, 0,
        halfSize, halfSize, 0,
        -halfSize, halfSize, 0
    });
    //-->
    quad.updateBound();
    Geometry geom = new Geometry("Ship", quad);
    Material mat = assets.loadMaterial("Materials/ShipMaterial.j3m");
    geom.setMaterial(mat);
    return geom;
}

Material:

Material ShipMaterial : MatDefs/StaticSpriteShader.j3md {
     MaterialParameters {
        AniTexMap : Flip Textures/Subspace/ships.gif
        numTilesX : 10
        numTilesY : 32
        numTilesOffsetX : 0
        numTilesOffsetY : 3
     }
    AdditionalRenderState {
      Blend Alpha
      Wireframe Off
    }
}

You might try creating a simple test case that illustrates the issue.

Ah, yes, you also have to set the material parameter “AlphaDiscard” or something like that to at least 0.1, then it should work!

Idk if you even need that blend Mode but since you have an 8bit alpha Channel you have to set the alphadiscardthreshold so when it should consider something invisible

No, guys… alphaDiscardThreshold is only necessary when you have Z-buffer sorting issues.

Presumably these are sprites in the GUI node and are sorted back to front by Z. alphaDiscardThreshold should not be needed.

alphaDiscardThreshold is ONLY about what is/isn’t written to the z-buffer. (gui bucket doesn’t even have a z-buffer, basically)

I was about to say that I have a testcase without alphadiscard or alphadiscardthreshold - which works.

Ah interesting, thanks! I didn’t know that :slight_smile:

What is different in that testcase?

Very little. Here is my testcase:

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad;

public class MainShaderTest extends SimpleApplication {

    public static void main(String[] args) {
        MainShaderTest app = new MainShaderTest();
        app.start();
    }
    Quad quad;
    private Geometry geom;
    private Material mat;
    @Override
    public void simpleInitApp() {
        viewPort.setBackgroundColor(ColorRGBA.Blue);
        quad = new Quad(0.5f, 0.5f);
        
        geom = new Geometry("Bounty", quad);
        mat = assetManager.loadMaterial("Materials/AnimatedBombSpriteShader.j3m");
        mat.setInt("numTilesOffsetY", 10);
        geom.setMaterial(mat);
        geom.setLocalTranslation(0,0,5);
        rootNode.attachChild(geom);
    }
}

With the following material:

Material PriceMaterial : MatDefs/AnimatedBombSpriteShader.j3md {
     MaterialParameters {
        AniTexMap : Flip Textures/Subspace/bombs-newer.gif
        numTilesX : 10
        numTilesY : 13
        TileWidth : 16  
        TileHeight : 16
        Speed : 20
        numTilesOffsetX : 0
        numTilesOffsetY : 0
     }
    AdditionalRenderState {
      Blend Alpha
      Wireframe Off
    }
}

It has the same parameter Blend Alpha

I’ll try moving code bit-by-bit to my test-case from my main, and see if anything breaks the test-case

Ah. missed:

    geom.setQueueBucket(RenderQueue.Bucket.Transparent);

Even though it wasn’t in my testcase :slight_smile:

But I’m still not sure why this is. If I dont have it in my test-case - then I must have done something differently in my main. Oh a tired mind can wonder long.

1 Like

The transparent bucket’s only job is to render things in a particular order. They render after the opaque bucket and they render back to front.

This may be a good refresher on why:

1 Like