如何有效地在 GLSL ES 2.0 中的许多颜色属性之间进行插值

How effectively interpolate between many color attributes in GLSL ES 2.0

我正在使用 OpenGl ES 2.0 开发项目。我的网格中的每个顶点都有固定数量的颜色属性(比如说 5)。最终的逐顶点颜色计算为两个选定颜色属性之间的插值。

在我的实现中,两种颜色的选择是基于两个给定的索引。我知道 if 语句可能会对性能造成很大影响,因此选择将所有属性放入一个数组中并使用索引来检索所需的颜色。我仍然看到性能显着下降。

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;
}

有没有更好更有效的方法来计算最终的颜色插值?或者至少是选择插值颜色的更好方法?

您在这里使用的索引是统一的。这意味着每个渲染命令中的每个顶点都使用相同的索引。如果是这样的话......你为什么要费心在 VS 中获取这些东西?

您应该只有 2 个颜色输入值。然后,您使用 glVertexAttribPointer 选择将在两者之间进行插值的两个数组。

您的 "significant performance drop" 可能与您获取此类值的方式无关,而与您发送大量从未用于任何用途的逐顶点数据这一事实有关。