Make certain colors in texture transparent

Is it possible to make certain colors in texture transparent, like colorkeying?

Or is it possible to replace a certain color with a pixel with alpha=0?

I want to display a crosshair, but the image has a pink background color which I want to make transparent.

Have tried unsing a png with an alpha channel? Combined with the use of an AlphaState it should give you what you need.

Thank you for answer, but I already have lots of textures and I do not want to change them, I want to solve this programmatically.


Only option that comes to my mind is writing a separate program that takes the original image and color value as inputs and outputs a new texture that has alpha channel added. I do not think you would want to do this inside your main program anyways as it seems like something that should be done only 1-ce, not every time the program is started.

In directX there is an option when loading a texture to automatically replace a certain color with transparency.

Also the DirectX fixed function AlphaState has the possibility to give a color key which pixels are skipped during rendering.

I expected that at least one of these methods would be possible with jme.



The problem is that Iam a total noob when it comes to graphics editors and I do not know how to use a transparent color.

I've heard only a few programs support that.

I'd imagine you'd be able to do it with a pixel shader.

According to this thread you’re on the right track with using the transparent render queue, your problem most likely lies in the test functions.

Thanks for replying, but I did everything as recommended in this thread, I only use blend and not test.

But as I said, if I try to use QUEUE_TRANSPARENT the crosshair disappears, I don't know whether is has then something to do with the Z-Buffer or whatever :frowning:

Hmm… sounds like a good addition to jME's texture utils.

OK, I tried doing the same using the same settings you have posted above in the transparent queue, and it worked. So I think you may need to look elsewhere for your problem. Try a simple test case outside your app and see if it works for you.

I had to deactivate the Zbuffer for that object to get it working with semi-transparency.



crosshair.setRenderQueueMode(Renderer.QUEUE_ORTHO);



ZBufferState zbs = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();

zbs.setEnabled(false);

crosshair.setRenderState(zbs);



thank you all for reply.

Old thread, but I've the same problem.



What I've done so far is this:


      //Do the crosshair:
      DisplaySystem display = DisplaySystem.getDisplaySystem();
        crosshair = new Node("Crosshair");

        Quad q = new Quad("Crosshair quad", display.getWidth() / 20, display.getWidth() / 20);
        crosshair.attachChild(q);

        TextureState ts = display.getRenderer().createTextureState();
        ts.setTexture(TextureManager.loadTexture(JMEGame.class.getClassLoader().getResource("textures/crosshair.png"),
                Texture.MinificationFilter.BilinearNearestMipMap, Texture.MagnificationFilter.Bilinear));
        ts.setEnabled(true);
        q.setRenderState(ts);

        crosshair.setLightCombineMode(LightCombineMode.Off);
       
        BlendState as = display.getRenderer().createBlendState();
        as.setBlendEnabled(true);
        as.setTestEnabled(false);
        as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
        as.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
        as.setEnabled(true);
        crosshair.setRenderState(as);

        crosshair.setRenderQueueMode(display.getRenderer().QUEUE_ORTHO);

        ZBufferState zbs = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
        zbs.setEnabled(false);
        crosshair.setRenderState(zbs);
       
        crosshair.setLocalTranslation(new Vector3f(display.getWidth() / 2, display.getHeight() / 2, 0));

        rootNode.attachChild(crosshair);

        crosshair.updateGeometricState(0, true);
        crosshair.updateRenderState();



A simple copy and paste of the above, made in JME 2. My problem: The crosshair is painted white on black. Neither the white nor the black color is transparent! I don't know why! Has anyone a clue?

Thanks!

is there an alpha channel in the image you are using for the texture?

Yeah… well… you know…



Of course that was the problem! I'm sooo stupid.  Thanks for the tip!!!  :smiley: