简体   繁体   中英

How effectively interpolate between many color attributes in GLSL ES 2.0

I'm working on project with OpenGl ES 2.0. Every vertex in my mesh has fixed number of color attributes (lets say 5). The final per-vertex color is computed as an interpolation between two selected color attributes.

In my implementation, choice of the two colors is based on two given indexes. I'm aware that if statement may by a big performance hit so a choose to put all attributes into one array and use indexing to retrieve wanted colors. Still i see a significant performance drop.

attribute vec4 a_position;
//The GLSL ES 2.0 specification states that attributes cannot be declared as arrays.
attribute vec4 a_color;
attribute vec4 a_color1;
attribute vec4 a_color2;
attribute vec4 a_color3;
attribute vec4 a_color4;

uniform mat4 u_projTrans;
uniform int u_index;
uniform int u_index1;
uniform float u_interp;

varying vec4 v_color;


void main()
{
   vec4 colors[5];
   colors[0] = a_color;
   colors[1] = a_color1;
   colors[2] = a_color2;
   colors[3] = a_color3;
   colors[4] = a_color4;

   v_color = mix(colors[u_index], colors[u_index1], u_interp);

   gl_Position =  u_projTrans * a_position;
}

Is there a better more efficient way of computing the final color interpolation? Or at least a better way to choose interpolation colors?

The indices you use here are uniforms. That means every vertex in each rendering command uses the same indices. If that's the case... why do you bother fetching this stuff in the VS at all?

You should only have 2 color input values. You then use glVertexAttribPointer to pick the two arrays that will be interpolated between.

Your "significant performance drop" likely has nothing to do with how you fetch such values, and everything to do with the fact that you're sending lots of per-vertex data that never gets used for anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM