Glsl 如果禁用VertexAttributeArray,WebGL 2.0 Integer属性将引发类型错误

Glsl 如果禁用VertexAttributeArray,WebGL 2.0 Integer属性将引发类型错误,glsl,webgl,Glsl,Webgl,我只是花了几个小时来隔离WebGL的一个行为,我不明白 让我们假设一个具有整数属性的顶点着色器: precision highp int; in vec4 vtx_pos; in vec3 vtx_nrm; in ivec4 vtx_int; // <- integer attribute void main() { // blah blah... } 精度高int; 在vec4 vtx_位置; 在vec3中,vtx_nrm; 在ivec4 vtx_int;// 引用德国劳埃德船

我只是花了几个小时来隔离WebGL的一个行为,我不明白

让我们假设一个具有整数属性的顶点着色器:

precision highp int;
in vec4 vtx_pos;
in vec3 vtx_nrm;
in ivec4 vtx_int; // <- integer attribute

void main() {
   // blah blah...
}
精度高int;
在vec4 vtx_位置;
在vec3中,vtx_nrm;

在ivec4 vtx_int;// 引用德国劳埃德船级社ES 3.0§2.8:

如果插槽索引处着色器属性的基本类型不是浮点型(例如有符号或无符号整数),则结果属性值未定义

WebGL 2.0

所以,默认情况下,非数组属性的值实际上是一个浮点向量。如果着色器中的实际属性是无符号整数值上的整数,则应通过调用
vertexAttribI4*
,手动指定其值,即:


谢谢现在我想知道是否最好使用一个需要一些特殊过程的
ivec4
属性,或者我是否应该将其保留为标准的
vec4
,然后在着色器中将浮点值强制转换为整数。。。(ivec4用于数组下标)@Sedenion如果精度不成问题,那么我相信没有技术理由选择一个而不是另一个。但是,使用
ivec4
的实现可能更简洁:)
var p = gl.createProgram();

gl.bindAttribLocation(p, 0, "vtx_pos");
gl.bindAttribLocation(p, 1, "vtx_nrm");
gl.bindAttribLocation(p, 2, "vtx_int"); // <- ivec4 on array #2
gl.vertexAttribPointer(0, 4, gl.FLOAT, false, 28, 0);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 28, 16);
gl.enableVertexAttribArray(1);
gl.disableVertexAttribArray(2); // <- here is the devil
gl.vertexAttribI4i(2, 0, 0, 0, 0);