2

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.

I get the following error:

glDrawElementsInstancedANGLE: attempt to draw with all attributes having non-zero divisors

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?

4

1 回答 1

4

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

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,可从Khronos OpenGL ES API Registry获得)一起阅读。除非另有说明,否则每种方法的行为由 OpenGL ES 3.0 规范定义。该规范可能与 OpenGL ES 3.0 不同,以确保互操作性或安全性,通常定义 OpenGL ES 3.0 让实现定义的区域。这些差异在 WebGL 和 OpenGL ES 3.0 之间的差异部分进行了总结。

不幸的是,他们似乎忘记指定至少一个属性必须具有与 OpenGL ES 3.0 不同的非零除数。我提交了一个错误

需要添加的部分是

divisorINVALID_OPERATION 由 DrawArraysInstanced 或 DrawElementsInstanced 生成,如果在用于绘制命令的程序中没有至少一个启用的顶点属性数组具有零并且绑定到活动的通用属性值。

于 2017-06-09T02:48:30.083 回答