Fragment Shader get texture size

Hi,

I am newbie with shaders and I am trying to make a simple texture division, and for it I make this example.

Look at the trees color and from middle to left is dark and from middle to right is a default texture. If you look the code you will find a hard code to cut the middle like if(var_texcoord0.y > 0.68), this 0.68 was a suggestion for this image, but I need implement this effect in others image, to do it I need the middle of texture. Someone have some tips for this solution?
Well, my question is about how can I get dynamically this middle point…

File: tree.vp

uniform mediump mat4 view_proj;
uniform mediump mat4 world;

attribute mediump vec4 position;
attribute mediump vec2 texcoord0;

varying mediump vec2 var_texcoord0;
varying mediump vec3 var_view_proj0;

void main()
{
    gl_Position = view_proj * vec4(position.xyz, 1.0);
    var_texcoord0 = texcoord0;
    vec3 direction = vec3(0.0);
	vec3 transformed_direction = vec3(view_proj * vec4(direction, 0.0));
    var_view_proj0 = transformed_direction;
}

File: tree.fp

varying mediump vec4 position;
varying mediump vec2 var_texcoord0;
varying mediump vec3 var_view_proj0;

uniform lowp sampler2D DIFFUSE_TEXTURE;

void main()
{
    
    lowp vec4 color = texture2D(DIFFUSE_TEXTURE,var_texcoord0.xy).rgba;
    if(var_texcoord0.y > 0.68){
    	color.x = color.x*0.3;
    	color.y = color.y*0.3;
    	color.z = color.z*0.3;
    }
    
    gl_FragColor = color;
}

Thanks advance
Victor C Tavernari

You can send your midpoint as a uniform instead of defining it as a fixed 0.68

uniform float midpoint;

Afterwards, you need to add it as a CONSTANT_TYPE_USER in your material.

You can then change it with sprite.set_constant() for each sprite.

3 Likes

Thanks…

I am learning about shaders and I have some difficult to understand the ways to resolve problems, like this simple solution.

I will implement it.