Adding transparency to a user supplied object?

Hello all,

Lets say I am allowing users to provide their own textures or models, but they aren’t transparent, am I able to make them transparent from within code, without having to alter each image in photoshop or another graphical program?

There is a program called “Lead Tools” that allows you to select a color and it will alter an image to go transparent from that color, just curious if there is something like that, that can be done as well?

Thanks!

You could do that with ImageRaster or using the ImagePainter plugin - just scan the image and modify the alpha channel based on the colour.

Or do it with a custom shader.

Depending on what you want.

1 Like

Thanks for the help, in my case I think all I would need is to work a texture with most likely a white outer area that needs to turn transparent.

I will look into each of those, probably wont need a shader, but not too sure about it either.

Is there a way you would recommend more, or should I try them all to see which would be best in my case?

Look at ImageRaster first since it’s in core. ImagePainter is available as a plugin and adds more advanced features to ImageRaster.

I’d leave the shader idea for now.

1 Like

Thanks, I was going to reply back earlier and say that ImageRaster seems ot be what I would need, ImagePainter seems to be for more advanced projects. I’ve seen ImagePainter before, it’s really cool for sure!

Thank you for the help!

image painter is a wrapper for image raster, it gives you a lot of convenience methods to quickly do common things.

I have another question for this, sorry it has been awhile.

Now when we create these edited images the way I see it is that I would have to then SAVE every one of these images again, which would slow things down and have a conversion time.

If not how does that work(would the ImageRaster changes save the image instantly)?

I’m curious also if we could use that Image we created internally and use it as an Asset? It seems from what I see that assets have to be grabbed directly from a file on the system? Can we set an asset internally?

Basically I have some images which are an exotic format, so I have to rewrite them as a png since I want transparency.

The thing is I want to turn these files into a .J3O(another thead here http://hub.jmonkeyengine.org/forum/topic/question-about-converting-user-submitted-textures-into-j3m-files/) so saving these images as a png is wasteful, so if I could internally change it(which I’m not too sure I can, but I would assume it’s possible) without having to save the files, then use the ImageRaster to add the transparency, and finally create an asset out of it that would be great!

Thanks!

How are you reading the “exotic format”? Maybe you can just write an adapter for asset manager.

@pspeed said: How are you reading the "exotic format"? Maybe you can just write an adapter for asset manager.

Well you were in the other thread as well, but I was reading them in normallyh, then writing them out as a png file. Since I figured I would save time (which you told me the most time I’m spending is converting them) if I am able to load in an asset from an image I’ve loaded in and changed with the ImageRaster.

How does the adapters work?

Thanks as always pspeed.

Can you show me code for how you are reading the images when/before you convert them? There may be no need to convert them if you can just read them natively… depending on how fast that is.

@pspeed said: Can you show me code for how you are reading the images when/before you convert them? There may be no need to convert them if you can just read them natively... depending on how fast that is.

"

I basically buffer it in, write it out into a new file, make my material, then pass the info into another method that creates the boxes.

s = s.substring(0, s.length()-2);

BufferedImage i1 = null;
BufferedImage i2 = null;
BufferedImage i3 = null;
try
{
i1 = ImageIO.read(new File(s.concat(“.1″)));
i2 = ImageIO.read(new File(s.concat(“.2″)));
i3 = ImageIO.read(new File(s.concat(“.3″)));

}
catch(IOException e) {}

File f1 = new File(s.concat(“.1.png”));
File f2 = new File(s.concat(“.2.png”));
File f3 = new File(s.concat(“.3.png”));

ImageIO.write(i1,”png”,f1);
ImageIO.write(i2,”png”,f2);
ImageIO.write(i3,”png”,f3);

Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
Material mat2 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
Material mat3 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat1.setTexture(“ColorMap”, assetManager.loadTexture(f1.getName()));
mat2.setTexture(“ColorMap”, assetManager.loadTexture(f2.getName()));
mat3.setTexture(“ColorMap”, assetManager.loadTexture(f3.getName()));

Node node = multiFace(i,j,mat1,mat2,mat3);
node.rotate(new Quaternion().fromAngleAxis(FastMath.DEG_TO_RAD*-180, new Vector3f(1,0,0)));
n.attachChild(node);

"

This is how I was doing it originally.

Now I want to read the image in, do the imageRaster changes needed, and then if possible internally convert and add the image as a material/asset. Then I want to create my atlas, and then the .J3O object to REUSE. So in reality the only thing I need to save is the .J3O I want to keep, and the images wont really be needed anymore, so saving a bunch of png or jpg files are kind of useless and a waste imo.

EDIT: I found this in the save and load tutorial

“Note that when you save a model that has textures, the references to those textures are stored as absolute paths, so when loading the j3o file again, the textures have to be accessible at the exact location (relative to the assetmanager root, by default the assets directory) they were loaded from. This is why the SDK manages the conversion on the project level.”

So wouldn’t this mean that I would have to save all of the files to readable formats then? I would like to hear what this adapter is, because I couldn’t find anything yet on it.

Does it take longer to read your special format than it does to read a PNG?

Otherwise, why bother saving as a PNG? Is it just to be compatible with JME? (Because an AssetLoader would do that, too.) Or is it something funky about the extensions or something? (If so, then can the user change them to something sensible?)

Either way, you can go directly from ImageIO loaded images right to JME images without writing them to files first.

@pspeed said: Does it take longer to read your special format than it does to read a PNG?

Otherwise, why bother saving as a PNG? Is it just to be compatible with JME? (Because an AssetLoader would do that, too.) Or is it something funky about the extensions or something? (If so, then can the user change them to something sensible?)

Either way, you can go directly from ImageIO loaded images right to JME images without writing them to files first.

When I asked previously it seemed I could only load in png or jpg for textures, so if there is a way to do it and be compatible with JME as you said I would like to take that route. Converting is NOT needed, compatibility is though :).

