在WebGL 2实例中访问gl_InstanceID [英] Access to gl_InstanceID in WebGL 2 Instancing
问题描述
我试图在WebGL 2中进行实例化.我想使用内置变量gl_InstanceID
索引到统一的float数组中.
I am trying to do instancing in WebGL 2. I want to use the built-in variable gl_InstanceID
to index into a uniform float array.
我收到以下错误:
glDrawElementsInstancedANGLE: attempt to draw with all attributes having non-zero divisors
使用顶点属性(实例化数组)的WebGL 2实例化是唯一允许的实例化吗?
Is the only instancing allowed in WebGL 2 instancing with vertex attributes (instanced arrays)?
此外,规范是唯一确定的位置了解这些功能?
Also, is the spec the only definitive place to find out about these capabilities?
推荐答案
基于下面的错误报告,看来此问题已解决.这是一个小的工作示例
Based on the bug report below it looks like this issue was fixed. Here's a small working example
function main() {
const gl = document.querySelector('canvas').getContext('webgl2');
if (!gl) {
return alert('need webgl2');
}
const vs = `#version 300 es
void main() {
float angle = float(gl_InstanceID) / 10.0 * 2.0 * radians(180.0);
float radius = float(gl_VertexID + 1) / 4.0 * 0.8;
gl_Position = vec4(vec2(sin(angle), cos(angle)) * radius, 0, 1);
gl_PointSize = mix(5.0, 20.0, float(gl_VertexID) / 3.0);
}
`;
const fs = `#version 300 es
precision highp float;
out vec4 foo;
void main() {
foo = vec4(1, 0, 0, 1);
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
gl.useProgram(prg);
gl.drawArraysInstanced(gl.POINTS, 0, 4, 10);
}
main();
<canvas></canvas>
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>
规范说,它基于 OpenGL ES 3.0规范
本文档的其余部分旨在与OpenGL ES 3.0规范(撰写本文时为3.0.4,可从 WebGL与OpenGL ES 3.0之间的差异部分.
The remaining sections of this document are intended to be read in conjunction with the OpenGL ES 3.0 specification (3.0.4 at the time of this writing, available from the Khronos OpenGL ES API Registry). Unless otherwise specified, the behavior of each method is defined by the OpenGL ES 3.0 specification. This specification may diverge from OpenGL ES 3.0 in order to ensure interoperability or security, often defining areas that OpenGL ES 3.0 leaves implementation-defined. These differences are summarized in the Differences Between WebGL and OpenGL ES 3.0 section.
不幸的是,他们似乎忘记了指定至少一个属性必须具有与OpenGL ES 3.0不同的非零除数. 我提交了错误
Unfortunately it looks like they forgot to specify that at least one attribute must have a non-zero divisor which is different from OpenGL ES 3.0. I filed a bug
需要添加的部分是
INVALID_OPERATION由DrawArraysInstanced或 DrawElementsInstanced,如果没有启用至少一个 顶点属性数组,其
divisor
为零,并绑定到 绘图命令所用程序中的有效通用属性值.
INVALID_OPERATION is generated by DrawArraysInstanced or DrawElementsInstanced if there is not at least one enabled vertex attribute array that has a
divisor
of zero and is bound to an active generic attribute value in the program used for the draw command.
这篇关于在WebGL 2实例中访问gl_InstanceID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!