Question

i'm trying simple texture splatting on ios opengl es 2.0 (ipad). I have 4 tiled textures in pvrt compressed atlas (2x2 tiles). 4 single textures on 4 texture units was terribly slow.

vertex shader:

attribute lowp vec4 position;
attribute lowp vec2 tex0;
varying lowp vec2 surfCoord;
uniform mat4 projection_modelview;
uniform lowp float uv_coef;

varying lowp vec2 texCoord1;
varying lowp vec2 texCoord2;
varying lowp vec2 texCoord3;
varying lowp vec2 texCoord4;

void main()
{
  gl_Position = projection_modelview * position;
  vec2 texCoord = fract(vec2(position.x / uv_coef, position.y / uv_coef));
  texCoord1 = texCoord * 0.5;
  texCoord2 = texCoord1 + vec2(0.5, 0);
  texCoord3 = texCoord1 + vec2(0, 0.5);
  texCoord4 = texCoord1 + vec2(0.5, 0.5);
  surfCoord = tex0;
}

fragment shader:

uniform sampler2D texture0;    // surface alpha map
uniform sampler2D texture1;    // atlas
varying lowp vec2 surfCoord;
varying lowp vec2 texCoord1;
varying lowp vec2 texCoord2;
varying lowp vec2 texCoord3;
varying lowp vec2 texCoord4;

void main()
{
  lowp vec4 surfTexel = texture2D(texture0, surfCoord);
  lowp vec4 texel1 = texture2D(texture1, texCoord1);
  lowp vec4 texel2 = texture2D(texture1, texCoord2);
  lowp vec4 texel3 = texture2D(texture1, texCoord3);
  lowp vec4 texel4 = texture2D(texture1, texCoord4);

  texel1 *= surfTexel.r;
  texel2 = mix(texel1, texel2, surfTexel.g);
  texel3 = mix(texel2, texel3, surfTexel.b);
  gl_FragColor = mix(texel3, texel4, surfTexel.a);
}

shows this one:

a surface
(source: inputwish.com)

My problem is probably texture unit interpolators but i don't how to resolve. I don't see mistake in my shaders. Any advice please?

Was it helpful?

Solution

mistake is using fract in vertex shader. it's too early. It should be in fragment shader:

lowp vec4 texel1 = texture2D(texture1, fract(texCoord) * 0.5);
lowp vec4 texel2 = texture2D(texture1, fract(texCoord) * 0.5 + vec2(0.5, 0.0));
lowp vec4 texel3 = texture2D(texture1, fract(texCoord) * 0.5 + vec2(0.0, 0.5));
lowp vec4 texel4 = texture2D(texture1, fract(texCoord) * 0.5 + vec2(0.5, 0.5));
... and mix them together..

anyway it's slow because dependent texture reads on ipad ios.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top