The users get these images in these formats, they wont change them I’m sure, so I have to be the one to either work with the files they give me,which can possibly be a jpg or png as well.

How do I go about reading Images into the assetManager directly, and as I posted above will I be able to work with them when I save them as a .J3O when it uses the “Absolute Path?”

Thanks as always.

What is the .j3o saving requirement for? Why do you need to save the models?

Anyway, for a test you can try just not loading them with asset manager in the first place. Take the BufferedImages and pass them to:
http://hub.jmonkeyengine.org/javadoc/com/jme3/texture/plugins/AWTLoader.html#load(java.awt.image.BufferedImage,%20boolean)

And actually, that class may be able to just load them directly, too. It’s other load method seems like it could take an InputStream and then is already using ImageIO to read them:
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java#185

Do these files have an identifiable extension? It could just be a matter of making sure it’s mapped to AWTLoader in AssetManager.

@pspeed said: What is the .j3o saving requirement for? Why do you need to save the models?

Anyway, for a test you can try just not loading them with asset manager in the first place. Take the BufferedImages and pass them to:
http://hub.jmonkeyengine.org/javadoc/com/jme3/texture/plugins/AWTLoader.html#load(java.awt.image.BufferedImage,%20boolean)

And actually, that class may be able to just load them directly, too. It’s other load method seems like it could take an InputStream and then is already using ImageIO to read them:
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/desktop/com/jme3/texture/plugins/AWTLoader.java#185

Do these files have an identifiable extension? It could just be a matter of making sure it’s mapped to AWTLoader in AssetManager.

Well basically since I am going to be creating boxes using the possibly 3 to 6 images provided, make a texture atlas, and then create and save the objects(I figured it would take time creating the atlas and such so loading the .J3O would save time)? I also would like to save my entire scene into .j3O files as well since it seems to also load much faster if I did an entire scene.

I tried to use Files.probeContentType(); on these images and I get null. These files extensions are usually .1 -.6 to denote sides, if that is what you were asking.

Thanks for the links by the way, I didn’t realize there were so many classes that implemented assetloader.

@KonradZuse said: Thanks for the links by the way, I didn't realize there were so many classes that implemented assetloader.

Note: for future referent, the javadoc will tell you stuff like this.

In general, if you don’t have the JME javadocs already open or at least one click away in your mind, then you are coding with one hand tied behind your back.

1 Like