Problem with alpha channel of code-created texture [SOLVED]

Hi monkeys,

right now I’m creating essentially a texture that needs to be transparent except for the borders. So I produce a rectangle, so to speak. The code is:

[java] int grid_res = 32;

ByteBuffer buf = ByteBuffer.allocateDirect(grid_resgrid_res4);

for (int y = 0; y < grid_res; y++) {

for (int x = 0; x < grid_res; x++) {

if (x == 0 || y == 0 || x == grid_res-1 || y == grid_res-1) {

buf.put((byte) 255);

buf.put((byte) 0);

buf.put((byte) 0);

buf.put((byte) 0);

} else {

buf.put((byte) 0);

buf.put((byte) 0);

buf.put((byte) 0);

buf.put((byte) 255);

}

}

}

gridImage = new Texture2D(new Image(Image.Format.RGBA8, grid_res, grid_res, buf));

[/java]

So conceptually I check whether I am at the border and insert a red pixel or else I insert a transparent one. The border itself comes out right but the area which is supposed to be transparent is just pitch black. I don’t see the mistake :confused:

i’ve never done what your trying but try RGBA32 (if it even exists), just a shot in the dark

Hey,



RGBA8 is correct already. It tells that every color channel is 8 bits, so all in all it’s 4 bytes.

I assume the material has its blend mode set to alpha?

Hi,

I use the standard TerrainLighting and it works perfectly when I load the texture from an image. Look here:



When I create the texture myself like above, I get this (here I changed it to green color, so don’t be confused :wink: ):



As you can see, in the above case the terrain shines through, in the second case there is no transparency for the black areas.



This is what the loaded image looks like. The white area is transparent and I just want to recreate this with the above code:

A quick glance at your code and it looks like the red pixel is transparent.

Are you generating this image and using it as a texture layer in Terrain.j3md? And is it supposed to overlay on top of the terrain as one of the texture layers?



And as Paul said, if you want to see through the terrain, the blend mode must be set to alpha.

Hi Sploreg,



I shoved my grid texture as the (unused) 16th sampler2d in the TerrainLighting.frag. I just extended the diffuseColor calculation with the following line

[java] diffuseColor = diffuseColor*texture2D(m_GridMap, texCoord * m_GridMap_scale);

[/java]

I use it as an overlay to display all the tiles of my terrain which I can click etc. Isn’t the blend mode already set to alpha? As I said: when I load the image from a file, it works. When I create the image myself, it fails. AFAIK an alpha value of 0 is opaque, 255 is completely transparent. Why do you think that the red color is transparent? I push 255 0 0 0 into the byte buffer which corresponds to opaque red, not?

0 is transparent

Hmm, doesn’t change a thing. Is the line in the shader maybe not correct? What I also realized (maybe that’s what you meant) is that in the image, the green color is actually transparent, i.e. you can see the terrain shining through. Here a screenshot with a lower resolution grid image:

Possibly transparent there, or maybe just a normal map having an effect on it (if you are using normal maps). Hard to tell.

I don’t think there is anything wrong with your line of shader code, the problem appears to be the image you are producing.



Something is definitely odd since your image had 0 alpha but still showed green, when it should have been transparent and no color.

Haha lol,



look at this (I abbreviated the code by putting ints instead of bytes, same effect):

[java]

if (x == 0 || y == 0 || x == grid_res-1 || y == grid_res-1) {

buf.putInt(Integer.parseInt(“00FF00FF”,16));

} else {

buf.putInt(Integer.parseInt(“00FF0000”,16));



}

[/java]



So I just put the same color and only change the alpha value. Look at the result :smiley: The alpha value has no effect! BTW I don’t use normal maps. It’s definitely the diffuse maps:



Allright, got it. It was the shader. I changed the line to



[java]

vec4 grid = texture2D(m_GridMap, texCoord * m_GridMap_scale);

diffuseColor.rgb = mix( diffuseColor, grid, grid.a ).rgb;

[/java]



So my idea was right, the way I wrote it was wrong. I suck at shaders :stuck_out_tongue:







Kudos to everyone helping me! :slight_smile: