WebGl "ERROR: unsupported shader version"

WebGl "ERROR: unsupported shader version"

我刚开始使用 webgl 并正在关注此 tutorial,但我 运行 收到一条奇怪的错误消息。 ERROR: unsupported shader version。 VertexShader 看起来像这样:

 var vertexShaderSource = `#version 300 es

  // an attribute is an input (in) to a vertex shader.
  // It will receive data from a buffer
  in vec4 a_position;

  // all shaders have a main function
  void main() {

    // gl_Position is a special variable a vertex shader
    // is responsible for setting
    gl_Position = a_position;
  }
  `;

片段着色器如下所示:

 var fragmentShaderSource = `#version 300 es

  // fragment shaders don't have a default precision so we need
  // to pick one. mediump is a good default. It means "medium precision"
  precision mediump float;

  // we need to declare an output for the fragment shader
  out vec4 outColor;

  void main() {
    // Just set the output to a constant reddish-purple
    outColor = vec4(1, 0, 0.5, 1);
  }
  `;

然后使用以下函数将它们转换为着色器,然后记录上述错误:

function createShader(gl,type,source) {
  var shader = gl.createShader(type);
  gl.shaderSource(shader,source);
  gl.compileShader(shader);
  var success = gl.getShaderParameter(shader,gl.COMPILE_STATUS);
  if(success) {
    return shader;
  }
  console.log(gl.getShaderInfoLog(shader));
  gl.deleteShader(shader);
}

我真的希望你能帮助我,在此先感谢。

如果你想使用GLSL ES 3.00 shaders, then you have to create a WebGL 2.0上下文。

参见 HTMLCanvasElement.getContext()。例如:

var ctx = canvas.getContext("webgl2");

参见WebGL 2.0 Specification - 4.3 GLSL ES 3.00 support

In addition to supporting The OpenGL ES Shading Language, Version 1.00, the WebGL 2.0 API also accepts shaders written in The OpenGL ES Shading Language, Version 3.00 , with some restrictions. [...]