Mr.*_*dso 3 javascript shader glsl webgl
我刚刚开始使用 webgl 并正在遵循本教程,但遇到了一条奇怪的错误消息。ERROR: unsupported shader version。顶点着色器看起来像这样:
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;
}
`;
Run Code Online (Sandbox Code Playgroud)
片段着色器如下所示:
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);
}
`;
Run Code Online (Sandbox Code Playgroud)
然后使用以下函数将它们转换为着色器,然后记录上述错误:
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);
}
Run Code Online (Sandbox Code Playgroud)
我真的希望你能帮助我解决这个问题,并提前致谢。
如果您想使用GLSL ES 3.00着色器,则必须创建WebGL 2.0上下文。
看HTMLCanvasElement.getContext()。例如:
var ctx = canvas.getContext("webgl2");
Run Code Online (Sandbox Code Playgroud)
请参阅WebGL 2.0 规范 - 4.3 GLSL ES 3.00 支持:
除了支持 OpenGL ES 着色语言版本 1.00 之外,WebGL 2.0 API 还接受使用 OpenGL ES 着色语言版本 3.00 编写的着色器,但有一些限制。[...]