nablevertexattribarrayarray索引超出范

nablevertexattribarrayarray索引超出范

这是我的顶点和片段着色器:

    <script id="shader-fs" type="x-shader/x-fragment">
        precision mediump float;

        uniform sampler2D uSampler;

        varying vec4 vColor;
        varying vec2 vTextureCoord;

        void main(void) {
            gl_FragColor = vColor;
            // gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        }
    </script>
    <script id="shader-vs" type="x-shader/x-vertex">
        attribute vec3 aVertexPosition;
        attribute vec4 aVertexColor;
        attribute vec2 aTextureCoord;

        uniform mat4 uMVMatrix;
        uniform mat4 uPMatrix;

        varying vec4 vColor;
        varying vec2 vTextureCoord;

        void main(void) {
          gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
          vColor = aVertexColor;
          // vTextureCoord = aTextureCoord;
        }
    </script>

这是我的着色器初始值设定项:
function initShaders() {
  var fragmentShader = getShader(gl, "shader-fs");
  var vertexShader = getShader(gl, "shader-vs");

  shaderProgram = gl.createProgram();

  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);

  if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
      alert("Could not initialise shaders");
  }
  gl.useProgram(shaderProgram);

  shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

  shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
  gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

  shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
  gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);

  shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
}

错误来自此行:
  gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
  >> enablevertexattribarray index out of range

我该如何处理?

最佳答案

那仅仅是因为您不在顶点程序中使用aTextureCoord,所以GLSL编译器通过删除它来对其进行优化。您确实应该检查gl.GetAttribLocation()的结果是否有错误,并仅启用程序中存在的属性。在缺少属性的情况下发出警告就足够了,我知道没有办法将着色器创作错误与编译器的优化区分开。

关于html - WebGL:enablevertexattribarrayarray索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17313685/

10-12 00:08