webgl2 - 在 WebGL 2 Instancing 中访问 gl_InstanceID

标签 webgl2

我正在尝试在 WebGL 2 中进行实例化。我想使用内置变量 gl_InstanceID 来索引一个统一的 float 组。

我收到以下错误:

glDrawElementsInstancedANGLE:尝试使用具有非零除数的所有属性进行绘制

在 WebGL 2 实例化中是否只允许使用顶点属性(实例化数组)?

此外,是 spec了解这些功能的唯一权威地点?

最佳答案

根据下面的错误报告,该问题似乎已得到修复。这是一个小的工作示例

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>

---旧答案---

spec说它基于 OpenGL ES 3.0 spec

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 不同。 I filed a bug

需要补充的部分是

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.

关于webgl2 - 在 WebGL 2 Instancing 中访问 gl_InstanceID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427286/

相关文章:

javascript - 如何在 iOS 中创建 WebGL 2 渲染器?

google-chrome - WebGL2 支持的显卡

javascript - 创建高效的早期实例剪辑 WebGL2 顶点着色器

c++ - WebGL2和C++上浮点计算的结果不同

google-chrome - Firefox 和 Chrome 之间的 webgl 性能差异

javascript - ND-Buffer 和 G-Buffer 有什么区别?

webgl - 使用 gl.uniform1fv 上传数组的最大大小是多少?

glsl - 来自同一程序的多个输出纹理

javascript - WEBGL-FBO : Why is the DEPTH_COMPONENT texture blank after rendercall