user: Can you set up a webgl demo for me? I'd like an example of a basic webgl shader. Make sure the output is available in index.html, but you can install any packages necessary.
assistant: Sure, I'll create a simple WebGL demo with a basic shader for you. This will include an HTML file to display the WebGL context and a script that sets up the shaders. First, let's create the `index.html` file: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL Demo</title> <style> body { margin: 0; } canvas { display: block; } </style> </head> <body onload="initWebGL()"> <canvas id="glCanvas"></canvas> <script src="shader.js"></script> </body> </html> ``` Now, let's create the `shader.js` file with a basic WebGL shader program: ```javascript const canvas = document.getElementById('glCanvas'); const gl = canvas.getContext('webgl'); if (!gl) { console.error("WebGL is not supported by your browser."); } // Vertex Shader source code const vsSource = ` attribute vec4 aVertexPosition; void main() { gl_Position = aVertexPosition; } `; // Fragment Shader source code const fsSource = ` precision mediump float; uniform vec4 uColor; void main() { gl_FragColor = uColor; } `; function initWebGL() { // Initialize shaders const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); // Create the shader program const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { console.error('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); return; } // Setup buffers and attributes const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); const positions = [ -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); const position = gl.getAttribLocation(shaderProgram, 'aVertexPosition'); gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(position); // Set the clear color to sky blue gl.clearColor(0.5, 0.7, 1.0, 1.0); // Clear the canvas gl.clear(gl.COLOR_BUFFER_BIT); // Use the shader program gl.useProgram(shaderProgram); // Set the uniform color (red) const color = gl.getUniformLocation(shaderProgram, 'uColor'); gl.uniform4f(color, 1.0, 0.0, 0.0, 1.0); // Draw the rectangle gl.drawArrays(gl.TRIANGLE_FAN, 0, 4); } function loadShader(gl, type, source) { const shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } ``` The `index.html` file sets up a basic canvas where WebGL can be drawn on. The `shader.js` file contains the vertex and fragment shaders for rendering a filled rectangle in red. To run this demo, save the HTML code in an `index.html` file and the JavaScript code in a `shader.js` file within the same directory. Open the `index.html` file in a web browser that supports WebGL to see the result of your simple shader program.
assistant: