A while ago, @spacemonkey made a game example of using his LitMesh class (URL: http://twolivesleft.com/Codea/Talk/discussion/2379/ads-lighting-class#Item_1), which can add light sources & bump maps for a texture or 3-D mesh. I’ve been coding away, and I’ve got a game almost ready for alpha testing. It’s going to be a small game called “Robot Explorer,” where you can run around through randomly generated worlds exploring structures. I want to add 3-D objects, though, and I’ve ran across a problem. I made a test class called Object, which basically took an image, and rotated it 360 degrees while rendering to make it appear a solid object. I was testing this with the Platformer Art’s “Grass” texture, and when it was rendering it, the background appeared black. Where there was transparency in the image, there was just blackness, which looked really weird. Does anybody know how you could make a modification to the LitMesh class to add transparency support? LitMesh class: https://gist.github.com/sp4cemonkey/5112117
By the way, once I can get objects in, I’ll probably be able to post the code for everyone to try out!
You may be running into the problem where OpenGL treats transparent pixels as solid, so if you draw your grass before the background, OpenGL will not draw the background behind the (already drawn) transparent pixels in the grass.
I have written quite a lot about this, starting here
I think there are two solutions. One is to adjust the LitMesh class to include a shader that discards transparent pixels rather than drawing them. But this may leave an edge round your grass. Better is to sort your meshes so you draw them from furthest to nearest, which is something you can do yourself. I show how to do this in a later post on my blog above.
Having a quick look at litMesh it doesn’t handle alpha (transparency) properly. An issue with where I was at in my learning when I wrote it. I’ll fix it tonight.
Also, what Ignatz says is right, if you have loads of transparent objects on top of each other, you must order them by the distance from camera and draw from furthest to nearest to get something approximately right. If you only have a small number of transparent objects in a largely opaque world, and the transparents don’t overlap much, then you can just ensure you draw all the opaque stuff first, and the transparent object last.
But currently that still won’t work with LitMesh, because I mess up the alpha…
Maybe it is not the cause of your current problem, but remember that importing images via the ipad camera roll loses transparency (it is turned to black). To keep transparency you must import your png via http request (or iexplorer)
I just figured out how to add alpha support! After fiddling around with alpha checks for a while and getting lots of errors, I found one line of code that fixes it.
if (texture2D( texture, vTexCoord ).a != 1.0) discard;
Just add that into the Fragment shader up at the top, and it won’t render any pixels that have transparency. Thanks for the help anyway, though. My game will be ready for alpha testing soon!
I’ve updated litMesh to work with transparency. @SkyTheCoder by adding that line, you can ignore z-order issues, but any pixel even partially transparent get’s discarded, so you can’t have partially transparent. Also, some of the pixels on the edges of your textures will be interpolated potentially, so you are possibly trimming a few pixels off.
Nothing wrong with that, if you desired effect is just gaps, but the new litMesh (same location) will also let you have translucent objects, as long as you worry about depth order drawing.
Yeah, I noticed that. I’m gonna try finding a better transparency cutoff later, but for now it’s good enough. I also need to work on texture coordinates… Anyway, if you want to see what I’ve been working on, I’m gonna post a preview video in a few seconds. I’ll release the sourcecode later, but for now I have over 1,900 lines of code, last time I checked.