Author Topic: "Transparent" texture clamping (what ?)  (Read 1278 times)

Offline atreyu64

  • byte
  • *
  • Posts: 44
    • View Profile
"Transparent" texture clamping (what ?)
« on: October 14, 2014, 09:51:29 pm »
Hi,


I'm trying to split an object texture into 2x2 parts without modifying the mesh, just by re-asigning UVs.
The problem is about the "border" polygons (which overlap several parts, so they may have several textures), because the result is ugly either with clamping or repeating textures.

Is there a way to make a kind of "invisible" clamping, meaning that UVs outside [0,1] range would be completely transparent ?

I hope what I'm saying make a sens...  :o


Thanks in advance, cheers !

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: "Transparent" texture clamping (what ?)
« Reply #1 on: October 14, 2014, 09:54:35 pm »
Do you have a screen shot that shows the problem? I'm not entirely sure that i really got it...

Offline atreyu64

  • byte
  • *
  • Posts: 44
    • View Profile
Re: "Transparent" texture clamping (what ?)
« Reply #2 on: October 14, 2014, 11:07:08 pm »
Sure :


texture2x2 by atreyu64, on Flickr

Offline atreyu64

  • byte
  • *
  • Posts: 44
    • View Profile
Re: "Transparent" texture clamping (what ?)
« Reply #3 on: October 14, 2014, 11:11:02 pm »
Sorry, I forgot the comment : this is the center of the model, where the four regions meet.
We can see the repeating textures on polygons that intersect several regions. These bad polygons have actually several textures, but we can only see one because I use TextureInfo.MODE_REPLACE.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: "Transparent" texture clamping (what ?)
« Reply #4 on: October 15, 2014, 10:13:26 pm »
I see...i had a similar problem while thinking about splitting the terrain in my rpg into square parts to minimize texture accuracy problems on mobile GPUs. I wish that i could tell that i found a solution...but i didn't. At least none that i wanted to implement. One solution would be to clip these border polygons to the edges, making 2-3 polygons out of what is currently 1. That should do it and it shouldn't be too hard to implement, but i couldn't be bothered.
Your idea with transparency might work as well if there were such an option. And there actually is, it's just not a very good one in some cases: You could write a shader that does this operation in the fragment part. The problem is that this might affect performance, at least on slower/mobile gpus. It might be worth a try though.

Offline atreyu64

  • byte
  • *
  • Posts: 44
    • View Profile
Re: "Transparent" texture clamping (what ?)
« Reply #5 on: October 15, 2014, 10:38:39 pm »
Ok, thanks for your answer.
I think I'll finally add some polygons as you suggested.

I've made a simple workaround which works fine in my case : the 4 small textures actually cover a slightly bigger area than a quarter of the orignal texture, so as the UVs won't be outside [0,1] range.
It looks basically like this :


 
Code: [Select]
float reductionPercent; // example : 0.9f if the original image has been reduced at 90% to solve side effects (and then cut into 4)
float ratio = 2 * reductionPercent;
float margin = 1 - ratio/2;

[...]

for(int i=0; i<nbTriangles; i++) {

// read polygon UV :
SimpleVector uv1 = polygonMgr.getTextureUV(i, 0);
SimpleVector uv2 = polygonMgr.getTextureUV(i, 1);
SimpleVector uv3 = polygonMgr.getTextureUV(i, 2);
u1 = uv1.x;
v1 = uv1.y;
u2 = uv2.x;
v2 = uv2.y;
u3 = uv3.x;
v3 = uv3.y;

boolean west = u1 < 0.5;
boolean south = v1 < 0.5;
if(west) {
u1bis = u1 * ratio;
u2bis = u2 * ratio;
u3bis = u3 * ratio;
}
else {
u1bis = (u1 - 0.5f) * ratio + margin;
u2bis = (u2 - 0.5f) * ratio + margin;
u3bis = (u3 - 0.5f) * ratio + margin;
}
if(south) {
v1bis = v1 * ratio;
v2bis = v2 * ratio;
v3bis = v3 * ratio;
}
else {
v1bis = (v1 - 0.5f) * ratio + margin;
v2bis = (v2 - 0.5f) * ratio + margin;
v3bis = (v3 - 0.5f) * ratio + margin;
}
if(west && south) texID = textureID1;
else if(west && !south) texID = textureID2;
else if(!west && south) texID = textureID3;
else if(!west && !south) texID = textureID4;

polygonMgr.setPolygonTexture(i, new TextureInfo(texID, u1bis, v1bis, u2bis, v2bis, u3bis, v3bis));
}



This can only be used with 2x2 textures (not NxN), but is usefull to bypass texture maximum size on terrain (epecially on Android...)