I need to get sprite center in world coordinates inside shader. How can i get it?
I make a shader that tint sprites, using light map texture.World consist of cells, every cell have color. Something like this.
When sprite is fully inside cell, everything ok. But when sprite bigger then cell, or near the cell border.It use two diffetent lights color.And it looks bad.
My idea is use center of sprite to find it light color. But i can’t get center in shader.
I can’t use sprite.set_constant(). Because it will be break batching.
Any ideas?
Example
vertex
uniform mediump mat4 view_proj;
// positions are in world space
attribute mediump vec4 position;
attribute mediump vec2 texcoord0;
varying mediump vec2 var_texcoord0;
varying mediump vec4 pos;
void main()
{
gl_Position = view_proj * vec4(position.xyz, 1.0);
pos = position; // send world position to fragments
var_texcoord0 = texcoord0;
}
fragment
varying mediump vec2 var_texcoord0;
varying mediump vec4 pos;
uniform lowp sampler2D DIFFUSE_TEXTURE;
uniform lowp sampler2D LIGHT_MAP_TEXTURE;
uniform mediump vec4 fog_color;
uniform mediump vec4 light_map;
uniform mediump vec4 fog; //x start distance y end dist.
void main()
{
float dist = gl_FragCoord.z/gl_FragCoord.w;
vec4 spriteColor = texture2D(DIFFUSE_TEXTURE, var_texcoord0.xy);
if(spriteColor.a < 0.01){discard;}
//get light color use world positions
vec4 lightColor = texture2D(LIGHT_MAP_TEXTURE, vec2((pos.x+0.00001)/light_map.x,(pos.z+0.00001)/light_map.y));// multiply to fix wall on cell borders
vec3 color = spriteColor.rgb * lightColor.rgb;
float f = 1.0 /exp((dist-fog.x) * fog.z);
f = clamp(f, 0.0, 1.0);
vec3 total_color = (1.0-f) * fog_color.rgb + f * color.rgb;
total_color = mix(vec3(0.0),total_color, f);
gl_FragColor = vec4(total_color,spriteColor.a);
}