gpt4 book ai didi

javascript - WebGL "ERROR: unsupported shader version"

转载 作者:行者123 更新时间:2023-12-02 16:47:56 27 4
gpt4 key购买 nike

我刚开始使用 webgl 并且正在关注这个 tutorial ,但我遇到了一条奇怪的错误消息。 错误:不支持的着色器版本。顶点着色器看起来像这样:

 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着色器,那么你必须创建一个 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. [...]

关于javascript - WebGL "ERROR: unsupported shader version",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com