TypeError: Failed to execute 'shaderSource' on 'WebGLRenderingContext': parameter 1 is not of type 'WebGLShader'.
时间: 2023-12-23 15:05:44 浏览: 119
This error occurs when the first parameter passed to the WebGLRenderingContext.shaderSource() method is not a valid WebGLShader object.
To fix this error, ensure that the first parameter passed to the shaderSource() method is a valid WebGLShader object. You can create a WebGLShader object by calling the WebGLRenderingContext.createShader() method and passing in the appropriate shader type (e.g. gl.VERTEX_SHADER or gl.FRAGMENT_SHADER).
Here's an example of how to create a vertex shader and set its source code:
```
// create a vertex shader
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
// set the source code for the vertex shader
const vertexShaderSource = `
attribute vec4 a_position;
void main() {
gl_Position = a_position;
}
`;
gl.shaderSource(vertexShader, vertexShaderSource);
```
In this example, we create a new vertex shader with createShader(), and then set its source code with shaderSource(). The first parameter passed to shaderSource() is the vertexShader object we just created. The second parameter is a string containing the GLSL source code for the vertex shader.
Make sure that the WebGL context is initialized before calling any WebGL methods. Also, double-check that the shader type (e.g. VERTEX_SHADER or FRAGMENT_SHADER) matches the type of shader you are trying to create.
阅读全文