I see that Defold generates vertex buffers every frame to batch the draw of sprites and meshes/models with materials of the “World” vertex space. The different values of uniform constants break these batches.
I think that it will be useful to have material constants that Defold adds as attributes of vertices. Yes, the engine will generate a bit more data to draw sprites. But the different alpha values are not going to break batches anymore!
So, those new material constants can have float
and vec4
data types. It’ll be enough.
How can look a modified sprite.vp
/sprite.fp
, without tint
constant:
uniform highp mat4 view_proj;
// positions are in world space
attribute highp vec4 position;
attribute mediump vec2 texcoord0;
attribute lowp float alpha; // ADDED
varying mediump vec2 var_texcoord0;
varying lowp float var_alpha; // ADDED
void main()
{
gl_Position = view_proj * vec4(position.xyz, 1.0);
var_texcoord0 = texcoord0;
var_alpha = alpha; // ADDED
}
varying mediump vec2 var_texcoord0;
varying lowp float var_alpha; // ADDED
uniform lowp sampler2D texture_sampler;
void main()
{
gl_FragColor = texture2D(texture_sampler, var_texcoord0.xy) * var_alpha; // MODIFIED
}