【问题标题】:How effectively interpolate between many color attributes in GLSL ES 2.0如何有效地在 GLSL ES 2.0 中的许多颜色属性之间进行插值
【发布时间】:2024-01-01 07:47:01
【问题描述】:

我正在使用 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;
}

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

【问题讨论】:

    标签: opengl-es glsl glsles


    【解决方案1】:

    您在此处使用的索引是制服。这意味着每个渲染命令中的每个顶点都使用相同的索引。如果是这样的话……你为什么还要费心在 VS 中获取这些东西呢?

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

    您的“显着性能下降”可能与您获取此类值的方式无关,而与您发送的大量每个顶点数据从未被用于任何事情这一事实有关。

    【讨论】:

    • 你说得对,我没有同时使用所有属性。但是网格目前是作为顶点缓冲区对象完成的。使用顶点数组时不会有相当大的开销吗?我的用例是相当静态的。创建网格,使用制服为其设置动画,在不需要时将其销毁。
    • @plastique: "但是网格目前是作为顶点缓冲区对象完成的。使用顶点数组时不会有相当大的开销吗?" ...你 使用顶点数组。你认为你在缓冲区对象中存储了什么?我没有建议你应该使用 client-side 顶点数组。
    最近更新 更多